Good morning everyone!
I've been working on a little project and I thought I was done. So, I have a bunch of pages, but I have one where there's a MYSQLi query with over 100k results, I figured it would be nice not to display all 100k+ results, so I added pagination to the site. The problem I am having is, whenever someone sorts the page and clicks next page, the sort goes away.
As I was writing this, I kind of sort of figured it out, but here's my code:
$sort = "id";
if(isset($_GET['sort'])) {
switch ($_GET['sort'] ) {
case 0:
$sort = 'id';
break;
case 1:
$sort = 'priority DESC';
break;
case 2:
$sort = 't_name';
break;
case 3:
$sort = 'loc';
break;
case 4:
$sort = 'w_req';
break;
case 5:
$sort = 'tel';
break;
case 6:
$sort = 'maint_user';
break;
}
}
// pagination
$total_results = mysqli_num_rows(mysqli_query($mysqli,"SELECT w_status FROM w_o WHERE (w_status = 'active' OR w_status = 'open') ORDER BY $sort"));
if(!isset($page_number))
$page_number = (int)$_GET['page'] <= 0 ? 1 : (int)$_GET['page']; // grab the page number
$perpage = 15; // number of elements perpage
if($page_number > ceil($total_results/$perpage))
$page_number = ceil($total_results/$perpage);
$start = ($page_number - 1) * $perpage;
$result = mysqli_query($mysqli,"SELECT * FROM w_o WHERE (w_status = 'active' OR w_status = 'open') ORDER BY $sort LIMIT $start, $perpage");
I think my issue is that $sort= "id"; and every time I click on next, the $sort gets reset to = "id" So I am thinking, maybe include the $sort in the pagination code? I appreciate any help I can get. Thank you :)
Here's the html:
<form action="re_assign_a.php" method="post" name="view_order">
<table border="0" width="100%">
<tr>
<td align="left">Previous</td>
<td align="right">Next</td>
</tr>
</table>
<br>
<table border='2' style='width: 100%; margin: auto; border-width: 1px'>
<tr>
<th><span title="More">Order #</span></th>
<th><span title="More">Priority</span></th>
<th><span title="More">Tenant Name</span></th>
<th><span title="More">Apartment</span></th>
<th><span title="More">Work Requested</span></th>
<th><span title="More">Resident Phone #</span></th>
<th><span title="More">Assigned to</span></th>
<th>Check to Re-Assign</th>
</tr>
<?php
$i = 0;
while($row = mysqli_fetch_array($result))
{
$test_id = $row['id'];
echo "<tr class='table_work'>";
echo "<td align='center'><a href='one_order.php?id=$test_id'>" . $row['id'] . "</td>";
echo
"<td align='center'><a href='one_order.php?id=$test_id'>";
if ($row['priority'] == '1') { echo "<div class='priority1'> </div>"; } elseif ($row['priority'] == '2') { echo "<div class='priority2'> </div>"; } elseif ($row['priority'] == '3') { echo "<div class='priority3'> </div>"; };
echo "</a></td>
<td align='center'><a href='one_order.php?id=$test_id'>"; echo custom_echo1 ($row['t_name']); echo "</a></td>
<td align='center'><a href='one_order.php?id=$test_id'>"; echo custom_echo1 ($row['loc']); echo "</a></td>
<td align='center'><a href='one_order.php?id=$test_id'>"; echo custom_echo($row['w_req']); echo "</a></td>";
echo "<td align='center'><a href='one_order.php?id=$test_id'>"; echo custom_echo2($row['tel']); echo "</a></td>";
echo "<td align='center'><a href='one_order.php?id=$test_id'>" . $row['maint_user'] . "</a></td>";
echo "<td align='center'><input name='assign[$i]' type='checkbox' value='" . $row['id'] . "' ></td>";
++$i;
}
?>
</table><br>
<table border="0" width="100%">
<tr>
<td align="left">Previous</td>
<td align="right">Next</td>
</tr>
</table>
You just need to adjust your links for going to the pages to include the sort order as a GET parameter. Store the GET parameter as a variable defaulted to zero
$sortOrder = $_GET['sort'] ? : 0;
<td align="left">Previous</td>
<td align="right">Next</td>
Now when the next or previous page link is clicked, the page number and the sort order are passed to the server.
You have to extend the URLs in your HTML. That's a quick & dirty solution! You have to define the variables in the code correctly.
Next page URL
<td align="left">Previous</td>
<td align="right">Next</td>
And the sort URL
<th><span title="More">Order #</span></th>
Related
My goal is to have the items in $row['status'] to change to red if value is OFF and green if value is ON. Any help would be greatly appreciated.
<?php
include("connection.php");
$r = mysqli_query($dbc, "SELECT * FROM enclosure ORDER BY computer ASC");
echo "<table id='table' align='center' border='1' cellspacing='3' cellpadding='3' width='75%'>
<tr>
<td align='left'><b>Enclosure Name</b></td>
<td align='left'><b>Screen Status</b></td>
<td align='left'><b>Time Screen in status</b>
</td><td align='left'><b>Temperature of Enclosure</b></td>
</td><td align='left'><b>Incoming Voltage</b></td>
</tr>";
while($row = mysqli_fetch_array($r)){
$row['voltage'] = $row['voltage'] /1000;
echo "<tr>
<td align='left'>".$row['computer']."</td>
<td align='left'>".$row['status']."</td>
<td align='left'>".gmdate("H:i:s",$row['length'])."</td>
<td align='left'>".$row['temp']."</td>
<td align='left'>".$row['voltage']."</td>
</tr>";
}
mysqli_close($dbc);
$page = $_SERVER['PHP_SELF'];
$sec = "5";
?>
DevlishOne's answer is correct, however you should also consider setting the class of the <td> rather than the color. If you're formatting desires become more sophisticated than red/green (e.g. transitions) or if this is or become a larger chunk of code you will appreciate having the styles shifted out to a css file. It's generally considered a good idea.
ADDITION:
You asked for a simple version using classes:
echo "<td align='left' class='STATUS" . $row['status'] . "'>" . $row['status'] . "</td>";
This uses classes named STATUSON and STATUSOFF so you could use a css stylesheet with
STATUSON {
color: green;
}
STATUSOFF {
color: red;
}
Obviously, you can add more formatting to each of those classes if that's helpful.
Change:
<td align='left'>".$row['status']."</td>
To:
echo "<td align='left' style='color:" . ($row['status'] == "OFF" ? "red" : "green") . "'>" . $row['status'] . "</td>";
i know it's a silly question, but it is creating a problem for me....
I want to print data from mysql with auto increment, but when i use the below code, it auto increments the value in the same page but i want it to be continued to the next pages also, here is my code
<?php
$perpage = 10;
$start = (isset($_GET['id'])) ? $_GET['id'] : 0;
$TotalRec = mysql_result(mysql_query("SELECT COUNT(*) FROM register where
r_bid='".$_SESSION["id"]."'"), 0);
$select = "SELECT * FROM register where r_bid='".$_SESSION["id"]."' LIMIT
$start,$perpage";
$result = mysql_query($select) or die(mysql_error());
$res = mysql_query("select * from regi_balic where b_id='".$_SESSION["id"]."'");
$row1=mysql_fetch_array($res);
$i=1;
while($row = mysql_fetch_array($result))
{
echo '<tr>
<td align="center" width=5%><font size=3>'.$i.'</font></td>
<td width=12%><font size=3>'.$row1['name'].'</font></td>
<td align="center" width=5%><font size=3><a href="edit_detail.phpid='.$row["r_id"].'
&cand_id='.$_SESSION["id"].'&email='.$row["email"].'">'.$row['name'].'</a></font></td>
<td align="center" width=5%><font size=3>'.$row['reference'].'</font></td>
<td align="right" style="padding-right:8px" width=12%>
<fontsize=3>'.$row['age'].'</font></td>
<td align="right" style="padding-right:8px" width=12%>
<fontsize=3>'.$row['occupation'].'</font></td>
<td width=12%><font size=3>'.$row['mob_no'].'</font></td>
<td width=2%><a href="process_del_client.php?id='.$row['r_id'].'"
onClick="returnConfirmSubmit(\'Are You sure ?\')"><img src = "images/delete.png">
</a></td>
</tr>';
}
$i++;
echo '</table>';
if($start == 0)
{
echo "<br>Previous Page ";
}
else
{
echo '<br><a href='."./view.php?id=" . ($start - $perpage) . '>'.
"PreviousPage".'</a> ';
}
if($start + $perpage >= $TotalRec)
{
echo " Next Page<br>";
}
else
{
echo ' <a href='."./view.php?id=" . ($start + $perpage) . '>'."Next Page".'
</a><br>';
}
?>
update value in a session variable and use that onto another page for updation .
Ex..
$_SESSION['value']=0
$_SESSION['value']=$_SESSION['value']++;
try changing
$i=1;
to
$i=1+$start;
I have written code to sort column by clicking header column.
I have created array of column name to be sorted then created select query with $sort and $order. In query string it passes the columname and ASC or DESC but problem it takes me to blank page and it if I pass name of page then also it shows blank page. Can anyone help me to find error.
<?php
function list_users()
{
$y = 0;
$sortDefault = 'id';
// select array for columns
$sortColumns = array('id','first_name','last_name');
// select query with sort and orderby
$sort = isset($_GET['sort']) && in_array($_GET['sort'], $sortColumns) ? $_GET['sort'] : $sortDefault;
$order = (isset($_GET['order']) && strcasecmp($_GET['order'], 'DESC') == 0) ? 'DESC' : 'ASC';
$sql = "select * from contacts ORDER BY $sort $order";
$result = mysql_query($sql) or die ("Can't run query because ". mysql_error());
echo "<form method='post'>
<table width='50' align='right' border='0' cellspacing='0' cellpadding='0'>
<tr>
<td colspan='2' align='center' style='font-size:18px; font-weight:bold;'>"; ?>
<?php if(isset($_SESSION['username']))
{
$s="Hello,".$_SESSION["username"];
$r=$_SESSION["userrole"];
echo $s;
}
echo "<a href='logout.php' id='logout'>Logout</a></td>
</tr>
</table>
<table width='400' align='center' cellpadding='0' cellspacing='0' border='1'>
<tr><td colspan='2' align='center' style='font-size:18px; font-weight:bold;'>Displayed Data</td></tr>
<tr><td colspan='2'></td></tr>
<tr><td colspan='2'><a href='".$_SERVER['PHP_SELF']."?action=add'>Add a new contact</a></td></tr>
<tr><td colspan='2'> </td></tr>
</table>
<br/>
<br/>";
if (mysql_num_rows($result)){
(($y % 2) == 0) ? $bgcolor = "#8FBC8F" : $bgcolor=" #9ACD32";
echo "<table width='400' align='center' cellpadding='0' cellspacing='0' border='1'>
<tr style='background-color:$bgcolor;' align=center>
<td><input type='checkbox' id='all' name='mainchk' /></td>";?>
<td><a href='?sort=name&order=<?php echo $order == 'DESC' ? 'ASC' : 'DESC' ?>'>Name</a></td>
<td><a href='?sort=last_name&order=<?php echo $order == 'DESC' ? 'ASC' : 'DESC' ?>'>LastName</td>
<td>Status</td>
<td>Action</td>
<?php "</tr>";
while($rows = mysql_fetch_array($result)){
$name = $rows['first_name'];
$lname = $rows['last_name'];
$status = $rows['contact_status'];
$id = $rows['id'];
($status == 0) ? $status = "Available to contact" : $status = "Do not contact at present.";
echo"<tr align=center>
<td><input type='checkbox' value='$id' name='data[]' id='data'></td>
<td>$name</td>
<td>$lname</td>
<td>$status</td>
<td><a href='".$_SERVER['PHP_SELF']."?action=delete&id=$id' class='confirmation'><img src='delete.png' height='16px' width='16px'></a> <a href='".$_SERVER['PHP_SELF']."?action=edit&id=$id'><img src='write.png' height='16px' width='16px'></a></td>
<tr>";
$y++;
}
echo "</table>";
}else{
echo "<tr><td colspan='2' align='center'><b>No data found.</b></td></tr>";
}
echo"<div align='center'><input name='delete' type='submit' value='Delete' id='delete'></a></form>";
}
?>
This is what I get in url::::
/mainpage.php?sort=first_name&order= DESC and blank page is displayed.
I got to my problem is that as I am displaying html table by function list_users() which is called in following way::
if ((empty($_POST))&&(empty($_GET)))
{
list_users();
die();
}
If I remove if condition then it problems to my other function add contact form is displayed below list. If only list_user() is called it sorts column.
Solution:
As I called displayed result in a function earlier so now I directly displayed result set rather than any function so the sort function which I have written for column header is working now properly.And my other function are also working properly.
echo "<form method='post'>
<table width='50' align='right' border='0' cellspacing='0' cellpadding='0'>
<tr>
<td colspan='2' align='center'>"; ?>
<?php if(isset($_SESSION['username']))
{
$s="Hello,".$_SESSION["username"];
$r=$_SESSION["userrole"];
echo $s;
}
echo "<a href='logout.php' id='logout'>Logout</a></td>
</tr>
</table>
<table width='500' align='center' cellpadding='0' cellspacing='0' border='1'>
<tr><td colspan='6' align='center' style='font-size:18px; font-weight:bold;'>Displayed Data</td></tr>
<tr><td colspan='6'><a href='".$_SERVER['PHP_SELF']."?action=add'>Add a new contact</a></td></tr>";
$y = 0;
$sortDefault = 'id';
// select array
$sortColumns = array('id','first_name','last_name');
// select query with sort
$sort = isset($_GET['sort']) && in_array($_GET['sort'], $sortColumns) ? $_GET['sort'] : $sortDefault;
$order = (isset($_GET['order']) && strcasecmp($_GET['order'], 'DESC') == 0) ? 'DESC' : 'ASC';
$sql = "select * from contacts ORDER BY $sort $order";
$result = mysql_query($sql) or die ("Can't run query because ". mysql_error());
if (mysql_num_rows($result)){
(($y % 2) == 0) ? $bgcolor = "#8FBC8F" : $bgcolor=" #9ACD32";
echo "
<tr style='background-color:#5CB3FF' align=center>";?>
<!-- <td><input type='checkbox' id='all' name='mainchk' /></td>-->
<td>Chk</td>
<td><a href='?sort=first_name&order=<?php echo $order =='DESC' ? 'ASC' : 'DESC' ?>'>Name</a></td>
<td><a href='?sort=last_name&order=<?php echo $order =='DESC' ? 'ASC' : 'DESC' ?>'>LastName</td>
<td><a href='?sort=email&order=<?php echo $order =='DESC' ? 'ASC' : 'DESC' ?>'>Email</td>
<td>Status</td>
<td id="actid">Action</td>
<?php "</tr>";
while($rows = mysql_fetch_assoc($result)){
$name = $rows['first_name'];
$lname = $rows['last_name'];
$email = $rows['email'];
$status = $rows['contact_status'];
$id = $rows['id'];
($status == 0) ? $status = "Available to contact" : $status = "Do not contact at present.";
echo"<tr align=center style='background-color:#C0C0C0'>
<td><input type='checkbox' value='$id' name='data[]' id='data'></td>
<td>$name</td>
<td>$lname</td>
<td>$email</td>
<td>$status</td>
<td><a href='".$_SERVER['PHP_SELF']."?action=delete&id=$id' class='confirmation'><img src='delete.png' height='16px' width='16px'></a>
<a href='".$_SERVER['PHP_SELF']."?action=edit&id=$id'><img src='write.png' height='16px' width='16px'></a></td>
<tr>";
$y++;
}
echo"</table>";
}
else
{
echo "<tr><td colspan='2' align='center'><b>No data found.</b></td></tr>";
}
echo"<div align='center' width='50' ><a href='#' id='all' name='mainchk'>CheckAll/UnCheckAll</a>
<input type='image' src='delete.png' alt='Submit' width='16px' height='16px' id='delete'>
</div></form>";
Hello i have a table with some fields like
here i want make colors for table entire rows..means if ASR value is 75 to 100 should get one color and 50 to 75 should get another color and below 50 should get another color.
and here is my php code
<table width="75%" border="1">
<tr>
<td align="center">channel no</td>
<td align="center">IP</td>
<td align="center">Total calls</td>
<td align="center">Connected calls</td>
<td align="center">Disconnected calls</td>
<td align="center">Duration</td>
<td align="center">ASR</td>
<td align="center">ACD</td>
</tr>
<?php
while ($row = mysql_fetch_assoc($result)) {
//$minutes = gmdate("H:i:s", $row['tduration']);
echo "<tr>
<td>".$row['channel']." </td>
<td>".$row['ip']." </td>
<td>".$row['totalcalls']." </td>";
if ($row['totalcalls']>1){
$sql1 = "SELECT count(duration) as count FROM gateways where duration=0 and ip='".$_POST['ip']."' and channel='".$row['channel']. "' and (connect_datetime BETWEEN ' ".$_POST['toval']." ' and '".$_POST['fromval']."' or disconnect_datetime BETWEEN ' ".$_POST['toval']." ' and '".$_POST['fromval']."' ) Group by channel";
$result1 = mysql_query($sql1, $link);
$norow=mysql_fetch_assoc($result1);
$attenedcalls=($row['totalcalls']-$norow['count']);
echo "<td>".$attenedcalls." </td>";
$disconnectedcalls=($row['totalcalls']-$attenedcalls);
echo "<td>".$disconnectedcalls." </td>";
echo " <td>".$row['tduration']." </td>";
echo "<td>".(($attenedcalls/$row['totalcalls'])*100)."</td>";
}else{
echo "<td>".$row['totalcalls']."</td>";
echo "<td>100</td>";
}
$minutes = gmdate("H:i:s", ($row['tduration']/$attenedcalls));
echo " <td>".$minutes." </td>
</tr>";
}
?>
</table>
thanks in advance
You can try like this
<table width="75%" border="1">
<tr>
<td align="center">channel no</td>
<td align="center">IP</td>
<td align="center">Total calls</td>
<td align="center">Connected calls</td>
<td align="center">Disconnected calls</td>
<td align="center">Duration</td>
<td align="center">ASR</td>
<td align="center">ACD</td>
</tr>
<?php
while ($row = mysql_fetch_assoc($result)) {
$color = '';
if ($row['totalcalls']>1){
$sql1 = "SELECT count(duration) as count FROM gateways where duration=0 and ip='".$_POST['ip']."' and channel='".$row['channel']. "' and (connect_datetime BETWEEN ' ".$_POST['toval']." ' and '".$_POST['fromval']."' or disconnect_datetime BETWEEN ' ".$_POST['toval']." ' and '".$_POST['fromval']."' ) Group by channel";
$result1 = mysql_query($sql1, $link);
$norow=mysql_fetch_assoc($result1);
$attenedcalls=($row['totalcalls']-$norow['count']);
$asr = (($attenedcalls/$row['totalcalls'])*100);
if($asr >= 75 && $asr <=100 ){
$color = 'red';
}else if($asr >= 50 && $asr < 75){
$color = 'cyan';
}else if($asr < 50){
$color = 'blue';
}
}
//$minutes = gmdate("H:i:s", $row['tduration']);
echo "<tr style='background-color : ".$color."'>
<td>".$row['channel']." </td>
<td>".$row['ip']." </td>
<td>".$row['totalcalls']." </td>";
if ($row['totalcalls']>1){
echo "<td>".$attenedcalls." </td>";
$disconnectedcalls=($row['totalcalls']-$attenedcalls);
echo "<td>".$disconnectedcalls." </td>";
echo " <td>".$row['tduration']." </td>";
echo "<td>".$asr."</td>";
}else{
echo "<td>".$row['totalcalls']."</td>";
echo "<td>100</td>";
}
$minutes = gmdate("H:i:s", ($row['tduration']/$attenedcalls));
echo " <td>".$minutes." </td>
</tr>";
}
?>
</table>
[...]
while ($row = mysql_fetch_assoc($result)) {
$asrVal=(($attenedcalls/$row['totalcalls'])*100);
if($asrVal>=50 && $asrVal <=75) $class="from50to75";
if($asrVal>=75 && $asrVal <=100) $class="from75to100";
if($asrVal<50) $class="below50";
//$minutes = gmdate("H:i:s", $row['tduration']);
echo "<tr class='$class'>
[...]
then add:
<style>
tr.from50to75 td{background-color:red;}
tr.from75to100 td{background-color:green;}
tr.below50 td{background-color:blue;}
</style>
Modify your while loop so that you compute the ASR value before emitting the <tr> tag. Use that value to select a class according to the classification you have set up, and emit a tag of the form <tr class=foo> where foo is the class name you have selected. Then it’s just a matter of writing CSS rules for the classes, using class selectors like tr.foo.
(Provided that you have not set color on the td cells. If you have, you need to use selectors like tr.foo td to override such settings.)
Sorry for asking an implement my feature question type question last time. I am new to Stackoverflow.com and also to php that's why.
What I was trying to ask is:
I have made a admin account. Members have registration page so a member will register. When user registers in the database table I will have a field for which 0 value will be initialised which means he is not approved. In admin account I have code to get the list of members. The code is given below:
<h2><?php echo "User list"; ?></h2>
<table border="0" cellpadding="0" cellspacing="0">
<tr bgcolor="#f87820">
<td><img src="img/blank.gif" alt="" width="10" height="25"></td>
<td class="tabhead"><img src="img/blank.gif" alt="" width="150" height="6"><br><b><?php echo "first name"; ?></b></td>
<td class="tabhead"><img src="img/blank.gif" alt="" width="150" height="6"><br><b><?php echo "lastname name"; ?></b></td>
<td class="tabhead"><img src="img/blank.gif" alt="" width="150" height="6"><br><b><?php echo "member id"; ?></b></td>
<td class="tabhead"><img src="img/blank.gif" alt="" width="50" height="6"><br><b><?php echo "delete"; ?></b></td>
<td><img src="img/blank.gif" alt="" width="10" height="25"></td>
</tr>
<?php
}
$result=mysql_query("SELECT member_id,firstname,lastname,login FROM members ORDER BY firstname");
$i = 0;
while($row = mysql_fetch_array($result)) {
if ($i > 0) {
echo "<tr valign='bottom'>";
echo "<td bgcolor='#ffffff' height='1' style='background-image:url(img/strichel.gif)' colspan='6'></td>";
echo "</tr>";
}
echo "<tr valign='middle'>";
echo "<td class='tabval'><img src='img/blank.gif' alt='' width='10' height='20'></td>";
echo "<td class='tabval'><b>".$row['lastname']."</b></td>";
echo "<td class='tabval'>".$row['firstname']." </td>";
echo "<td class='tabval'>".$row['member_id']." </td>";
echo "<td class='tabval'><a onclick=\"return </span></a></td>";
echo "<td class='tabval'></td>";
echo "</tr>";
$i++;
}
?>
</table>
in this i wanna add tho more things in the table 1 to delete a member and 2 to have approved or denied option for that i made two functiom
below code is to delete
if($_REQUEST['action']=="del")
{
$memberId = mysql_real_Escape_string($_REQUEST['member_id']);
mysql_query("DELETE FROM members WHERE member_id=$memberId");
}
below one for approving members
But my problem is I don't know how to include a button or radio button in the table which can pass value delete or approve to these functions.
Please tell me how the syntax is to add this button so that for approving I can change the value 0 that I gave in the database to 1 so that member get approved.
Try this:
echo '<td><a href="http://yourwebsite/yourscriptname.php?action=del&member_id='
. htmlspecialchars($row['member_id']) . '">Delete</a>';
if ($row['approved'] == 0) {
echo ' <a href="http://yourwebsite/yourscriptname.php?action=approve&member_id='
. htmlspecialchars($row['member_id']) . '">Approve</a>';
}
echo '</td>';
And make sure ALL of your database values are being sent to the browser in htmlspecialchars().
On the flipside,
$member_id = 0;
if (isset($_GET['member_id'])) $member_id = intval($_GET['member_id']);
$action = '';
if (isset($_GET['action'])) $action = $_GET['action'];
$sql = '';
switch($action) {
case 'approve':
$sql = "UPDATE members SET approval = 1 WHERE member_id = $member_id";
break;
case 'delete':
$sql = "DELETE FROM member WHERE member_id = $member_id";
break;
}
if (!empty($sql) && !empty($member_id)) {
// execute the sql.
}
What I would do is to set up a form inside of the table.
?> <form name="deleteUser" id="deleteUser" method="post" action="">
<input type="hidden" name="member_id" id="member_id" value="<?php echo $row['member_id'] ?>
<input type="submit" name="action" id="action" value="del" />
</form><?php
I would insert that in between your <td> tag.
<td class='tabval'>INSERT HERE</td>";