Extra empty row generating while displaying data through php - php

I don't know why this code generates an extra row in the last, it only happens when i use PHP to display my data. There's no extra row in table tho. The extra row shows empty data.
<div class="tbl-header">
<table cellpadding="0" cellspacing="0" border="0">
<thead>
<tr>
<th>ID</th>
<th>Product Name</th>
<th>Brand</th>
<th>Category Name</th>
<th>Description</th>
<th>Picture</th>
<th>Quantity</th>
<th>Price</th>
</tr>
<?php
$data = mysqli_query($con, "select `shopifyv2`.`products`.`ID` AS `ID`,`shopifyv2`.`products`.`ProductName` AS `ProductName`,`shopifyv2`.`brands`.`BrandName` AS `BrandName`,`shopifyv2`.`category`.`Name` AS `CategoryName`,`shopifyv2`.`products`.`Description` AS `Description`,`shopifyv2`.`products`.`Picture` AS `Picture`,`shopifyv2`.`products`.`Quantity` AS `Quantity`,`shopifyv2`.`products`.`Price` AS `Price` from `products` join `shopifyv2`.`brands` on `products`.`BrandID` = `brands`.`ID` join `shopifyv2`.`category` on `products`.`CateID` = `category`.`ID`");
$row = mysqli_num_rows($data);
for ($i = 0; $i < $row; $i++) {
$row = mysqli_fetch_array($data);
echo "<tr>";
echo "<td>$row[0]</td><td>$row[1]</td><td>$row[2]</td><td>$row[3]</td><td>$row[4]</td><td><img height='30' width='20' src='../AdminPanel/Pictures/$row[5]'/ /></td><td>$row[6]</td><td>$row[7]</td>";
echo "<td><a href='ModifyProduct.php?ID=$row[0]' class='btn btn-info'>Modify</a></td>";
echo "<td><a href='ShowProduct.php?id=$row[0]'>Delete</a></td>";
echo "</tr>";
}
if (isset($_GET['id'])) {
$delid = $_GET['id'];
mysqli_query($con, "delete from products where id = $delid");
header("location: ShowProduct.php");
}
?>
</thead>
</table>

You might check if the row is not empty:
$row = mysqli_num_rows($data);
for ($i = 0; $i < $row; $i++) {
$row = mysqli_fetch_array($data);
if(!$row[0]) continue; // <- HERE

Related

MySql Table with different colors by value

I am using mysql queries to fetch data from db. All my data are showing fine in tables. Now I want to color the status by value less than 2 or more than 3.
The below code is not working.
Need help.
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
$output .= '<div class="table-responsive">
<table class="table table bordered">
<tr>
<th>name</th>
<th>genre</th>
<th>time</th>
<th>status</th>
<th>more...</th>
</tr>';
?>
<?php
function status_style($row) {
if ($row < 2) return 'background-color: #ff0000'; //red
if ($row > 3) return 'background-color: #33cc33'; //green
return '';
}
?>
<?php
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td>'.$row["name"].'</td>
<td>'.$row["genre"].'</td>
<td>'.$row["time"].'</td>
<td>'.$row["status"].'</td>
<td>'.$row["more"].'</td>
</tr>
';
}
echo $output;
{
echo 'Data Not Found';
}
?>
You have already created function to color the rows but haven't called the function during showing output. That's why it is not working.
Change your $output variable with:
$output .= '
<tr>
<td>'.$row["name"].'</td>
<td>'.$row["genre"].'</td>
<td>'.$row["time"].'</td>
<td style="'.status_style($row["status"]).'">'.$row["status"].'</td>
<td>'.$row["more"].'</td>
</tr>
';
You can try something like this:
<?php
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
$output .= '<div class="table-responsive">
<table class="table table bordered">
<tr>
<th>name</th>
<th>genre</th>
<th>time</th>
<th>status</th>
<th>more...</th>
</tr>';
function status_style($row) {
if ($row < 2) return $color = '#ff0000'; //red
if ($row > 3) return $color = '#33cc33'; //green
return $color = '';
}
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr style='"'background-color: '"' . $color . '"'>
<td>'.$row["name"].'</td>
<td>'.$row["genre"].'</td>
<td>'.$row["time"].'</td>
<td>'.$row["status"].'</td>
<td>'.$row["more"].'</td>
</tr>
';
}
echo $output;
{
echo 'Data Not Found';
}
You didn't do anything with the background color.

Query data from multiple database tables into form table (MYSQL,PHP)

