...

View Full Version : PHP, MYSQL, SMARTY Problem with loop



herbert
03-29-2009, 01:00 PM
Hi guys, I am newly registered but visit here a lot for advice.

Ok, basically I am attempting to create a spash page for an online store style thingy. The user can click on a category (mySQL backed) and get a list of items within that category. I initially wanted the items to display underneath the clicked category link, but as the categories are being displayed using a for each loop, the items were being displayed under EVERY category, not just the relevant one. But I gave up on that. any suggestion welcome tho.

The problem now is, I am displaying the items underneath the ENTIRE category list in a different section that says "Your chosen category is ......". .Code Below


<body>
<h1>The Buying and Selling Stuff Online Store Thingy!</h1>

<p>
Welcome to The Buying and Selling Stuff Online Store Thingy!
</p>
<hr>
<h2>Categories</h2>
{foreach item=cats from=$catRes}
<p><b><a href="item_list.php?id={$cats.id}">{$cats.cat_title}</b></a> - {$cats.cat_desc}</p>
{/foreach}

<h2>The category you have selected is: {$id} </h2>
<p>Items listed in the above category are: </p>
{foreach item=product from=$item_list}
<ul>
<li><b>{$product.item_title}</b> - {$product.item_desc}</li>
</ul>
{/foreach}





<hr>
<h2>Search by Name, Description or Vendor</h2>


</body>
</html>

Ok, so where '$id' is I want it to display the category name. The trouble is that using '$cats.cat_title' only shows up the very last category that was interated over in the foreach loop. So no matter which category (out of 5) I click on, the category stays static. However, if I use $id (which is assigned to $_GET['id'] in my template) it will display the correct id (as an integer) for the category. Dunno, If I've explained this well. I guess, what I need to know is, is there a way within the <a href> tag that I can parse the $cat.cat_title information to my .php script so I can turn into a smarty variable and use it that way? I mean, I am parsing the id info, can I also parse the cat_title info seperately and grab it using $_GET?

Also, in case it's needed, here is the php function that grabs the category info:

function showCats() {

$connection = mysql_open();

// Construct query,
$query = "SELECT * from store_categories ORDER BY id ";

// Get list of items on this page
$results = mysql_query($query, $connection) or showerror();

// Copy result set to array
$category = array();
while ($row = mysql_fetch_array($results)) {
$category[] = $row;
}

mysql_close($connection) or showerror();
return $category;
}

and the php code:

<?php
require '/usr/local/Smarty/libs/Smarty.class.php';
require "includes/defs.php";

$id = $_GET['id'];
$item_list = get_items($id);
$catRes = showCats();

$smarty = new Smarty;
$smarty->assign('id', $id);
$smarty->assign('catRes', $catRes);
$smarty->assign("item_list", $item_list);
$smarty->display("index.tpl");
?>

Old Pedant
03-29-2009, 09:50 PM
If you don't mind translating an ASP answer to PHP, look here:
http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=154

It's the same concept in PHP: *ONE* query that JOINs your category/subcategory tables. (Or, if they are all in one table, no join needed.)

The important part is the "trick" where you remember the prior category and only output a new category header/start when the category changes.

Now, how (and if) you integrate that with "Smarty", I haven't a clue.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum