Forum Moderators: coopster
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> </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.
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?
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.
$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>
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?
$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> </td><td class=\"cola\">
<input type=\"checkbox\" name=\"cid[]\" value=\"$cats[cid]\"$chk>$cats[title]
</td></tr>";
$chk="";
}
untested by the way ;)
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> </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).