pagination within if not working properly - php

I am building a search page where the user can search by a field and then the table is displayed.I am trying to implement pagination through php in it but the pagination is not working.Any guidelines to paginate will be helpful.I am presenting the code of one if where the user has entered 1st field and the other fields are left blank,there are other condition as well.
if($item!="" && $brand=="" && $model=="") {
$sql ="SELECT * FROM table WHERE Product LIKE '%".$item."%' ";
$result = mysql_query($sql);
//$result1 = mysql_query($sql1);
$r = mysql_fetch_array($result);
$p=$r['Product'];
$b=$r['Brand'];
$m=$r['Model'];
if ($item!=$p) {
echo "<font style=\"color:Red;\"><h3>No Item Found!!!</h3></font>";
} else {
echo "<table class='table table-striped table-bordered bootstrap-datatable datatable'>
<thead>
<tr>
<th>Item</th>
<th>Brand</th>
<th>Model</th>
<th>Dealer Price</th>
<th>Quantity</th>
<th>Description</th>
</tr>
</thead>";
if (!(isset($pagenum))) {
$pagenum = 1;
}
//Here we count the number of results
//Edit $data to be your query
$data = mysql_query("SELECT * FROM table WHERE Product='$item' ") or die(mysql_error());
$rows = mysql_num_rows($data);
//This is the number of results displayed per page
$page_rows = 10;
//This tells us the page number of our last page
$last = ceil($rows/$page_rows);
//this makes sure the page number isn't below one, or more than our maximum pages
if ($pagenum < 1) {
$pagenum = 1;
} elseif ($pagenum > $last) {
$pagenum = $last;
}
//This sets the range to display in our query
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
$sqll ="SELECT * FROM table WHERE Product='$item' $max ";
$resultt = mysql_query($sqll);
echo "<tbody>";
while($row = mysql_fetch_array($resultt)) {
echo "<tr>";
echo "<td>" . $row['Product'] . "</td>";
echo "<td>" . $row['Brand'] . "</td>";
echo "<td>" . $row['Model'] . "</td>";
echo "<td>" . $row['Dprice'] . "</td>";
echo "<td>" . $row['Quantity'] . "</td>";
echo "<td>" . $row['Quality'] . "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
echo " --Page $pagenum of $last-- <p>";
// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum == 1) {
} else {
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
}
//just a spacer
echo " ---- ";
//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum == $last) {
} else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
}
} elseif($item=="" && $brand!="" && $model=="") {
...so on
I have tried paginating the table,10 rows are visible in the first page,but on going to the next page no table is showing,although there are other data s in the table.

The error is that you must add at the top of first file
$pagenum = filter_input(INPUT_GET, 'pagenum', FILTER_SANITIZE_NUMBER_INT);
You can't access to $pagenum directly without assign it a value from $_GET param.
I hope this is useful!

Related

Find % according to values in one column (Need to add iteration loop) in php

I want to update 3rd column according to values based in 2nd column. Right to now I could print % value only for 1st team correctly. Other teams have wrong values.
Any help please.
echo "<table border="."1"."align="."right".">
<tr>
<th align="."center".">ID</th>
<th>Internal Team Names</th>
<th width="."10px"." >Passed % for aw34_0530_tc1015</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['TeamInternalNames'] . "</td>";
$TotalTestsForTeam = mysqli_query($conn ,"SELECT * from aw34_0530_tc1015 where Application="."'".$application_l."'"); //rows for ace application
$TotalTestsForTeamCount = $TotalTestsForTeam->num_rows; //rows for ace application
$PassedTestsForTeam_query= "SELECT * from aw34_0530_tc1015 where Result='passed' AND Application='".$row['TeamInternalNames']."'"; //ace passed count
$passed = mysqli_query($conn ,$PassedTestsForTeam_query);
$TotalPaassedTestsForTeam = $passed->num_rows; //rows for ace application
//echo "Count of passed ace in aw34_0530_tc1015 = TotalPaassedTestsForTeam Results "; //rows for ace application
$passed_percentForTEam = ($TotalPaassedTestsForTeam/$TotalTestsForTeamCount)*100 | 0;
echo "<td>" . $passed_percentForTEam . "</td>";
// if ($row['Result'] == "passed") {
// echo "<td style="."background-color:#33FF38".">P</td>";
// } else {
// echo "<td style="."background-color:#FF334C".">F</td>";
// }
echo "</tr>";
}
echo "</table>";

