Forum Moderators: coopster

Message Too Old, No Replies

A interesting DB / PHP problem need help

         

havoc

12:57 pm on Aug 19, 2003 (gmt 0)

10+ Year Member



I have a db with serveral 100 records , what i am trying to do is have navigation based on the records..

eg (this isnt the code this is to get the idea of what i use the varibles on)

$next = $id+1;
$prev = $id-1;

$prevquery="SELECT record FROM table WHERE id = $prev";

Index ¦ $next ¦ $prev

the thing is when a record is DELETED and lets say $prev doesnt exist this obviously causes a problem.

is there a easy way of getting the next "available" record from mysql in php?

RussellC

1:55 pm on Aug 19, 2003 (gmt 0)

10+ Year Member



you can just say

$qry = "SELECT record FROM table";
$qry2 = @mysql_query($qry);
while ($row = @mysql_fetch_array($qry2)) {
$record = $row["record"];
echo("$record<br />");
}

that will list all of the records you can format how they are displayed.

RussellC

1:57 pm on Aug 19, 2003 (gmt 0)

10+ Year Member



Wait after looking at you problem I might have misunderstood you..let me know if that e.g. helped you.

havoc

2:46 pm on Aug 19, 2003 (gmt 0)

10+ Year Member



I wrote a fix but im 2000% sure this could be optimised maybe suggest? I setup up 10 tries and it fails

<mysql query here>

$PrevCheck = mysql_num_rows($prevname);

while($PrevCheck == 0)
{
if ($fail < 10)
{
$prev--;
$prevquery="SELECT Record FROM Table WHERE id = $prev";
$prevname = mysql_query($prevquery);
$PrevCheck = mysql_num_rows($prevname);
$fail++;
}
else
{
$PrevCheck++;
$prevresult = "No Previous";
$prevexist=No;
}
}

timster

4:02 pm on Aug 19, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As your db ages, I imagine you'll get a lot of gaps. Maybe you want to just ask the db for the right record at the start, instead of coping with lots of fails.

$sql = "SELECT Record FROM Table where id >= $id order by Record";
# (You may want to throw in a "limit 2" at the end if that's your style)

# The second row is always the one you want (except if you're at last rec already)

And of course

$sql = "SELECT Record FROM Tablle where id <= $id order by Record DESC";'

# Again, the 2nd row has the goods

You can also extend this to sort your records as desired.

jatar_k

5:03 pm on Aug 19, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you could also add LIMIT 1 to timster's examples giving you only the next or previous record alone.

timster

6:49 pm on Aug 19, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



jatar_k is right. The two rows are not necessary. But you would need to change:

>= $id
to
> $id

and likewise for <= to <.

(I'd better run get some more coffee...)

havoc

2:55 am on Aug 20, 2003 (gmt 0)

10+ Year Member



Thanks guys i knew someone would know .. its taked .003 seconds off execution which is really cool :)

i used the SELECT record, id FROM table WHERE id < $id ORDER BY id DESC limit 1

I am still not familar with mysql querying and optimising php code .. im getting there though . You guys are always a good help :)