Page 1 of 1

PHP Arrays and Smarty

Posted: Sun Feb 21, 2010 2:44 pm
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

Re: PHP Arrays and Smarty

Posted: Mon Feb 22, 2010 4:25 am
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'];
}