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;
Related
I have the following: The result I am getting is the image attached. I would like the username to be listed only once and then the win, loose across the page. not a row for each result.
$sql_events = mysql_query("SELECT * FROM weekpicks ORDER BY 'username' asc ")
or die (mysql_error());
while ($row = mysql_fetch_array($sql_events)) {
$username = $row["username"];
$week = $row["win_loose"] ;
$row_color = ($row_count++ % 2 == 0 ? $color1 : $color2);
echo '<tr style="background-color: '.$row_color.';">';
echo '<td style="width: 100" align="center"><font size="2">'.$username.'</td>';
echo '<td style="width: 50" align="center"><font size="2">'.$week.'</td>';
echo '<td style="width: 50" align="center"><font size="2">'.$week.'</td>';
echo '<td style="width: 50" align="center"><font size="2">'.$week.'</td>';
echo '<td style="width: 50" align="center"><font size="2">'.$week.'</td>';
you have to update your code. You are querying correctly but taking the data in a wrong way. You have to try associative array to solve your problem. Please try following codes-
$sql_events = mysql_query("SELECT * FROM weekpicks ORDER BY 'username' asc ") or die (mysql_error());
while ($row = mysql_fetch_array($sql_events)) {
$username[$row["username"]][] = $row["win_loose"];
}
foreach($username as $key=>$val){
echo '<td style="width: 100" align="center"><font size="2">'.$username.'</td>';
foreach($val as $value){
echo '<td style="width: 50" align="center"><font size="2">'.$value.'</td>';
}
}
One more thing to mention, it is not a good practice to use inline css on every table column. You could use a class and get the css to the css file attached to that class.
Hope it helps...:)
You're pre-echoing weeks for which you don't have data yet. The idea is, print a cell for each week as long as it's referring to the same user:
$sql_events = mysql_query("SELECT * FROM weekpicks ORDER BY username asc ")
or die(mysql_error());
$current_username = null;
while ($row = mysql_fetch_array($sql_events)) {
$username = $row["username"];
//User changed
if ($current_username == null || $current_username != $username) {
if ($current_username != null) {
echo '</tr>'; //Had another user before so end the row
}
$row_color = ($row_count++ % 2 == 0 ? $color1 : $color2);
echo '<tr style="background-color: ' . $row_color . ';">';
echo '<td style="width: 100" align="center"><font size="2">' . $username . '</td>';
$current_username = $username;
}
$week = $row["win_loose"];
echo '<td style="width: 50" align="center"><font size="2">' . $week . '</td>';
}
echo '</tr>';
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>
I have a pagination code that fetch data from DB and show them in Table paginated, but always missing the last row added in the database, I have no idea why..
this my code ;
if(isset($_GET['page'])){
$page=$_GET['page'];
}else $page=1;
$obj=new action_a_DB();
$totall= $obj->getNmbLgForPages() ; // number of all articles.
$pagination=new pagination($totall,10 /*number of content per page*/,$page,5 /*number of button to show*/);
$start = $pagination->Start ;
$end = $pagination->End ;
// get the info from the db
$total_page = ceil($totall/10);
$result = $obj->getAllArticle($start,$end) ;
}
pagination.php
...
public function Show_Pagination($link,$get='page',$div_class_name='pagination')
{
if($this->Pages==1)return;
echo'<div class="'.$div_class_name.'">';
if($this->Page_number>1)echo 'Pre ';
else echo '<a >Pre</a> ';
//print button
$this->Buttons=(int)$this->Buttons;
$start_counter = $this->Page_number-floor($this->Buttons/2);
$end_conter = $this->Page_number+floor($this->Buttons/2);
if($start_counter<1) $end_conter=$end_conter+abs($start_counter);
if($end_conter>$this->Pages) $start_counter=$start_counter-($end_conter-$this->Pages);
if(($this->Page_number-floor($this->Buttons/2))<1)$end_conter ++;
for ($i=$start_counter;$i<=$end_conter;$i++)
{
if($i>$this->Pages || $i<1)continue; //no print less than 1 value or grater than totall page
if($i==$this->Page_number)echo ' <a class="cur">'.$i.'</a> ';
else echo ' '.$i.' ';
}
if($this->Page_number<$this->Pages)echo 'Suiv ';
else echo '<a >Suiv</a> ';
echo'</div>';
}
...
my funtions in action_a_DB.php
function getAllArticle($Start, $End){
$sql = "SELECT * FROM articles order by id desc limit $Start , $End";
$req = mysql_query($sql) or die('Req Invalide: ' . mysql_error());
//return mysql_fetch_array($req);
return $req;
}
function getNmbLgForPages(){
$query=mysql_query("SELECT count(id) from articles") or die('Erreur :'.mysql_error()); //
$totall=mysql_result($query,0);
return $totall;
}
My table
</tr>
<?php if(mysql_fetch_row($result)!=0){
while($data = mysql_fetch_array($result)) {?>
<tbody>
<tr>
<td class="first style1"><?php echo $data['titre']; ?> </td>
<td><?php echo $data['descrition']; ?></td>
<td><img src="img/edit-icon.gif" width="16" height="16" alt="" /></td>
<td><img src="img/hr.gif" width="16" height="16" alt="" /></td>
<td><img src="img/save-icon.gif" width="16" height="16" alt="save" /></td>
</tr></tbody> <?php }
?>
<tfoot><tr id="nav"><td colspan="5"><div> <?php
$pagination->Show_Pagination("index.php?param1=value1",'page','pagination');
?></div></td>
</tr>
<tr id="total"><td colspan="5"><?php echo 'Page : '.$page.' sur '.$total_page.' Nombre totales d\'articles: '.$totall.'' ; ?></td>
</tr>
<?php } else{ ?>
<tr><td align="center" colspan="5">Rien a afficher</td>
</tr></tfoot>
<?php } ?>
</table>
How can i Fix this problem ?
Thanks a lot,
My mistake was here
<?php if(mysql_fetch_row($result)!=0){
I have to use mysql_num_rows not mysql_fetch_row
So I use
<?php if(mysql_num_rows($result)!=0){
Why, When my query is executed, it returns a result set with a pointer to the first record. Then each fetch returns a line and moves the pointer to the next line or returns false if there is no next row in the result set, then the while to process all the results. In my case, the first line of my resource is used in if so it fails in the display.
Now work fine, thanks to Bovino for explain and to you all
I have a pagination question. When I load this page in the browser and click on the category link to show my products the first page shows with the products limited and the links for pagination are in place but it keeps sending me to the first page which is zero for whatever pagination link I click on. But the odd thing is is when I change
$offset=($pageNum - 1) * $perPage;
to
$offset=($pageNum)= $perPage;
if shows the rest of the products I'm trying to show after clicking on the category. So the problem might be in the page or somewhere around there.
Here is my code.
<?php
$productUlList="";
error_reporting (E_ALL ^ E_NOTICE);
include_once "convenientglobal2localhost.php";
$result = mysql_query("SELECT * FROM category WHERE 1")or die(mysql_error());
while($rowp=mysql_fetch_array($result)){
$categoryId=$rowp['catId'];
$categoryName=$rowp['catName'];
$productUlList.='
<ul id="ul" >
<li id="lists"> '.$categoryName.' </li>
</ul>';
}
?>
<?php
$msg_to_user3='';
$productList.='';
$categoryList='';
include_once "convenientglobal2localhost.php";
$perPage= 3;
if(isset($_GET['category']))
$categoryNames=$_GET['category'];
$pageNum=(isset($_GET['page']))? (int)$_GET['page']: 1;
$pages_query= mysql_query("SELECT * FROM products INNER JOIN category ON categoryName=catName WHERE categoryName='$categoryNames'");
$numrows= mysql_num_rows($pages_query);
$maxpages=ceil($numrows / $perPage);
$offset=($pageNum-1) * $perPage;
if ($offset < 0)
{
$offset = 0 ;
}
include_once "convenientglobal2localhost.php";
$results = mysql_query("SELECT * FROM products WHERE categoryName='$categoryNames' LIMIT $offset, $perPage")or die(mysql_error());
$num=mysql_num_rows($results);
if($num > 0){
while($row=mysql_fetch_array($results)){
$productId=$row['productId'];
$productName=$row['name'];
$productDescription=$row['description'];
$productPrice=$row['price'];
$productDiscountedPrice=$row['discountedPrice'];
$productStock=$row['stock'];
$productCategory=$row['categoryName'];
$categoryId=$row['catId'];
$catName=$row['catName'];
$categoryList='<table><th id="toptable"></th></table>
<table id="categorytable">
<th><img src="inventory_category_images/' . $categoryId . '.jpg" width="498px"; height="125px";/></th>
</table>';
$productList.='<table id="productoutputtable">
<tr>
<td rowspan="7" valign="top"><img style="border-style=solid; border-color:#767475; padding=; "src="inventory_images/' . $productId . '.jpg" width="150" height="135"/>
</td>
</tr>
<tr>
<td id="tablecolor" ><strong>Product</strong></td>
<td colspan="2">' . $productName . ' </td>
<td id="tablecolor"><strong>Category</strong></td>
<td>' . $productCategory . ' </td>
</tr>
<tr>
<td id="tablecolor"><strong>Description:</strong></td>
<td colspan="3">' . $productDescription . ' </td>
</tr>
<tr>
<td id="tablecolor" ><strong>Price:</strong></td>
<td>$' . $productPrice . ' </td>
</tr><tr>
<td id="tablecolor"colspan="1"><strong>Sale Price:</strong></td>
<td>$' . $productDiscountedPrice . ' </td>
<td id="tablecolor"colspan="2"><strong>In Stock </strong></td>
<td>' . $productStock . ' </td>
</tr>
</table>';
}
$self= $_SERVER['PHP_SELF'];
for($page=1; $page<=$maxpages; $page++){
if($page == $pageNum){
$nav= "$page";
}
else{
$nav= "$page";
}
}
if($page > 1)
{
$page=$pageNum-1;
$prev ="[Prev]";
$first="[First Page]";
}
else
{
$prev= " ";
$first=" ";
}
if($pageNum < $maxPages)
{
$page=$pageNum+1;
$next ="[Next]";
$last="[Last Page]";
}
else
{
$next= " ";
$last=" ";
}
$pageList.= $first. $prev. $nav. $next. $last;
}
else{
$msg_to_user3="You have no products listed.";
}
//$pageList.="";
//for($i = 0 ; $i <= $maxpages ; $i++) {
//if($i == $page) {
//$pageList.= "<B>$i</B>";
//}else{
//$pageList.= ''.$i.'';
//
//}
//}
?>
Thanks for all you help!!!
I'll ignore the inefficiencies (refer to comments on your question). The problem is not your offsetting code—that works fine. Your links are broken.
When generating your numbered links into $nav, you need to append, not overwrite. Use .=, not =. Also, beware of capitalization. $maxpages is not $maxPages.
Here's updated code. Proof this works. Unless your database query is misconstructed (I can't test that, sorry!), you should be good to go.
$self= $_SERVER['PHP_SELF'];
for($page=1; $page<=$maxpages; $page++){
if($page == $pageNum){
$nav.= "$page";
}
else{
$nav.= "$page";
}
}
if($page > 1)
{
$page=$pageNum-1;
$prev ="[Prev]";
$first="[First Page]";
}
else
{
$prev= " ";
$first=" ";
}
if($pageNum < $maxpages)
{
$page=$pageNum+1;
$next ="[Next]";
$last="[Last Page]";
}
else
{
$next= " ";
$last=" ";
}
$pageList.= $first. $prev. $nav. $next. $last;
Here is my code the records shows in four columns but if my records is blank it shows three balng images, any suggestions?
$query = mysql_query("SELECT * from rbf_events_images where event_id='".$_GET['id']."'");
echo '<table border="1">';
if(count(mysql_num_rows($query)>0)):
$tropentags='<tr>';
$troclosingtags='</tr>';
$formTags="";
$tdTags="";
$count=1;
while($row = mysql_fetch_array($query)){
$tdTags.='<td align="left" valign="middle" class="td" >$row['image']</td>';
if ($count>3)
{
$formTags.=$tropentags.$tdTags.$troclosingtags;
$tdTags="";
$count=0;
}
$count=$count+1;
}
if ($count>0)
{
for($i = 1; $i <= (4-$count) ; $i++)
{
$tdTags.='<td align="left" valign="middle" class="td" >$row['image']</td>';
}
$formTags.=$tropentags.$tdTags.$troclosingtags;
}
echo $formTags;
endif;
Thanks for your help!really appreciated!
I noticed that on lines like this one:
$tdTags.='<td align="left" valign="middle" class="td" >$row['image']</td>';
You are delimiting the string with single quotes ('), and you are also trying to embed a variable in the string that uses single quotes. I'm not sure how you did not get compile errors for that. I would switch to:
$tdTags= '<td align="left" valign="middle" class="td">' . $row['image'] . '</td>';
Here's what I usually do to put records in columns:
$id = mysql_real_escape_string($_GET['id']);
$query = mysql_query("SELECT * from rbf_events_images where event_id='$id'");
echo '<table border="1"><tbody><tr>';
if (mysql_num_rows($query) > 0) {
$count = 0;
while ($row = mysql_fetch_array($query)) {
if ($count && $count % 4 == 0) echo '</tr><tr>';
echo '<td align="left" valign="middle" class="td">'.$row['image'].'</td>';
$count++;
}
}
echo '</tr></tbody></table>';