How to display the numbers of each rows in PHP - php

I am having problem to display the numbers of each rows. I am displaying a table with LIMIT of up to 20 rows. I can display the numbers in the while loop but when user click for next rows it will display the number back to 1. How can I display it as a continue number example, 1-20, 21-40, etc? Below are my codes:
<?php
//SQL AND LIMIT codes here
$x = '1';
if(mysqli_num_rows($result) > 0){
while($row=mysqli_fetch_assoc($result)){
$pr_id=$row['pr_id'];
?>
<tr>
<td align="center"><?php echo $x; //display the number ?></td>
<td><?php echo html_entity_decode($row['title']); ?></td>
<td align="center"><?php echo date("d/m/y",strtotime($row['submit_date'])); ?></td>
<td><?php echo $row['purchase_type']; ?></td>
</tr>
<?php
$x++;
}
}
mysqli_free_result($result);
mysqli_close($conn);
echo 'Next';
?>

As you mentioned $next is page number:
One solution is you can do:
$x = 1 + (($next -1 ) * 20);
So...
When $next = 1 (first page) $x = 1 + (0*20) = 1
When $next = 2 (second page) $x = 1 + (1*20) = 21
When $next = 3 (third page) $x = 1 + (2*20) = 41
So on...

Can you please use limit in your sql query and you use below code for getting limit.
$start = 1 + ((page number)*20);
$end = $start + 19;
Now you can set it in your sql query
$sql = "select * from table name where limit".$start.", ".$end;

try like this
<?php
if(isset($_REQUEST['pn'])){
$pn = $_REQUEST['pn'];
} else {
$pn = '1';
}
$x = $pn * 20; //place your no. of rows instead of 20
if(mysqli_num_rows($result) > 0){
while($row=mysqli_fetch_assoc($result)){
$pr_id=$row['pr_id'];
?>
<tr>
<td align="center"><?php echo $x; //display the number ?></td>
<td><?php echo html_entity_decode($row['title']); ?></td>
<td align="center"><?php echo date("d/m/y",strtotime($row['submit_date'])); ?></td>
<td><?php echo $row['purchase_type']; ?></td>
</tr>
<?php
$x++;
}
}
mysqli_free_result($result);
mysqli_close($conn);
echo 'Next';
?>

Related

Row number is not displaying first and last row number in paginated table in php