Paginate multiple search query using single file

I have one file example.com/search_category.php. There are different subcategories in database. I don't want to open file for each category, rather I want to paginate using single file. If I open file for each category such as example.com/search_category/subcategory1.php, example.com/search_category/subcategory2.php etc then there is no problem. How is it possible to paginate using single file. I am using the following pagination code. Thanks in advance.
// how many rows to show per page
$rowsPerPage = 5;
// by default we show first page
$page_num = 1;
// if $_GET['page'] defined, use it as page number, $_GET gets the page number out of the url
//set by the $page_pagination below
if(isset($_GET['page'])){$page_num = $_GET['page'];}
//the point to start for the limit query
$offset = ($page_num - 1) * $rowsPerPage;
// Zero is an incorrect page, so switch the zero with 1, mainly because it will cause an error with the SQL
if($page_num == 0) {$page_num = 1;}
// counting the offset
$sql = "SELECT * FROM xyz WHERE subcategory='$subcategory' ORDER BY info_id ASC LIMIT $offset, $rowsPerPage ";
$res = mysql_query($sql) or die(mysql_error());
// how many rows we have in database
$sql2 = "SELECT COUNT(subcategory) AS numrows FROM xyz WHERE subcategory= '$subcategory' ";
$res2 = mysql_query($sql2) or die(mysql_error());
$row2 = mysql_fetch_array($res2);
$numrows = $row2['numrows'];
// print the random numbers
while($row = mysql_fetch_array($res))
{
extract ($row);
//Echo out your table contents here.
echo "<tr class=\"alt\" onmouseover=\"ChangeColor(this, true);\" \n";
echo "style=\"cursor: pointer;\"\n";
echo " onmouseout=\"ChangeColor(this, false);\" \n";
echo " onclick=\"DoNav('http://www.example.com/searched_page.php?info_id={$info_id}');\">\n";
echo "\n";
$info_id = $row['info_id'];
echo "</td>";
echo "<td>". $info_id . "</td>";
echo "<td>". $bname . "</td>";
echo "<td>". $text . "</td>";
echo "</tr>";
}
?>
<?php
// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);
$self = "http://www.example.com/search_category.php";
// creating 'previous' and 'next' link
// plus 'first page' and 'last page' link
// print 'previous' link only if we're not
// on page one
if ( $page_num > 1 ) {
$page = $page_num - 1;
$prev = " [Prev] ";
$first = " [First Page] ";
} else {
$prev = ' [Prev] '; // we're on page one, don't enable 'previous' link
$first = ' [First Page] '; // nor 'first page' link
}
// print 'next' link only if we're not
// on the last page
if ( $page_num < $maxPage ) {
$page = $page_num + 1;
$next = " [Next] ";
$last = " [Last Page] ";
} else {
$next = ' [Next] '; // we're on the last page, don't enable 'next' link
$last = ' [Last Page] '; // nor 'last page' link
}
// print the page navigation link
//echo $first . $prev . " Showing page <strong>$page_num</strong> of <strong>$maxPage</strong> pages " . $next . $last;
?>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center" class="smaller">
<?php
// print the page navigation link
echo '<br>';
echo $first . $prev . " Page <strong>$page_num</strong> of <strong>$maxPage</strong>" . $next . $last;
?>
</table>

"Next" button doesn't refresh page with next 25 results

