473,890 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4811
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 , otherInformatio n1, otherInformatio n2 FROM
sometable
//
// Thus. when fetching result:
// $temp[0] = comparableValue
// $temp[1] = otherInformatio n1
// $temp[2] = otherInformatio n2
//
// 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 , otherInformatio n1, otherInformatio n2
FROM sometable
//
// Thus. when fetching result:
// $temp[0] = comparableValue
// $temp[1] = otherInformatio n1
// $temp[2] = otherInformatio n2
//
// 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3728
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 results: $query = "select id, name, addr1, addr2, city from mytable where id=" ..$id; $connection = mysql_connect(etc etc) ....
8
17059
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 following values I want a SELECT statement that selects the rows "WHERE myField='1' and myField = '2' ": 5 6 1
1
2020
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
1877
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 out userid, password, and click on "login", what exactly is sent to Yahoo? 2. When I click on "next" to fetch the next mail, what exactly is sent to Yahoo?
1
3665
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 slideshows with a certain number of images. What Iwant is clear and simple. I don't want to display all the images. I need a javascript code that displays each image enlarged on the right and so on for each one through a slidebar "Next" and "PreviOus"...
3
2228
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 values. I need it to advance the form to the next group of duplicate values not just the next record.
6
4628
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 connects to SQL server. The application is deployed to 100+ users and in most cases works fine. I have a sub-set of users (~20) who have an auditing function. They review transactions entered by the other 100+ and accept or reject them. There are a...
2
3836
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 Colors Red = 5 Green = 7 Blue = 16 Yellow = 2
2
1456
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, unmanaged code. Everything is humming, but I have the following problem I cannot figure out. Say we have the following: template <typename Tclass SLL { public: SLL<T>* AddHead(T value);
0
9814
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11214
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10801
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10903
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8006
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4659
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4257
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3266
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.