Display number sequence based on the number of data in database - php

How can I display the number based on the number of data from database.
For example, the above picture, shows data extract from the database. In my database there are two data. Therefore I would like to display number 1 and 2 at the No column.
And if there is four data in the database, I would like to display number 1, 2, 3 and 4 at the No column.
Coding to display the above table
<?php
$sql= mysql_query ("SELECT * FROM employee INNER JOIN cash ON employee.emp_id = cash.emp_id WHERE cash_status='Pending'");
echo "<table id='dataTable' width='850' border='1' align='center'>";
echo "<tr>
<th height='50'>No</th>
<th height='50'>Employee Number</th>
<th height='50'>Name</th>
<th height='50'>Department</th>
<th height='50'>Date Apply</th>
<th height='50'>Date Cash To Be Use</th>
<th height='50'>Amount</th>
<th height='50'>Status</th>
<th height='50'>Cash Id</th>
<th height='50'>View</th>
</tr>";
while ($row = mysql_fetch_array($sql))
{
echo "<tr>";
echo "<td align='center' height='30'></td>";
echo "<td align='center' height='30'>" .$row['emp_id']. "</td>";
echo "<td align='center' height='30'>" .$row['emp_name']. "</td>";
echo "<td align='center' height='30'>" .$row['emp_department']. "</td>";
echo "<td align='center' height='30'>" .$row['cash_dapply']. "</td>";
echo "<td align='center' height='30'>" .$row['cash_duse']. "</td>";
echo "<td align='center' height='30'>RM" .$row['cash_amount']. "</td>";
echo "<td align='center' height='30'>" .$row['cash_status']. "</td>";
echo "<td align='center' height='30'>" .$row['cash_id']. "</td>";
echo"<td height='30'><img src='../img/view_user.png' width='20' height='20'></td>";
echo "</tr>";
}
echo "</table>";
?>
Thanks.

Try with the Below Code :
<?php
$i = 0;
$sql= mysql_query ("SELECT * FROM employee INNER JOIN cash ON employee.emp_id = cash.emp_id WHERE cash_status='Pending'");
echo "<table id='dataTable' width='850' border='1' align='center'>";
echo "<tr>
<th height='50'>No</th>
<th height='50'>Employee Number</th>
<th height='50'>Name</th>
<th height='50'>Department</th>
<th height='50'>Date Apply</th>
<th height='50'>Date Cash To Be Use</th>
<th height='50'>Amount</th>
<th height='50'>Status</th>
<th height='50'>Cash Id</th>
<th height='50'>View</th>
</tr>";
while ($row = mysql_fetch_array($sql))
{
echo "<tr>";
echo "<td align='center' height='30'>".$++i."</td>";
echo "<td align='center' height='30'>" .$row['emp_id']. "</td>";
echo "<td align='center' height='30'>" .$row['emp_name']. "</td>";
echo "<td align='center' height='30'>" .$row['emp_department']. "</td>";
echo "<td align='center' height='30'>" .$row['cash_dapply']. "</td>";
echo "<td align='center' height='30'>" .$row['cash_duse']. "</td>";
echo "<td align='center' height='30'>RM" .$row['cash_amount']. "</td>";
echo "<td align='center' height='30'>" .$row['cash_status']. "</td>";
echo "<td align='center' height='30'>" .$row['cash_id']. "</td>";
echo"<td height='30'><img src='../img/view_user.png' width='20' height='20'></td>";
echo "</tr>";
}
echo "</table>";
?>

#Hardy Change .$++i. to .++$i.

Related

I want to display "No Image" when there's no image 'BLOB' at MySQL database