I am implementing a website in which admin has access to maintain stock. I am displaying stock from the database in html table. i have used pagination to display 10 records per page. I want that when i am on page 1 it displays "Showing 1-10 of total record" and when i am on page 2 it displays "Showing 11-20 of total record" and vice versa.
CODE:
<?php
$link = mysql_connect("localhost", "root", "");
mysql_select_db("login", $link);
$result = mysql_query("SELECT COUNT(*) FROM add_stock");
$row = mysql_fetch_row($result);
$num = $row[0];
?>
<table align="center" border="0" id="myTable" class="table table-striped table-bordered table-list">
<tr>
<th>Sr.No</th>
<th>Product Code</th>
<th>Brand</th>
<th>Price</th>
<th>Gender</th>
<th>Category</th>
<th>Material</th>
<th>Size</th>
<th>Description</th>
<th>Quantity</th>
<th><b>Image</b></th>
</tr>
$num_rec_per_page=10;
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $num_rec_per_page;
$sql = "SELECT * FROM add_stock ORDER BY id DESC LIMIT $start_from, $num_rec_per_page";
$rs_result = mysql_query ($sql); //run the query
$total_records = mysql_num_rows($rs_result); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
$next_page = $page + 1;
$prev_page = $page - 1;
$i= $start_from;
$start=1;
while ($result=mysql_fetch_array($rs_result) )
{
?>
<tr>
<td><?php echo $i+$start; ?></td>
<td><?php echo $result['id']; ?></td>
<td><?php echo $result['brand_name']; ?></td>
<td><?php echo $result['price']; ?></td>
<td><?php echo $result['gender_name']; ?></td>
<td><?php echo $result['category_name']; ?></td>
<td><?php echo $result['material_name']; ?></td>
<td><?php echo $result['size_name']; ?></td>
<td><?php echo $result['dress_description']; ?></td>
<td><?php echo $result['dress_quantity']; ?></td>
<td><a href="javascript:window.open('<?php echo $result['image'] ?>','mypopuptitle', '_parent')" >View Image</a></td>
</tr>
<?php
$i++;
} ?>
</table>
<?php
$sql = "SELECT * FROM add_stock";
$rs_result = mysql_query($sql); //run the query
$total_records = mysql_num_rows($rs_result); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
?>
<div style="margin-left: 5px;">
<div class="pagination">
<?php
echo "<a href='viewstock.php?page=1'><b>«</b></a>";
if ($page==1) {
$page=1;
}
else{
echo "<a href='viewstock.php?page=$prev_page'><b>Prev</b></a>"; }
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='viewstock.php?page=".$i."'><b>".$i."</b></a>";
}
if ($page==$total_pages) {
$page=$total_pages;
}
else{
echo "<a href='viewstock.php?page=$next_page'><b>Next</b></a>"; }
echo "<a href='viewstock.php?page=$total_pages'><b>»</b></a> ";
?>
</div>
</div>
<p>Displaying <?php echo $start ?> - <?php echo $total_records ?> of Records: <?php echo $num ?></p>
This will work:
<p>Displaying <?php echo $start_from+1 ?> - <?php echo $start_from + $total_records ?> of Records: <?php echo $num ?></p>
as long as you:
1) Move the output below the code where the numbers you need are calculated
2) Don't re-use the variable $total_records after your SELECT * FROM add_stock. Instead give this instance of the variable a new name so it can't be confused with the earlier version, which means something different, and is needed for your output.
Please note also my first comment regarding your SQL Injection vulnerability, which you should take steps to fix as soon as possible.

php code to select sum and count of column