I have the code below and am trying to get the next 25 results from my sql table to appear on page. However, whenever I click the next button, no information is displayed. I have my offset = ($page - 1) * $items_per_page......I'm struggling to figure this out as it seems so simple compared the other code I've written, but is proving to be very elusive to me....any assistance would be greatly appreciated. My primary issue is that the next link does not provide the next 25 results and I'm unable to determine why and how to correct.
echo "<h3 style='text-align:center;'>Welcome to the Exchange Portal, " . $row['name'] . "! </h3>";
$items_per_page = 25;
$sql_count = "SELECT pin, title, title2, email, phone FROM crown_acura";
$result_cnt = mysqli_query($conn, $sql_count);
if(false === $result_cnt) {
throw new Exception('Query failed with: ' . mysqli_error());
} else {
$row_count = mysqli_num_rows($result_cnt);
// free the result set as you don't need it anymore
//mysqli_free_result($result_cnt);
}
echo $row_count;
echo " ";
if (!isset($_GET['Page'])) {
$Page = 1;
} else {
$Page = $_GET['Page'];
}
echo $page;
echo " ";
$page_count = 0;
if (0 === $row_count) {
// maybe show some error since there is nothing in your table
} else {
// determine page_count
$page_count = (int)ceil($row_count / $items_per_page);
// double check that request page is in range
if($page > $page_count) {
// error to user, maybe set page to 1
$page = 1;
}
}
echo " ";
echo $page_count;
echo " ";
echo $items_per_page;
$offset = ($page-1)*$items_per_page;
//echo $paging_info;
//echo " ";
echo "<br />";
//Query for displaying results
$list_sql = "SELECT pin, title, title2, email, phone FROM crown_acura LIMIT $offset, $items_per_page";
$result_query = $conn->query($list_sql);
//Table for displaying query results
echo "<table class='verify'>";
echo "<tr >";
echo "<td><h3>Name</h3></td><td> </td><td><h3>E-mail</h3></td><td><h3>Phone</h3></td>";
echo "</tr>";
for($i = 1; $i<= $page_count; $i++) {
if ($result_query->num_rows > 0) {
// output data of each row
while($row3 = mysqli_fetch_array($result_query)) {
echo "<tr>";
echo "<td class='dltd2 dlcl'>" . $row3["title"] . "</td><td>" . $row3["title2"] . "</td><td><a href='mailto:" . $row3['email'] . "'>" . $row3["email"] . "</a> </td><td>" . $row3["phone"] . " </td>";
echo "</tr>";
}
} else {
echo "0 results";
}
}
echo "<tr></tr>";
$next_page = $page + 1;
$last_page = $page - 1;
if($paging_info['curr_page'] <= 1) {
echo "<tr>";
echo "<td></td><td colspan='2'><a class='loadlink' href='" . $_PHP_SELF . "'>Next 25</a></td><td></td>";
echo "</tr>";
} elseif ($paging_info['curr_page'] < $page_count) {
echo "<tr>";
echo "<td></td><td><a href='" . $_PHP_SELF . "?page=" . $last_page . "'>Prev 25</a></td><td><a href='" . $_PHP_SELF . "?page=" . $next_page . "'>Next 25</a></td><td></td>";
echo "</tr>";
} elseif ($paging_info['curr_page'] === $page_count) {
echo "<tr>";
echo "<td></td><td colspan='2'><a href='" . $_PHP_SELF . "?page=" . $last_page . "'>Prev 25</a></td><td></td>";
echo "</tr>";
}
echo "</table>";
}
}
}
Have you tried to run the rendered SQL.
Output to browser:
"SELECT pin, title, title2, email, phone FROM crown_acura LIMIT $offset, $items_per_page"
try this... and change $page for different values (2,3,...,etc)
<?php
$items_per_page = 25;
$sql_count = "SELECT pin, title, title2, email, phone FROM crown_acura";
$result_cnt = mysqli_query($conn, $sql_count);
if (false === $result_cnt) {
throw new Exception('Query failed with: ' . mysqli_error());
} else {
$row_count = mysqli_num_rows($result_cnt);
// free the result set as you don't need it anymore
//mysqli_free_result($result_cnt);
}
echo $row_count;
echo " ";
if (!isset($_GET['Page'])) {
$Page = 1;
} else {
$Page = $_GET['Page'];
}
echo $page;
echo " ";
$page_count = 0;
if (0 === $row_count) {
// maybe show some error since there is nothing in your table
} else {
// determine page_count
$page_count = (int)ceil($row_count / $items_per_page);
// double check that request page is in range
if ($page > $page_count) {
// error to user, maybe set page to 1
$page = 1;
}
}
echo " ";
echo $page_count;
echo " ";
echo $items_per_page;
$offset = ($page - 1) * $items_per_page;
//echo $paging_info;
//echo " ";
echo "<br />";
//Query for displaying results
$list_sql = "SELECT pin, title, title2, email, phone FROM crown_acura LIMIT $offset, $items_per_page";
$result_query = $conn->query($list_sql);
echo ("RESULTS: ".$result_query->num_rows());
?>

