I have tried my hand at some pagination but it gives me very unexpected results....
The first is that it on the first page of the pagination it seems to override the CSS and align all divs to the right hand side of the page... when you then click 'next' to view the next three results and so on the page goes back as per the CSS tells it!
The second problem is that the table displaying the results doesn't seem to work. The very first result on each page of the pagination is in the table, the next two results (three results per page) are displayed on the page however below and not in the table!
The code is quite long but here we go:
<?php
session_start();
if ( !isset($_SESSION['username']))
{
header("Location:index.php");
exit();
}
//connect to database
require "dbconn.php";
$per_page = 3;
$start = $_GET['start'];
$sort = #$_POST['order'];
if (!empty($sort)) {
$query = "SELECT bookname, bookauthor, bookpub, bookisbn
FROM booktable
ORDER BY ".mysql_real_escape_string($_POST['order'])." ASC";
}
else {
$query = "SELECT bookname, bookauthor, bookpub, bookisbn
FROM booktable
ORDER BY bookname ASC";
}
$results = mysql_query($query)
or die (mysql_error());
$record_count = mysql_num_rows($results);
if (!$start)
$start = 0;
$get = mysql_query("SELECT * FROM booktable LIMIT $start, $per_page");
?>
<?php
if (isset($_GET['showerror']))
$errorcode = $_GET['showerror'];
else
$errorcode = 0;
?>
Then I will cut out all the unnecesary html
<div id="mid">
<?php
echo "<table border='2px'>";
echo "<tr>";
echo "<th>";
echo "</th>";
echo "<th>";
echo "Book Title";
echo "</th>";
echo "<th>";
echo "Book Author";
echo "</th>";
echo "<th>";
echo "Book Publisher";
echo "</th>";
echo "<th>";
echo "Book ISBN";
echo "</th>";
echo "<th>";
echo "</th>";
echo "</tr>";
while ($row = mysql_fetch_assoc($get))
{
// get data
$bookname = $row['bookname'];
$bookauthor = $row['bookauthor'];
$bookpub = $row['bookpub'];
$bookisbn = $row['bookisbn'];
echo "<tr>";
echo "<td>";
echo "<a href='addtolist.php?bookname=".$bookname."&bookauthor=".$bookauthor."&bookpub=".$bookpub."&bookisbn=".$bookisbn."'>Add to basket</a>";
echo "</td>";
echo "<td>";
echo $bookname;
echo "</td>";
echo "<td>";
echo $bookauthor;
echo "</td>";
echo "<td>";
echo $bookpub;
echo "</td>";
echo "<td>";
echo $bookisbn;
echo "</td>";
echo "</tr>";
echo "</table>";
}
$prev = $start - $per_page;
$next = $start + $per_page;
if (!($start<=0))
echo "<a href='products.php?start=$prev'>Prev</a> ";
//set variable for first page number
$i=1;
//show page numbers
for ($x = 0; $x < $record_count; $x = $x + $per_page)
{
if ($start != $x)
echo "<a href='products.php?start=$x'>$i</a>";
else
echo "<a href='products.php?start=$x'><b>$i</b></a>";
$i++;
}
//show next button
if (!($start >= $record_count - $per_page))
echo "<a href='products.php?start=$next'>Next</a>";
?>
<?php echo $record_count; ?>
This is how the page looks (image 1) (the table border shows the problem)
As I said before I also get the problem when the list first gets displayed and the page ends up looking like this: (image2)
You can see how they differ!
I hope that I have made sense!
For starters, you have your </table> tag in your while loop. Change that and see if everything else falls into place.
Related
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.
this file is included in my index file, i want not more than 5 posts on page and under posts 1 2 3 4... and etc. (links to the next pages) and that it look like this index.php?page=2
Sorry for my bad grammar.
<?php
if(isset($_GET['post_edit'])) {
$p_id = $_GET['post_edit'];
$p_query = mysql_query("SELECT title, post FROM posts WHERE id='$p_id'");
$p_array = mysql_fetch_array($p_query);
$title = $p_array['title'];
$post = $p_array['post'];
}
?>
<?php
if(isset($_POST['edit'])){
$title_edit = $_POST['titleedit'];
$post_edit = $_POST['postedit'];
if(empty($title) or empty($post)){
echo "<p>Fields empty!</p>";
} else {
mysql_query("UPDATE posts SET title='$title_edit', post='$post_edit' WHERE id='$p_id'");
echo "Edit succesful!</br>";
header('location: index.php');
}
}
?>
<?php
if(isset($_GET['post_edit']) && !empty($_GET['post_edit'])){
include 'edit_post.php';
} else {
?>
<?php
$query = mysql_query("SELECT * FROM posts ORDER BY date DESC");
while($row = mysql_fetch_array($query)){
echo "<div class='poststitle'>";
echo "<div style='font-weight: bold;'>";
echo $row['title'];
echo "</div>";
echo "</div>";
echo "<div class='posts'>";
echo $row['post'];
echo "</br>";
echo "<hr>";
$user_name = mysql_query("SELECT username FROM users WHERE id = '".$row['user']."' ");
$user_name_array = mysql_fetch_array($user_name);
$post_id = $row['id'];
echo "Posted by: <b>";
echo $user_name_array['username'];
echo "</b> | ";
echo "Views: <b>";
echo $row['views'];
echo "</b> | ";
echo "Posted on: ";
echo "<b>";
echo $row['date'];
echo "</b><hr>";
echo '</div>';
if (loggedin()){
if($user_level == 1){
echo "<div class='postoptions'>";
echo "<a href='index.php?post_edit=$post_id'><img src='img/optionicons/edit.png' width='15' height='15' alt='edit' /></a>";
echo "<a href='del_post.php?del=$post_id'><img src='img/optionicons/cancel.png' width='15' height='15' alt='Delete' /></a>";
echo "</div>";
} else {
echo "";
}
}
}
}
?>
SELECT * FROM table_name LIMIT N, M
Where N - number of limited rows and M(offset) = N * (PAGE -1)
You should use the LIMIT clause in your MySQL query, see the docs
in order to calculate the numbe rof pages, you also need the total number of rows, this can be found using SQL_CALC_FOUND_ROWS, see the docs or this question
I've a html form which is insert data to mysql database and then get those data with following php code (From supplier_jv table)
<?php
include("include/address2.php");
include("include/menu.php");
$uname_ad = $_SESSION['uname_ad'];
$id = $_GET['id'];
$sql = mysql_query("SELECT * FROM supplier_jv WHERE jv_id = '$id'");
$num = mysql_num_rows($sql);
if($num == 0)
{
echo "<p><font color=red>Accounts is emtpy</font></p>";
}
else
{
$re_name = mysql_fetch_array($sql);
echo "<center><h2>";
echo "<strong>Accounts of </strong>";
echo $re_name['jv_name'];
echo "</h2></center>";
echo "<center>";
echo "<table>";
echo "<table border='0' cellpadding='5' cellspacing='5' width='1000'>";
echo "<tr/>";
echo "<td><strong>Date</strong></td>";
echo "<td><strong>Particular</strong></td>";
echo "<td><strong>Folio(C)</strong></td>";
echo "<td><strong>Folio(J)</strong></td>";
echo "<td><strong>Debit</strong></td>";
echo "<td><strong>Credit</strong></td>";
echo "<td><strong>Balance</strong></td>";
echo "</tr>";
while($re= mysql_fetch_array($sql))
{
$day = $re['day'];
$month $re['month'];
$year = $re['year'];
$parti = $re['particulars'];
$folio = $re['folio'];
$folio2 = $re['folio2'];
$debit = $re['debit'];
$credit = $re['credit'];
$balance = $re['balance'];
$b = $debit - $credit;
$total_debit = mysql_query("SELECT SUM(debit) FROM supplier_jv");
$re_t = mysql_fetch_array($total_debit);
$t_d = $re_t['SUM(debit)'];
$total_credit = mysql_query("SELECT SUM(credit) FROM supplier_jv");
$re_t2 = mysql_fetch_array($total_credit);
$t_c = $re_t2['SUM(credit)'];
$b = $t_d - $t_c;
echo "<tr>";
echo "<td>$day/$month/$year</td>";
echo "<td>$parti</td>";
echo "<td>$folio</td>";
echo "<td>$folio2</td>";
echo "<td>";
echo number_format($debit);
echo "</td>";
echo "<td>";
echo number_format($credit);
echo "</td>";
echo "<td></td>";
echo "</tr>";
}
echo "<tr bgcolor='#f3f3f3'>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td><strong>Total Balance-</strong></td>;
echo "<td><strong>";
echo number_format($b);
echo "</strong></td>";
echo "</tr>";
echo "</table>";
echo "</center>";
}
?>
Well, After insert data then it's show:
Notice: Undefined variable: b in E:\xampp\htdocs\Accounts\admin\content
\supplier_account.php on line 108
But if i insert data in second time then it's OK!!.
Any idea or solution.
Thanks
shibbir.
You need to use isset to see if it is set, not null and also avoid the Notice: Undefined variable error:
if (isset($b))
{
// your code here
}
Where you have:
$b = $t_d - $t_c;
Make sure that there is some value coming up for $t_d and $t_c
I hope that title makes sense.... Basically I created PHP script to take data from a database and display in, I then wrote some code to use a drop down menu to order that data. That all worked ok until I tried to utilise some pagination. I can make the pagination work, and I can make the ordering work, but not at the same time! At the moment the code that I have will allow me to order the list and almost paginate it. There are 40 results and I want to display 10 at a time. When not using the ordering code I can paginate it perfectly but when I try to intergrate the two bits of code it will only display the first 10 results and not give me an option to go to the next page to see the rest! (please bear in mind that I only started learning 5 das ago so I am still getting to grips with it!
The code:
if (!isset($_GET['start']))
{
$_GET['start'] = 0;
}
$per_page = 10;
$start = $_GET['start'];
if (!$start)
$start = 0;
$sort = #$_POST['order'];
if (!empty($sort)) {
$get = mysql_query("SELECT bookname, bookauthor, bookpub, bookisbn
FROM booktable
ORDER BY ".mysql_real_escape_string($_POST['order'])." ASC
LIMIT $start, $per_page");
}
else {
$get = mysql_query("SELECT bookname, bookauthor, bookpub, bookisbn
FROM booktable
ORDER BY bookname ASC
LIMIT $start, $per_page");
}
$record_count = mysql_num_rows($get);
?>
<?php
if (isset($_GET['showerror']))
$errorcode = $_GET['showerror'];
else
$errorcode = 0;
?>
wont include all the html rubbish and the ordering menu!
<div id="mid">
<?php
echo "<table>";
echo "<tr>";
echo "<th>";
echo "</th>";
echo "<th>";
echo "Book Title";
echo "</th>";
echo "<th>";
echo "Book Author";
echo "</th>";
echo "<th>";
echo "Book Publisher";
echo "</th>";
echo "<th>";
echo "Book ISBN";
echo "</th>";
echo "<th>";
echo "</th>";
echo "</tr>";
while ($row = mysql_fetch_assoc($get))
{
// get data
$bookname = $row['bookname'];
$bookauthor = $row['bookauthor'];
$bookpub = $row['bookpub'];
$bookisbn = $row['bookisbn'];
echo "<tr>";
echo "<td>";
echo "<a href='addtolist.php?bookname=".$bookname."&bookauthor=".$bookauthor."&bookpub=".$bookpub."&bookisbn=".$bookisbn."'>Add to basket</a>";
echo "</td>";
echo "<td>";
echo $bookname;
echo "</td>";
echo "<td>";
echo $bookauthor;
echo "</td>";
echo "<td>";
echo $bookpub;
echo "</td>";
echo "<td>";
echo $bookisbn;
echo "</td>";
echo "</tr>";
}
echo "</table>";
$prev = $start - $per_page;
$next = $start + $per_page;
if (!($start<=0))
echo "<a href='products.php?start=$prev'>Prev</a> ";
//set variable for first page number
$i=1;
//show page numbers
for ($x = 0; $x < $record_count; $x = $x + $per_page)
{
if ($start != $x)
echo "<a class='pagin' href='products.php?start=$x'> $i </a>";
else
echo "<a class='pagin' href='products.php?start=$x'><b> $i </b></a>";
$i++;
}
//show next button
if (!($start >= $record_count - $per_page))
echo "<a class='pagin' href='products.php?start=$next'> Next </a>";
?>
Thank you so much for reading!
Simpliest way just change $sort = #$_POST['order']; to $sort = #$_REQUEST['order']; and add to your pagination links &order=$order
i try to store the booking booths into database based on user selection, there are 10 check boxes for each booths and user can choose which day they want to reserve booths. For each check box has it own field in database, if user choose booth A01, D1 and D2, when he press reserve button, it will insert value into D1 and D2. But I dont know how to get the checked checkbox value to store in database
my booth interface
http://i.imgur.com/umYcI.gif
my table structure
http://i.imgur.com/vKh6R.gif
My coding
<?php
session_start();
if ( !isset($_SESSION['AUTHORIZED_USERNAME']) || empty($_SESSION['AUTHORIZED_USERNAME']) ) {
header("location:index.php");
}else{
$user=$_SESSION['AUTHORIZED_USERNAME'];
}
include('db.php');
if($_REQUEST){
$id = $_REQUEST['search_category_id'];
$query2 = mysql_query("SELECT filenameBig, filename, url FROM eventinfo where eventID ='$id'");
$row = mysql_fetch_array($query2, MYSQL_ASSOC);
if($id == -1)
{
echo "<style type='text/css'>#btn_submit{visibility:hidden}</style>";
}
else{
/*echo "<a href='{$row['url']}'>Click me!</a>";*/
echo "<p><br><img src='{$row['filename']}' alt='' /></p>";
echo "<p></p>";
echo "<p align='right'><a href='$row[filenameBig]' target='_blank'>Click to view large image</a></p>";
echo "<hr size='1'>";
echo "<div style='padding-left:4px;' align='left'><strong>Booths Listing</strong>";
echo "<p></p>";
$query = "select boothAlias, totalDay from booths, eventinfo where booths.eventID=eventinfo.eventID && booths.eventID = ".$id."";
$_SESSION['EVENT_ID']=$id;
$result = mysql_query($query);
$result2= mysql_query($query);
echo "<table border='0' style='width:400px;table-layout:fixed' >";
$rows2 = mysql_fetch_array($result);
$Day=$rows2['totalDay'];
echo "<table>";
for ($day = 0; $day <= $Day; ++$day) {
if($day==0){
echo "<th>Booth</th>";
}else{
echo "<th>D".$day."</th>";
}
}
while($rows = mysql_fetch_array($result2)){
$boothAlias=$rows['boothAlias'];
$totalDay=$rows['totalDay'];
echo "<tr><td>$boothAlias</td>";
for ($day2 = 1; $day2 <= $totalDay; ++$day2) {
echo "<td><input name='day2[]' type='checkbox' value='$day2' /></td>";
}
echo "</tr>";
}
echo "</table>";
}
}
?>
I think that SET type would be good solution for this.
http://dev.mysql.com/doc/refman/5.0/en/set.html