Forum Moderators: coopster

Message Too Old, No Replies

Misbehaving for loop

         

lorax

5:15 pm on Aug 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I need a second pair of eyes - mine are missing something. The problem child:


for($i=0; $i<=$num_rows; $i++) {
$cats = mysql_fetch_array($catr);
for($c=0; $c<=$num_pairs; $c++) {
$link2cat = mysql_fetch_array($pairs);
echo "<tr><td>$cats[cid]</td><td>$link2cat[cid]</td></tr>";
if ($link2cat[cid] == $cats[cid]) {
$chk = " checked";
break;
}
}
echo "<tr><td>&nbsp;</td><td class=\"cola\">
<input type=\"checkbox\" name=\"cid[]\" value=\"$cats[cid]\"$chk>$cats[title]
</td></tr>";
$chk="";
}

The first loop returns all of the possible categories for my links db. The second loop returns all categories currently assigned to the link I'm editing. The concept being that I want to know if the link has been assigned to the current category as I loop through all of the possibilities so I can check the input box. So I loop through the possibles and for each one run a loop to compare with the categories already assigned.

The line that echoes $cats[cid] and $link2cat[cid] works once. The loop works because the rows still get added and the code does output $cats[cid] but somehow $link2cat[cid] just dies after the first loop.

jatar_k

5:51 pm on Aug 23, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



so this is specifically not working?


for($c=0; $c<=$num_pairs; $c++) {
$link2cat = mysql_fetch_array($pairs);
echo "<tr><td>$cats[cid]</td><td>$link2cat[cid]</td></tr>";
if ($link2cat[cid] == $cats[cid]) {
$chk = " checked";
break;
}
}

is it dying because of some error or is it dropping into the if and breaking out of the loop?

ams_david

5:56 pm on Aug 23, 2003 (gmt 0)

10+ Year Member



maybe try continue; instead of break;

lorax

5:59 pm on Aug 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Glad you're on today jatar_k,

On the first pass it outputs the values for both vars on the line "<tr><td>$cats[cid]</td><td>$link2cat[cid]</td></tr>" - then it finds a match, sets $chk and breaks out of the loop to continue with the first loop.

On the second pass it only outputs the $cats[cid] of the above line.

jatar_k

6:01 pm on Aug 23, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



$link2cat = mysql_fetch_array($pairs);

what's in pairs then? since that is where it gets it's value from. The fetch_array may be the misbehaving bit.

lorax

6:02 pm on Aug 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The 2 queries

$query = "SELECT *
From fgs_linkcats";
$catr = mysql_query($query);
$num_rows = mysql_num_rows($catr);

$query = "SELECT *
From link2cats
Where lid = $lid";
$pairs = mysql_query($query);
$num_pairs = mysql_num_rows($pairs);

$lid = link id (the current record being edited)

fgs_linkcats is is the links category table
link2cats contains cid (category id) and lid (link id)

<edited>typos</edited>

jatar_k

6:18 pm on Aug 23, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



This is what I see

1. select all cats
2. select all connections(links to cats) for the present link being edited

np so far but here is where your logic seems to mess me up

why the dual nested for loops with the fetch_array inside?

The outer uses $num_rows as its control and runs through the fetch_array just fine

but

the inner for runs the fetch_array through once on every iteration of the outer loop

that seems to be the logic problem

after you run the fetch_array through all the returned query data it's useless. When you try to rerun it there are no more rows. It doesn't have anything else to run through and therefore it nukes your value.

preload that into an array before the outer for and then walk through the array, then you will be able to walk the array as many times as you want.

make sense?

lorax

6:23 pm on Aug 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes that makes sense. I just stuck the query for the cat/link matches inside the 1st loop and it worked. So what you're saying follows. I'll try that and be back in a bit.

jatar_k

6:29 pm on Aug 23, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



something like this?

$query = "SELECT * From fgs_linkcats";
$catr = mysql_query($query);
$num_rows = mysql_num_rows($catr);

$query = "SELECT * From link2cats Where lid = $lid";
$pairs = mysql_query($query);

while ($row = mysql_fetch_array($pairs)) {
$link2cat[] = $row;
}

for($i=0; $i<=$num_rows; $i++) {
$cats = mysql_fetch_array($catr);
$counter = 0;
while (is_array($link2cat[$counter])) {
echo "<tr><td>$cats[cid]</td><td>$link2cat[$counter][cid]</td></tr>";
if ($link2cat[$counter][cid] == $cats[cid]) {
$chk = " checked";
break;
} else {
$counter++;
}
}
echo "<tr><td>&nbsp;</td><td class=\"cola\">
<input type=\"checkbox\" name=\"cid[]\" value=\"$cats[cid]\"$chk>$cats[title]
</td></tr>";
$chk="";
}

untested by the way ;)

lorax

6:39 pm on Aug 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I didn't see you post until I after I built this:

for($c=1; $c<=$num_pairs; $c++) {
$link2cat = mysql_fetch_array($pairs);
$matchs[] .= $link2cat[cid];
$matchs++;
}

for($i=1; $i<=$num_rows; $i++) {
reset($matchs);
$cats = mysql_fetch_array($catr);
for($c=0; $c<=$num_pairs; $c++) {
echo "<tr><td>$cats[cid]</td><td>$matchs[$c]</td></tr>";
if ($matchs[$c] == $cats[cid]) {
$chk = " checked";
break;
}
}
echo "<tr><td>&nbsp;</td><td class=\"cola\">
<input type=\"checkbox\" name=\"cid[]\" value=\"$cats[cid]\"$chk>$cats[title]
</td></tr>";
$chk="";
}

I like how you used the while statement instead of the for (which I seem to default to).

jatar_k

6:43 pm on Aug 23, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I am the other way, I usually use while loops and counters. Old habits ;)

does it work now?

lorax

6:44 pm on Aug 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Changed the first section to:

while ($link2cat = mysql_fetch_array($pairs)) {
$matchs[] = $link2cat[cid];
}

Much better - works very well. Thank you! NFFC says you're a cheap date so I'll buy you a pint at Pubcon. ;)