By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,780 Members | 1,520 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,780 IT Pros & Developers. It's quick & easy.

PHP / Mysql - Retreive "Next" Row data in "this" row

P: n/a
I am trying to get the next row within a loop for a script i am
developing...

I need to display a final table row within the table that i have
displayed on the page, but i only want to show it if value of the
current field is not the same value of the next row.
eg:

value 1
value 1
value 1
SHOW ROW
value 2
value 2
SHOW ROW
value 3
value 3
SHOW ROW
value 4
value 4
SHOW ROW

I have tried mysql_data_seek with very little luck.

Any help would be grateful.

Craig

Jan 3 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
craig.keightley wrote:
value 1
value 1
value 1
SHOW ROW
value 2
value 2
SHOW ROW
value 3
value 3
SHOW ROW
value 4
value 4
SHOW ROW
Thinking laterally, is "SELECT DISTINCT" an option?

If not, the basic technique is this:

<?php
$oldvals = array();

while ($newvals = fetch_row_from_db())
{
if ($newvals is different from $oldvals)
{
print $oldvals;
}
$oldvals = $newvals;
}

print $oldvals;
?>

You will need to adapt that to your situation.

This way, rather than trying to "look ahead" to the next row in the data
set, you're actually "printing behind" the previous row. Same effect, but
much easier to implement, because the next row is always a mystery,
whereas the previous row is already known.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Jan 3 '07 #2

P: n/a
Another way to do it VIA array

<?php
$vals = array();
while ($temp = fetch_row_from_db()) $vals[$temp[0]] = $temp;
var_dump($vals);
?>

This way, you are sure to get the last row of every value.

Hendri Kurniawan
Toby Inkster wrote:
craig.keightley wrote:
>value 1
value 1
value 1
SHOW ROW
value 2
value 2
SHOW ROW
value 3
value 3
SHOW ROW
value 4
value 4
SHOW ROW

Thinking laterally, is "SELECT DISTINCT" an option?

If not, the basic technique is this:

<?php
$oldvals = array();

while ($newvals = fetch_row_from_db())
{
if ($newvals is different from $oldvals)
{
print $oldvals;
}
$oldvals = $newvals;
}

print $oldvals;
?>

You will need to adapt that to your situation.

This way, rather than trying to "look ahead" to the next row in the data
set, you're actually "printing behind" the previous row. Same effect, but
much easier to implement, because the next row is always a mystery,
whereas the previous row is already known.
Jan 3 '07 #3

P: n/a
Thanks for the tips

In the end i created an array based on the loop. Reset the mysql loop
and loop through again. From here i compared the necessary values

Tahnks again

Craig
Hendri Kurniawan wrote:
Another way to do it VIA array

<?php
$vals = array();
while ($temp = fetch_row_from_db()) $vals[$temp[0]] = $temp;
var_dump($vals);
?>

This way, you are sure to get the last row of every value.

Hendri Kurniawan
Toby Inkster wrote:
craig.keightley wrote:
value 1
value 1
value 1
SHOW ROW
value 2
value 2
SHOW ROW
value 3
value 3
SHOW ROW
value 4
value 4
SHOW ROW
Thinking laterally, is "SELECT DISTINCT" an option?

If not, the basic technique is this:

<?php
$oldvals = array();

while ($newvals = fetch_row_from_db())
{
if ($newvals is different from $oldvals)
{
print $oldvals;
}
$oldvals = $newvals;
}

print $oldvals;
?>

You will need to adapt that to your situation.

This way, rather than trying to "look ahead" to the next row in the data
set, you're actually "printing behind" the previous row. Same effect, but
much easier to implement, because the next row is always a mystery,
whereas the previous row is already known.
Jan 4 '07 #4

P: n/a
Hendri Kurniawan wrote:
Another way to do it VIA array

<?php
$vals = array();
while ($temp = fetch_row_from_db()) $vals[$temp[0]] = $temp;
var_dump($vals);
?>

This way, you are sure to get the last row of every value.

Hendri Kurniawan
Hendri,
Would you please take pity on a PHP newbie and explain your code.
It is elegant and dense I can't quite make sense of it.
Shouldn't there be a bracket after the while statement ?\

bill
Jan 4 '07 #5

P: n/a
craig.keightley wrote:
In the end i created an array based on the loop. Reset the mysql loop
and loop through again. From here i compared the necessary values
Two pass? Bah -- ugly solution.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Jan 4 '07 #6

P: n/a
bill wrote:
Shouldn't there be a bracket after the while statement ?
Not needed.