As I'm new to PHP, I want to know that how to put data from different database tables into one table form on the page.
My codes so far as below,
<?php
include('DBconnect.php');
mysql_query("USE onlinerecruitment");
$username =$_SESSION['user'];
$result = mysql_query("SELECT * FROM application_data_file");
$rows = mysql_fetch_array($result, MYSQL_ASSOC);
$pos_id = $rows['Position_ID'];
$resultt = mysql_query("SELECT * FROM position WHERE Position_ID = '".$pos_id."' ");
$resulttt = mysql_query("SELECT * FROM resume_data_file WHERE App_Email = '".$pos_id."' ");
?>
<TABLE border ='1'>
<table style="width:100%">
<tr>
<th>Application ID</th>
<th>Applicant E-mail</th>
<th>Position Selected</th>
<th></th>
<th></th>
<th></th>
</tr>
<?php
while ($row = mysql_fetch_array($result, MYSQL_ASSOC) & $rowss = mysql_fetch_array($resultt, MYSQL_ASSOC)){
echo "<TR>";
echo "<TD>".$row['App_Data_ID']."</TD>";
echo "<TD>".$row['App_Email']."</TD>";
echo "<TD>".$rowss['Position_Name']."</TD>";
echo "<TD><a href='view-app-form.php?app_mail=".$row['App_Email']."'>View Application Data</a></TD>";
echo "<TD><a href='view-resume-form.php?app_mail=".$row['App_Email']."'>View Resume Data</a></TD>";
echo "<TD><a href='view-test-score.php?app_mail=".$row['App_Email']."'>View Testing Score Data</a></TD>";
echo "</TR>";
}
?>
</table>
I will focus the part here.
<TABLE border ='1'>
<table style="width:100%">
<tr>
<th>Application ID</th>
<th>Applicant E-mail</th>
<th>Position Selected</th>
<th></th>
<th></th>
<th></th>
</tr>
<?php
while ($row = mysql_fetch_array($result, MYSQL_ASSOC) & $rowss = mysql_fetch_array($resultt, MYSQL_ASSOC)){
echo "<TR>";
echo "<TD>".$row['App_Data_ID']."</TD>";
echo "<TD>".$row['App_Email']."</TD>";
echo "<TD>".$rowss['Position_Name']."</TD>";
echo "<TD><a href='view-app-form.php?app_mail=".$row['App_Email']."'>View Application Data</a></TD>";
echo "<TD><a href='view-resume-form.php?app_mail=".$row['App_Email']."'>View Resume Data</a></TD>";
echo "<TD><a href='view-test-score.php?app_mail=".$row['App_Email']."'>View Testing Score Data</a></TD>";
echo "</TR>";
}
?>
</table>
But if there is any problem in the section that I didn't focused, I still appreciate your solution.
Thank you in advance.
To do this you would need to use a JOIN in the sql statement.
mysql_query("SELECT resume_data_file.App_Email, position.Position_ID FROM position INNER JOIN resume_data_file ON position.Position_ID = position.Position_ID WHERE position.Position_ID = '".$pos_id."' ");
http://www.w3schools.com/sql/sql_join.asp

how to total all row in page that was sql LIMIT

i put limit in this code but how can i calculate the totalmin in this page?
here is my code
<?php
$id = $_GET['id'];
$sql = "SELECT * FROM time WHERE id = $id ORDER BY id DESC LIMIT 15";
$result = mysql_query($sql);
?>
<table border="0" style="width:50%">
<tr>
<th>Time in</th>
<th>Time Out</th>
</tr>
<?php
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td><center>".$row['totalmin']."</center></td>";
echo "</tr>";
}
</table>
mysql_close();
i want to know how to total all the mins in 15 row with php code
Something like this may do the trick, if you want to show sum only
But you should NOT use mysql_ functions, and this code is not sequre from SQL Injects
$id = $_GET['id'];
$sql = "SELECT * FROM time WHERE id = $id ORDER BY id DESC LIMIT 15";
$result = mysql_query($sql);
$sum = 0;
$num = 0;
?>
<table border="0" style="width:50%">
<tr>
<th>Time in</th>
<th>Time Out</th>
</tr>
<?php
while($row = mysql_fetch_array($result)){
$num++;
$sum += $row['totalmin'];
echo "<tr>";
echo "<td><center>".$row['totalmin']."</center></td>";
echo "</tr>";
if($num == 15){
echo "<tr>";
echo "<td><center>".$sum."</center></td>";
echo "</tr>";
}
}
?>
</table>
<?php
mysql_close();
You need to store $totalmin into your while loop and update it with new value if $row['totalmin'] is less then $totalmin.
Use something like this:
$totalmin = null;
while($row = mysql_fetch_array($result)) {
if (is_null($totalmin) || $row['totalmin'] < $totalmin) {
$totalmin = $row['totalmin'];
}
echo "<tr>";
echo "<td><center>" . $totalmin . "</center></td>";
echo "</tr>";
}
Sum up the minutes as you loop through the result:
$sum = 0;
while($row = mysql_fetch_array($result)){
$sum += floatval($row['totalmin']); //or intval()
echo "<tr>";
echo "<td><center>".$row['totalmin']."</center></td>";
echo "</tr>";
}
echo $sum;
make sure you close your while loop

