473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

mysql results <-- previous row, and next row, showing next ok

Can anyone help?

I query a database and return a result on the column "reference".
There might be 7 listings. Each row is displayed in a table, with
links through to a detail page. I am working on having a "previous"
record and a "next" record link on the detail page.

This code below works, for the "next" record, by searching the values
in the array $myarray for the variable $ref. It then returns the key
value and the key value, as a number, is deducted by one and plus'd by
one to give me the previous row and the next row.

Returning the value of the next row works a treat, but for some reason
the previous row, deduct 1, while it echoes a value to screen, doesn't
work.

I'd appreciate any feedback, any help. It's been a challenge for a
newbie to sort this out.

This is the code:

(query database and then:)
//$ref is a variable carried into the page

$num_rows = mysql_num_rows($result2);

$i=0;
while ($i < $num_rows) {
$next=mysql_result($result2,$i,"reference");

++$i;

$myarray = array("$i"=>"$next");
foreach($myarray as $key=>$value) {
if ($value == "$ref"){

$b=($key);
$c=($key+1);
$a=($key-1);

}}

if ($a == "0") {
echo "No Preceding Record";

} else {
if ($key == "$a") echo "<a
href=../detail/index.php?name=$value>Previous Record</a>";
}

if ($key == "$b") echo "This is the current Record";

if ($num_rows < $c) {
echo "Records End";
} else {
if ($key == "$c") echo "<a href=../detail/index.php?name=$value>Next
Record</a>";
}
}

} else {
echo "Sorry, no records were found";
}

?>
Jul 17 '05 #1
10 10940
I noticed that Message-ID:
<bc*************************@posting.google.com> from george contained
the following:
++$i;


I always write $i++;
--
Geoff Berrow
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #2
On Sat, 27 Sep 2003 10:11:31 +0100, Geoff Berrow <bl@ckdog.co.uk.the.cat>
wrote:
I noticed that Message-ID:
<bc*************************@posting.google.com > from george contained
the following:
++$i;


I always write $i++;


If it's on its own line, it makes no difference; whether it's pre- or post-
increment only matters if it's part of a larger expression.

(But it could get you into a very long religious argument in a C or C++
group!)

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #3
Andy Hassall wrote:
On Sat, 27 Sep 2003 10:11:31 +0100, Geoff Berrow <bl@ckdog.co.uk.the.cat>
wrote:
++$i;
I always write $i++;


If it's on its own line, it makes no difference; whether it's pre- or post-
increment only matters if it's part of a larger expression.


On my machine, on this script, pre-increment is 5% faster than post-incrment :-)

<?php
$p1 = microtime();
for ($i=0; $i<1000000; $i++) {} // do nothing
$p2 = microtime();
for ($i=0; $i<1000000; ++$i) {} // do nothing
$p3 = microtime();
$t = explode(' ', $p1); $t1 = (float)$t[0]+(float)$t[1];
$t = explode(' ', $p2); $t2 = (float)$t[0]+(float)$t[1];
$t = explode(' ', $p3); $t3 = (float)$t[0]+(float)$t[1];
$delta1 = $t2-$t1;
$delta2 = $t3-$t2;
echo '<table>';
echo '<tr><td>post-increment</td><td>', $delta1, '</td></tr>';
echo '<tr><td>pre-increment</td><td>', $delta2, '</td></tr>';
echo '</table><br />';
if ($delta2 < $delta1)
echo 'pre-increment was ', number_format(100-$delta2/$delta1*100, 2), '% faster than post-increment';
else
echo 'post-increment was ', number_format(100-$delta2/$delta1*100, 2), '% faster than pre-increment';
?>

(But it could get you into a very long religious argument in a C or C++
group!)


And why not a PHP group too? :-)

--
I have a spam filter working.
To mail me include "urkxvq" (with or without the quotes)
in the subject line, or your mail will be ruthlessly discarded.
Jul 17 '05 #4
Pedro mis-wrote:
if ($delta2 < $delta1)
echo 'pre-increment was ', number_format(100-$delta2/$delta1*100, 2), '% faster than post-increment';
else
echo 'post-increment was ', number_format(100-$delta2/$delta1*100, 2), '% faster than pre-increment';


$delta2 and $delta1 should have been swapped in the last echo

