Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieving records one at a time

Status
Not open for further replies.

kjspear

Programmer
Feb 13, 2002
173
US
I'm still having serious problems with retrieving records one at a time. This is actually the last partof the program.

Perhaps my code below won't work for that function. The code successfully retieves all records from the database. These are links to images. But for the life of me I can't seem to find a way to get php/sql to display one record at a time so that the user can move to the next record for viewing. I've looked at many samples, but most of them have some sort of link to a page to it. I do not have any links of this sort. The records in the database simply links to the image such as 'src='happy.gif', along with the path.

Is there a place that I could go to where I may find coding as such? Does anybody has a solution to this problem? Please let me know if you need more detail.

Any helpwould be greatful!

Thanks,

KJ

<?php

$db=mysql_connect(&quot;localhost&quot;,&quot;user&quot;,&quot;&quot;)
or die(&quot;Could not connect : &quot; . mysql_error());

mysql_select_db(&quot;mylogin&quot;) or die(&quot;Could not select database&quot;);


$query = (&quot;SELECT * FROM myimage &quot;);


$result = mysql_query($query) or die(&quot;Query failed : &quot; . mysql_error());
$num_rows = mysql_num_rows($result);


while ($row= mysql_fetch_array($result))

{

print &quot;<table width=75% border=1>\n&quot;;
print &quot;<td width=8%>&quot;;
echo '<img src=' .$row['imagefilepath']. '>';

print &quot;</td>&quot;;


}



?>
 
your query seems to be retrieving all rows in the table

you say you want to retrieve them one at a time, so how do you specify which one you want?

the answer is, you have to feed in some type of qualifier in the query's WHERE clause

select * from myimages
where imageid = 34

how do you know which value to feed in?

it's usually determined by some sort of choice on the part of the user

for example, your home page might have a link for the most popular image, e.g. <a href=showimage.php?id=34&quot;>smiley</a>

then your php page takes the id from the url and feeds it to the query

does that make sense?

rudy
 
Yes, Rudy. The above makes sense. Here's the senerio. The user uploads an image (artwork). MySql inserts the link in the database. Cool. Not problem here. I understand that a value must be entered as stated '<a href=showimage.php?id=34&quot;>smiley</a>'.

The qestion is there a simple way that the user can simply press the same link over and over to scroll through the different records in the database?

Thanks,
KJ

 
in the script which retrieves image 34, you will, as sleipnir214 suggests, also want to retrieve the id which you will format as the &quot;next&quot; link

select min(id) as &quot;next&quot;
from myimages
where imageid > 34

you might also want

select max(id) as &quot;prev&quot;
from myimages
where imageid < 34

so this makes 3 separate queries to run in the one script to display an image with next and prev links

be careful to check for no results in the next/prev queries, which indicate that you are sitting on the first or last image, and thus should &quot;grey out&quot; that link

rudy
 
kjspear:
<sarcasm>
You're thinking like a desktop application programmer again, and I distinctly remember telling you to stop that.
</sarcasm>

Remember, on a desktop application, it just might be the same button over and over. But in a web application, every time the page is refreshed, it's a brand new button. It may look like the old button. It may act like the old button. But it is a brand new button, produced by a brand new run of your program.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
O.K, guys. I figured it out. It was so simple I almost got sick. In my situation, I needed numbers to increment as the user scrolls through the database. So I used the session variable. See below;

session_start();
session_register('count');

// increment count

$count++;
echo $count;
$db=mysql_connect(&quot;localhost&quot;,&quot;user&quot;,&quot;password&quot;)
or die(&quot;Could not connect : &quot; . mysql_error());
print &quot;Connected successfully&quot;;
print &quot;<br>&quot;;
mysql_pconnect();
mysql_select_db(&quot;mylogin&quot;) or die(&quot;Could not select database&quot;);

$query = (&quot;SELECT * FROM myimagexx where ID=$count&quot;);


$result = mysql_query($query) or die(&quot;Query failed : &quot; . mysql_error());
$num_rows = mysql_num_rows($result);

while ($row= mysql_fetch_array($result))

{
print &quot;<table width=75% border=1>\n&quot;;
print &quot;<td width=8%>&quot;;

$image = $row['imagefilepath'];
echo &quot;<img src=$image>&quot;;

print &quot;</td>&quot;;

}

/* Free resultset */
mysql_free_result($result);

/* Closing connection */
mysql_close($db);

?>

Right now, when the page is revisted by the user in the current session (here I just refresh the browser until I put the appropiate links in.), the $count increases by one. That number is then passed to the Where ID= query. I just have to clean up the script. But now I see how I can do it.

Thanks for your patience and assistance.

KJ
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top