Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Displaying Database Results on page
Old 05-16-2005, 11:58 AM Displaying Database Results on page
Experienced Talker

Posts: 32
Trades: 0
Hi there,

I am using the following code to display results drawn from MYSQL databse on my webpage:


<?php
include("....");

$connection = mysql_connect($host,$user,$password)
or die ("couldn't connect to server");
$db = mysql_select_db($database,$connection)
or die ("Couldn't find databse");

/* Select pets of the given type */
$query = "SELECT * FROM Blah Blah";
$result = mysql_query($query)
or die ("Couldn't execute query.");
echo "<tr><td class='header' colspan='7'>Under Fiver</td></tr>\n";
echo "<tr class='film_box' colspan='7'>\n";
echo "<td width='5'></td>";
while ($row = mysql_fetch_array($result,MYSQL_ASSOC))
{




/*display row for each pet */

if ($row['TitleID'] > 100000)
{
echo "<td height='60' width ='100' align='center'> <img src='../images/images/V0{$row['TitleID']}.JPG' border='0' width='60' height='85' alt='{$row['Title']}'>
<br>
{$row['Title']}
<br>
}
else {
echo "<td height='60' width ='100' align='center'> <img src='../images/images/V0{$row['TitleID']}.JPG' border='0' width='60' height='85' alt='{$row['Title']}'>
<br>
{$row['Title']}
<br>
}
echo "<td width='5'></td>";

}echo "</tr>";
?>



The command works fine, but it displays every result in the same line and i wish to limit the amount of results displayed per row to 7.

How do i do this? If you need me to explain with more clarity then let me know and i'll do my best!

Thanks in advance for any help.
hershel is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-16-2005, 12:03 PM
OmuCuSucu's Avatar
Vi Veri Veniversum Vivus

Posts: 1,168
Name: Dragos-Valentin
Location: Cluj-Napoca, RO
Trades: 0
add a for(),
for ($i = 0; $i < 7 ; $i++) {
[[[[[[do the echoing here]]]]
}

would that help?
__________________
.
» Please remember to add to my Talkupation if you enjoyed my post. Thank you :)
.
OmuCuSucu is offline
Reply With Quote
View Public Profile
 
Old 05-16-2005, 12:12 PM
Experienced Talker

Posts: 32
Trades: 0
would i put that in before the "where" statement?
hershel is offline
Reply With Quote
View Public Profile
 
Old 05-16-2005, 12:25 PM
OmuCuSucu's Avatar
Vi Veri Veniversum Vivus

Posts: 1,168
Name: Dragos-Valentin
Location: Cluj-Napoca, RO
Trades: 0
what does if do and what does else? (in "display row for each pet")

i am too tired to try and follow the code right now.
__________________
.
» Please remember to add to my Talkupation if you enjoyed my post. Thank you :)
.
OmuCuSucu is offline
Reply With Quote
View Public Profile
 
Old 05-16-2005, 02:48 PM
Experienced Talker

Posts: 32
Trades: 0
these can be ignored - they are specific to the IDs given to each pet
hershel is offline
Reply With Quote
View Public Profile
 
Old 05-16-2005, 03:23 PM
leavethisplace's Avatar
Ultra Talker

Posts: 297
Trades: 0
no no no!! There is no need to use PHP for this, mySQL can do this for you (it'll be faster too). mySQL has a handy little parameter for such functions as SELECT, called LIMIT.

Basically, this parameter tell mySQL to limit the result set to a certain record range. So for you're example you want 7 results per page, so we add this to the end of the SQL statement
PHP Code:
LIMIT 0 
This will give us the first 7 results (the second argument in the parameter) starting from the first record (which will always be 0).

But what if you want to show the next page?? Well that's pretty simple, all we have to do is supply an "offset" variable in the query string, so we've gone through the first 7 records right? We need the next 7, so our offset is going to be 8 (because the first record was 0, think bout it ). Thus our statement will be
PHP Code:
LIMIT 8 
This will give us 7 results, starting (and including) from the 8th record.
__________________
A lie gets halfway around the world before the truth has a chance to get its pants on. - Sir Winston Churchill

Please visit my sites:
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
leavethisplace is offline
Reply With Quote
View Public Profile
 
Old 05-16-2005, 03:50 PM
Experienced Talker

Posts: 32
Trades: 0
excellent - this is what i was looking for.

however i want to have more than one row on each page. so i appreciate that i just have to use LIMIT 8 , 7 and LIMIT 15,7 etc. But what if there are 1000's of results returned, i obviously don't want to have to do this a 1000 times!

any ideas?
hershel is offline
Reply With Quote
View Public Profile
 
Old 05-16-2005, 03:54 PM
OmuCuSucu's Avatar
Vi Veri Veniversum Vivus

Posts: 1,168
Name: Dragos-Valentin
Location: Cluj-Napoca, RO
Trades: 0
i think this would be a good while () .... but now i'm not sure i'm looking at the easiest solution again

use a variable for the count and multiply it by 7 on every step.
__________________
.
» Please remember to add to my Talkupation if you enjoyed my post. Thank you :)
.
OmuCuSucu is offline
Reply With Quote
View Public Profile
 
Old 05-16-2005, 09:27 PM
leavethisplace's Avatar
Ultra Talker

Posts: 297
Trades: 0
I'm not sure im understanding you clearly? The SQL statement won't return one row, it will return 7 into the mysql_fetch_array(), so the while that you have there (while ($row = mysql_fetch_array($result,MYSQL_ASSOC))) does that anyway.

That's what the loop is for, so you don't write out seperate code for every result row.

To find out how many pages you're going to need per search, all you do is divide the total result set for the search (so you'll have to do a seperate SQL SELECT before any code) and divide it by 7 (or however many results per page) and then get PHP to round to the nearest, highest number.
__________________
A lie gets halfway around the world before the truth has a chance to get its pants on. - Sir Winston Churchill

Please visit my sites:
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
leavethisplace is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Displaying Database Results on page
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.33611 seconds with 12 queries