How to display Serial Numbers based on the number raw count with mysql query result in my html table.
Which help my table user count the no of record exist.How can I code this? I have try to use
<?php if(count($records) > 0) { ?>
<?php
foreach ($records as $row){?>
but it shows error:
<table border="1" cellspacing="0" cellpadding="2" >
<thead>
<tr>
<th> Serial No </th>
<th> Agent ID </th>
<th> Name of Agent</th>
<th> Agent Mobile</th>
<th> Agent Card No</th>
<th> POS Terminal </th>
<th> APN Mobile No</th>
<th> Update</th>
</tr>
</thead>
<tbody>
<?php
include('connect.php');
$result = $db->prepare("SELECT `Agentid`,`agentname`,`phone`,
`meghna_c_no`, `pos_no`, `apn_mobile` FROM `agent` where `status`=2
ORDER BY Agentid DESC");
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
?>
<tr class="record">
<td><?php echo $row['Agentid']; ?></td>
<td><?php echo $row['agentname']; ?></td>
<td><?php echo $row['phone']; ?></td>
<td><?php echo $row['meghna_c_no']; ?></td>
<td><?php echo $row['pos_no']; ?></td>
<td><?php echo $row['apn_mobile']; ?></td>
<td><a href="editform.php?Agentid=<?php echo $row['Agentid']; ?>">
Insert </a> </td>
</tr>
<?php
}
?>
</tbody>
</table>
use a counter variable and increment it in each row like this
$result->execute();
$counter = 0; // initialize the counter
for($i=0; $row = $result->fetch(); $i++){
$counter+=1; // increment the counter by 1
?>
<tr class="record">
<td><?php echo $counter; ?></td> <!-- display the counter -->
<td><?php echo $row['Agentid']; ?></td>
<td><?php echo $row['agentname']; ?></td>
<td><?php echo $row['phone']; ?></td>
<td><?php echo $row['meghna_c_no']; ?></td>
<td><?php echo $row['pos_no']; ?></td>
<td><?php echo $row['apn_mobile']; ?></td>
<td><a href="editform.php?Agentid=<?php echo $row['Agentid']; ?>">
Insert </a> </td>
</tr>
<?php
}
?>
you can also use your $i variable for it. like this
<td><?php echo ($i+1); ?></td>
instead of
<td><?php echo $counter; ?></td>
Use The $count Variable For Printing Serial Number, It Will continuously adding 1 Number Until The Loop Running.
<table border="1">
<thead>
<tr>
<th> Serial No </th>
<th> Agent ID </th>
<th> Name of Agent</th>
<th> Agent Mobile</th>
<th> Agent Card No</th>
<th> POS Terminal </th>
<th> APN Mobile No</th>
<th> Update</th>
</tr>
</thead>
<tbody>
`<?php
include('connect.php');
$result = $db->prepare("SELECT `Agentid`,`agentname`,`phone`,`meghna_c_no`, `pos_no`, `apn_mobile` FROM `agent` where `status`=2
ORDER BY Agentid DESC");
$result->execute();
$count = 0; // Initializing The Counter
for($i=0; $row = $result->fetch(); $i++)
{
$count++; //increment the counter ; ?>`
<tr class="record">
<td><?php echo $count; ?></td>
<td><?php echo $row['Agentid']; ?></td>
<td><?php echo $row['agentname']; ?></td>
<td><?php echo $row['phone']; ?></td>
<td><?php echo $row['meghna_c_no']; ?></td>
<td><?php echo $row['pos_no']; ?></td>
<td><?php echo $row['apn_mobile']; ?></td>
<td><a href="editform.php?Agentid=<?php echo $row['Agentid']; ?>">
Insert </a>
}
</tr>
</td>
In case you are using while loop, initiate $i = 0, followed by while loop, then echo ($i=$i+1); to print serial number.
Related
I have a table for saving details including logging time.Just as soon as a new record is inserted I want the column 'Countdown' to start an automatic time countdown from 30min until it gets to zero(0).How do I go about it?Thanks.
Here is the table php code
`<?php $results = mysqli_query($db, "SELECT
Vehicle_name,Vehicle_make,Vehicle_color,Number_plate,Date,Time FROM
vehicle"); ?>
<div class="table">
<table>
<thead>
<tr>
<th>Vehicle name</th>
<th>Vehicle make</th>
<th>Vehicle color</th>
<th>Reg Number</th>
<th>Date</th>
<th>Time</th>
<th>Countdown</th>
<th colspan="4">Action</th>
</tr>
</thead>
<?php while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row['Vehicle_name']; ?></td>
<td><?php echo $row['Vehicle_make']; ?></td>
<td><?php echo $row['Vehicle_color']; ?></td>
<td><?php echo $row['Number_plate']; ?></td>
<td><?php echo $row['Date']; ?></td>
<td><?php echo $row['Time']; ?></td>
<td><?php echo $row['Time']; ?></td>
<td>
<a href="php_code.php?del=<?php echo $row['Number_plate']; ?>"
class="del_btn">Delete</a>
</td>
</tr>
<?php } ?>
`
I have a foreach in the table. I foreach a row for every row in my database. So my database has got 10 records, I and my table is showing all those records underneath eachother. So far so good.
I want to number them, from 1 to 10, displayed in front of every row.
This is my table:
<table class="table table-striped mt-3">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Team</th>
<th scope="col">Player</th>
<th scope="col">P</th>
<th scope="col">W</th>
<th scope="col">D</th>
<th scope="col">L</th>
<th scope="col">GF</th>
<th scope="col">GA</th>
<th scope="col">GD</th>
<th scope="col">P</th>
</tr>
</thead>
<tbody>
<?php $count = count($table); ?>
<?php foreach($table as $t): ?>
<tr>
<td><?php for($i = 1; $i < $count; $i++;)
{
echo $i; ?>}
</td>
<td><?php echo $t['team']; ?></td>
<td><?php echo $t['speler']; ?></td>
<td><?php echo $t['gw']; ?></td>
<td><?php echo $t['w']; ?></td>
<td><?php echo $t['g']; ?></td>
<td><?php echo $t['v']; ?></td>
<td><?php echo $t['dv']; ?></td>
<td><?php echo $t['dt']; ?></td>
<td><?php echo $t['ds']; ?></td>
<td><?php echo $t['points']; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
This is my method
public function fifaLeagueTable() {
$getTable = "SELECT * FROM fifa_league ORDER BY points DESC";
$table = $this->model->readAll($getTable);
$count = count($table);
include('app/views/fifaLeagueTable.php');
}
If I var_dump the $count, I receive int(10). So it's counting the amount of rows and I have access to the 10. I am getting a white page, so there might be something wrong in the for loop or something. What did I do wrong?
You just need to create one more variable and its done. Here is updated code :
<table class="table table-striped mt-3">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Team</th>
<th scope="col">Player</th>
<th scope="col">P</th>
<th scope="col">W</th>
<th scope="col">D</th>
<th scope="col">L</th>
<th scope="col">GF</th>
<th scope="col">GA</th>
<th scope="col">GD</th>
<th scope="col">P</th>
</tr>
</thead>
<tbody>
<?php $count = count($table); $num = 1; ?>
<?php foreach($table as $t): ?>
<tr>
<td><?php echo $num; ?>
</td>
<td><?php echo $t['team']; ?></td>
<td><?php echo $t['speler']; ?></td>
<td><?php echo $t['gw']; ?></td>
<td><?php echo $t['w']; ?></td>
<td><?php echo $t['g']; ?></td>
<td><?php echo $t['v']; ?></td>
<td><?php echo $t['dv']; ?></td>
<td><?php echo $t['dt']; ?></td>
<td><?php echo $t['ds']; ?></td>
<td><?php echo $t['points']; ?></td>
</tr>
<?php $num++ ; endforeach; ?>
</tbody>
</table>
I have two tables one for products and another for items.
I want to show all items details for each product in one table like this:
I have tried to make a nested while loops but the result not as I want.
<table border="1">
<thead>
<th>Product No.</th>
<th>Product Name</th>
<th>T.Qty</th>
<th>Item No.</th>
<th>Item Name</th>
<th>Qty </th>
</thead>
<tbody>
<tr>
<td><?php echo $Product_no ?></td>
<td><?php echo $Product_name?></td>
<td><?php echo $TQty ?></td>
<?php
// my problem is here
$Items= $connect->prepare("Query Statment?");
$Items->execute();
$res = $Items->get_result();
while($GetItems = $res->fetch_assoc()){
?>
<td><?php echo GetItems['Item_no'];?></td>
<td><?php echo GetItems['Item_name']; ?></td>
<td><?php echo GetItems['Qty']; ?></td>
<?php } ?>
</tr>
</tbody>
</table>
but the items displayed beside each other not below.
You have a problem in your html table code. You should close the tag for each row you have, and, in case it is not the first line for that element, insert 3 cells with no data:
<tbody>
<tr>
<td><?php echo $Product_no ?></td>
<td><?php echo $Product_name?></td>
<td><?php echo $TQty ?></td>
<?php
// my problem is here
$Items= $connect->prepare("Query Statment?");
$Items->execute();
$res = $Items->get_result();
$i=0;
while($GetItems = $res->fetch_assoc()){
if ($i!=0){
echo "<td></td><td></td><td></td>";
}
?>
<td><?php echo GetItems['Item_no'];?></td>
<td><?php echo GetItems['Item_name']; ?></td>
<td><?php echo GetItems['Qty']; ?></td>
<?php
echo "</tr>";
$i++;
} ?>
</tbody>
I wrote code to retrieve data from database and to print in form of tables on my web. I have stored 4 columns of data in my database but while retrieving it's only showing one column.
My database image
My webpage
My code:
<?php
$con = mysqli_connect("localhost", "root", "", "project");
if(!$con)
{
die('not connected');
}
$result= mysqli_query($con, "SELECT name, stay, food, travel,
SUM(stay + food + travel) AS totalamount,doj
FROM placedetails ");
?>
<div class="container">
<table class="table table-hover">
<thead>
<tr>
<th>place</th>
<th>stay cost</th>
<th>food cost</th>
<th>flight cost</th>
<th>Date of journey</th>
<th>Total cost</th>
</tr>
</thead>
<?php
while($row =mysqli_fetch_array($result))
{
?>
<tbody>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['stay']; ?></td>
<td><?php echo $row['food'] ;?></td>
<td><?php echo $row['travel'] ;?></td>
<td><?php echo $row['doj'] ;?></td>
<td><?php echo $row['totalamount'] ;?></td>
</tr>
</tbody>
<?php
}
?>
</table>
</div>
</div>
Can anyone can tell where the mistake is?
And one more question: I want to diplay only the recent uploaded data on my web page. Suppose I have 4 names as mumbai, but uploaded at different times, I want to display the most recently added mumbai name on my web page
Can anyone help me out in this matter? I will be very thankful..
update your code as move tbody from php loop
<div class="container">
<table class="table table-hover">
<thead>
<tr>
<th>place</th>
<th>stay cost</th>
<th>food cost</th>
<th>flight cost</th>
<th>Date of journey</th>
<th>Total cost</th>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_assoc($result))
{
?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['stay']; ?></td>
<td><?php echo $row['food'] ;?></td>
<td><?php echo $row['travel'] ;?></td>
<td><?php echo $row['doj'] ;?></td>
<td><?php echo $row['totalamount'] ;?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
1) <tbody> should be outside of while loop
2) if you want show only one record means no need to use while loop
3) if you want show recent record means just do descending sort by date column or id column
Query :
SELECT name, stay, food, travel, SUM(stay + food + travel) AS totalamount,doj FROM placedetails order by doj desc limit 1;
Table :
<tbody>
<?php
$row = mysqli_fetch_assoc($result); //fetch first record set only
?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['stay']; ?></td>
<td><?php echo $row['food']; ?></td>
<td><?php echo $row['travel']; ?></td>
<td><?php echo $row['doj']; ?></td>
<td><?php echo $row['totalamount'];?></td>
</tr>
</tbody>
I have a problem, I want that each id in the foreign key can output the name instead of their id. Here is the image.
Here's my code :
<table class="altrowstable" data-responsive="table" >
<thead >
<tr>
<th> IDno</th>
<th> Lastname </th>
<th> Firstname </th>
<th> Department </th>
<th> Program </th>
<th> Action</th>
</tr>
</thead>
<tbody>
<div style="text-align:center; line-height:50px;">
<?php
include('../connection/connect.php');
$YearNow=Date('Y');
$result = $db->prepare("SELECT * FROM student,school_year where user_type =3 AND student.syearid = school_year.syearid AND school_year.from_year like $YearNow ");
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
?>
<tr class="record">
<td><?php echo $row['idno']; ?></td>
<td><?php echo $row['lastname']; ?></td>
<td><?php echo $row['firstname']; ?></td>
//name belong's to their id's
<td><?php echo $row['dept_id']; ?></td>
<td><?php echo $row['progid']; ?></td>
<td><a style="border:1px solid grey; background:grey; border-radius:10%; padding:7px 12px; color:white; text-decoration:none; " href="addcandidates.php?idno=<?php echo $row['idno']; ?>" > Running</a></div></td>
</tr>
<?php
}
?>
</tbody>
</table>
Thanks guys need a help
Just replace this line
<td><?php echo $row['progid']; ?></td>
by this one
<td><?php echo $row['prog_name']; ?></td>
To use the field you want
You also need to adapt your query to select program infos if not already in the table school_year or student (with a join) :
$result = $db->prepare("SELECT * FROM student
INNER JOIN school_year ON student.syearid = school_year.syearid
INNER JOIN program ON student.progid = program.progid
WHERE user_type =3
AND school_year.from_year like $YearNow ");
Assuming the program table is called "program"
Change
<td><?php echo $row['progid']; ?></td>
to
<td><?php echo $row['prog_name']; ?></td>
And I would recommend to study both php and sql
Change
<td><?php echo $row['progid']; ?></td>
by
<td><?php echo $row['prog_name']; ?></td>
inside the loop:
for($i=0; $row = $result->fetch(); $i++){
$program = $db->prepare("SELECT * FROM program where progid = :progid");
$program->execute(array('progid' => $row['progid']));
$p = $program->fetch();
now you can use it as follow: $p['prog_name']