I'm newbee about php,trying to learn.
I've search other topic about while loop pagination but not satisfied.
I have 70 user records in my database,wants to list them with html table. I'm showing all records with this code. How can i make simple pagination with these codes? Please help me.
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Surname</th>
<th>Email</th>
<th>Password</th>
<th>Date</th>
<th>Gender</th>
</tr>
</thead>
<tbody>
<?php
$q = "SELECT * FROM users ORDER BY uid ASC";
$r = mysqli_query($dbc,$q);
while($userlist = mysqli_fetch_assoc($r)){ ?>
<tr>
<td><?php echo $userlist['uid']; ?></td>
<td><?php echo $userlist['name']; ?></td>
<td><?php echo $userlist['surname']; ?></td>
<td><?php echo $userlist['email']; ?></td>
<td><?php echo $userlist['password']; ?></td>
<td><?php echo $userlist['date']; ?></td>
<td><?php echo $userlist['gender']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
The follow provide very simple pagination as a starting point. You will need to supply formatting for the pagination and such.
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Surname</th>
<th>Email</th>
<th>Password</th>
<th>Date</th>
<th>Gender</th>
</tr>
</thead>
<tbody>
<?php
$q = "SELECT count(*) as `numrows` FROM `users` ORDER BY `uid` ASC";
$c = mysqli_query($dbc,$q);
if($c) {
if($t = mysqli_fetch_assoc($c)) {
$numrows = $t['numrows'];
}
}
$numrows = 0;
$rowsperpage = 10;
$currpage = isset($_REQUEST['currpageno']) && $_REQUEST['currpageno'] != 0 ? $_REQUEST['currpageno'] : 1;
$numpages = ceil($numrows / $rowsperpage);
$startrow = ($currpage - 1) * $rowsperpage;
if($startrow > $numrows) {
$startrow = $numrows - $rowsperpage;
}
if($startrow < 0) {
$startrow = 0;
}
$q = "SELECT * FROM `users` ORDER BY `uid` ASC LIMIT ".$startrow.",".$rowsperpage.";";
$r = mysqli_query($dbc,$q);
while($userlist = mysqli_fetch_assoc($r)){
?>
<tr>
<td><?php echo $userlist['uid']; ?></td>
<td><?php echo $userlist['name']; ?></td>
<td><?php echo $userlist['surname']; ?></td>
<td><?php echo $userlist['email']; ?></td>
<td><?php echo $userlist['password']; ?></td>
<td><?php echo $userlist['date']; ?></td>
<td><?php echo $userlist['gender']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<div id='pagination'>
<?php
for($pgno = 1;$pgno <= $numpages;$pgno++) {
echo "<a class='' href='?currpageno=".$pgno."'>".$pgno."</a>";
}
?>
</div>
Related
I am using DataTables and I want to add new TR at the end of while loop.
I know we can add <tfoot></tfoot>, but I don't want to add '' because I am filtering data with custom Ajax.
I have tried below code but it's not working:
<?php
$Itesres = mysqli_query($con_db,"SELECT * FROM tbl_area ORDER BY `tbl_area`.`name` ASC");
while($ItemResult = mysqli_fetch_array($Itesres)){
?>
<table id="printData" class="table table-bordered table-hover ">
<thead>
<tr>
<th>Group</th>
<th>Party Name</th>
<th>Balance</th>
</tr>
</thead>
<tbody id="getGroups">
<?php
$i = 1;
while($row = mysqli_fetch_array($sdetails)){
$totalAmount += $row['total_debtors'];
$i++;
?>
<tr>
<td><?php echo getAreaName($row['area_id']); ?></td>
<td><?php echo GrabAccountIDName($row['client_id']); ?></td>
<td><?php echo abs($row['total_debtors']); ?></td>
</tr>
<?php if( $i == ( $numRows - 1 ) ) { ?>
<tr>
<td> </td>
<td style="text-align:right">Total:</td>
<td><?php echo abs($totalAmount); ?></td>
</tr>
<?php } } ?>
</tbody>
</table>
Also, when I use <tfoot></tfoot> it's not printable.
Probably your problem is in $numRows which is not defined.
So you can try this:
<?php
$Itesres = mysqli_query($con_db,"SELECT * FROM tbl_area ORDER BY `tbl_area`.`name` ASC");
$numRows = mysqli_num_rows($Itesres);
while($ItemResult = mysqli_fetch_array($Itesres)){
?>
<table id="printData" class="table table-bordered table-hover ">
<thead>
<tr>
<th>Group</th>
<th>Party Name</th>
<th>Balance</th>
</tr>
</thead>
<tbody id="getGroups">
<?php
$i = 1;
while($row = mysqli_fetch_array($sdetails)){
$totalAmount += $row['total_debtors'];
$i++;
?>
<tr>
<td><?php echo getAreaName($row['area_id']); ?></td>
<td><?php echo GrabAccountIDName($row['client_id']); ?></td>
<td><?php echo abs($row['total_debtors']); ?></td>
</tr>
<?php if( $i == ( $numRows - 1 ) ) { ?>
<tr>
<td> </td>
<td style="text-align:right">Total:</td>
<td><?php echo abs($totalAmount); ?></td>
</tr>
<?php } } ?>
</tbody>
</table>
I have created a table from a SQL query and displayed it in the same order as they appear in the table. (Table A in image).
This is working okay.
However it would be great if the data could be clubbed under the member category. As in Table B in image.
SQL Query ...
$row = mysqli_num_rows($sql);
if($row > 0) {
while ($result = mysqli_fetch_assoc($sql)){
$category[] = trim($result['category']);
$name[] = trim($result['f_name']).' '.trim($result['l_name']);
$memid[] = trim($result1['memid']);
$addr[] = trim($result['addr']);
$phone[] = trim($result['phone']);
}
} ?>
<table>
<tr>
<th>Category</th>
<th>Mem ID</th>
<th>Name</th>
<th>Address</th>
<th>Phone</th>
</tr>
<?php
if ($row>0) {
for ($i=0; $i<=$row-1; $i++){ ?>
<tr>
<td><?php echo $category[$i]; ?></td>
<td><?php echo $memid[$i]; ?></td>
<td><?php echo $name[$i]; ?> </td>
<td><?php echo $addr[$i]; ?> </td>
<td><?php echo $phone[$i]; ?> </td>
</tr>
<?php }
} ?>
</table>
A little update of your code souhld do it:
$categories = [];
$row = mysqli_num_rows($sql);
if($row > 0) {
while ($result = mysqli_fetch_assoc($sql)) {
$result_category = trim($result['category']);
if (!isset($categories[$result_category])) {
$categories[$result_category] = [];
}
$new = [];
$new['category'] = $result_category;
$new['name'] = trim($result['f_name']).' '.trim($result['l_name']);
$new['memid'] = trim($result['memid']);
$new['addr'] = trim($result['addr']);
$new['phone'] = trim($result['phone']);
$categories[$result_category][] = $new;
}
} ?>
<table>
<tr>
<th>Category</th>
<th>Name</th>
<th>Phone</th>
</tr>
<?php
if ($row>0) {
foreach ($categories as $category_name => $data){ ?>
<tr>
<td><?php echo $category_name; ?></td>
<td></td>
<td></td>
</tr>
<?php foreach ($data as $row) {?>
<tr>
<td><?php echo $row['memid']; ?></td>
<td><?php echo $row['name']; ?> </td>
<td><?php echo $row['phone']; ?> </td>
</tr>
<?php }
}
} ?>
</table>
While loop though the data mark on array with group of category name and then print the result
$row = mysqli_num_rows($sql);
if($row > 0) {
$mainArray = [];
while ($result = mysqli_fetch_assoc($sql)){
$category = $result['category'];
if(isset($mainArray[$category])){
$mainArray[$category][] = $result;
} else {
$mainArray[$category] = $result;
}
}
}
foreach($mainArray as $cateName => $data){ ?>
<tr>
<td style="text-align:left"><?php echo $cateName; ?></td>
<td></td>
<td></td>
</tr>
<?php
foreach($data as $row){ ?>
<tr>
<td><?php echo $row['memid']; ?></td>
<td><?php echo $row['f_name'].' '.$row['l_name']; ?></td>
<td><?php echo $row['phone']; ?></td>
</tr>
<?php } ?>
}
?>
I am trying to display all the info in the table but when I query the information then put it into a PHP table it doesn't show any data in the last table.
Here is my PHP code for the table
<table border='1'>
<tr>
<th>Ticket ID</th>
<th>Username</th>
<th>Message</th>
<th>Date</th>
<th>Error #</th>
<th>Priority</th>
</tr>
<?php
if(!$query){
die('Invalid query: ' .mysql_error());
}
while ($row = mysql_fetch_array($query)) { ?>
<tr>
<td><?php echo $row['TicketID']; ?></td>
<td><?php echo $row['Message']; ?></td>
<td><?php echo $row['Date']; ?></td>
<td><?php echo $row['Error']; ?></td>
<td><?php echo $row['Priority']; ?></td>
</tr>
<?php } ?>
</table>
Here is the PHP code for query
<?php
$server = mysql_connect("localhost", "root", "**password**");
$db = mysql_select_db("minecraft", $server);
$query = mysql_query("SELECT * FROM tickets");
?>
All of my row names are correct but it doesn't want to put the data into that column.
Here is my table structure
You Omitted
<td><?php echo $row['Username']; ?></td>
that should be after
<td><?php echo $row['TicketID']; ?></td>
While loop not working not showing anything.
My code is working when I use without creating a function on the same page but when I create function on functions.php and call it, it does not work.
<?php
include "include/db.php";
function search(){
global $connection;
$record_search = $_GET['search'];
$record_search = mysqli_real_escape_string($connection, $record_search);
$query_search = "SELECT * FROM users WHERE u_name='$record_search' OR u_roll='$record_search' ";
$result_search = mysqli_query($connection, $query_search);
if(!$result_search){
die("FAIL" . mysqli_error($connection));
}
while ($row_search = mysqli_fetch_array($result_search)) {
$name_search = $row_search[1];
$f_search = $row_search[2];
$school_search = $row_search[3];
$roll_search = $row_search[4];
$email_search = $row_search[5];
$class_search = $row_search[6];
}
}
?>
and
<?php
if(isset($_GET['search'])){
global $connection;
global $result_search;
search();
?>
<table class="table">
<thead>
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<th>Father Name</th>
<th>School</th>
<th>Roll No</th>
<th>Email</th>
<th>Class</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td><?php echo #$name_search; ?></td>
<td><?php echo #$f_search; ?></td>
<td><?php echo #$school_search; ?></td>
<td><?php echo #$roll_search; ?></td>
<td><?php echo #$email_search; ?></td>
<td><?php echo #$class_search; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
You can see nothing happen:
Did you try to connect to the mysql before the execution of the query. Maybe, you have put the connect code in the other file, however couldn't see it here so just wondering.
your code should be like below, hope it will work to you.
function search(){
global $connection;
$record_search = $_GET['search'];
$record_search = mysqli_real_escape_string($connection, $record_search);
$result_search = mysqli_query($connection, $query_search);
if(!$result_search){
die("FAIL" . mysqli_error($connection));
}
$query_search = "SELECT * FROM users WHERE u_name='$record_search' OR u_roll='$record_search' ";
return $row_search = mysqli_fetch_array($result_search);
}
<table class="table">
<thead>
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<th>Father Name</th>
<th>School</th>
<th>Roll No</th>
<th>Email</th>
<th>Class</th>
</tr>
</thead>
<tbody>
<?php
$search_results = search();
$i=1;
foreach($search_results as $row_search){
$name_search = $row_search[1];
$f_search = $row_search[2];
$school_search = $row_search[3];
$roll_search = $row_search[4];
$email_search = $row_search[5];
$class_search = $row_search[6];
?>
<tr>
<th scope="row"><?php echo $i ?></th>
<td><?php echo $name_search; ?></td>
<td><?php echo $f_search; ?></td>
<td><?php echo $school_search; ?></td>
<td><?php echo $roll_search; ?></td>
<td><?php echo $email_search; ?></td>
<td><?php echo $class_search; ?></td>
</tr>
<?php
$i++;
} ?>
</tbody>
</table>
I need to insert two loops in one table, but I have a problem.
<table border="1">
<tr>
<th>Position</th>
<th>Name</th>
</tr>
<?php
for ($x=1; $x<=2; $x++) {
?>
<tr>
<td><?php echo $x ?></td>
<?php
}
?>
<?php
while($row = mysql_fetch_array($query)) {
$id = $row['id'];
$name = $row['name'];
?>
<td><?php echo $name ?></td>
</tr>
<?php
}
?>
</table>
But the result is:
http://prntscr.com/6m9v25
One name is in the wrong position.
Just put while loop into for loop.
// Code goes here
<table border="1">
<tr>
<th>Position</th>
<th>Name</th>
</tr>
<?php
for ($x=1; $x<=2; $x++) {
?>
<tr>
<td><?php echo $x ?></td>
<?php
while($row = mysql_fetch_array($query)) {
$id = $row['id'];
$name = $row['name'];
?>
<td><?php echo $name ?></td>
</tr>
<?php
}
?>
<?php
}
?>
</table>
<table border="1">
<tr>
<th>Position</th>
<th>Name</th>
</tr>
<?php
$x = 1;
while($row = mysql_fetch_array($query)) {
$id = $row['id'];
$name = $row['name'];
?>
<tr>
<td><?php echo $x ?></td>
<td><?php echo $name ?></td>
</tr>
<?php
$x++
}
?>
</table>
Try This Code!! (Edited Again!)