PDA

View Full Version : PHP display per page counter


Will
12-05-2001, 07:09 PM
ok, on my illustrious news script i wrote the submit part today, it works great all the fields are entered correctly. however my main page will be 50000000 scroll lengths long, how do i limit the number of news items to display on the page? i havent written the news page yet however if you need to see my code i can write it tonight and post it tomorrow.

thanks,
Will

scoutt
12-05-2001, 11:23 PM
will the news scripts being in the database?

if so you can have it limit the SQL select to only show so many.

Will
12-06-2001, 11:06 AM
the page that will display the news will more than likely be index.php, but all the news, including all of the attributes that go with that news post, will be in the database. so index php will look something like this:

<?
include "header.php";

insert info from db

include "footer.php";
?>

thats basically how it will be layed out.

scoutt
12-06-2001, 12:09 PM
well aren't you in luck. I jsut taught myself how to do this exact thing on my site. I was so confused until I figured it out.

on my site there are a couple of tutorials for php navigation or something or other.

here is one way (http://www.snippetlibrary.com/tutorials.php?kid=16&catname=Navigation)

if you get confused I can tell you how I did it.

Will
12-06-2001, 01:03 PM
im afraid that wen tway over my head =\ :(

scoutt
12-06-2001, 01:35 PM
no surprise there as it did the same for me. :D

well it would go somehting like this.

if ($submit)
if(!$num_links2){
$num_links = '0';
}
if ($num_links2 < 1 ): // do not reset offset unless a new search
if ($offset == 0 ):
$offset = '0'; // The default offset is zero
endif;
endif;
$comma = ",";
$limitq = "LIMIT " . $offset . $comma . $number_of_ads_per_page;


$sql_select = "select * from $news_tbl where whatever = '$here' $limitq";
$sql_select2 = "select * from $news_tbl where whatever = '$here'";
$result = mysql_query ($sql_select);
$num_links = mysql_num_rows($result);
$result2 = mysql_query ($sql_select2);
if($offset == 0){
$num_links2 = mysql_num_rows($result2);
}
$resultmax = $offset + $number_of_ads_per_page; // assign the outer result range number
if ( $resultmax > $num_links2 ):
$resultmax = $num_links2;
endif;
$firstrowonpage = $offset + 1;
if (!$sql_select)
{echo mysql_error();}

echo"<b>Total: $num_links2 &nbsp;&nbsp;&nbsp;Showing Results $firstrowonpage to $resultmax </b>

that is the part that that counts how many you have and displays the total of how many.
this next part shows the button for the next page and set the limit to the next one.

print("<tr><td align=\"center\" colspan=\"2\">");
$pagestoshow = $num_links2 - $number_of_ads_per_page;
if ( $offset < $pagestoshow ): // MULTIPAGE - if present offset is less than the number of the returned rows then do multipage
$displayNextButton = "true";
$offset = ($offset + $number_of_ads_per_page); // here the offset is incremented to point to the next set of results desired
include("nextpage.php");
else:
echo"<input class=\"box\" type=\"button\" name=\"goBack\" value=\"$previous_ads\" onclick=\"history.back(1)\">";
echo "<b>&nbsp;&nbsp;&nbsp;End of Results</b>";
echo"</form>";
endif;
print("</td></tr>");
echo "</table>";

and here is the nextpage.php


<form name="Nextpage" method="post" action="<? $PHP_SELF ?>">
<?
// Previous and next link generator

if ( $offset > 15 ){
echo"<input class=\"box\" type=\"button\" name=\"goBack\" value=\"$previous_ads\" onclick=\"history.back(1)\">";
}
if ( $displayNextButton == "true" ):
?><input class="box" type="submit" name="submit" value="<? echo $next_ads ?>">
<?

endif;

// END of Previos and next link generator
?>
<input type="hidden" name="catid2" value="<?php echo $catid2;?>">

<input type="hidden" name="offset" value="<?php echo $offset;?>">
<input type="hidden" name="num_links2" value="<?php echo $num_links2;?>">

if you have any questions let me know. but look it over and see if htere is anyhting you don't understand. :)

Will
12-06-2001, 07:30 PM
Originally posted by scoutt
[B]no surprise there as it did the same for me. :D

well it would go somehting like this.

if ($submit)
if(!$num_links2){
$num_links = '0';
}
if ($num_links2 < 1 ): // do not reset offset unless a new search
if ($offset == 0 ):
$offset = '0'; // The default offset is zero
endif;
endif;
$comma = ",";
$limitq = "LIMIT " . $offset . $comma . $number_of_ads_per_page;


$sql_select = "select * from $news_tbl where whatever = '$here' $limitq";
$sql_select2 = "select * from $news_tbl where whatever = '$here'";
$result = mysql_query ($sql_select);
$num_links = mysql_num_rows($result);
$result2 = mysql_query ($sql_select2);
if($offset == 0){
$num_links2 = mysql_num_rows($result2);
}
$resultmax = $offset + $number_of_ads_per_page; // assign the outer result range number
if ( $resultmax > $num_links2 ):
$resultmax = $num_links2;
endif;
$firstrowonpage = $offset + 1;
if (!$sql_select)
{echo mysql_error();}

echo"<b>Total: $num_links2 &nbsp;&nbsp;&nbsp;Showing Results $firstrowonpage to $resultmax </b>



ok i get that this portion is for when someone searches, but i want the page to limit the number of posts when it is displayed automatically. what i mean is the news page will be my index page, and the index page will be displaying the news, its just that i dont want 200 news items on the front page, if im looking at this correctly doesnt the second block of code display a next button to show the next page of data? still very confused :(

Will
12-06-2001, 07:32 PM
do {
printf("<b>Personality:</b><br>%s<br><b>Position held:</b><br>%s<br><b>Email:</b><br><a href=mailto:$myrow[email]>%s</a><br><b>Aim/Icq:</b><br>%s<br><b>Location:</b><br>%s&nbsp;<div align=left><hr width=30px>\n", $myrow["name"], $myrow["position"], $myrow["email"], $myrow["aimicq"], $myrow["locale"]);
} while ($myrow = mysql_fetch_array($result));
echo "<br><font size=-2>to join our staff click <a href=/aboutus/join.php>here</a>\n";
} else {
echo "Sorry, no records were found!";
}

?>


that is the code for another part of my site, which gets the info about our staff and displays it on the page, now couldnt i just assign a counter variable at the top of the page and then in that do loop create a counter? the counter could count each news item displayed and then an if statement to limit how many counts the counter goes through?

scoutt
12-06-2001, 09:28 PM
in theory that sounds good, but you have to know how many is coming from the database (num_rows). you might get away with it, but the way I used makes a button on the page to show the next page and that button gets the rest of the rows that was not displayed.

you would still be using the limit tag in the select statement to get the rest of the rows to display.

Will
12-06-2001, 09:31 PM
im still lost :(:(

scoutt
12-06-2001, 09:40 PM
ok, take out your cotton balls and clean your ears :D:D j/k

if you use that code to get the info from the database it will display what ever the counter is set to, correct. then when you want to display the rest on another page the database will start from the beginning again and display the same results. that is why you need to use the limit in the select so it will only show what the limit is set at.

if limit is set at 4 then it will only show 4 articles on the page. so you make your counter set to 4 and have limit set to your counter. then you will need to have that counter goto the next page but increment to the next set of articles. so you have 4 on the next page then you will have to have it set at 8 and have another counter at 4 so it will show 4 to 8.

is that a little better?

I guess it makes a difference if you have only 4 articles showing and that is it. will you be having another page to show the rest?

Will
12-06-2001, 09:56 PM
Originally posted by scoutt
I guess it makes a difference if you have only 4 articles showing and that is it. will you be having another page to show the rest?

no, well, i was going to have it so the rest will just be kept in the database, so that if the users want to look at previous articles all they have to do is search the archives, that way they can search by month, day, and keyword, but not many sites have links to the rest of their articles on the site.... i kinda like that idea. alot...

Will
12-06-2001, 10:03 PM
why is there an if($submit) there though... that still puzzles me

scoutt
12-06-2001, 11:02 PM
well that makes a big difference. if you only have 4 or so showing all you have to do is put the limit in the select statement and it will show the first 4 new ones only.

then you don't need the counter. and the rest of that script I showed you. ignore the if($submit) statement. :)

I was going over board as I thought you wanted something different. sorry,

so just use this

$comma = ',';
$offset = '0';
$number_of_ads_per_page = '4';
$limitq = "LIMIT " . $offset . $comma . $number_of_ads_per_page;
$sql_select = "select * from $news_tbl where whatever = '$here' $limitq";

Will
12-06-2001, 11:33 PM
well, i kinda like the idea of doing it your way with a "next page - previous page" link at the bottom.
however ill mess with this tomorrow as ive got about 3 hours of statistics to do tonight *sigh* hehe :)