storing mysql query result in session in codeigniter

This is my code in controller...
function get_product(){
$purchase_id=$_POST['purchase_id'];
if($purchase_id!=''){
//$post_array['cart']='';
$res = $this->db->query("select * from phppos_productdetails WHERE
purchase_id='$purchase_id'");
?>
<tr>
<th>Product Name</th>
<th>Quantity </th>
<th>Unit </th>
<th>Unit Rate</th>
<th>Action</th>
</tr>
<?php
$i=0;
foreach($res->result() as $row )
{
$sess_products[$i]['product_id'] = $row->product_id;
$sess_products[$i]['quantity'] = $row->quantity;
$sess_products[$i]['unit'] = $row->unit;
$sess_products[$i]['unit_rate'] = $row->unit_rate;
$this->session->set_userdata('sess_products',$sess_products);
$query = $this->db->query("SELECT product_name FROM phppos_product WHERE
product_id='".$row->product_id."'");
foreach ($query->result() as $row1 )
{
$product_name=$row1->product_name;
}
echo "<tr>";
echo "<td>".$product_name."</td>";
echo "<td>".$row->quantity."</td>";
echo "<td>".$row->unit."</td>";
echo "<td>".$row->unit_rate."</td>";
echo "<td><a href='javascript:void(0)' rownum='".$i."' class='remove_from_cart'><img src='images/close.png'/></a></td>";
echo "</tr>";
$i++;
}
}
}
This is function in controller that is being called by ajax call. So how do I store that query result in session array in codeigniter??
I am acessing like this
$demo_arr['cart']= $this->session->userdata('sess_products');

DataTable plugin not working with dynamically generated php table (arrows wouldn't show up too)

I created a table from using php and added the plugin (used demo_table.css). The arrows wouldn't show on the result page and it doesn;t apply any css.
Here is my php code:
$result = mysqli_query($link, $sql);
$num_rows = mysqli_num_rows($result);
echo '<table cellpadding = "0" cellspacing="0" border="0" id="datatables" class="display">';
echo '<thead>';
echo '<tr>';
echo '<th>SNo.</th>
<th>Username</th>
<th>Trip ID</th>
<th>Destination</th>
<th >Leave Date</th>
<th>Return Date</th>
<th>Total</th>
<th>Submit Date</th>
<th >Status</th>
<th >Reason</th>
<th >Update</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
} else {
echo('not valid user');
}
if (!$result) {
die(mysqli_error($link));
}
if ($num_rows<=0){
echo('not valid user');
}
elseif ($num_rows>0){
$i=1;
while($row = mysqli_fetch_assoc($result)) {
$grandtotal = $row['flight_estimate'] + $row['registration_fee'] + $row['total_cost'];
$status = $row['status'];
if(($status == 'Approved') || ($status == 'Denied')){
$newcolumn = '<td></td>';
}
else{
$newcolumn = '<td>Approve/Deny</td>';
}
echo '<tr>';
echo '<td>'. $i++ .'</td><td>'.$row['username'].'</td><td>'.$row['trip_id'].'</td><td>'.$row['destination'].'</td><td>'.date('d/m/Y',strtotime($row['leave_date'])).'</td><td >'.date('d/m/Y',strtotime($row['return_date'])).'</td><td >'. round($grandtotal,2).'</td><td >'.date('d/m/Y',strtotime($row['updateddate'])).'</td><td >'.$row['status'].'</td><td >'.$row['reason'].'</td>'.$newcolumn;
}
echo '</tr>';
echo '</tbody>';
echo '</table>';
}
?>
Is there anything that I'm missing? All it does is center all the column values. But when I click on header nothing happens.
You need to set 'bJQueryUI' to true when you init dataTable
$("#datatables").dataTable({ 'bJQueryUI' : true });
It would help if you posted your js script as that is where your problem is.

Categories