I'm about to start work on a new project and wanted some advice before beginning.
I have a MySQL table that I need to display on my website. The table has over 100 million lines in it. The table has 2 columns in it: domainid and domainname.
The domainid column is simply numbers starting at 1 and going up to over 100 million. In the domainname table I have a list of domains such as this:
test1
test2
test3
test4
test5
test6
Basically what I'm trying to do is this:
I want to display 1000 domains per page on my website - in a table. The table would be 4 columns and 250 lines. To start with, I'm not sure exactly how to get data from the database and actually display it in my HTML table. I know it can be done with PHP of course, just not sure how.
The second thing I need to add is some sort of pagination. It doesn't need to be pretty, I just need a way to have pages auto generated at the bottom of the list so that people can go from one page to the next and view 1000 records on each page - obviously I wouldn't want all 100 million records displayed on a single page.
So here's the problem I'm having: I'm not sure how to get the MySQL data and display it on my website quickly - I don't want it taking forever to load the pages just because the database is large. And second I really need a way to add pagination to the table.
If anyone could point me in the right direction, it would be much appreciated. I'm not sure how difficult doing this is going to be, but any assistance you can provide would be great.
This will give you the total number of rows
Select count(*) from yourtable
Then to display a page, do some whizzy maths like this
$rows_per_page = 1000;
$page_no = 2; // hint, start counting pages at page 0, it will make things easier
$offset = $page_no * $rows_per_page;
// 0 = offset (where to start)
// 1000 = number of rows returned
select * from yourtable limit $offset,$rows_per_page
Because you have so many rows, I would use an html select to choose which page you want to go to, or just have a next and previous button and leave it like that
To figure out how many page there are, you need some more maths
$total_rows = 100000000;
$rows_per_page = 1000;
$num_pages = $total_rows / $rows_per_page;
USE THIS CODE :
Create pages config,paginate,index in php.
Here Paginate page creates design for format(prev and next button to scroll page).
Config create connection with database.
index page firstly define the variable for create number of result per page, page number , total result etc
index page(design) defines the how page show ,which can be modify by you according to you priority to show page design.
CONFIG.PHP
$mysql_hostname = "host";
$mysql_user = "usr";
$mysql_password = "pass";
$mysql_database = "db";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Opps some thing went wrong");
mysql_select_db($mysql_database, $bd) or die("Error on database connection");
PAGINATE.PHP
function paginate($reload, $page, $tpages) {
$adjacents = 2;
$prevlabel = "‹ Prev";
$nextlabel = "Next ›";
$out = "";
// previous
if ($page == 1) {
$out.= "<span>".$prevlabel."</span>\n";
} elseif ($page == 2) {
$out.="<li>".$prevlabel."\n</li>";
} else {
$out.="<li>".$prevlabel."\n</li>";
}
$pmin=($page>$adjacents)?($page - $adjacents):1;
$pmax=($page<($tpages - $adjacents))?($page + $adjacents):$tpages;
for ($i = $pmin; $i <= $pmax; $i++) {
if ($i == $page) {
$out.= "<li class=\"active\"><a href=''>".$i."</a></li>\n";
} elseif ($i == 1) {
$out.= "<li>".$i."\n</li>";
} else {
$out.= "<li>".$i. "\n</li>";
}
}
if ($page<($tpages - $adjacents)) {
$out.= "<a style='font-size:11px' href=\"" . $reload."&page=".$tpages."\">" .$tpages."</a>\n";
}
// next
if ($page < $tpages) {
$out.= "<li>".$nextlabel."\n</li>";
} else {
$out.= "<span style='font-size:11px'>".$nextlabel."</span>\n";
}
$out.= "";
return $out;
}
index.php
<?php
include('config.php'); //include of db config file
include ('paginate.php'); //include of paginat page
$per_page = 1000; // number of results to show per page
$result = mysql_query("SELECT * FROM YOURTABLE");
$total_results = mysql_num_rows($result);
$total_pages = ceil($total_results / $per_page);//total pages we going to have
//-------------if page is setcheck------------------//
if (isset($_GET['page'])) {
$show_page = $_GET['page']; //current page
if ($show_page > 0 && $show_page <= $total_pages) {
$start = ($show_page - 1) * $per_page;
$end = $start + $per_page;
} else {
// error - show first set of results
$start = 0;
$end = $per_page;
}
} else {
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
}
// display pagination
$page = intval($_GET['page']);
$tpages=$total_pages;
if ($page <= 0)
$page = 1;
?>
// DESIGNING and displaying data(code continuing in index.php)
<?php
$reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages;
echo '<div class="pagination"><ul>';
if ($total_pages > 1) {
echo paginate($reload, $show_page, $total_pages);
}
echo "</ul></div>";
// display data in table
echo "<table class='table table-bordered'>";
echo "<thead><tr><th>DOMAIN CODE</th> <th>DOMAIN Name</th></tr></thead>";
// loop through results of database query, displaying them in the table
for ($i = $start; $i < $end; $i++) {
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) {
break;
}
// echo out the contents of each row into a table
echo "<tr " . $cls . ">";
echo '<td>' . mysql_result($result, $i, 'domainid') . '</td>';
echo '<td>' . mysql_result($result, $i, 'domainname') . '</td>';
echo "</tr>";
}
// close table>
echo "</table>";
// pagination
?>
Related
I am using this coed in PHP to show next and previous buttons for records in a mysql database:
$sql="SELECT * from customer";
$rs=mysql_query($sql,$conn) or die(mysql_error());
$MaxRowsPerPage = 25;
$total_records = mysql_num_rows($rs);
$total_pages = ceil($total_records / $MaxRowsPerPage);
if(isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page=1;
}
$start_from = ($page-1) * $MaxRowsPerPage;
$sql.=" LIMIT $start_from, $MaxRowsPerPage";
I am echoing $total_records to show the total amount, how can i show the number from and to on the current page. for example, on page 1 it will be showing records 1 to 25 (because max rows per page is 25) and then page 2 will be showing records 26 to 50 and so on...
There a are many ways of doing this, but here's a simple pagination example I made. It will also show 1-25, 26-50 etc. It's heavily commented so it should be easy to understand.
<?php
// Connect to database
include 'includes/db_connect.php';
// Find total number of rows in table
$result = mysql_query("SELECT COUNT(*) FROM example_table");
$row = mysql_fetch_array($result);
$total_rows = $row[0];
// Set rows per page
$rows_per_page = 25;
// Calculate total number of pages
$total_pages = ceil($total_rows / $rows_per_page);
// Get current page
$current_page = (isset($_GET['p']) && $_GET['p'] > 0) ? (int) $_GET['p'] : 1;
// If current page is greater than last page, set it to last.
if ($current_page > $total_pages)
$current_page = $total_pages;
// Set starting post
$offset = ($current_page - 1) * $rows_per_page;
// Get rows from database
$result = mysql_query("SELECT * FROM example_table LIMIT $offset, $rows_per_page");
// Print rows
echo '<hr>';
while($row = mysql_fetch_array($result))
{
echo $row['id'].'<br />';
echo $row['text'];
echo '<hr>';
}
// Build navigation
// Range of pages on each side of current page in navigation
$range = 4;
// Create navigation link for previous and first page.
if ($current_page > 1)
{
$back = $current_page - 1;
echo ' PREV ';
if ($current_page > ($range + 1))
echo ' 1... ';
}
else
echo ' PREV ';
// Create page links, based on chosen range.
for ($i = $current_page - $range; $i < ($current_page + $range) + 1; $i++)
{
if ($i > 0 && $i <= $total_pages)
if ($i == $current_page)
echo ' [<strong>'.$i.'</strong>] ';
else
echo ' '.$i.' ';
}
// Create navigation link for next and last page.
if ($current_page != $total_pages)
{
$next = $current_page + 1;
if (($current_page + $range) < $total_pages)
echo ' ...'.$total_pages.' ';
echo ' NEXT ';
}
else
echo ' NEXT ';
?>
I was able to create a working pagination system for my application. But the problem I am having is when a user does a search, it can/will display over 100 pages in the pagination.
I am trying to figure out how to show only like 5 on each side of the current page. I would like to create a FIRST page button, and a LAST page button, but I'll deal with that later.
So here is the first part of code that counts the records in the database table:
<?php
function countRecords()
{
// The application takes a lot of user input, which then builds a query here.
// The user input goes into a session variable called $_SESSION['where']
// I'll start with the actual query code
$sql = "SELECT COUNT(DISTINCT CONTAINER_NUMBER) AS TOTAL
FROM 'contTable'" . " WHERE (" . $_SESSION['where'] . ");";
$sqlres = #mysql_query($sql) or die();
$row = mysql_fetch_row($sqlres);
$return $row[0];
}
So code above gets the count from the table depending on the search criteria.
Here is the next piece of code that prints the grid. I'll keep it as short as possible:
function displayrecords()
{
$rec_limit = 100;
$targetpage = "containers.php";
if(isset($_GET['page']))
{
$page = $_GET['page'];
$offset = $rec_limit * ($page - 1);
}
else
{
$page = 1;
$offset = $rec_limit * ($page - 1);
}
$left_rec = countRecords() - ($page * $rec_limit);
$select = "";
$_SESSION['where'] = "";
// user input variables are here that build a variable called $_SESSION['where']
// I'll skip the code down to the query
if ($_SESSION['where'] != "") $select = "SELECT * FROM 'contTable'" . " WHERE
(" . $_SESSION['where'] . ") GROUP BY container, bol LIMIT " . $offset . ",
" . $rec_limit . ";";
}
Please excuse any typos or missing brackets and whatnot. Just know the above code works.
Now here is the code for the rest of the pagination:
$total_records = countRecords();
$total_pages = ceil($total_records / $rec_limit);
$adjacents = 5; // I just added this variable. I don't know what to do with it
$previousPage = $page - 1;
$nextPage = $page + 1;
$querystring = "";
foreach ($_GET as $key => $value)
{
if ($key != "page") $querystring .= "$key=$value&";
}
echo '<ul style="border: 0px solid red; margin: 3px;" class="pager">';
if ($left_rec < $rec_limit)
{
$last = $page - 2;
echo #"<li><a href=\"$targetpage?page=$previousPage&$querystring\">
Previous</a></li>";
for($i = 1; $i <= $total_pages; $i++)
{
echo #"<li " . ((($page+1)==$i)? "class=\"active\"" : "") . ">
$i</li>";
}
}
else if ($page == 0)
{
for($i = 1; $i <= $total_pages; $i++)
{
echo #"<li " . ((($page+1)==$i)? "class=\"active\"" : "") . ">
$i</li>";
}
echo #"<li>Next</li>";
}
else if ($page > 0)
{
$last = $page - 2;
echo #"<li><a href=\"$targetpage?page=$previousPage&$querystring\">
Previous</a></li> ";
for($i = 1; $i <= $total_pages; $i++)
{
echo #"<li " . ((($page+1)==$i)? "class=\"active\"" : "") . ">
$i</li>";
}
echo #"<li>Next</li>";
}
echo '</ul>';
}
So, with all of the code above, I can display a grid, and display the pages at the bottom of the application. But I don't want to show all 100 pages, only 5 on each side of the current page. I know I need to utilize the variable called $adjacents and plug it in to the paging portion of the code. But I'm not exactly sure how to do it.
I hope I am being clear on my request.
Please help.
Instead of looping through all of the pages:
for($i = 1; $i <= $total_pages; $i++)
Try doing something like this:
$start = ($page < $adjacents ? 1 : $page - $adjacents);
$end = ($page > $total_pages - $adjacents ? $total_pages : $page + $adjacents);
for($i= $start; $i <= $end; $i++)
//Here you can loop through the numbers within adjacents of the current page
Pagination problem in search result
I am trying to paginate the search results, Here is the code that i'm using to look up for the search queries from user's form. I am getting search results paginated, but when i click 2 nd page of results it shows undefined index for those item posted...While gone through tutorials i have seen to use $_GET instead of $_POST ,after doing that chane also no improvement in results
As i am new to this php code, can you guys help or guide me in the right direction?
/////////////test.php//////////////////
mysqli_report(MYSQLI_REPORT_ERROR);
// number of results to show per page
$per_page = 2;
// figure out the total pages in the database
if ($result = $mysqli->query("SELECT * FROM bridegroom where Gender like '%$Name%' and Castest like'%$caste%' and Location like '%$location%' order by AGE"))
{
if ($result->num_rows != 0){
$total_results = $result->num_rows;
// ceil() returns the next highest integer value by rounding up value if necessary
$total_pages = ceil($total_results / $per_page);
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
$show_page = $_GET['page'];
if ($show_page > 0 && $show_page <= $total_pages)
{
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}
else
{
$start = 0;
$end = $per_page;
}
}
else
{
$start = 0;
$end = $per_page;
}
// display pagination
echo "<p> <b>View search results:</b> ";
for ($i = 1; $i <= $total_pages; $i++)
{
if (isset($_GET['page']) && $_GET['page'] == $i)
{
echo $i . " ";
}
else
{
echo "<a href='test.php?page=$i'>$i</a> ";
}
}
echo "</p>";
// display data in table
// loop through results of database query, displaying them in the table
for ($i = $start; $i < $end; $i++)
{
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) { break; }
// find specific row
$result->data_seek($i);
$row = $result->fetch_row();
// echo out the contents of each row into a table
echo ("Name:$row[1]<br><span class=\"old\"> Age:$row[4]</span><br><span class=\"sold\">Caste:$row[5]</span><br><span class=\"old\">Location:</span><span class=\"sold\">$row[6]</span><br><br>");
}
// close table>
}
else
{
echo "No results to display!";
}
}
// error with the query
else
{
echo "Error: " . $mysqli->error;
}
// close database connection
$mysqli->close();
// display pagination
echo "<p> <b>Your search results:</b> ";
for ($i = 1; $i <= $total_pages; $i++)
{
if (isset($_GET['page']) && $_GET['page'] == $i)
{
echo $i . " ";
}
else
{
echo "<a href='test.php?page=$i'>$i</a> ";
}
}
echo "</p>";
?>
I am creating a page that lists celebrities on Twitter. The page displays 10 different results as it should for the first loop but it doesn't seem to update the $result to pull the next 10 values from the database (for the next set of pages etc).
<?php
// connect to the database
$con = mysql_connect("localhost","someuid","somepwd");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("celebrity_twitter", $con);
$result = mysql_query("SELECT * FROM celebrities");
// number of results to show per page
$per_page = 10;
$total_results = mysql_num_rows($result);
$total_pages = ceil($total_results / $per_page);
// check if the 'page' variable is set in the URL (ex: view-paginated.php?page=1)
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
$show_page = $_GET['page'];
// make sure the $show_page value is valid
if ($show_page > 0 && $show_page <= $total_pages)
{
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}
else
{
// error - show first set of results
$start = 0;
$end = $per_page;
}
}
else
{
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
}
// display data in table
echo "<table class='table table-striped'>";
echo "<tr> <th>Avatar</th> <th>Celebrity Name</th> </tr>";
// loop through results of database query, displaying them in the table
for($i = $start; $i < $end; $i++ && $row = mysql_fetch_array($result) )
{
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) { break; }
// echo out the contents of each row into a table
echo "<tr>";
echo "<td><img height='73' width='73' src=" . "http://api.twitter.com/1/users/profile_image?screen_name=" . $row['avatar'] . "&size=bigger></td>";
echo "<td><a href=" . $row['url'] . " target='_blank'>" . $row['uid'] . "</td></a>";
echo "</tr>";
}
// close table>
echo "</table>";
// display pagination
echo "<strong>Page: </strong>";
for ($i = 1; $i <= $total_pages; $i++)
{
echo "<a href='index.php?page=$i'>$i</a> ";
}
?>
I'm using this at the moment and it's working perfectly fine for me, I hope it works for you too : PHP Pagination
You are not positioning yourself in the database, you always read the first elements:
// loop through results of database query, displaying them in the table
for($i = $start; $i < $end; $i++ && $row = mysql_fetch_array($result) )
{
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) { break; }
The proper way of doing this would be using the LIMIT clause of MySQL, with a special clause for recovering the true number of records
SELECT SQL_CALC_FOUND_ROWS * FROM celebrities LIMIT $start, $resultsperpage;
SELECT FOUND_ROWS(); // This is number of celebrities
This means that you need to calculate $start "before" running the query, i.e., "before" you know whether $start is a valid start at all. So you have to prepare for the case in which no rows are returned.
A quick alternative, which is pretty wasteful, is to retrieve everything and display only wanted rows:
// loop through results of database query, displaying them in the table
for ($i = 0; $i < $total_results; $i++)
{
$row = mysql_fetch_array($result);
if ($i < $start) continue;
if ($i == $end) break;
LIMIT implementation
mysql_select_db("celebrity_twitter", $con);
// number of results to show per page
$per_page = 10;
// We REALLY ought to move on to PDO: mysql_* will be deprecated sooner or later
$query = mysql_query('SELECT SQL_CALC_FOUND_ROWS * FROM celebrities');
// check if the 'page' variable is set in the URL (ex: view-paginated.php?page=1)
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
$show_page = (int)$_GET['page'];
// make sure the $show_page value is valid
if ($show_page > 0)
$start = ($show_page -1) * $per_page;
else
$start = 0;
$query .= " LIMIT $start, $per_page;";
$paged = True;
}
else
{
$paged = False;
// The query having no LIMIT, it will retrieve everything
// if (!$paged), later we will display a link "Show Paged".
}
// Now run the query
$result = mysql_query($query);
// Also fetch REAL number of rows in table {{{
$exec = mysql_query("SELECT FOUND_ROWS() AS results;");
$tuple = mysql_fetch_assoc($exec);
$results= $tuple['results'];
mysql_free_result($exec);
unset($exec, $tuple);
// }}}
// If paging, calculate page number. Later, if ($paged), we'll display the pager.
if ($paged)
{
$maxpages = ceil($results / $per_page);
if ($page > $maxpages)
$page = $maxpages+1; // We fetch nothing.
else
$page = floor($start / $per_page)+1;
}
// display data in table. It is best to calculate $html first
// and then output it all together at end if no error.
$html = "<table class='table table-striped'>";
$html .= "<tr> <th>Avatar</th> <th>Celebrity Name</th> </tr>";
// loop through results of database query, displaying them in the table
// Here, we get all and only good results, so we need not check anything
while($row = mysql_fetch_array($result))
{
// echo out the contents of each row into a table
$html .= "<tr>";
}
I have a small problem with my PHP pagination, I have 6 records and I display 2 at a time, when I click on next and it displays from 2 to 4 records, that works fine, But to display from 4 to 6 records, that does not work. I am not sure what im doing wrong. Anyone have any ideas ? the problem is to do with the calculation for the Next records to be displayed
<?php
$per_page = 2;
$start = $_GET['start'];
$query = mysql_query("SELECT * FROM Directory");
$record_count = mysql_num_rows($query);
$record_count = round($record_count / $per_page);
if(!$start) {
$start = 0;
}
$query = mysql_query("SELECT * FROM Directory LIMIT $start,$per_page") or die(mysql_error());
$row = mysql_num_rows($query);
// Output Records here
// Setup next and previous button variables
$prev = $start - $per_page;
$next = $start + $per_page;
echo '<p> <h4>';
if($prev < 0) {
echo 'Previous';
} else {
echo '<a href="directory.php?start='.$prev.'>Previous</a>';
}
echo ' ' . $start . ' of ' . $record_count;
if($next < $record_count) {
echo ' <a href="directory.php?start='.$next.'>Next</a>';
} else {
echo ' Next';
}
echo '</h4> </p>';
?>
It looks like it's a formatting issue. When I look at your source for the URL http://gebsbo.limewebs.com/directory/directory.php?start=1 I see:
<br /><p> <h4>Previous 1 of 4 <a href="directory.php?start=3>Next</a></h4> </p> </body>
It looks like you're missing a quote on the href attribute.
In your code, you want:
echo 'Previous';
and
echo ' Next';
use this code for calculating :
$num = 10; // number of items on page
$p = $_GET['page']; // the var that comes from url (for ex: htttp://..../xxx.php?page=3)
$r = mysql_query(" select * from your_table ");
$posts=mysql_num_rows($r); // total posts
$total=(($posts-1)/$num)+1; // toatal pages
$total=intval($total);
$p=intval($p);
if(empty($p) or $p<0 or !isset($p)) $p=1;
if($p>$total) $p=$total;
$start=$p*$num-$num;
// here print the html for pagination (for ex: htttp://...../xxx.php?page=1 .. 2 ...3 .... and so on ...) you can make a for() here
$r = mysql_query(" select * from your_table limit ".$start.", ".$num);
while (....) {
.....
}