search results in table with rows limit php

I created a web site and i added search option which show search results in table, i want to limit rows number to 5 by page please help me to do that:
i would like also to make search results in random sort for each search operation, not the same results are shown on the first page each search :
this is my search code :
$query = mysql_query("SELECT * FROM table WHERE company LIKE '$company' and activity LIKE '$activity'");
echo "<h3>search results</h3><p>";
echo "<table border='1' align='center' >
<tr>
<th>phone</th>
<th>city</th>
<th>activity</th>
<th>company</th>
</tr>";
while($row = mysql_fetch_array($query))
{
echo "<tr>";
echo "<td>" . $row['phone']. "</td>";
echo "<td>" . $row['city']. "</td>";
echo "<td>" . $row['activity']. "</td>";
echo "<td>" . $row['company']. "</td>";
echo "</tr>";
}
echo "</table>";
$anymatches=mysql_num_rows($result );
if ($anymatches == 0)
{
echo "<h3>sorry no results</h3>";
}
?>
thank you.
Change your query first into
$query = mysqli_query($con,"SELECT * FROM table WHERE company LIKE '%$company%' and activity LIKE '%$activity%' ORDER BY RAND()");
We can use a counter to limit the fetching to 5.
$counter=1;
while($row = mysqli_fetch_array($query)){
if($counter<6){
echo "<td>" . $row['phone']. "</td>";
echo "<td>" . $row['city']. "</td>";
echo "<td>" . $row['activity']. "</td>";
echo "<td>" . $row['company']. "</td>";
}
else {
/* NOTHING TO DO */
}
$counter=$counter+1;
} /* END OF WHILE LOOP */
$anymatches=mysqli_num_rows($result);
if ($anymatches == 0)
{
echo "<h3>sorry no results</h3>";
}
OR you can try this, just changing your query into:
$query = mysqli_query("SELECT * FROM table WHERE company LIKE '%$company%' and activity LIKE '%$activity%' ORDER BY RAND() LIMIT 5");
Please make it clear, if you want to show only 5 results or a pagination with 5 rows per page.
If pagination is what you're looking for:
We should start by storing the data first into a table storage. For example we have a table named search with even just one field, searchfield.
$searchword=mysqli_real_escape_string($con,$_POST['searchword']);
mysqli_query($con,"UPDATE search SET searchfield='$searchword'");
/* this is where you store your search text for later purposes */
And we'll fetch it right away (Don't get me wrong, this is for the pagination purposes)
$selectsearch = "SELECT * FROM search";
$querysearch = mysqli_query($con, $selectsearch) or die(mysqli_error($selectsearch));
while($rows = mysqli_fetch_array($querysearch)){
$search = $rows['searchfield'];
}
$query = "SELECT * FROM table WHERE company LIKE '%$search%' OR activity LIKE '%$search%'"; /* do your query search */
$result = mysqli_query($con, $query);
$count = mysqli_num_rows($result);
$r = mysqli_fetch_row($result);
$numrows = $r[0];
echo '<b> '.$count.' result/s found for "'.$search.'"</b>';
$rowsperpage = 5;
$totalpages = ceil($count / $rowsperpage);
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
$currentpage = (int) $_GET['currentpage'];
} else {
$currentpage = 1;
}
if ($currentpage > $totalpages) {
$currentpage = $totalpages;
}
if ($currentpage < 1) {
$currentpage = 1;
}
$offset = ($currentpage - 1) * $rowsperpage;
$select="SELECT * FROM table WHERE company LIKE '%$search%' OR activity LIKE '%$search%' LIMIT $offset, $rowsperpage"; /* do the query again but with limit */
$result=mysqli_query($con, $select);
/*start of the table*/
{
echo "<table border='1'>
<tr>
<th>phone</th>
<th>city</th>
<th>activity</th>
<th>company</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['city'] . "</td>";
echo "<td>" . $row['activity']. "</td>";
echo "<td>" . $row['company']. "</td>";
echo "</tr>";
}
echo "</table>";
}
echo '<table border="0"><tr><td>';
/* ***** build the pagination links ***** */
$range = 2;
if ($currentpage > 1) {
$prevpage = $currentpage - 1;
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'>Previous</a> ";
}
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
if (($x > 0) && ($x <= $totalpages)) {
if ($x == $currentpage) {
echo " <font color='#546f3e'><b>$x</b></font> ";
} else {
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
}
}
}
if ($currentpage != $totalpages) {
$nextpage = $currentpage + 1;
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>Next</a> ";
} // end if
/* ***** end build pagination links ***** */
echo '</td></tr></table>';
You haven't written any pagination code, use MySQL LIMIT , clause.
For ordering randomly, try this:
SELECT * FROM table WHERE company LIKE '$company' and activity LIKE '$activity' ORDER BY RAND()
Start with this. In this you need to pass the page number in URL (Eg yourwebsite/your-php.php?page=0)
$page = $_GET['page'];
$perPage = 5;
$start = $page * $perPage;
$query = mysql_query("SELECT * FROM table WHERE company LIKE '$company' and activity LIKE '$activity' limit " . $start . ',' . $perPage);
echo "<h3>search results</h3><p>";
echo "<table border='1' align='center' >
<tr>
<th>phone</th>
<th>city</th>
<th>activity</th>
<th>company</th>
</tr>";
while($row = mysql_fetch_array($query))
{
echo "<tr>";
echo "<td>" . $row['phone']. "</td>";
echo "<td>" . $row['city']. "</td>";
echo "<td>" . $row['activity']. "</td>";
echo "<td>" . $row['company']. "</td>";
echo "< /tr>";
}
echo "</table>";
$anymatches=mysql_num_rows($result );
if ($anymatches == 0)
{
echo "<h3>sorry no results</h3>";
}
?>
You need to implement pagination in to this by giving link to different page numbers.
I suggest to use some MVC framework rather than having all data retrieval and view in one file.

