Results 1 to 9 of 9
Thread: PHP Question
-
05-27-2004, 03:28 PM #1Junior Guru Wannabe
- Join Date
- May 2004
- Posts
- 84
PHP Question
Good morning, noon, or night. I'm in the process of building a website with PHP and a mySQL database backend. Here is my question:
If I'm pulling info out of a database with
$result=mysql("$DBName","SELECT * FROM Items WHERE Category='$CA' ORDER BY ItemName");
and creating an array with the rows to assign variables so that I can display the info in my chosen format.
How would I then go about splitting the info up if the records exceeded a given amount. Like having a little clickable 1, 2, 3, in the bottom corner so that the display page doesn't end up way too long if there are lots of records in the table. I'm guessing I would create an if statement whereby rows exceeding a specified total would be displayed on a second page but I'm not sure how the links would work and what the functions would look like. Please assist if you are able. Thanks.
-
05-27-2004, 04:00 PM #2Web Hosting Master
- Join Date
- Mar 2004
- Location
- USA
- Posts
- 4,345
you will have to use the mysql_fetch_array, then the subscript will be accordingly with those in data base (e.g. if userid if first fild, subscript 0 will be the userid and so on)
PHP Code://This if you only have one record you want to display
$split_rows = @mysql_fetch_array($result);
//Displaying all records
while( $split_rows = @mysql_fetch_array($result))){
echo 'userid is: ' . $split_rows[0];
echo 'name is:' . $split_rows[1];
echo 'email is:' . $split_rows[2];
// You get the picture and so on..
}
Testing 1.. Testing 1..2.. Testing 1..2..3...
-
05-27-2004, 04:18 PM #3Web Hosting Master
- Join Date
- Jan 2003
- Posts
- 1,715
I think he means record count / page numbering.
Look up the 'LIMIT' clause in the MySQL docs. You may also want to use the COUNT(*) function to determine how many pages of results you have.Game Servers are the next hot market!
Slim margins, heavy support, fickle customers, and moronic suppliers!
Start your own today!
-
05-27-2004, 11:12 PM #4Web Hosting Master
- Join Date
- Mar 2004
- Location
- USA
- Posts
- 4,345
sometimes I do read too fast ...lol
On top of page
PHP Code:if(isset($_GET['np'])) {
$num_pages = $_GET['np'];
} else {
$query = "SELECT * FROM listings WHERE `list_id`='$scat'";
$query_result = mysql_query($query);
$num_records = mysql_num_rows($query_result);
if($num_records > $display) {
$num_pages = ceil ($num_records/$display);
} else {
$num_pages = 1;
}
}
if(isset($_GET['s'])){
$start = $_GET['s'];
} else {
$start = 0;
}
$query = "SELECT * FROM listings WHERE `list_id`='$scat' ORDER BY list_title ASC LIMIT $start,$display";
$query_result = mysql_query($query);
$count= mysql_num_rows($result);
PHP Code:f($count > $display){
echo '<p align="center">';
$current_page = ($start/$display)+1;
if($current_page != 1) {
echo '<a href="showscat.php?scat=' . $scat . '&s=' . ($start-$display) . '&np=' . $num_pages . '" class="link" onmouseover="return vprev()" onmouseout="return clearit()">Previous</a>';
}
for ($i = 1; $i <= $num_pages ; $i++) {
if($i != $current_page){
echo ' [<a href="showscat.php?scat=' . $scat . '&s=' . (($display * ($i - 1))) . '&np=' . $num_pages . '" class="link" onmouseover="return vpage()" onmouseout="return clearit()">' . $i . '</a>] ';
} else {
echo '<font color="gray"> [' . $i . '] </font>';
}
}
if($current_page != $num_pages){
echo '<a href="showscat.php?scat=' . $scat . '&s=' . ($start+$display) . '&np=' . $num_pages . '" class="link" onmouseover="return vnext()" onmouseout="return clearit()" >Next</a>';
}
echo '</p>';
}
edit the above based on ur specisfcation
salam,Testing 1.. Testing 1..2.. Testing 1..2..3...
-
05-28-2004, 12:31 PM #5Junior Guru Wannabe
- Join Date
- May 2004
- Posts
- 84
Hey Thanks a million for your help! I really appreciate it.
-
05-28-2004, 01:05 PM #6Web Hosting Master
- Join Date
- Mar 2004
- Location
- USA
- Posts
- 4,345
$20 bucks :p
Testing 1.. Testing 1..2.. Testing 1..2..3...
-
05-29-2004, 02:46 AM #7Web Hosting Master
- Join Date
- Jul 2003
- Location
- Kuwait
- Posts
- 5,104
You can also get (for free) the PEAR:B_Pager class which does this and a whole lot of other things.
-
05-29-2004, 05:43 AM #8Junior Guru
- Join Date
- Apr 2004
- Posts
- 245
Originally posted by azizny
$20 bucks :p
-
05-29-2004, 09:50 AM #9Web Hosting Master
- Join Date
- Mar 2004
- Location
- USA
- Posts
- 4,345
Originally posted by shraz
how about a kick in da nuts?
I'll stick to the 20 bucksTesting 1.. Testing 1..2.. Testing 1..2..3...