Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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";
}
share|improve this question
3  
Are you sure the query for $result2 is returning rows? –  Jrod Oct 21 '11 at 12:54
1  
Where is the code for that query and that result-set? Also, does it return something? –  JNDPNT Oct 21 '11 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..." –  teo6389 Oct 21 '11 at 14:04
add comment

1 Answer

up vote 0 down vote accepted

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";
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.