473,216 Members | 1,321 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,216 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 10924
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...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.