472,799 Members | 1,579 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,799 software developers and data experts.

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

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
10 4699
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
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
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
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
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
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
<?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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: joe | last post by:
Can anyone help me out by either telling me how to get to the result i need or by pointing me to some documentation about the following question: A certain query to a database give me eg 100...
8
by: Adam Nemitoff | last post by:
Is is possible to construct a SELECT statement that contains a WHERE clause that uses the value from a column in the "next" row? ie. given a table with a single field named "myField" with the...
1
by: tnhoe | last post by:
Hi, <Form method='post' action="next.htm?btn="+"this.myform.myobj.value"> What is the correct syntax for above ? Regards Hoe
2
by: John Smith | last post by:
I have more than 4,000 pieces of email in my Yahoo account. I would like to write a program to download them all. In order to write such a program, I need to know a few things. 1. After I fill...
1
by: midiweb01 | last post by:
Sorry for my bad english here! I try my best. I would like to make a slideshow for my site about paintings. I have four menus: Portraits-landscapes-Still Life-Animals Each Menu has his own...
3
by: domcatanzarite | last post by:
How would one create a button that on click advances the form to the next "non recurring record" as opposed to the next record. The field the button needs to que from has groups of duplicate...
6
by: ZRexRider | last post by:
I've been reading the newsgroups and most of the solutions for this message are simple and obvious but unfortunately don't solve my problem. I have an MS-Access 2002 ADP application that...
2
by: =?Utf-8?B?aGVyYmVydA==?= | last post by:
how do I code generic functions to return the next item in an enumeration a) sorted by name, b) sorted by value c) sorted by declaration in a round-robin style ? for example the enum is Enum...
2
by: Sergei Shelukhin | last post by:
Hi. I have barely written any C++ for past 3-4 years, I need to refresh it in my memory so I decided to do some coding starting with classic algos and data structures, using Visual Studio 9,...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.