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

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

P: n/a
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
Share this Question
Share on Google+
10 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.