there are multiple column in my table ..
col1 col2 col3 price
500 700 100 10
501 700 100 20
502 700 100 30
503 700 100 10
4 70
I need to get count of col1 and display its sum.
and also display sum of price column...
But the main thing is i need to display this in last row....after all data...how can select this and echo in last row...
plz help me...
I need to echo exactly as i put the data in above table...
I need sql query and also need help to echo only sum of this two column in last row only......
SELECT *,IFNULL(col1,'SUM'), count(*) as count FROM coupon_entry WHERE Group By col1 WITH ROLLUP
<?php if (is_array($data)) { foreach($data as $row){ ?>
<tr>
<td><?php echo htmlspecialchars($row['col1']); ?></td>
<td><?php echo htmlspecialchars($row['col2']); ?></td>
<td><?php echo htmlspecialchars($row['col3']); ?></td>
<td><?php echo htmlspecialchars($row['price']); ?></td>
</tr>
<?php } }?>
One solution is to calculate the sum in PHP with variables :
<?php if (is_array($data)) {
$totalCol1 = 0;
$totalPrice = 0;
foreach($data as $row){
$totalCol1 += $row['col1'];
$totalPrice += $row['price'];
?>
<tr>
<td><?php echo htmlspecialchars($row['col1']); ?></td>
<td><?php echo htmlspecialchars($row['col2']); ?></td>
<td><?php echo htmlspecialchars($row['col3']); ?></td>
<td><?php echo htmlspecialchars($row['price']); ?></td>
</tr>
<?php }
<tr>
<td><?php echo $totalCol1;?></td>
<td></td>
<td></td>
<td><?php echo $totalPrice;?></td>
</tr>
}?>
Either you have to do 2 separate queries or else you have to do your calculations in PHP - either one is fairly simple although the PHP solution will probably be slightly (negligibly?) faster.
double-query:
$r = query('SELECT *
FROM yada-yada');
while ($row = fetch($r)) {
echo "<td>$row[col1]</td>\n";
echo "<td>$row[col2]</td>\n";
echo "<td>$row[col3]</td>\n";
echo "<td>$row[price]</td>\n";
}
$r2 = query('SELECT COUNT(*) as cnt, sum(col1) as sum_col1,
sum(price) as sum_price
FROM yada-yada...');
if ($row = fetch($r2)) {
echo "<td>$row['cnt']</td><td>$row['sum_col1']</td>...$row['sum_price']...\n";
}
calculate in PHP:
$cnt = $sum_col1 = $sum_price = 0;
$r = query('SELECT *
FROM yada-yada');
while ($row = fetch($r)) {
echo "<td>$row[col1]</td>\n";
echo "<td>$row[col2]</td>\n";
echo "<td>$row[col3]</td>\n";
echo "<td>$row[price]</td>\n";
$cnt++;
$sum_col1 += $row['col1'];
$sum_price += $row['price'];
}
echo "<td>$cnt</td><td>$sum_col1</td>...$sum_price...\n";
Below is my sql query to get count and sum of column.
SELECT COUNT(coupon) As Total,SUM(Price) AS TotalPrice FROM coupon_entry
And put this in seprate row below table...
<?php
foreach($tot as $tota)
?>
<tr style="background-color:#33CCFF;">
<td colspan="2" style="text-align:right; font-weight:bold;">Total Coupon</td>
<td><?php echo $tota['Total'];?></td>
<td style="text-align:right; font-weight:bold;">Total Amount in Rs</td>
<td><?php echo $tota['TotalPrice'];?></td>
</tr>
<?php }?>

PHP Pagination for user?

I've tried to make a pagination with PHP, but it seems to not work. I want to limit my data which showing in each page, I have this code and it uses PHP code:
showUsers.php
<?php
$query = mysql_query("select * from users");
while ($data = mysql_fetch_array($query)) {
?>
<td><?php echo $data['no_peg']; ?></td>
<td>
<?php
echo $data['username'];
//previlage admin
if ($_SESSION['role'] == 'admin') {
?>
<div class="row-actions">
Edit
<?php if ($data['role'] != 'admin') {?>
| Delete
<?php } ?>
</div>
<?php } ?>
</td>
<td><?php echo $data['fullname']; ?></td>
<td><?php echo $data['telephone']; ?></td>
<td><?php echo $data['email']; ?></td>
I want to show just 10 names per page, to avoid long scrolling, but how can it work?
Hi,if you want to make a pagination, you should add in a LIMIT clause into your queries, like this:
SELECT* FROM users LIMIT 0, 10
With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return.
So, what's the next?
You need to add a parameter to your query url specifies the number of page when you list the users.
Such as:
showUsers.php?page=1
Then, in your program, you can get parameter by this:
$page = isset($_GET['page']) ? $_GET['page'] : 1;
I hope it will help you, I'm new here.
<?php
$sqlCount = "select count(id_user) from users";
$rsCount = mysql_fetch_array(mysql_query($sqlCount));
$totalData = $rsCount[0];
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$limit = 10;
$start_from = $limit * ($page - 1);
$sql_limit = "SELECT * FROM users limit $start_from, $limit";
$result = mysql_query($sql_limit);
while ($data = mysql_fetch_array($result)) {
?>
<td><?php echo $data['no_peg']; ?></td>
<td>
<?php
echo $data['username'];
//previlage admin
if ($_SESSION['role'] == 'admin') {
?>
<div class="row-actions">
Edit
<?php if ($data['role'] != 'admin') {?>
| Delete
<?php } ?>
</div>
<?php } ?>
</td>
<td><?php echo $data['fullname']; ?></td>
<td><?php echo $data['telephone']; ?></td>
<td><?php echo $data['email']; ?></td>
</tr>
<?php
}
?>
<?php
$totalPage = ceil($totalData / $limit);
echo 'Page : ';
for($i = 1; $i <= $totalPage; $i++){
if($page != $i){
echo '['.$i.'] ';
}else{
echo "[$i] ";
}
}
?>
Where is your pagination code? Your code just shows this query:
$query = mysql_query("select * from users");
But for basic pagination you need to set a LIMIT like so:
$query = mysql_query("select * from users LIMIT 0,10");
So that would only grab the first 10 items. And then—let’s say, on page 2 you could do this:
$query = mysql_query("select * from users LIMIT 11,10");
That would grab the next 10 items starting from item 11.
That’s the basic concept. But you have to code the logic for passing along pagination values & such.
<?php
if(is_int($_GET('pageNo'))) // getting the page number from the URL i.e script.php?pageNo=2
{
$query = mysql_query("select * from users limit ".$_GET['pageNo']." ,10");
while ($data = mysql_fetch_array($query)) {
?>
<td><?php echo $data['no_peg']; ?></td>
<td>
<?php
echo $data['username'];
//previlage admin
if ($_SESSION['role'] == 'admin') {
?>
<div class="row-actions">
Edit
<?php if ($data['role'] != 'admin') {?>
| Delete
<?php } ?>
</div>
<?php } ?>
</td>
<td><?php echo $data['fullname']; ?></td>
<td><?php echo $data['telephone']; ?></td>
<td><?php echo $data['email']; ?></td>
</tr>
<?php
}
} // not sure where the closing if should be you figure it out :P
?>

How to showing report that group by archieve with PHP & MySQL?

I want to recapitulate report that grouped by month. But the results of sum is incorrect. It's just sum data in one month and show in all rows.
<?php
$query = mysql_query("SELECT * FROM barang GROUP BY arsip");
$i = 1;
while ($data = mysql_fetch_assoc($query)) {
?>
<tr class="<?php if ($i % 2 == 0) { echo "odd"; } else { echo "even"; } ?>">
<td><?php echo $i; ?></td>
<td><?php echo $data['arsip']; ?></td>
<td><?php
$Masuk= mysql_query("SELECT SUM(barang_in) AS masuk FROM barang GROUP BY arsip");
if($Masuk){
$data = mysql_fetch_assoc($Masuk);
echo $data['masuk'];
}
?></td>
<td><?php
$keluar= mysql_query("SELECT SUM(bijih_out+htm_out+pth_out) AS keluar FROM barang GROUP BY arsip");
if($keluar){
$data = mysql_fetch_assoc($keluar);
echo $data['keluar'];
}?></td>
<td><?php
$efisiensi= mysql_query("SELECT SUM((bijih_out+htm_out+pth_out)-barang_in) AS efisiensi FROM barang GROUP BY arsip");
if($efisiensi){
$data = mysql_fetch_assoc($efisiensi);
echo $data['efisiensi'];
}
?></td>
<td><?php
$persen= mysql_query("SELECT SUM(barang_in/(bijih_out+htm_out+pth_out))*1 AS persen FROM barang GROUP BY arsip");
$simbol = "%";
if($persen){
$data = mysql_fetch_assoc($persen);
echo number_format($data['persen'],2); echo $simbol;
}
?></td>
<?php
$i++;
}
?>
so that's my code. what's wrong with that? There are some columns for displaying income items, outcome items, efficiency ( difference of outcome and income items ) and percent of efficiency.
finally I found out the right code. Thankyou for #RandomSeed who helps me. This is my final code and it works!
<?php
$query = mysql_query("SELECT arsip, SUM(barang_in) AS income, SUM(bijih_out+htm_out+pth_out) AS outcome, SUM((bijih_out+htm_out+pth_out
)-barang_in) AS efficiency, SUM((barang_in / ( bijih_out + htm_out + pth_out))*100) AS percent FROM barang GROUP BY arsip");
$i = 1;
while ($data = mysql_fetch_assoc($query)) {
?>
<tr class="<?php if ($i % 2 == 0) { echo "odd"; } else { echo "even"; } ?>">
<td><?php echo $i; ?></td>
<td><?php echo $data['arsip']; ?></td>
<td><?php echo $data['income']; ?></td>
<td><?php echo $data['outcome']; ?></td>
<td><?php echo $data['efficiency']; ?></td>
<td><?php $simbol = "%"; echo number_format($data['percent'],0); echo $simbol; ?></td>
<?php
$i++;
}
?>

Divide list in two columns

I have list of values that I want to fetch from my database. So List is long I want to fetch list in two columns. How can I break it in 2 columns. Eg..
taking list of aphabets
A | B
C | D
E | F
G | H
I | J
K | L
IN this way. I used following approach...
<?php
$sql = mysql_query("SELECT * FROM poet WHERE status='Publish' ORDER BY name ") or die(mysql_error());
$total_rows = mysql_num_rows($sql);
$i = 1;
$j = ceil($total_rows/2);
while ( $i <= $j ) {
$result = mysql_fetch_assoc($sql);
?>
<tr>
<? if($i%2 == 0) { ?>
<td class="navigation"><?php echo fetch_table_poet( $result["pid"], 1, 3); ?></td>
<? } elseif($i%2 != 0) { ?>
<td class="navigation"><?php echo fetch_table_poet( $result["pid"], 1, 3); ?></td>
<? } ?>
</tr>
<?php $i++; } ?>
Its giving me output in this way..
A | A
B | B
C | C
NOTE - I wish to do it using PHP not using any jquery or javascript.
It seems you are resetting your result every time you loop. Set the $results outside the while. The more accepted way to do this is:
while ($row = mysql_fetch_assoc($sql)){
}
Then test $i to see if it is divisible by 2 (meaning you have 2 columns and need to add the end of row and beginning of new row tags).
if($i%2 == 0){
echo '</tr><tr>';
}
try:
<?php
$sql = mysql_query("SELECT * FROM poet WHERE status='Publish' ORDER BY name ") or die(mysql_error());
$i = 0;
$total_rows = mysql_num_rows($sql);
while ( $result = mysql_fetch_assoc($sql) ) {
$i++;
?>
<? if($i&1 == 1) { ?>
<tr>
<td class="navigation"><?php echo $result["pid"]; ?></td>
<? } elseif($i&1 == 0) { ?>
<td class="navigation"><?php echo $result["pid"]; ?></td>
</tr>
<? }
if($i == $total_rows && $total_rows&1 == 1){ echo "</tr>"; } ?>
<?php } ?>
Edited: Bug fixes..
i think Colmn rows this will work:
<?php
$sql = mysql_query("SELECT * FROM poet WHERE status='Publish' ORDER BY name ") or die(mysql_error());
$total_rows = mysql_num_rows($sql);
$iOne = 1;
$iTwo = floor($totalRows/2);
foreach($iOne=1,$iTwo=ceil($totalRows/2); $iTwo < $total_rows; $iOne++,$iTwo++){
?>
<tr>
<td class="navigation"><?php echo mysql_result($sql,$iOne,'pid') ?></td>
<td class="navigation"><?php echo mysql_result($sql,$iTwo,'pid') ?></td>
</tr>
<?php
}
?>
I would suggest jQuery columnizer as a solution to your problem http://welcome.totheinter.net/columnizer-jquery-plugin/
Finally I succeeded with code.
<?php
$sql = mysql_query("SELECT * FROM poet WHERE status='Publish' ORDER BY name ") or die(mysql_error());
$total_rows = mysql_num_rows($sql);
$i = 1;
while ( $result = mysql_fetch_assoc($sql) ) { ?>
<?php if($i%2 == '1') { ?>
<tr>
<td class="navigation"><?php echo fetch_table_poet( $result["pid"], 1, 3); ?></td>
<?php } elseif($i%2 == '0') { ?>
<td class="navigation"><?php echo fetch_table_poet( $result["pid"], 1, 3); ?></td>
</tr>
<?php } ?>
<?php $i++; } ?>

Categories