FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


How to.....





salman_500
hey,

i wana know this thing... you see i have this code...

Code:

$tempreqtype = ucfirst($_GET['type']);
$tempreq1 = "SELECT * FROM templates WHERE type = '$tempreqtype' ORDER BY 'name' ASC";
$tempreq2 = mysql_query($tempreq1) or die(mysql_error());
while ($tempreq3 = mysql_fetch_array($tempreq2)) { ..........................


i wana know...how can i display this code over various pages automatically and also show a navigation like... 1 2 3 ...etc...

i want the number of templates to be displayed, to be customizable...

thnx ! Very Happy
hexkid
salman_500 wrote:
how can i display this code over various pages automatically and also show a navigation like... 1 2 3 ...etc...

You use LIMIT in your SQL statement.

Let's say, to make it easier, your table has 24 records and you want 10 records per page.
The first page you get with
Code:
SELECT ... LIMIT 0, 10

The second page you get with
Code:
SELECT ... LIMIT 10, 10

The third page you get with
Code:
SELECT ... LIMIT 20, 10


So, if you have a variable with the page number
Code:
$page = (int)$_GET['p'];
use that for the query
Code:
SELECT ... LIMIT ($page-1)*10, 10


For the navigation you need to know how many records there are in the complete set
Code:
select count(*) ...;
$records = mysql_result();
$pages = floor(($records + 9) / 10);


Now, just put it all together ... Smile
salman_500
hey,

thnx but....it seems to give me and error...

Code:
You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '(1-1)*10, 10' at line 1


i dunno wats rong....tried a couple combinations but still didnt work....

and how can i actually display the nave... like..

<< | < | 1 | 2 | 3 | 4 | 5 | > | >>

thnx !
hexkid
salman_500 wrote:
it seems to give me and error...

Code:
You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '(1-1)*10, 10' at line 1


i dunno wats rong....tried a couple combinations but still didnt work....

You have to make the calculation in PHP; MySQL won't do it Sad
Code:
$start_record = ($page - 1) - 10;
$sql = "SELECT ... LIMIT $start_record, 10"


salman_500 wrote:
and how can i actually display the nave... like..

<< | < | 1 | 2 | 3 | 4 | 5 | > | >>
Well ... let's say you have 5 pages ($page_count) and you're currently viewing page 3 ($page).
Code:
echo '<a href="display.php?p=1">&lt;&lt;</a> | <a href="display.php?p="', $page-1, '>&lt;</a>';
for ($i = 1; $i <= $page_count; ++$i) {
  echo ' | <a href="display.php?p=', $i, '>', $i, '</a>';
}
echo ' | &gt; | &gt;&gt;'; ### needs hrefs and stuff; I'll leave that to you

Start with this code and change it to suit your needs.
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.