while (x) y;

is equivalent to:

while (x)
{
y;
}

You only need to use curly brackets after while() if you want to iterate
over more than one line, like this:

while (x)
{
y;
z;
}

The same is true of if(), elseif(), else, for(), foreach() and probably
others that I'm forgetting.

For example:

<?php
$countTo99 = rand(0,1);
if ($countTo99)
for ($i=0;$i<10;$i++)
for ($j=0;$j<10;$j++)
print "{$i}{$j}\n";
else
print "I decided not to count.\n";
?>

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Jan 4 '07 #7

P: n/a
<?php
// initialize array
$vals = array();

// Fetch data from database, put them into associative array
//
// I am assuming the SQL will be something like:
// SELECT comparableValue, otherInformation1, otherInformation2 FROM
sometable
//
// Thus. when fetching result:
// $temp[0] = comparableValue
// $temp[1] = otherInformation1
// $temp[2] = otherInformation2
//
// With associative array, the newer values will always
// re-write the older value. See example
//
// Say you fecth the row, it returned array('value1', 'someinfo',
'someinfo')
// I will put them into assoc array, where 'value1' will be the key.
// The next fetch, it returns array('value1', 'someinfoagain',
'someinfotoo')
// If I put them into the same associative array, with the same key,
// it will over-write the previous value.
// and so on.
//
// Try first. If still don't understand. please ask again in ng
//
// PS. for Craig. 2 pass loop is not an elegant solution. But it's just
an advice
//
while ($temp = fetch_row_from_db()) $vals[$temp[0]] = $temp;

// This just dump the whole array to screen
var_dump($vals);
?>
Hendri Kurniawan

bill wrote:
Hendri Kurniawan wrote:
>Another way to do it VIA array

<?php
$vals = array();
while ($temp = fetch_row_from_db()) $vals[$temp[0]] = $temp;
var_dump($vals);
?>

This way, you are sure to get the last row of every value.

Hendri Kurniawan

Hendri,
Would you please take pity on a PHP newbie and explain your code.
It is elegant and dense I can't quite make sense of it.
Shouldn't there be a bracket after the while statement ?\

bill
Jan 4 '07 #8

P: n/a
Toby Inkster wrote:
bill wrote:
>Shouldn't there be a bracket after the while statement ?

Not needed.
....

<?php
$vals = array();
while ($temp = fetch_row_from_db()) $vals[$temp[0]] = $temp;
var_dump($vals);
?>

Ah, so the var_dump ($vals); is executed just once ?

bill
Jan 5 '07 #9

P: n/a
Hendri Kurniawan wrote:
<?php
// initialize array
$vals = array();

// Fetch data from database, put them into associative array
//
// I am assuming the SQL will be something like:
// SELECT comparableValue, otherInformation1, otherInformation2
FROM sometable
//
// Thus. when fetching result:
// $temp[0] = comparableValue
// $temp[1] = otherInformation1
// $temp[2] = otherInformation2
//
// With associative array, the newer values will always
// re-write the older value. See example
//
// Say you fecth the row, it returned array('value1', 'someinfo',
'someinfo')
// I will put them into assoc array, where 'value1' will be the key.
// The next fetch, it returns array('value1', 'someinfoagain',
'someinfotoo')
// If I put them into the same associative array, with the same key,
// it will over-write the previous value.
// and so on.
//
// Try first. If still don't understand. please ask again in ng
//
// PS. for Craig. 2 pass loop is not an elegant solution. But it's
just an advice
//
while ($temp = fetch_row_from_db()) $vals[$temp[0]] = $temp;

// This just dump the whole array to screen
var_dump($vals);
?>
Hendri Kurniawan
Lovely, and very much appreciated.
Now I just need to print it out and study it.
Thanks

bill
>
bill wrote:
>Hendri Kurniawan wrote:
>>Another way to do it VIA array

<?php
$vals = array();
while ($temp = fetch_row_from_db()) $vals[$temp[0]] = $temp;
var_dump($vals);
?>

This way, you are sure to get the last row of every value.

Hendri Kurniawan

Hendri,
Would you please take pity on a PHP newbie and explain your code.
It is elegant and dense I can't quite make sense of it.
Shouldn't there be a bracket after the while statement ?\

bill
Jan 5 '07 #10

P: n/a
bill wrote:
<?php
$vals = array();
while ($temp = fetch_row_from_db()) $vals[$temp[0]] = $temp;
var_dump($vals);
?>

Ah, so the var_dump ($vals); is executed just once ?
Yep.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Jan 5 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.