based on my question above, currently, when there is no image at the database, it will display a broken image while if there is an image, it will display the image. Now how to display "No Image" when there's no image 'BLOB' at MySQL database.
I uses BLOB format at the MySQL Database. Below is the code:
<?php
$query = $conn->query("SELECT * FROM report LEFT JOIN users ON report.badgeid = users.badgeid LEFT JOIN team ON team.team_id = users.team_id WHERE team.team_id = '$team' ORDER BY report.report_date DESC LIMIT 10");
$query -> execute();
$results = $query -> fetchAll(PDO::FETCH_OBJ);
if($query->rowCount() == 0){
echo "<table class = 'table-bordered' width ='100%'>";
echo "<thead>";
echo "<tr>";
echo "<th>Date</th>
<th>Task Name</th>
<th>Before Task</th>
<th>After Task</th>
<th>OT From</th>
<th>OT To</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody >
<tr>
<td colspan='8'>No report at this moment</td>
</tr>
</tbody>
</table>";
}else{
echo "<table class = 'table-bordered' width ='100%'>";
echo "<thead>";
echo "<tr>";
echo "<th>Date</th>
<th>Task Name</th>
<th>Before Task</th>
<th>After Task</th>
<th>OT From</th>
<th>OT To</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody >";
$query = $conn->query("SELECT * FROM report LEFT JOIN users ON report.badgeid = users.badgeid LEFT JOIN team ON team.team_id = users.team_id WHERE team.team_id = '$team' ORDER BY report.report_date DESC LIMIT 10");
while($row = $query->fetch(PDO::FETCH_ASSOC)){
echo "<tr>";
echo "<td rowspan='2'>". $row['report_date'] . "</td>";
echo "<td rowspan='2'>". $row['task_name'] . "</td>";
echo "<td align='center'><img src='data:image/jpeg;base64,".$before."'/></td>";
echo "<td align='center'><img src='data:image/jpeg;base64,".$row['photo_after']."'/></td>";
echo "<td rowspan='2' align='center'>". $row['ot_start']. "</td>";
echo "<td rowspan='2' align='center'>".$row['ot_end']. "</strong></td>";
echo "<td rowspan='2'><strong>". $row['report_status'] . "</strong></td>";
echo "<td rowspan='2' align='center'>";
echo "<a class='btn-view btn-primary btn-sm' href='../view_task/view_task.php?report_id=". $row['report_id'] ."' data-toggle='tooltip'>View</a>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td align='center'>". $row['time_photo_before'] . "</td>";
echo "<td align='center'>". $row['time_photo_after'] . "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table><br>";
echo "</div>";
echo "<div>";
}
?>
Just put a condition to check whether the image exist or not empty in the row.
if(!isset($row['photo_after']) || empty($row['photo_after'])) {
echo "<td align='center'><img src='default.png'/></td>";
}
else
{
echo "<td align='center'><img src='data:image/jpeg;base64,".$row['photo_after']."'/></td>";
}
Try this..., I hope this will work for you.
Inside your while loop, I made a condition which checks there is something in your field or it's empty. if it's empty a variable store the text you want to print and if it's not empty it will show your image in variable $image and I echo the variable where you want to show your image.
<?php
$query = $conn->query("SELECT * FROM report LEFT JOIN users ON report.badgeid = users.badgeid LEFT JOIN team ON team.team_id = users.team_id WHERE team.team_id = '$team' ORDER BY report.report_date DESC LIMIT 10");
$query -> execute();
$results = $query -> fetchAll(PDO::FETCH_OBJ);
if($query->rowCount() == 0){
echo "<table class = 'table-bordered' width ='100%'>";
echo "<thead>";
echo "<tr>";
echo "<th>Date</th>
<th>Task Name</th>
<th>Before Task</th>
<th>After Task</th>
<th>OT From</th>
<th>OT To</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody >
<tr>
<td colspan='8'>No report at this moment</td>
</tr>
</tbody>
</table>";
}else{
echo "<table class = 'table-bordered' width ='100%'>";
echo "<thead>";
echo "<tr>";
echo "<th>Date</th>
<th>Task Name</th>
<th>Before Task</th>
<th>After Task</th>
<th>OT From</th>
<th>OT To</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody >";
$query = $conn->query("SELECT * FROM report LEFT JOIN users ON report.badgeid = users.badgeid LEFT JOIN team ON team.team_id = users.team_id WHERE team.team_id = '$team' ORDER BY report.report_date DESC LIMIT 10");
while($row = $query->fetch(PDO::FETCH_ASSOC)){
// Here is my code...
if(empty($row['photo_after'])) {
$image = "NO IMAGE FOUND..."; //anything you want to diaplay if there is no image
}
else{ $image = "<img src='data:image/jpeg;base64,".$row['photo_after']."'/>"; }
// Check the $image variable in your table
echo "<tr>";
echo "<td rowspan='2'>". $row['report_date'] . "</td>";
echo "<td rowspan='2'>". $row['task_name'] . "</td>";
echo "<td align='center'><img src='data:image/jpeg;base64,".$before."'/></td>";
echo "<td align='center'>".$image."</td>"; // $image store your data
echo "<td rowspan='2' align='center'>". $row['ot_start']. "</td>";
echo "<td rowspan='2' align='center'>".$row['ot_end']. "</strong></td>";
echo "<td rowspan='2'><strong>". $row['report_status'] . "</strong></td>";
echo "<td rowspan='2' align='center'>";
echo "<a class='btn-view btn-primary btn-sm' href='../view_task/view_task.php?report_id=". $row['report_id'] ."' data-toggle='tooltip'>View</a>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td align='center'>". $row['time_photo_before'] . "</td>";
echo "<td align='center'>". $row['time_photo_after'] . "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table><br>";
echo "</div>";
echo "<div>";
}
?>

make a list of products with partial same product number

I want to make an (html) list of products that have the same unique number behind the full stop (.). For example, 1.0001 is white bread sold on Monday 2.0001 is white bread sold on Tuesday and so on.
Now I want to make a list that sums of all white bread sold throughout the week. This is what I have so far:
<?php
echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";
echo "<tr style='font-weight: bold;'>";
echo "<td width='100' align='center'>Produkt</td><td width='100' align='center'>Baklijst</td>";
include("dbopen.php");
$result = mysql_query(
"SELECT order_item_name, SUM( product_quantity )
FROM wntl_virtuemart_order_items
WHERE wntl_virtuemart_order_items.order_item_sku REGEXP '0001$'
AND order_status = 'U'");
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td align='center' width='200'>" . $row['order_item_name'] . "</td>";
echo "<td align='center' width='200'>" . $row['SUM(product_quantity)'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
output:
Notice: Undefined index: SUM(product_quantity) in *.php on line 17
Try this:
<?php
echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";
echo "<tr style='font-weight: bold;'>";
echo "<td width='100' align='center'>Produkt</td><td width='100' align='center'>Baklijst</td>";
include("dbopen.php");
$result = mysql_query(
"SELECT order_item_name, SUM( product_quantity ) AS Sum
FROM wntl_virtuemart_order_items
WHERE wntl_virtuemart_order_items.order_item_sku REGEXP '0001$'
AND order_status = 'U'");
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td align='center' width='200'>" . $row['order_item_name'] . "</td>";
echo "<td align='center' width='200'>" . $row['Sum'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>

Notice: Undefined index:

I have a problem with my code.
if I use select * from $table it's all good but
now with select distinct it shows me
Notice: Undefined index: L_code in /home/lab_users/web_tue25/web_pages/2014-12-09/stats03-04e.php on line 70
and it show the same for all the variables A/A etc.
Can anyone help me find my mistake(s)???
Thank you
<?php
include_once "dbconnect.php";
$sql = "SELECT DISTINCT akexam = '2003-04.e' FROM $table ";
$result = $dblink->query($sql);
if ($result->num_rows !== 0)
{
echo '<table border="1" style="width:100%; border: 1px solid black; border-collapse:collapse; text-align: center;">
<tr bgcolor= #787878>
<th>A/A</th>
<th>ak.εξαμ.</th>
<th>L_code</th>
<th>τ.ε</th>
<th>ck</th>
<th>rb</th>
<th>L_descr</th>
<th>ΔΗΛ</th>
<th>ΣΥΜ</th>
<th>a</th>
<th>b</th>
<th>c</th>
<th>d</th>
<th>e</th>
<th>f</th>
<th>ΣΥΝ</th>
<th>ok</th>
<th>ok%</th>
</tr>';
while($row = $result->fetch_assoc())
{
echo '<tr bgcolor= purple>';
if(strstr($row["L_code"],"241"))
{
echo "<td style= text-align:left>".$row["A/A"]."</td>";
echo "<td style= text-align:left>".$row["akexam"]."</td>";
echo "<td style= text-align:left>".$row["L_code"]."</td>";
echo "<td>".$row["te"]."</td>";
echo "<td> <input type='checkbox' name= 'ch' value='1'></td>";
echo "<td> <input type='radio' name='labs' value='1'></td>";
echo "<td>".$row["L_descr"]."</td>";
echo "<td style= text-align:right>".$row["dhl"]."</td>";
echo "<td style= text-align:right>".$row["sym"]."</td>";
echo "<td style= text-align:right>".$row["a"]."</td>";
echo "<td style= text-align:right>".$row["b"]."</td>";
echo "<td style= color:blue>".$row["c"]."</td>";
echo "<td style= color:blue>".$row["d"]."</td>";
echo "<td style= color:blue>".$row["e"]."</td>";
echo "<td style= color:blue>".$row["f"]."</td>";
echo "<td style= text-align:right>".$row["syn"]."</td>";
echo "<td style= text-align:right>".$row["ok"]."</td>";
echo "<td style= text-align:right>".$row["ok%"].'%';"</td>";
}
else
{
echo '<tr bgcolor= baby blue>';
echo "<td style= text-align:left>".$row["A/A"]."</td>";
echo "<td style= text-align:left>".$row["akexam"]."</td>";
echo "<td style= text-align:left>".$row["L_code"]."</td>";
echo "<td>".$row["te"]."</td>";
echo "<td> <input type='checkbox' name= 'ch' value='1'></td>";
echo "<td> <input type='radio' name='theories' value='1'></td>";
echo "<td>".$row["L_descr"]."</td>";
echo "<td style= text-align:right>".$row["dhl"]."</td>";
echo "<td style= text-align:right>".$row["sym"]."</td>";
echo "<td style= text-align:right>".$row["a"]."</td>";
echo "<td style= text-align:right>".$row["b"]."</td>";
echo "<td style= color:blue>".$row["c"]."</td>";
echo "<td style= color:blue>".$row["d"]."</td>";
echo "<td style= color:blue>".$row["e"]."</td>";
echo "<td style= color:blue>".$row["f"]."</td>";
echo "<td style= text-align:right>".$row["syn"]."</td>";
echo "<td style= text-align:right>".$row["ok"]."</td>";
echo "<td style= text-align:right>".$row["ok%"].'%';"</td>";
}
echo '</tr>';
}
echo '</table>';
}
else
{
echo "0 results";
}
Yuo have to change the query.
if you select distinct akexam, you will found a list of different akexam values in the table. Your rows will be just $row["akexam"]
If you select distinct * you will select all the different rows in the table.
It will look like a normal select but it will discard the rows that are identical (if you have a primary key in the table all the rows are different, i warn you).
So please provide an explanation of why you want to select distinct and i can give you more precise solution.
You have to select each column individually.
SELECT DISTINCT `akexam`, `L_code`, .... /*Fetch other columns*/
FROM `table`
WHERE akexam = '2003-04.e'

How to Show Popup window on clicking dynamically generated link

T have one page on which results are get created dynamically. And I
want to display popup window on clicking image named view.jpg. The
image is hyperlink. So please tell me how should i do it. I am showing
my code as follows.
code :
echo "<table width='' height='' border='1' align='center'>
<tr><td>Title</td>
<td >Type</td>
<td >Date</td>
<td>Expiry Date</td>";
if($typeofuser=='admin' || $typeofuser=='Accountant' || $typeofuser=='secretary')
{
echo "<td>View</td>
<td>Edit</td>
<td>Delete</td>";
}
echo "</tr>";
$qry2 = mysqli_query($con,"SELECT nId,nTitle,nDescription,nDate,nExpiryDate FROM tblnoticemanager where Society_id = '$socId' and category='General'") or die(mysqli_error($con));
while($NoticeData = mysqli_fetch_array($qry2))
{
echo "<tr>";
echo "<td align='center' class='tdletter' style='text-transform:capitalize;'>" .$NoticeData['nTitle']. "</td>";
echo "<td align='center' class='tdletter' ><div class='overflowDiv'>" . $NoticeData['nDescription']."</div></td>";
echo "<td align='center' class='tdletter'>" . $NoticeData['nDate'] ."</td>";
echo "<td align='center' class='tdletter'>" . $NoticeData['nExpiryDate'] ."</td>";
if($typeofuser=='admin' || $typeofuser=='Accountant' || $typeofuser=='secretary')
{
echo "<td align='center' class='tdletter'><div><a href='?id=".urlencode(base64_encode($NoticeData['nId']))."'><img src='images/view.png' width='30' height='30' align='center' /></a></div></td>";
echo "<td align='center' class='tdletter'><div><a href='?id=".urlencode(base64_encode($NoticeData['nId']))."' ><img src='images/edit.jpg' width='30' height='30' align='center'/></a></div></td>";
echo "<td align='center' class='tdletter'><span id='".$NoticeData['nId']."' class='trash'><img src='images/delete1.jpg' width='30' height='30' align='center' /></span></td>";
}
echo "</tr>";
}
echo "</table>";
You can use attribute selector [attribute='value']
$("[src='images/view.png']").click(function(){
alert("clicked");
});

table header repeating in xml data table

Im pulling some data from an xml on a separate site and trying to display that data in a table, i can pull the data but when i tried to display the data in a table for each row i got the table headers, now i know why this is happening (because the table header echo is being called for every row) but i cant see how to fix it.
<?php
$url = "http://elcu.herobo.com/testarea/include/cd_catalog.xml";
$xml = simplexml_load_file($url);
foreach($xml->CD as $cd){
echo "<table border='0' cellpadding='1' cellspacing='1' width'90%' id='1' class='tablesorter'><thead><tr> <th>Title</th> <th>Artist</th> <th>Company</th><th>Price</th></thead><tbody>";
echo "<td width='25%'> ".$cd->TITLE."</td>";
echo "<td width='25%'> ".$cd->ARTIST."</td>";
echo "<td width='25%'> ".$cd->COMPANY."</td>";
echo "<td width='25%'> ".$cd->PRICE."</td>";
echo "</tbody></table>";
}
?>
You mean:
<?php
$url = "http://elcu.herobo.com/testarea/include/cd_catalog.xml";
$xml = simplexml_load_file($url);
echo "<table border='0' cellpadding='1' cellspacing='1' width'90%' id='1' class='tablesorter'>\n";
echo "<thead><tr> <th>Title</th> <th>Artist</th> <th>Company</th><th>Price</th></thead>";
echo "<tbody>";
foreach($xml->CD as $cd){
echo "<tr>";
echo "<td width='25%'> ".$cd->TITLE."</td>";
echo "<td width='25%'> ".$cd->ARTIST."</td>";
echo "<td width='25%'> ".$cd->COMPANY."</td>";
echo "<td width='25%'> ".$cd->PRICE."</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
?>
Note that you called table inside your foreach loop, and you also didn't specify the start and end of the row with tr
i think the table will come before the foreach loop so that each record will create a row rather than a table
echo "<table border='0' cellpadding='1' cellspacing='1' width'90%' id='1' class='tablesorter'><thead><tr> <th>Title</th> <th>Artist</th> <th>Company</th><th>Price</th></thead><tbody>";
foreach($xml->CD as $cd)
{
echo "<td width='25%'> ".$cd->TITLE."</td>";
echo "<td width='25%'> ".$cd->ARTIST."</td>";
echo "<td width='25%'> ".$cd->COMPANY."</td>";
echo "<td width='25%'> ".$cd->PRICE."</td>";
}
echo "</tbody></table>";

Categories