0

How can I move mysql_fetch_array pointer in middle of a while loop? something like this:

 while ($subject = mysql_fetch_array($subject_qry))
{
 $output = "<div class=\"left\" id=\"{$subject['id']}\"></div>";
  next($subject);
     $output = "<div class=\"right\" id=\"{$subject['id']}\"></div>";
  return $output;
    }
1

3 Answers 3

1

How about this:

$counter = 0;
while ($subject = mysql_fetch_array($subject_qry))
{
  if ($counter % 2 == 0)
  {
     $output = "<div class=\"left\" id=\"{$subject['id']}\"></div>";
  }
  else
  {
     $output = "<div class=\"right\" id=\"{$subject['id']}\"></div>";
  }
  // do something with $output here...
  $counter ++;
}

Or you could achieve the same result with a boolean:

$left = true;
while ($subject = mysql_fetch_array($subject_qry))
{
  if ($left)
  {
     $output = "<div class=\"left\" id=\"{$subject['id']}\"></div>";
  }
  else
  {
     $output = "<div class=\"right\" id=\"{$subject['id']}\"></div>";
  }
  // do something with $output here...
  $left = !$left;
}
Sign up to request clarification or add additional context in comments.

4 Comments

I'm trying to get the next row in $subject, how does $counter move the pointer?
I guess it depends on what you're trying to achieve. If you just want to do one thing with even-numbered lines, and another thing with odd-numbered, then my code would solve the problem. If that's not it, let us know what you're trying to do... I.e., why do you want to move the pointer on?
$subject_qry retrieves from the database 10 categories and I wanna float them left and right. (class="left")
Right -- so it goes left, right, left, right, etc.? So I reckon the code I've suggested does that.
1

There are a few things that don't make sense about your example. First, you can't use a name with mysql_fetch_array when retrieving the result. Mysql_fetch_array uses the numeric indices, not the column name. Second, you don't return a value from within a looping while statement. You'd either build up a string or echo it as you're progressing through the loop.

Have you tried something like this:

$counter = 1;
while ($subject = mysql_fetch_array($subject_qry)) {
    echo "<div class=\"left\" id=\"{$subject[0]}\"></div>";
    mysql_data_seek($subject_qry,$counter);
    $subject = mysql_fetch_array($subject_qry);
    echo "<div class=\"right\" id=\"{$subject[0]}\"></div>";
    $counter=$counter+2;
}

Comments

0

You can't do this while you are extracting the data from the database. You have not read the next row with mysql_fetch_array() yet so you can't just skip to the next one.

You'll have to populate an array with all the results first to manipulate the "index" of the array. Alternatively you could just execute an additional mysql_fetch_array() within your loop to read the next row.

Comments

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.