Menan Posted March 18, 2007 Share Posted March 18, 2007 hi every one,i have a doubt how to do pages indexing like for example http://www.tutorialized.com/ if u go there the tutorials indexed 10 in every pages. so how do we do that like send every ten to other pages using php and sql.??ThanksMenan Link to comment Share on other sites More sharing options...
Anders Moen Posted March 18, 2007 Share Posted March 18, 2007 It's called pagination.Here's a script that works: <?php // config------------------------------------- $host = "localhost"; //your database host $user = "username"; // your database user name $pass = "password"; // your database password $db = "db_name"; // your database name$table = "table_name"; // your table name$filename = "index.php"; // name of this file $option = array (5); // in the url you'll see &go=5, so if you write $option = array(5, 10, 15); you can write &go=5, &go=10 or &go=15 (after how many records that should display on the page).$default = 5; // This is what it displays if you don't change anything in the url (&go=some_number)$action = $_SERVER['PHP_SELF']; // if this doesn't work, enter the filename // end config--------------------------------- $opt_cnt = count ($option); $go = $_GET['go']; if ($go == "") { $go = $default; } elseif (!in_array ($go, $option)) { $go = $default; } elseif (!is_numeric ($go)) { $go = $default; } $nol = $go; $limit = "0, $nol"; $count = 1; $connection = mysql_connect ($host, $user, $pass) or die ("Unable to connect"); mysql_select_db ($db) or die ("Unable to select database $db"); // control query------------------------------ /* this query checks how many records you have in your table. change this to match your own table*/ $off_sql = mysql_query ("SELECT * FROM $table") or die ("Error in query: $off_sql".mysql_error()); $off_pag = ceil (mysql_num_rows($off_sql) / $nol); //-------------------------------------------- $off = $_GET['offset']; if (get_magic_quotes_gpc() == 0) { $off = addslashes ($off); } if (!is_numeric ($off)) { $off = 1; } if ($off > $off_pag) { $off = 1; } if ($off == "1") { $limit = "0, $nol"; } elseif ($off <> "") { for ($i = 0; $i <= ($off - 1) * $nol; $i ++) { $e = 0 + $i; $limit = "$e, $nol"; $count = $e + 1; } } // Query to extract records from database. Change this to match your own table, but leave "LIMIT $limit" part unchanged. $sql = mysql_query ("SELECT * FROM $table ORDER BY id DESC LIMIT $limit") or die ("Error in query: $sql".mysql_error()); while ($row = mysql_fetch_array($sql)) {echo 'This is where you select from the database.';$count += 1; } if ($off <> 1) { $prev = $off - 1; echo " <a href=\"$filename?page=news&offset=$prev&go=$go\">Previous</a> "; } for ($i = 1; $i <= $off_pag; $i ++) { if ($i == $off) { echo " [ <b> $i </b> ] "; } else { echo " <a href=\"$filename?page=news&offset=$i&go=$go\">$i</a> "; } } if ($off < $off_pag) { $next = $off + 1; echo " <a href=\"$filename?page=news&offset=$next&go=$go\">Next</a> "; } echo "<br />\r\n"; echo "Page <b>$off</b> of <b>$off_pag</b><br />\r\n";?> Just one thing: this script is taken directly of my simple forum so you might need to remove some tables and stuff. Link to comment Share on other sites More sharing options...
Menan Posted March 19, 2007 Author Share Posted March 19, 2007 ohhh true i get it now, thanks. i was searching for this on google i couldn't get any results thats y i posted here.thanks a lot andersmoenMenan Link to comment Share on other sites More sharing options...
Anders Moen Posted March 19, 2007 Share Posted March 19, 2007 No problem :)Glad I could help. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now