Avatar of John Account
John Account
 asked on

smarty

I am using a free open source shopping cart as a stock photo website.  I've modified the heck out of it and so am encountering problems because the functions that were initally doing the jobs are no longer being called hence creating some minor bugs.  The main problem I'm having right now is because of smarty.  This was designed with the smarty template engine and I don't know nothing about it.  

when I display the categories in the amdinistrative section of my site, I also display the count for the products in each category.   The problem is that this count was being retrieved from a table that was being upldate every time a product or service was added.  Now, I don't want to use htis.   I prefer to just count the entries in the DB and return the count with mysql_num_rows.

I did this, and even figured out how to assigne the  variable to smarty.  this variable is being assigned in a "while" statement and should change with every category, but now, it displays the same number everywhere.  It's is the correct count of the selected category, but not for each category... don't know if I'm explaining this right here...  


Here is the code in the php file
 
	//calculate how many products are there in the root category
		$q = db_query("SELECT count(*) FROM ".PRODUCTS_TABLE." WHERE categoryID=0") or die (db_error());
		$cnt = db_fetch_row($q);
		$smarty->assign("products_in_root_category",$cnt[0]);
 
		//create a category tree
		$c = fillTheCList(0,0);
		$smarty->assign("categories", $c);
 
		//show category name as a title
		$row = array();
		if (!isset($_GET["categoryID"]) && !isset($_POST["categoryID"]))
		{
			$categoryID = 0;
			$row[0] = ADMIN_CATEGORY_ROOT;
		}
		else //go to the root if category doesn't exist
		{
			$categoryID = isset($_GET["categoryID"]) ? $_GET["categoryID"] : $_POST["categoryID"];
			$q = db_query("SELECT name FROM ".CATEGORIES_TABLE." WHERE categoryID<>0 and categoryID='$categoryID'") or die (db_error());
			$myFile = "dunx.txt";
			$fh = fopen($myFile, 'w') or die("can't open file");
			$stringData = "SELECT name FROM ".CATEGORIES_TABLE." WHERE categoryID<>0 and categoryID='$categoryID'";
			fwrite($fh, $stringData);
			fclose($fh);
 
			$row = db_fetch_row($q);
			if (!$row)
			{
				$categoryID = 0;
				$row[0] = ADMIN_CATEGORY_ROOT;
			}
		}
 
		$smarty->assign("categoryID", $categoryID);
		$smarty->assign("category_name", $row[0]);
 
		//get all products
		$q = db_query("SELECT productID, name, customers_rating, Price, in_stock, picture, big_picture, thumbnail, items_sold, enabled, product_code FROM ".PRODUCTS_TABLE." WHERE categoryID='$categoryID'  ORDER BY name;") or die (db_error());
		
		$result = array();
		$num = mysql_num_rows($q);
		$i=0;
	
		while ($row = db_fetch_row($q)) $result[$i++] = $row;
	
		//update result
		for ($i=0; $i<count($result); $i++)
		{
			if (!trim($result[$i][5]) || !file_exists("./products_pictures/".trim($result[$i][5])))
				$result[$i][5] = "";
			if (!trim($result[$i][6]) || !file_exists("./products_pictures/".trim($result[$i][6])))
				$result[$i][6] = "";
			if (!trim($result[$i][7]) || !file_exists("./products_pictures/".trim($result[$i][7])))
				$result[$i][7] = "";
		}
 
		//products list
		$smarty->assign("products", $result);
	$smarty->assign("products_count", $num);
		//set main template
		$smarty->assign("admin_sub_dpt", "catalog_products_categories.tpl.html");
 
	}
Then in the template this is what calls the variable :
 
{* categories *}
<tr>
<td valign="top" bgcolor="#E2E2FF" width="25%">
 
	<table width="100%" border="0">
	 <tr>
	 <td colspan=3><a href="admin.php?dpt=catalog&sub=products_categories&categoryID=0" style="font-weight: bold;">{$smarty.const.ADMIN_CATEGORY_ROOT}</a> ({$products_in_root_category})</td>
	 </tr>
	 {section name=i loop=$categories}
	 <tr>
	 <td>{section name=j loop=$categories[i][5] max=$categories[i][5]}&nbsp;&nbsp;&nbsp;&nbsp;{/section}<a href="admin.php?dpt=catalog&sub=products_categories&categoryID={$categories[i][0]}"{if $categories[i][5] eq 0} style="font-weight: bold;"{/if}>{$categories[i][1]}</a></td>
	
 
<td>({$products_count})</td>
	 <td align="right"><font color="red">[</font><a class="small" href="javascript:open_window('category.php?c_id={$categories[i][0]}&w={$categories[i][4]}',400,400);">edit</a><font color=red>]</font></td>
	 </tr>
	 {/section}
	</table>
 
	<br><center>[ <a href="javascript:open_window('category.php?w=-1',400,400);">{$smarty.const.ADD_BUTTON}</a> ]</center><br>
 
</td>
 
any help is appreciated...

Open in new window

PHP

Avatar of undefined
Last Comment
John Account

8/22/2022 - Mon
djberriman

No nothing of this software but from what i can see when you display the product count you are not specifying the array element after it so you will always get element 0.

You need to specify which element to display [i]?

Hope that makes sense.
John Account

ASKER
<td>({$products_count})</td>
is how I display... why would I need an element?  just trying to follow...
djberriman

because you have stored it in an array just like all the other details (category id and category name)

so to display it you need to say which array element.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
John Account

ASKER
$q = db_query("SELECT productID, name, customers_rating, Price, in_stock, picture, big_picture, thumbnail, items_sold, enabled, product_code FROM ".PRODUCTS_TABLE." WHERE categoryID='$categoryID'  ORDER BY name;") or die (db_error());
            
            $result = array();
            $num = mysql_num_rows($q);
            $i=0;

My $num is in an array?
djberriman

       $smarty->assign("products_count", $num);
John Account

ASKER
If $num is not an array.  $num returns a number value.  So how is my $smarty->assign("products_count", $num); an array?   Anyway, then by your rationale, I shoudl call my variable like this?

 
<td>({$products_count[i][1]})</td>

???
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
djberriman

I didn't say num was an array

but from what I see products_count is and you should display it as you do categories.

            $smarty->assign("categories", $c);
            $smarty->assign("categoryID", $categoryID);
            $smarty->assign("category_name", $row[0]);
                $smarty->assign("products", $result);
                $smarty->assign("products_count", $num);


when you display categories you use

$categories[i][...]
$categories[i][0]

so I would expect something similar for products_count eg $products_count[i] or $products_count[i][0]
ASKER CERTIFIED SOLUTION
John Account

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.