Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP, MYSQL, SMARTY Problem with loop

    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

    Code:
    <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:
    Code:
    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:
    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");
    ?>
    Last edited by herbert; 03-29-2009 at 12:26 PM. Reason: amend

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,184
    Thanks
    80
    Thanked 4,451 Times in 4,416 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •