Jump to content

[SOLVED] moving down mysql_fetch array rows in for loop?


katie77

Recommended Posts

Hi there,

 

I've got an associative array that I've called and I'm trying to find a simple way of parsing through the rows.

 

$dataset_row = mysql_fetch_array($result, MYSQL_ASSOC);

for ($column_counter = 1; $column_counter <= $num_columns; $column_counter += 1)
{
echo $dataset_row['ext_url']."<br />;
}

 

How can I get the row values of $dataset_row?

 

Can I use $dataset_row[1] or something?  The trouble is I need to specify the rows between and after - eg loop between rows 3-7.

 

Can anyone help!

Link to comment
Share on other sites

I'm assuming you want to loop through the rows of your mysql result set...you're not very clear.

 

while ($row = mysql_fetch_assoc($result)) {
  echo $row['ext_url'];
}

 

The easiest way to loop through only rows 3-7 is to only select those rows from your database.  Reformat your query to better select your data, or use a limit on the end of your select statement.

 

There are other methods, but using SQL to select out only the data you want is the best in my opinion.

Link to comment
Share on other sites

hi there,

 

that' looks like it'll work - with regards to the 3-7 select - it's because I'm using the same information 10-15 times in different ways on the same page, so if there is a more efficient way of loading the table and presenting it in different ways, I'd be interested!

 

Link to comment
Share on other sites

You can use mysql_data_seek to change the starting row.

 

http://www.php.net/mysql_data_seek

 

// I'm assuming that $result has > 10 rows
mysql_data_seek($result, 2); // the result set starts @ 0, so 2 == the third row

// print results 3 - 7
for ($x = 3; $x <= 7; $x++) {
  $row = mysql_fetch_assoc($result);
  print_r($row);
}

mysql_data_seek($result, 0);  // move back to the first result

//print the first 3 results.
for ($x = 0; $x <= 2; $x++) {
  $row = mysql_fetch_assoc($result);
  print_r($row);
}

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.