I am trying to do a simple pagination exercise with HTML table but only page 2 shows info. It doesn't show table information when I hit 'previous' button.
For example, when I open the page, it shows nothing. When I click on page 2, there is information. When I reach the end, all is ok but then I click 'previous' button or page 1 and it does not show anything again.
$rowperpage = 5; // Total rows display
$row = 0;
if(isset($_GET['page'])){
$row = $_GET['page']-1;
if($row < 0){
$row = 0;
}
}
$link = pg_connect("host=127.0.0.1 port=5432 dbname=swxxxg5_en user=swapng5_control password=xxxxxxxx");
$query = "select id from registrados";
$resultfichados = pg_query($link, $query);
$totalfichados = pg_num_rows($resultfichados);
$allcount = pg_num_rows($resultfichados);
//echo $allcount;
$limitrow = $row*$rowperpage;
$db = pg_connect("host=127.0.0.1 port=5432 dbname=swxxxg5_en user=swapng5_control password=xxxxxxxxxxxxx");
$result = pg_query($db,"select
id,
usuario,
nombre,
apellido,
flag,
reputacion,
ingreso,
lastlogin,
passport,
loyalty,
certified
from registrados order by id asc limit " . $limitrow . " offset 0");
//from registrados order by reputacion asc limit 5 offset 0");
?>
<table align='center' class='table table-hover table-striped' id='t01'>
<td align='center' style='font-weight:bold; color:#fff; background: #3498db;'>id</td>
<td align='center' style='font-weight:bold; color:#fff; background: #3498db;'>Usuario</td>
<td align='center' style='font-weight:bold; color:#fff; background: #3498db;'>Nombre</td>
<td align='center' style='font-weight:bold; color:#fff; background: #3498db;'>Apellido</td>
<td align='center' style='font-weight:bold; color:#fff; background: #3498db;'>Flag</td>
<td align='center' style='font-weight:bold; color:#fff; background: #3498db;'>Reputacion</td>
<td align='center' style='font-weight:bold; color:#fff; background: #3498db;'>Ingreso</td>
<td align='center' style='font-weight:bold; color:#fff; background: #3498db;'>Lastlogin</td>
<td align='center' style='font-weight:bold; color:#fff; background: #3498db;'>Passport</td>
<td align='center' style='font-weight:bold; color:#fff; background: #3498db;'>Loyalty</td>
<td align='center' style='font-weight:bold; color:#fff; background: #3498db;'>Certified</td>
<?php
while($rowdatauser=pg_fetch_assoc($result)){
echo "<tr>";
echo "<td align='center' width='200' style='color:#7f8c8d; font-weight:bold;'>" . $rowdatauser['id'] . "</td>";
echo "<td align='center' width='200' style='color:#7f8c8d; font-weight:bold;'>" . $rowdatauser['usuario'] . "</td>";
echo "<td align='center' width='200'>" . $rowdatauser['nombre'] . "</td>";
echo "<td align='center' width='60'>" . $rowdatauser['apellido'] . "</td>";
echo "<td align='center' width='60' style='background: #e74c3c; color:#fff; font-weight:bold;'>" . $rowdatauser['flag'] . "</td>";
echo "<td align='center' width='200' style='background: #ff9900; font-weight:bold;'>" . $rowdatauser['reputacion'] . "</td>";
echo "<td align='center' width='200'>" . $rowdatauser['ingreso'] . "</td>";
echo "<td align='center' width='200' style='background: #58c0ce;'>" . $rowdatauser['lastlogin'] . "</td>";
echo "<td align='center' width='200'>" . $rowdatauser['passport'] . "</td>";
echo "<td align='center' width='200'style='background: #7863a0; color:white; font-weight:bold;'>" . $rowdatauser['loyalty'] . "</td>";
echo "<td align='center' width='200'>" . $rowdatauser['certified'] . "</td>";
echo "</tr>";
}
echo " <td align='center' style='background: #fff;'></td>";
echo " <td align='center' style='background: #fff;'></td>";
echo " <td align='center' style='background: #fff;'></td>";
echo " <td align='center' style='background: #fff;'></td>";
echo " <td align='center' style='background: #fff;'></td>";
echo " <td align='center' style='background: #fff;'></td>";
echo " <td align='center' style='background: #fff;'></td>";
echo " <td align='center' style='background: #fff;'></td>";
echo " <td align='center' style='background: #fff;'></td>";
echo " <td align='center' style='background: #4567;'>TOTALES</td>";
echo "<td align='center' width='200' style='background: #eee;'>", $totalfichados ,"</td>";
echo "</table>";
?>
<!-- Number list (start) -->
<ul class="pagination">
<?php
// calculate total pages
$total_pages = ceil($allcount / $rowperpage);
$i = 1;$prev = 0;
// Total number list show
$numpages = 5;
// Set previous page number and start page
if(isset($_GET['next'])){
$i = $_GET['next']+1;
$prev = $_GET['next'] - ($numpages);
}
if($prev <= 0) $prev = 1;
if($i == 0) $i=1;
// Previous button next page number
$prevnext = 0;
if(isset($_GET['next'])){
$prevnext = ($_GET['next'])-($numpages+1);
if($prevnext < 0){
$prevnext = 0;
}
}
// Previous Button
echo '<li >Previous</li>';
if($i != 1){
echo '<li ><a href="?page='.($i-1).'&next='.$_GET['next'].'" ';
if( ($i-1) == $_GET['page'] ){
echo ' class="active" ';
}
echo ' >'.($i-1).'</a></li>';
}
// Number List
for ($shownum = 0; $i<=$total_pages; $i++,$shownum++) {
if($i%($numpages+1) == 0){
break;
}
if(isset($_GET['next'])){
echo "<li><a href='?page=".$i."&next=".$_GET['next']."'";
}else{
echo "<li><a href='?page=".$i."'";
}
// Active
if(isset($_GET['page'])){
if ($i==$_GET['page'])
echo " class='active'";
}
echo ">".$i."</a></li> ";
}
// Set next button
$next = $i+$rowperpage;
if(($next*$rowperpage) > $allcount){
$next = ($next-$rowperpage)*$rowperpage;
}
// Next Button
if( ($next-$rowperpage) < $allcount ){
if($shownum == ($numpages)){
echo '<li >Next</li>';
}
}
?>
</ul>
<!-- Numbered List (end) -->
I guess you are trying to paginaate using LIMIT a OFFSET b system. In that case, your a should be the page size and b the place where you start.
This system has it's limitations, like when rows are added or removed the same time you are viewing data, but if this is acceptable, then it might be the easiest way.
Other ways to paginate are shown in this article.
Two notes: One DB handle is enough, you don't need two connections. And if you want to count the rows in yous set, SELECT count(*) FROM... is easier than pg_num_rows()
Related
This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 6 years ago.
I am a newbie in PHP. I am working on searching function, but it does not work well and I could not find why. The problem is; the $query has been sent and accepted well however it could not find the $query in the database even though the $query existed. I think, the $sql command might be wrong somewhere, but could not find it anyway. Thank you.
Here is my code: asset_search.php
<?php
//Search data in database
$query = $_GET['query'];
$min_length = 3;
if(strlen($query) >= $min_length)
{
//$query = htmlspecialchars($query);
//$query = mysql_real_escape_string($query);
$query = strtoupper($query);
$sql = "SELECT * FROM asset WHERE ('asset_name' LIKE '%".$query."%')";
$result = mysqli_query($conn, $sql);
$row_cnt = mysqli_num_rows($result);
$count = 0;
if($row_cnt > 0)
{
echo "<table style='padding: 5px; font-size: 15px;'>";
echo "<tr><th style='width: 30px; border: 1px solid black; align:'center''>No</th>";
echo "<th style='width: 200px; border: 1px solid black; align:'center''>Status</th>";
echo "<th style='width: 200px; border: 1px solid black; align:'center''>Asset Sub-identifier</th>";
echo "<th style='width: 200px; border: 1px solid black; align:'center''>Asset Name</th>";
echo "<th style='width: 200px; border: 1px solid black; align:'center''>Asset Type</th>";
echo "<th style='width: 200px; border: 1px solid black; align:'center''>Brand</th>";
echo "<th style='width: 200px; border: 1px solid black; align:'center''>Service Tag/ Product Tag/ Product S/N</th>";
echo "<th style='width: 200px; border: 1px solid black; align:'center''>CSM Tag</th>";
echo "<th style='width: 200px; border: 1px solid black; align:'center''>Action</th></tr>";
while($row = mysqli_fetch_assoc($result))
{
echo "<tr><td align='center'>" . ++$count . "</td>";
echo "<td align='center'>" . $row["asset_status"] . "</td>";
echo "<td align='center'><a href='asset_viewfull.php?asset_id=" . $row["asset_id"] . "'><ins>" . $row["asset_subidentifier"] . "</a></ins></td>";
echo "<td align='center'>" . $row["asset_name"] . "</td>";
echo "<td align='center'>" . $row["asset_type"] . "</td>";
echo "<td align='center'>" . $row["asset_brand"] . "</td>";
echo "<td align='center'>" . $row["asset_sertag"] . "</td>";
echo "<td align='center'>" . $row["asset_csmtag"] . "</td>";
if($row["asset_status"] == "DISPOSE")
{
echo "<td align='center'><a href='asset_delete.php?asset_id=" . $row["asset_id"] . "'>Delete</a>";
echo " ";
echo "<a href='asset_print.php?asset_id=" . $row["asset_id"] . "'>Print</a></td></tr>";
}else
{
echo "<td align='center'><a href='asset_editform.php?asset_id=" . $row["asset_id"] . "'>Edit</a>";
echo " ";
echo "<a href='asset_delete.php?asset_id=" . $row["asset_id"] . "'>Delete</a>";
echo " ";
echo "<a href='asset_disposeform.php?asset_id=" . $row["asset_id"] . "'>Dispose</a>";
echo " ";
echo "<a href='asset_print.php?asset_id=" . $row["asset_id"] . "'>Print</a></td></tr>";
}
}
}else
{
echo "<tr> There is no asset in the database </tr>";
}
echo "</table>";
}
else
{
echo "<script languange = 'Javascript'>
alert('Minimum length is' .$min_length);</script>";
}
//Close connection
mysqli_close($conn);
$count = 0;
?>
Change your query to the following:
SELECT * FROM asset WHERE (`asset_name` LIKE '%".$query."%')
Note the `` around asset_name instead of ''
you should try this without the brackets sometimes it trows out the search,
$sql = "SELECT * FROM asset WHERE `asset_name` LIKE '%{$query}%'";
this is how i preform this task and has never failed me yet!
when i click first row of left table am getting output in right side. Same when i click second row of first table, second row output should only come but first row output ans second row output both are displaying first row output not getting hide.
Can you pls anyone help me out to solve this issue.
php code:
<?php
session_start();
include "db.php";
$query = "select * from purna_orders";
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);
if($num_rows >= 1)
{
echo "<div id='showmenu' class='scroll'>";
echo "<table id='table_struct' cellspacing='0' cellpadding='1' border='1' width='400' height='30'>
<tr class='tr_class' bgcolor='white'>
<td align='center' style='font-weight:bold'> Select </td>
<td align='center' style='font-weight:bold'> order_id </td>
<td align='center' style='font-weight:bold'> customer_name </td>
<td align='center' style='font-weight:bold'> price </td>
<td align='center' style='font-weight:bold'> payment mode </td>
</tr>";
while($row = mysql_fetch_array($result))
{
$order_id = $row['order_id'];
$_SESSION['order_id'] = $order_id;
echo "<tr height='20' data-order_id='".$row['order_id']."'>
<td align='center'><input type='checkbox' class='case' name='case' value='1'></td>
<td align='center'>".$row['order_id']."</td>
<td align='center'>".$row['customer_name']."</td>
<td align='center'>".$row['order_value']."</td>
<td align='center'>".$row['bill_to_pincode']."</td>";
echo "</tr>";
}
echo "</table>";
echo "</div>";
}
if(!mysql_close($con))
{
echo "failed to close";
}
?>
Ajax Code:
<script type="text/javascript">
$('#table_struct tr').click(function() {
var $this = $(this);
var offset = $this.offset();
var height = $this.height();
var order_id = $this.data('order_id');
$.get('getuser.php?order_id=' + order_id, function(table) {
$('.menu').append(table);
$('.menu').css({
right: offset.right,
top: offset.top+height
});
});
});
</script>
getuser.php
<?php
include "db.php";
$order_id = intval($_GET['order_id']);
$sql="SELECT * FROM purna_order_items WHERE order_id = '".$order_id."'";
$result = mysql_query($sql);
echo "<div style=margin-top:-398px; margin-bottom:0px;'>";
echo "<table border='1' style='background-color:white; font-style:bold;'>
<tr>
<td align='center'><b>Increment Id</b></td>
<td align='center'><b>Po Order Id</b></td>
<td align='center'><b>Item Sku</b></td>
<td align='center'><b>Item Name</b></td>
<td align='center'><b>Item Price</b></td>
<td align='center'><b>Item Quantity</b></td>
<td align='center'><b>Item Weight</b></td>
</tr>";
while($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td align='center'>" . $row['order_id'] . "</td>";
echo "<td align='center'>" . $row['po_id'] . "</td>";
echo "<td align='center'>" . $row['sku'] . "</td>";
echo "<td align='center'>" . $row['item_name'] . "</td>";
echo "<td align='center'>" . $row['item_price'] . "</td>";
echo "<td align='center'>" . $row['item_qunatity'] . "</td>";
echo "<td align='center'>" . $row['weight'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "</div>";
mysql_close($con);
?>
output screenshot:
Your code does not have sufficient information, but try with this one, if it helps -
<script type="text/javascript">
$('#table_struct tr').click(function() {
var $this = $(this);
var offset = $this.offset();
var height = $this.height();
var order_id = $this.data('order_id');
$.get('getuser.php?order_id=' + order_id, function(table) {
$('.menu', $this).append(table);
$('.menu', $this).css({
right: offset.right,
top: offset.top+height
});
});
});
</script>
Thanks to stackoverflow and its great solution, I have found a way to limit the characters in a table but it doesn't work for me. I tried a lot but with no success.
This is my table
<?php
$result = mysqli_query($conn,"SELECT * FROM library ORDER BY `CreatedTime` DESC");
echo "<table class='table-fill' border='0' cellpadding='0' cellspacing='0'>
<tr>
<th position='fixed' overflow='hidden' width='10%'>Book Name</th>
<th width='5%'></th>
</tr>";
while($row = mysqli_fetch_array($result) ) {
echo "<tr>";
echo "<td colspan='2' style='padding-bottom: 0;'><a href='library.details.php?id=". $row['id']."' target='content' class='positiontitle-link'><font style='text-shadow: none; font-weight: 800;'>" . $row['bookname']. "</td>";
echo "</tr>";
echo "<tr style='border-top-width: 0; padding-top: 0;'>";
echo '<td style="max-height: 10px;">' . $str . '</td>';
echo "<td style=' padding-top: 0; padding-left: 15px; width: 40%;'> <font color='gray'>Author :</font> " .($row['authorname'] ). "</td>";
echo "<td width='5%' style=' padding-top: 0;'> <font color='gray'>Year Published </font>" . $row['yearpublished'] . "</td>";
echo "</tr>";
if (strlen($row['bookname']) > 1) $str = substr($row['bookname'], 0, 1) . "...";
}
echo"</table>";
?>
This is how it looks like
Any help will be appreciated.
I'm doing this out of my head so forgive me any format issues and such...
Move the string length check to just under the while.
Overwrite $row['bookname'] instead of creating $str.
Remove the line with:
echo '<td style="max-height: 10px;">' . $str . '</td>';
Result:
while($row = mysqli_fetch_array($result) ) {
if (strlen($row['bookname']) > 9) $row['bookname'] = substr($row['bookname'], 0, 9) . "...";
echo "<tr>";
echo "<td colspan='2' style='padding-bottom: 0;'><a href='library.details.php?id=". $row['id']."' target='content' class='positiontitle-link'><font style='text-shadow: none; font-weight: 800;'>" . $row['bookname']. "</td>";
echo "</tr>";
echo "<tr style='border-top-width: 0; padding-top: 0;'>";
echo "<td style=' padding-top: 0; padding-left: 15px; width: 40%;'> <font color='gray'>Author :</font> " .($row['authorname'] ). "</td>";
echo "<td width='5%' style=' padding-top: 0;'> <font color='gray'>Year Published </font>" . $row['yearpublished'] . "</td>";
echo "</tr>";
}
how can i put a tooltip in a specific date? Can someone please help me. I really need this
and how can i put a ajax so that my calendar doesn't refresh
This is my code.
<?php
error_reporting(0);
Class CALEVENTS
{
var $host;
var $user;
var $password;
var $database;
var $db=NULL;
function CALEVENTS($host="",$user="",$password="",$database="")
{
$this->setInfo($host,$user,$password,$database);
}
function setInfo($host,$user,$password,$database)
{
$this->host=$host;
$this->user=$user;
$this->password=$password;
$this->database=$database;
//$this->db=new DB();
//$this->db->open($this->host,$this->user,$this->password,$this->database);
$this->db=mysql_connect($this->host,$this->user,$this->password);
mysql_select_db($this->database);
}
function drawCalendar()
{
$db_event=$this->db;
if(isset($_GET['time']))
$time=$_GET['time'];
else
$time=time();
$today = getdate($time);
$mday = $today ['mday'];
$mon = $today ['mon'];
$year = $today ['year'];
$time = mktime(0, 0, 0, $mon,1,$year);
$today = getdate($time);
$wday = $today ['wday'];
$year = $today ['year'];
$weekday = $today ['weekday'];
$month = $today ['month'];
$i= -$wday;
$thisPage=$_SERVER['PHP_SELF'];
$time = mktime(0, 0, 0, $mon+1,0,$year);
$lastDay=date('j',$time);
$sql="SELECT DATE_FORMAT(eventDate,'%d') AS day,eventContent,eventTitle,eventID FROM events WHERE eventDate BETWEEN '$year/$mon/01' AND '$year/$mon/31'";
//$db_event->query($sql);
$result=mysql_query($sql,$db_event);
$events=array();
//while($row_event=$db_event->fetchObject())
// $events[intval($row_event->day)].=$row_event->eventContent."<br><br>";
while($row_event=mysql_fetch_object($result))
$events[intval($row_event->day)].=$row_event->eventTitle."<br>" .$row_event->eventContent;
echo "<style>\n";
echo ".event_cls {background-color: #b7c8ef; color:white; font-weight: bold; text-decoration: none; cursor: hand;}\n";
echo " <div id='event' style=' left: 395px; top: 192px; color:black'></div></td> </tr> </table>\n";
echo ".event_cls:hover {background-color:ced9f0;}";
echo ".event_col:hover {background-color:ced9f0;}";
echo ".event_head{background-color: #333b73; color:white; font-weight:bold;FONT-FAMILY: Verdana, Geneva, Arial, Helvetica, sans-serif;FONT-SIZE: 12px;}\n";
echo ".event_col{background-color:#e2e6ef; color:#000095; FONT-FAMILY: Verdana, Geneva, Arial, Helvetica, sans-serif;FONT-SIZE: 11px; height='75' width='75'}\n";
echo ".event_link {TEXT-DECORATION: none; color:#0DB0FF;}\n";
echo "</style>";
echo "<table width='870' border='0' align='center' style='border-style:solid;border-width:1px;position:relative;top:15px;left:15px;' cellspacing='1'>\n";
echo "<tr class='event_col' align='center'> \n";
echo "<td height='75'><b><a href='$thisPage?time=".mktime(0, 0, 0, $mon-1,$mday,$year)."' title='Previous Month' class='event_link'><</a></b></td>\n";
echo "<td colspan='5' class='event_col'> <b>$month $year</b></td>\n";
echo "<td height='75'><b><a href='$thisPage?time=".mktime(0, 0, 0, $mon+1,$mday,$year)."' title='Next Month' class='event_link'>></a></b></td>\n";
echo "</tr>\n";
echo "<tr class='event_col' height='25''>\n";
echo "<td align='center' height='75' width='75' class='event_head'>Sunday</td>\n";
echo "<td align='center' height='75' width='75' class='event_head'>Monday</td>\n";
echo "<td align='center' height='75' width='75' class='event_head'>Tuesday</td>\n";
echo "<td align='center' height='75' width='75' class='event_head'>Wednesday</td>\n";
echo "<td align='center' height='75' width='75' class='event_head'>Thursday</td>\n";
echo "<td align='center' height='75' width='75' class='event_head'>Friday</td>\n";
echo "<td align='center' height='75' width='75' class='event_head'>Saturday</td>\n";
echo "</tr>\n";
echo "<script>\n";
foreach($events as $key => $value)
echo "evnt$key=\"$value\"\n";
echo "function showevent(day)\n";
echo "{";
echo " evnt=eval('evnt'+day);\n";
echo " mydiv=document.getElementById('event');\n";
echo " mydiv.innerHTML=evnt\n";
echo "}";
echo "</script>\n";
for($j=0;$j<6;$j++){
echo "<tr> \n";
for($k=0;$k<7;$k++){
$i++;
$cls="class='event_col'";
if(array_key_exists($i,$events))
$cls="class=event_cls onmouseover='showevent($i)'";
echo "<td height='75' $cls>\n";
echo ($i>0&&$i<=$lastDay)?$i:'';
echo "</td>";
}
echo "</tr>" ; }
echo " <tr > <td colspan=8 >\n";
echo " </td> </tr> </table>\n";
echo"<br/><div id='event' style=' z-index:1000; left: 395px; top: 192px; color:#000;'></div>";
}?>
I really need help.. please help me
You can set "id" to each "td" and then in javascript you can setAttribute "title" to some value.
T have one page on which results are get created dynamically. And I
want to display popup window on clicking image named view.jpg. The
image is hyperlink. So please tell me how should i do it. I am showing
my code as follows.
code :
echo "<table width='' height='' border='1' align='center'>
<tr><td>Title</td>
<td >Type</td>
<td >Date</td>
<td>Expiry Date</td>";
if($typeofuser=='admin' || $typeofuser=='Accountant' || $typeofuser=='secretary')
{
echo "<td>View</td>
<td>Edit</td>
<td>Delete</td>";
}
echo "</tr>";
$qry2 = mysqli_query($con,"SELECT nId,nTitle,nDescription,nDate,nExpiryDate FROM tblnoticemanager where Society_id = '$socId' and category='General'") or die(mysqli_error($con));
while($NoticeData = mysqli_fetch_array($qry2))
{
echo "<tr>";
echo "<td align='center' class='tdletter' style='text-transform:capitalize;'>" .$NoticeData['nTitle']. "</td>";
echo "<td align='center' class='tdletter' ><div class='overflowDiv'>" . $NoticeData['nDescription']."</div></td>";
echo "<td align='center' class='tdletter'>" . $NoticeData['nDate'] ."</td>";
echo "<td align='center' class='tdletter'>" . $NoticeData['nExpiryDate'] ."</td>";
if($typeofuser=='admin' || $typeofuser=='Accountant' || $typeofuser=='secretary')
{
echo "<td align='center' class='tdletter'><div><a href='?id=".urlencode(base64_encode($NoticeData['nId']))."'><img src='images/view.png' width='30' height='30' align='center' /></a></div></td>";
echo "<td align='center' class='tdletter'><div><a href='?id=".urlencode(base64_encode($NoticeData['nId']))."' ><img src='images/edit.jpg' width='30' height='30' align='center'/></a></div></td>";
echo "<td align='center' class='tdletter'><span id='".$NoticeData['nId']."' class='trash'><img src='images/delete1.jpg' width='30' height='30' align='center' /></span></td>";
}
echo "</tr>";
}
echo "</table>";
You can use attribute selector [attribute='value']
$("[src='images/view.png']").click(function(){
alert("clicked");
});