PHP Arrays and Smarty

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
VarwigDude
Forum Newbie
Posts: 12
Joined: Tue Mar 31, 2009 8:08 pm

PHP Arrays and Smarty

Post by VarwigDude »

Hello Everyone. I have this Issue. I am using Smarty with Arrays. I cannot get this array to form correctly from a database. the category_name array is not forming correctly. It lays it our on the HTML side as 1=>"Category Name". How can i make a key and a value from a MySQL Database and have Smarty Place it down correctly,

Code: Select all

 
<?php
$db = new Database(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);
 
$db->connect();
 
$sql = "SELECT * FROM store_info";    
$record = $db->query_first($sql);
$store_name = $record['store_name'];
$sql_cat = "SELECT * FROM store_categories WHERE category_show_top_menu='Y'";
$rows = $db->fetch_all_array($sql_cat);
foreach($rows as $record){
    $category_name[] = "$record[category_id] => $record[category_name]";
}
 
$db->close(); 
 
?>
 
Here is my Smarty Code:

Code: Select all

 
<li><a href="?i=home" >Home</a></li>
{foreach from=$category_name key=k item=v}
    <li><a href="?i=product&x={$k}">{$v}</a></li>
{/foreach}
</ul>
 
Thanks Guys
iamngk
Forum Commoner
Posts: 50
Joined: Mon Jun 29, 2009 2:20 am

Re: PHP Arrays and Smarty

Post by iamngk »

Change your foreach in php like mentioned below:

Code: Select all

$category_name = array();
foreach($rows as $record){
    $category_name[$record['category_id']] =  $record['category_name'];
}
Post Reply