English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Hi,

I have the following code that prints out all of the rows of my database, is there any way to increment it so that it starts on the 5th record and skips the first four?

// Fetch and print all the records.
$bg = '#eeeeee'; // Set the background color.
while ($row = mysql_fetch_array($result, MYSQL_NUM)) { etc.........

2007-03-18 03:02:51 · 2 answers · asked by jeff lemon 1 in Computers & Internet Programming & Design

Mohammed,

I already treat it as an array. I have it print out in a table:


$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); // Switch the background color.
echo '

', stripslashes($row[0]), '

', $row[1], '

', $row[2], '

', $row[5], '

', $row[3], '





', $row[5], '






but then I have another table after this one with the same stuff in. After the first table I need to increase the row to teh next row. Any Ideas?

2007-03-18 03:20:04 · update #1

2 answers

Hello:
There are two ways doing this:

1) In your MySQL SQL Query you can add the LIMIT keyword to your statement like the following:

SELECT * FROM myTable LIMIT 5,100

That will select starting from 5th row and count 100 entries.

2) The other way to do it is converting that resultset into an array:
$i = 0;
$data = array();
while ($row = mysql_fetch_array($results)) {
$data[$i] = $row;
$i++;
}

Notice what I have done? Now you can treat the result as a simple array object.

$data[4]['color'] = will get the 4th row where the column is color.

for( $i = 5; $i < array_size($data); $i++)
{
echo 'The color of my shoe is '.$data[$i]['Color'];
echo 'But my name is '.$data[$i]['Name'];
}

So it starts at 5 since I told it so, normal array manipulation

Good Luck


UPDATE Response
=======

If you wish to increase the row to the next row, instead of treating the static integers as [1] , [2] ... for the array why not treat it as an dynamic structure? Like $i = 0

', stripslashes($row[$i]), '
', $row[$i+1], '
', $row[$i+2], '
', $row[$i+3], '
', $row[$i+4], '


', $row[$i+5],


Then just manipulate that integer for the next position.

2007-03-18 03:05:40 · answer #1 · answered by ? 6 · 0 0

Either you can add a "limt" clause in your query.
Or you can use "mysql_data_seek", a PHP function, to reset the internal pointer of a result set to any row.

2007-03-20 02:06:32 · answer #2 · answered by Atif Majid 3 · 0 0

fedest.com, questions and answers