What's wrong with my increment code?

Hello all,

I have a Table1 to display given categories, and a Table2 to display items for each of those categories. I wrote the following in hopes to increment ($i) each item, in order to create a div id, for later reference/toggling.
[php]/******
Cont’d from while($row = category results from Table1 {
//display each category and build jscript ($tog) for toggling…
******/
//select items per category using common id
$get_items = “SELECT * FROM Table2 WHERE item_cat = {$row[‘cat_id’]}”;
$i_result = $conn->query($get_items) or die(‘oops’);
// count cat items for incrementing later
$i_cnt = $i_result->num_rows;
//now complete code build
while($i_row = $i_result->fetch_assoc()) {
// add # for toggling div id’s
for ($i = 1;$i<$i_cnt+1;$i++) {
$tog .= ’
//toggle individual items
jQuery("#id’.$category."".$i.’").hide();
//toggle the componenet with class msg_body
jQuery("#tog’.$category."
".$i.’").click(function()
{
jQuery("#optns’.$category."".$i.’").slideToggle(500);
jQuery("#open’.$category."
".$i.’").slideToggle(100);
jQuery("#close’.$category."_".$i.’").slideToggle(100);
});’;
} //end for $i
}//end while ($i_row
} //end while ($row
$tog .= ’
});
';
echo $tog;//echo the built jscript for action[/php]
The problem I’m having is that the output contains the incremented items per category, but as many times as per item. For example, if Category A has 5 items, then the code shows 5 increments for those items (as intended) under said category… BUT FIVE TIMES instead of just incrementing through them once, like this:

[code]
//toggle individual items for categoryA
jQuery("#id_catA_1").hide();
//toggle the componenet with class msg_body
jQuery("#tog_catA_1").click(function()
{
jQuery("#id_catA_1").slideToggle(500);
jQuery("#open_catA_1").slideToggle(100);
jQuery("#close_catA_1").slideToggle(100);
});

			//toggle individual items for categoryA
			jQuery("#id_catA_2").hide();
	  		//toggle the componenet with class msg_body
	  		jQuery("#tog_catA_2").click(function()
	  		{
			jQuery("#id_catA_2").slideToggle(500);
			jQuery("#open_catA_2").slideToggle(100);
			jQuery("#close_catA_2").slideToggle(100);
			});

//this continues on until #id_catA_5 and then it repeats all of this 4 more times…

			//toggle individual items for categoryA
			jQuery("#id_catA_1").hide();
	  		//toggle the componenet with class msg_body
	  		jQuery("#tog_catA_1").click(function()
	  		{
			jQuery("#id_catA_1").slideToggle(500);
			jQuery("#open_catA_1").slideToggle(100);
			jQuery("#close_catA_1").slideToggle(100);
			});
			
			//toggle individual items for categoryA
			jQuery("#id_catA_2").hide();
	  		//toggle the componenet with class msg_body
	  		jQuery("#tog_catA_2").click(function()
	  		{
			jQuery("#id_catA_2").slideToggle(500);
			jQuery("#open_catA_2").slideToggle(100);
			jQuery("#close_catA_2").slideToggle(100);
			});
	  		{[/code]

I’ve tried moving the [php]for ($i=1;$<…[/php] statement to live before the [php]while($i_row…[/php]statement. But then everything gets a number 1 as its increment

Can anybody see where my problem is? Thanks in advance

I think your issue is higher up in the code we are not seeing…

The loops you have seem to behave properly… .

It’s the the $row loop that’s making it repeat, I see the closing tag below, but not the opening tag.

[php]} //end while ($row[/php]

So, unless the code cannot be nested inside this while loop, it’s pretty straight forward:
[php]
//collect categories from Table1
$sql = ‘SELECT * FROM catgories’;
$c_result = $conn->query($sql) or die(‘Could not access categories’);
$tog = ’
';
echo $tog;//echo the built jscript for action
[/php]
Do I need to include a mysqli command to clear the sql results from Table1 even though $c_row and $i_row are not using the same variable ($row)?

Just a guess,

But can you run

[php]SELECT * FROM catgories[/php]

In the database directly and make sure only 2 rows return?

No, directly from the database SELECT * FROM `online_ordering_categories` shows 4 results, 0-3.

OOPS!

I meant[php]SELECT * FROM categories[/php]
outputs 4 rows, 0-3

Veeps, now you’re confusing me…

In the code you posted

[php]$sql = ‘SELECT * FROM catgories’;[/php]

Then when you went directly against the database with

SELECT * FROM `categories`

So is the code that you posted did you spell the table name wrong?

no, sorry. I’d just changed it up for simplification via post.

so, the code is outputting, because I spelled things correctly in code (but not in my post apparently). It’s just not outputting how I’d prefer, which is once. $i_count seems to work $i_count times per category.

It seems to be doing the second while loop[php]while($i_row = $i_result->fetch_assoc()) {…[/php] $i_count amount of times within the first while loop [php]while($c_row…[/php]

So, for category 1, there are 5 items. And it writes category1_item1 (for example)…through category1_item5 FIVE DIFFERENT TIMES in the jscript build.

For category 3, there is only one item, so it displays category3_item1 only one time.

Do you even need that second while loop?

[php]while($i_row = $i_result->fetch_assoc()) {[/php]

Comment it out… and see what happens.

IT WORKED! I guess I didn’t need the second while loop, so long as I was just counting items and not needing specific info from them?

Next, I’ll need both the increment $i and data from ‘items’ table, to display in the html…I wonder if I’ll run into the same problem.

But, here’s the working code, for anyone trying to construct a javascript build (or anything) using data from two tables and a foreign key:
[php]
//collect categories from Table1
$sql = ‘SELECT * FROM categories’;
$c_result = $conn->query($sql) or die(‘Could not access categories’);
//begin the javascript code build for client interaction
$tog = ’
';
//spit out jscript
echo $tog;//echo the built jscript for action[/php]

And thanks a billion, Topcoder

Sponsor our Newsletter | Privacy Policy | Terms of Service