--
I have a spam filter working.
To mail me include "urkxvq" (with or without the quotes)
in the subject line, or your mail will be ruthlessly discarded.
Jul 17 '05 #5
The only clue I have to this behaviour is that $a loops through the
array and returns (number of rows) instances of "No Preceding Record"
- as if it can't find the row in the array, as if it has no value EVEN
though it outputs to screen properly. $c also outputs as an increment
and finds the array row. So my problem presumably lies in the
expression $a=($key-1) ?? but I can't think how else to express it a
simple deduct 1.

Many thanks for any comments.
Jul 17 '05 #6
On 27 Sep 2003 15:27:31 GMT, Pedro <he****@hotpop.com> wrote:
Andy Hassall wrote:
On Sat, 27 Sep 2003 10:11:31 +0100, Geoff Berrow <bl@ckdog.co.uk.the.cat>
wrote:
++$i;

I always write $i++;
If it's on its own line, it makes no difference; whether it's pre- or post-
increment only matters if it's part of a larger expression.


On my machine, on this script, pre-increment is 5% faster than post-incrment :-)


Here we go :-)
<?php
$p1 = microtime();
for ($i=0; $i<1000000; $i++) {} // do nothing
$p2 = microtime();
for ($i=0; $i<1000000; ++$i) {} // do nothing
$p3 = microtime();


However, if you make the test fairer by having the same loop, and doing an
isolated pre- or post-increment inside, you get different answers.

$p1 = microtime();
$j=0;
for ($i=0; $i<1000000; $i++) { $j++; }

$p2 = microtime();
$j=0;
for ($i=0; $i<1000000; $i++) { ++$j; }
$p3 = microtime();

The amount differs, with pre-increment going 1-5% quicker, but even got this
at one point:

post-increment 1.09205305576
pre-increment 1.09754097462

post-increment was 0.50% faster than pre-increment
(But it could get you into a very long religious argument in a C or C++
group!)


And why not a PHP group too? :-)


Unfortunately it doesn't help the OP :-(

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #7
george wrote:
The only clue I have to this behaviour is that $a loops through the
array and returns (number of rows) instances of "No Preceding Record"
- as if it can't find the row in the array, as if it has no value EVEN
though it outputs to screen properly. $c also outputs as an increment
and finds the array row. So my problem presumably lies in the
expression $a=($key-1) ?? but I can't think how else to express it a
simple deduct 1.

Many thanks for any comments.


Where do $a, $b, and $c come from?
You're only setting them inside the if ($value == "$ref") block;
if ($value != "$ref") they will be undefined or have the value from last
time through the loop.

Also, do you realize $myarray will never have more than one entry?
Maybe that's the way you want it -- but then: why make it an array? ???
Insert these statement at the very beginning of your script

ini_set('error_reporting', E_ALL);

so that every warning, notice and error gets displayed on the browser.
HTH

--
I have a spam filter working.
To mail me include "urkxvq" (with or without the quotes)
in the subject line, or your mail will be ruthlessly discarded.
Jul 17 '05 #8

On 27-Sep-2003, Pedro <he****@hotpop.com> wrote:
Andy Hassall wrote:
On Sat, 27 Sep 2003 10:11:31 +0100, Geoff Berrow
<bl@ckdog.co.uk.the.cat>
wrote:
++$i;

I always write $i++;


If it's on its own line, it makes no difference; whether it's pre- or
post-
increment only matters if it's part of a larger expression.


On my machine, on this script, pre-increment is 5% faster than
post-incrment :-)