Sort and Pagination combined, PHP with SQL

I've got a CV database, you can see the fields below and they are pretty standard. Retrieval is done by a simple form sending the information into an SQL database.
I was happy with my simple system till I was flooded with over 500 applicants in my inbox. My previous system allowed me to view the applicants only one by one which would have taken forever...
What I'm trying to achieve is a simple backend page similar to the phpmyadmin of the table view. (no i don't want to just use phpmyadmin as i'd like to give the CV sifting task to other employees)
Basically the concept is to display the table like an excel, allow sorting by clicking on headers, pagination [20 rows per page] and a check box to delete row.
I'm ok with asking for some help as I have put alot of effort into trying to figure this out ;)
So far what i've got is:
The sorting works no problem, clicking on one of the headers spits out localhost/mena/new3.php?sort=fname to the address bar and parses the correct Sql query and sorts the page.
The pagination so far does not work. The page displays all 815 candidates. It is providing the numbered links 1-42 that when clicked on result in the address bar changing to localhost/new3.php?page=2 but 0 change.
Also for the life of me i can't see how to include the php delete into this..
9 yo pseudo code idea of it is :
//Input the rows from SQL
While($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td> $checkbox1
if checkbox1=true then mysqli_query($con,"DELETE FROM cv WHERE .$row[].");
echo "<td>" . $row['title'] .
My code so far:
<?php
$con=mysqli_connect("localhost","root","","test_db-jil");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Pagination
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * 20;
// Sort, from headers.
if(isset($_REQUEST['sort'])){
if($_GET['sort'] == "title"){
$result = mysqli_query($con,"SELECT * FROM cv ORDER BY title");
}
elseif($_GET['sort'] == "fname"){
$result = mysqli_query($con,"SELECT * FROM cv ORDER BY fname");
}
elseif($_GET['sort'] == "lname"){
$result = mysqli_query($con,"SELECT * FROM cv ORDER BY lname");
}
elseif($_GET['sort'] == "gender"){
$result = mysqli_query($con,"SELECT * FROM cv ORDER BY gender");
}
elseif($_GET['sort'] == "dob"){
$result = mysqli_query($con,"SELECT * FROM cv ORDER BY dob");
}
elseif($_GET['sort'] == "nationality"){
$result = mysqli_query($con,"SELECT * FROM cv ORDER BY nationality");
}
elseif($_GET['sort'] == "language"){
$result = mysqli_query($con,"SELECT * FROM cv ORDER BY language");
}
elseif($_GET['sort'] == "phone"){
$result = mysqli_query($con,"SELECT * FROM cv ORDER BY phone");
}
elseif($_GET['sort'] == "email"){
$result = mysqli_query($con,"SELECT * FROM cv ORDER BY email");
}
elseif($_GET['sort'] == "uni"){
$result = mysqli_query($con,"SELECT * FROM cv ORDER BY uni");
}
elseif($_GET['sort'] == "prog"){
$result = mysqli_query($con,"SELECT * FROM cv ORDER BY prog");
}
elseif($_GET['sort'] == "graddate"){
$result = mysqli_query($con,"SELECT * FROM cv ORDER BY graddate");
}
elseif($_GET['sort'] == "startdate"){
$result = mysqli_query($con,"SELECT * FROM cv ORDER BY startdate");
}
elseif($_GET['sort'] == "grad"){
$result = mysqli_query($con,"SELECT * FROM cv ORDER BY grad");
}
else{
$result = mysqli_query($con,"SELECT * FROM cv ORDER BY fname");
}
}
else{ // Default if no parameters passed
$result = mysqli_query($con,"SELECT * FROM cv");
}
//Table of Content
echo "<table border='1'>
<tr>
<th><a href=new3.php?sort=title>Title</a></th>
<th><a href=new3.php?sort=fname>First Name</a></th>
<th><a href=new3.php?sort=lname>Last Name</a></th>
<th><a href=new3.php?sort=gender>Gender</a></th>
<th><a href=new3.php?sort=dob>Date Of Birth</a></th>
<th><a href=new3.php?sort=nationality>Nationality</a></th>
<th><a href=new3.php?sort=language>Language</a></th>
<th><a href=new3.php?sort=phone>Phone No</a></th>
<th><a href=new3.php?sort=email>Email</a></th>
<th><a href=new3.php?sort=uni>University</a></th>
<th><a href=new3.php?sort=prog>Program</a></th>
<th><a href=new3.php?sort=graddate>Graduated</a></th>
<th><a href=new3.php?sort=startdate>Start Date</a></th>
<th><a href=new3.php?sort=grad>Applying for</a></th>
<th>CV File</th>
</tr>";
//Input the rows from SQL
While($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['fname'] . "</td>";
echo "<td>" . $row['lname'] . "</td>";
echo "<td>" . $row['gender'] . "</td>";
echo "<td>" . $row['dob'] . "</td>";
echo "<td>" . $row['nationality'] . "</td>";
echo "<td>" . $row['language'] . "</td>";
echo "<td>" . $row['phone'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['uni'] . "</td>";
echo "<td>" . $row['prog'] . "</td>";
echo "<td>" . $row['graddate'] . "</td>";
echo "<td>" . $row['startdate'] . "</td>";
echo "<td>" . $row['grad'] . "</td>";
echo "<td>" . $row['cvfilename'] ."</td>";
echo "</tr>";
}
echo "</table>";
//Get total count of rows then ceil divide by 20 as pages
$sql = "SELECT COUNT(*) as 'num' FROM cv";
$total_pages = $con->query($sql) or die(mysqli_error($connection));
$row = $total_pages->fetch_assoc();
$total_pages = ceil($row['num'] / 20);
for ($i=1; $i<=$total_pages; $i++) {
//Can I ?page= and ?sort= ??????
echo "<a href='new3.php?page=".$i."'>".$i."</a> ";
};
mysqli_close($con);
?>
Recap, please help me fix pagination, have it work with sort and finally add a delete check box to each row. :)
You know you can optimize that entire block of "else if" statements by just assigning the
$_GET to a variable:
$type = $_GET;
Then use that in your mysqli:
$result = mysqli_query($con, "SELECT * FROM cv ORDER BY $type");
To limit your results use LIMIT:
$result = mysqli_query($con, "SELECT * FROM cv ORDER BY $type LIMIT 20, $page");
20 = how many to return
$page = where you want the results to start from

Categories