0

i have this part of a code and i cant understund why the second loop inside the first does not work. I query two tables and i placed an echo inside the second table to see if it echo's but it does not either . thanx in advance

while($row = mysql_fetch_array($result))
{
 echo "<li class=\"s01\"><a class=\"s03\" href=" . $row['link'] . "><span>". $row['onomaselidas'] ."</span></a>\n";
 echo "<ul class=\"pn2\">\n";
 $idd[]=$row['idwebsiteprimary'];
   while($row2 = mysql_fetch_array($result2))
   {
    echo "test";
   if($idd[$url]==$row2['idwebsite'])
    {
    echo "<li class=\"s01\"><a href=\"a\"><span>". $row2['name'] ."</span></a></li>\n";
    }
   }
 echo "</ul>\n";
}
3
  • 3
    Are you sure the query for $result2 is returning rows? Commented Oct 21, 2011 at 12:54
  • 1
    Where is the code for that query and that result-set? Also, does it return something? Commented Oct 21, 2011 at 12:55
  • oh my god i am so stupid, i was having the wrong query. but now i have another broblem if the $url equals to 1 i get "Notice: Undefined offset: 1 in..." Commented Oct 21, 2011 at 14:04

1 Answer 1

0

After the first iteration of the first loop, the internal pointer of the second result set is at the end, so the second loop will not execute, because mysql_fetch_array() will return false immediately. If you want to it exactly as above - the second result set is not dependant on the first - you will need to do this:

// First, get the results of set 2 into an array
$resultset2 = array();
while ($row = mysql_fetch_assoc($result2)) $resultset2[] = $row;

while ($row = mysql_fetch_assoc($result)) { // Do your thang
  echo "<li class=\"s01\"><a class=\"s03\" href=" . $row['link'] . "><span>".$row['onomaselidas'] ."</span></a>\n";
  echo "<ul class=\"pn2\">\n";
  $idd[] = $row['idwebsiteprimary'];
  foreach ($resultset2 as $row2) { // Foreach sets it's pointer to the beginning every time, so this should work
    echo "test";
    if ($idd[$url]==$row2['idwebsite']) {
      echo "<li class=\"s01\"><a href=\"a\"><span>". $row2['name'] ."</span></a></li>\n";
    }
  }
  echo "</ul>\n";
}

Alternatively, you can reset the internal pointer of $result2 on each iteration of the first loop by calling mysql_data_seek($result2, 0); like this:

while ($row = mysql_fetch_array($result)) {
  echo "<li class=\"s01\"><a class=\"s03\" href=" . $row['link'] . "><span>". $row['onomaselidas'] ."</span></a>\n";
  echo "<ul class=\"pn2\">\n";
  $idd[] = $row['idwebsiteprimary'];
  mysql_data_seek($result2, 0);
  while ($row2 = mysql_fetch_array($result2)) {
    echo "test";
    if($idd[$url]==$row2['idwebsite']) {
      echo "<li class=\"s01\"><a href=\"a\"><span>". $row2['name'] ."</span></a></li>\n";
    }
  }
  echo "</ul>\n";
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.