When I changed the sequence of the for loops, that is which increment was
first and ran the loops 20x each, it turns out that the sequence of the
loops determines which type of increment is faster not pre or post
increment. This makes sense to me. I don't see why different code would be
executed for pre or post increment.
<?php
echo '<table border=1>';
for ($txm=0;$txm<20;$txm++)
{
$p1 = microtime();
for ($i=0; $i<1000000; ++$i) {} // do nothing
$p2 = microtime();
for ($i=0; $i<1000000; $i++) {} // do nothing
$p3 = microtime();
$t = explode(' ', $p1); $t1 = (float)$t[0]+(float)$t[1];
$t = explode(' ', $p2); $t2 = (float)$t[0]+(float)$t[1];
$t = explode(' ', $p3); $t3 = (float)$t[0]+(float)$t[1];
$delta1 = $t2-$t1;
$delta2 = $t3-$t2;
$dif = number_format(100-$delta2/$delta1*100, 2);
echo "<tr><td>$delta1</td><td>$delta2</td><td>$dif%</td><td>++i
first</td></tr>";
}
echo "<tr><td></td><td></td><td></td><td></td></tr>";
for ($txm=0;$txm<20;$txm++)
{
$p1 = microtime();
for ($i=0; $i<1000000; ++$i) {} // do nothing
$p2 = microtime();
for ($i=0; $i<1000000; $i++) {} // do nothing
$p3 = microtime();
$t = explode(' ', $p1); $t1 = (float)$t[0]+(float)$t[1];
$t = explode(' ', $p2); $t2 = (float)$t[0]+(float)$t[1];
$t = explode(' ', $p3); $t3 = (float)$t[0]+(float)$t[1];
$delta2 = $t2-$t1;
$delta1 = $t3-$t2;
$dif = number_format(100-$delta2/$delta1*100, 2);
echo "<tr><td>$delta1</td><td>$delta2</td><td>$dif%</td><td>i++
first</td></tr>";
}
echo '</table>';
?>
--
Tom Thackrey
www.creative-light.com
Jul 17 '05 #9
Pedro <he****@hotpop.com> wrote in message news:<bl************@ID-203069.news.uni-berlin.de>...
if ($value != "$ref") they will be undefined or have the value from last
time through the loop.
yes that's why I said:

(query database and then:)
//$ref is a variable carried into the page

Insert these statement at the very beginning of your script

ini_set('error_reporting', E_ALL);


yep, it's excellent, and I get undefined variable.
Ever tried searching the web on "php define variable" so if it's
undefined perhaps I can define it - and the PHP manuals are like the
old DOS4 manuals - great if you know what they're talking about and
totally useless if you don't. I learnt DOS by trial and error and then
- only then - did the manuals make sense. So I appreciate any help
thanks !
Jul 17 '05 #10
george wrote:
Pedro <he****@hotpop.com> wrote in message news:<bl************@ID-203069.news.uni-berlin.de>...
Insert these statement at the very beginning of your script

ini_set('error_reporting', E_ALL);


yep, it's excellent, and I get undefined variable.


If you develop your scripts on a different server than the one where
they will be available, consider changing development's php.ini to show
all errors, warnings and notices.
This way you don't have to remember "ini_setting".

Happy Coding :-)

--
I have a spam filter working.
To mail me include "urkxvq" (with or without the quotes)
in the subject line, or your mail will be ruthlessly discarded.
Jul 17 '05 #11

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

Similar topics

1
by: tedqn | last post by:
I use the js onKeyup event to trigger server side db searches in a hidden iframe page and generate a list of selectable items in the parent page. The minimum characters to initiate a search is 3....
2
by: Deniz Bahar | last post by:
Hi, I'm working with a single linked list and want to delete elements by searching through the list (starting form the HEAD) then finding the element, then doing the following: NewElement =...
13
by: guitarromantic | last post by:
Hey everyone. I'm editing some stuff I did last summer, trying to bugfix and improve stuff. One improvement (or an oversight of the original design) is adding dynamic <title> tags to my pages....
3
by: hSiplu | last post by:
hi all, I am creating a web project with php/mysql. one of qeury result is too high. it returs several thousand rows. But I can't show them in one page. So I wanna create more than one pages...
0
Yika
by: Yika | last post by:
Hello all! :) I have a bit of a problem.. kind of! I have a table with several columns, of course, and most of them are searchable. Basically what I did, was if you entered any text, it would...
2
by: hecon | last post by:
My be I did not ask the correct question this scripts run ok and returns results fron db as a row in the from of text http://www.xxxxxxxxxxxxxxxxxxxx But the the results http is differerent for...
3
oranoos3000
by: oranoos3000 | last post by:
hi i'm a beginer javascript would you please help me i d like to findout this page has next page or previous page in history object i use this line of code if(window.history.length>0) and with...
1
by: ghjk | last post by:
In my php page there is a search function and when user search it will display table containing search results. I want to add <a href ..></a> . Because when user click one record I want to pass...
3
by: christy jones | last post by:
I would like to add 20 offers per page and becuase the database is updated daily. i need a way to allow access to other pages so that all offers will be displayed. i was thinking the next / previous...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
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...
0
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...

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.