HTML not showing after PHP Function - php

I have a page that will not show the bottom half of my HTML after my PHP Code. The only way I could get it to show was to place a RETURN at the end of the WHILE Loop, which also ended the loop. I know it is probably something simple and just need to find out what it is. Thanks in advance for your help.
The HTML:
<table border='0'>
<thead>
<tr>
<th scope="cols">Select</th>
<th scope="cols">Name</th>
<th scope="cols">Units</th>
<th scope="cols">Amounts</th>
<th scope="cols">Calories</th>
<th scope="cols">Sugars</th>
</tr>
</thead>
<tbody>
<?php
//Establishs the DB connection
$sql = new Mysql();
//Queries the Foods
$sql->food();
?> <!--NOTHING SHOWS FROM HERE DOWN-->
</tbody>
</table>
<h2>Training Plan</h2>
<table id="dairy">
<thead>
<tr>
<th scope="cols">Select</th>
<th scope="cols">Name</th>
<th scope="cols">Units</th>
<th scope="cols">Amounts</th>
<th scope="cols">Calories</th>
<th scope="cols">Sugars</th>
</tr>
...more HTML....
The PHP Function:
function food() {
//Query's the DB
$result = $this->conn->query("SELECT * FROM ingredient") or die(mysql_error());
//Display's all of the Contents in the DB
while ($row = $result->fetch_assoc() or die(mysql_error()))
{
echo'<tr class="normal" id="row'.$row['id'].'" onclick="onRow(this.id);">'."\n\t\t\t\t\t\t\t".'<td><input type="checkbox" value="'.$row['id'].'" id="checkbox" /></td>'."\n\t\t\t\t\t\t\t";
echo'<td>'.$row['Name'].'</td>'."\n\t\t\t\t\t\t\t";
echo'<td>'.$row['Units'].'</td>'."\n\t\t\t\t\t\t\t";
echo'<td>'.$row['Amount'].'</td>'."\n\t\t\t\t\t\t\t";
echo'<td>'.$row['Calories'].'</td>'."\n\t\t\t\t\t\t\t";
echo'<td>'.$row['Sugar'].'</td>'."\n\t\t\t\t\t\t";
echo'</tr>'."\n\t\t\t\t\t\t";
}
}

while ($row = $result->fetch_assoc() or die(mysql_error()))
The problem is in this line, if $result->fetch_assoc() returns falsy when there are no more rows(which I suspect it does) you script will be stopped. Leave out the die part
while ($row = $result->fetch_assoc())

It's not showing because you have an error. Is food a member of the $sql class?

Related

Little problem with display data from local server in html table with php and sql

Today I have a little problem, with basic and maybe funny thing. Today I wanted make table with data from sql server. For that I use Xampp for that and maybe is verry common choose for that. The problem is with table because i tried to display datas with my ideas and with my knowledge and results is maybe more than what i want to do, just maybe.
Below is my scripts and I would like to mention that I make some versions of scripts and here it's just one of them.
HTML + PHP Table
<div class="centred-container table">
<table class="table-results">
<tr class="tr-table">
<th class="th-table th-univ">Product name</th>
<th class="th-table th-univ">Cod PLU</th>
<th class="th-table th-univ">Cod Intern</th>
<th class="th-table th-univ">Cod EAN/Bare</th>
<th class="th-table th-univ">Unitate</th>
<th class="th-table th-univ">Pret</th>
<th class="th-table th-univ">Stoc</th>
</tr>
<tr class="tr-table">
<?php
while ($row = mysqli_fetch_assoc($result)) {
$nameP = $row["ProductName"];
$PLUP = $row["ProductPLU"];
$ICP = $row["ProductIC"];
$EANP = $row["ProductEAN"];
$UnitP = $row["ProductUnit"];
$PriceP = $row["ProductPrice"];
$StockP = $row["ProductStock"];
echo"<td>".$nameP."</td>
<td>".$PLUP."</td>
<td>".$ICP."</td>
<td >".$EANP."</td>
<td >".$UnitP."</td>
<td >".$PriceP."</td>
<td >".$StockP."</td> <br>";
}
?>
</table>
</div>
PHP part
if (isset($_POST["submit"])) {
session_start();
include_once 'includes/dbh.inc.php';
$codePLU = $_POST["productPLU"];
$codeEAN = $_POST["productEAN"];
$codeIC = $_POST["productIC"];
$sql = "SELECT * FROM products WHERE ProductPLU ='$codePLU' OR ProductEAN = '$codeEAN' OR ProductIC = '$codeIC';";
$result = mysqli_query($conn, $sql);
}
?>
Here I want display (SELECT) data from database and and display that in html table. The problem is I tried and method with <td><?php echo $variable ?></td> where $varable = $row["column_name"] and in this situation all my data is just maybe first or the last or just one row where there should have been more because I Know my sql query have more results (rows) than one row I see.
With pasted scripts I have all my rows returned by sql query but all rows is on one line and I want to be all in a classic table.
I want just little help for my little problem. I Know is just basic problem in php site but i want some case from you, or results, where I want to try for make my table a classic table without errors same as my errors above.
Sorry for my Englesh.
The problem is with your HTML table. Need to open and close each row (<tr>) within the while loop. code below
<table class="table-results">
<tr class="tr-table">
<th class="th-table th-univ">Product name</th>
<th class="th-table th-univ">Cod PLU</th>
<th class="th-table th-univ">Cod Intern</th>
<th class="th-table th-univ">Cod EAN/Bare</th>
<th class="th-table th-univ">Unitate</th>
<th class="th-table th-univ">Pret</th>
<th class="th-table th-univ">Stoc</th>
</tr>
<?php
while ($row = mysqli_fetch_assoc($result)) {
$nameP = $row["ProductName"];
$PLUP = $row["ProductPLU"];
$ICP = $row["ProductIC"];
$EANP = $row["ProductEAN"];
$UnitP = $row["ProductUnit"];
$PriceP = $row["ProductPrice"];
$StockP = $row["ProductStock"];
echo"<tr> <td>".$nameP."</td>
<td>".$PLUP."</td>
<td>".$ICP."</td>
<td >".$EANP."</td>
<td >".$UnitP."</td>
<td >".$PriceP."</td>
<td >".$StockP."</td> </tr>";
}
?>
</table>

I am sorting data on database but webpage doesn't take effect

I tried to sort/order data on database. I used this command:
SELECT * FROM `estates`
ORDER BY `estates`.`price` ASC
It take effect on database order. But on webpage it doesn't. How can I make it take effect on webpage too? Any idea? Thank you.
By the way I am using Laravel,
and retriving data from database with MVC
If you need to check to my page structure here it is:
<table cellspacing='0'> <!-- cellspacing='0' is important, must stay -->
<thead>
<tr>
<th width="150px">会社名</th>
<th width="150px">物件名</th>
<th width="250px">住所</th>
<th width="150px">販売価格</th>
<th width="100px">総戸数</th>
<th width="150px">専有面積</th>
<th width="100px">間取り</th>
<th width="100px">バルコニー面積</th>
<th width="100px">竣工時期</th>
<th width="100px">入居時期</th>
</tr>
<thead>
<tbody>
#foreach($estates as $estate)
<tr class="even">
<td>{{$estate->company_name}}</td>
<td>{{$estate->name}}<br/></td>
<td>{{$estate->address}}</td>
<td>{{$estate->price}}</td>
<td>{{$estate->hows_old}}</td>
<td>{{$estate->extend}}</td>
<td>{{$estate->rooms}}</td>
<td>{{$estate->balcon_m2}}</td>
<td>{{$estate->old}}</td>
<td>{{$estate->entery}}</td>
</tr>
#endforeach
</tbody>
</table>
And here is the controller:
public function sumos()
{
$estates = Estates::get();
//test
$data['estates'] = $estates;
return view('welcome', $data);
}
try to use orderBy
Estates::orderBy('price')->get();

Php help to remove remove td line

Hello i just create my support system and i get ticket detalis with echo but it show all td line as picture i need to show only the first line
my code
<table cellpadding="0" cellspacing="0" border="0" class="data">
<thead>
<tr>
<th width="115"><p2>Ticket ID</p2></th>
<th width="90"><p2>Status</p2></th>
<th width="160"><p2>Ticket Topic</p2></th>
<th width="160"><p2>Client</p2></th>
</tr>
</thead>
<tr> <?php
echo "<td><a href='?page=ticket&id=".$row[id]."'</a>#".$row[id]."</td>";
echo "<td>".$row[status]."</td>";
echo "<td>".$row[naslov]."</td>";
$userr = mysql_query('SELECT * FROM client WHERE clientid='.$row["user_id"].'');
$useri = mysql_fetch_array($userr);
echo "<td><a href='clientsummary.php?id=".$row[user_id]."'</a>".$useri['firstname']." ".$useri['lastname']."</td>";
?>
</tr>
you need to move your table header outside the loop. without seeing the mechanics of the loop, I'm guessing it will need to look similar to this:
<table cellpadding="0" cellspacing="0" border="0" class="data">
<thead>
<tr>
<th width="115"><p2>Ticket ID</p2></th>
<th width="90"><p2>Status</p2></th>
<th width="160"><p2>Ticket Topic</p2></th>
<th width="160"><p2>Client</p2></th>
</tr>
</thead>
<tbody>
<?php include 'missing.query.on.other.script.php';
while ($row = mysql_fetch_array($whatever_resource)){
?>
<tr> <?php
echo "<td><a href='?page=ticket&id=".$row[id]."'</a>#".$row[id]."</td>";
echo "<td>".$row[status]."</td>";
echo "<td>".$row[naslov]."</td>";
$userr = mysql_query('SELECT * FROM client WHERE clientid='.$row["user_id"].'');
$useri = mysql_fetch_array($userr);
echo "<td><a href='clientsummary.php?id=".$row[user_id]."'</a>".$useri['firstname']." ".$useri['lastname']."</td>";
?>
</tr>
<?php } // end while ?>
</tbody>
</table>

while loop didnt work

Hi guys, I'm trying to do the while loop.
The while loop will loop through all the orders.
It works but somehow it doesn't while loop through the order status.
For example, order 4 status is pending, but order 5 status shows nothing.
I suspect I placed the brackets wrongly.
This is my code.
<form action="results-action" method="post" enctype="multipart/form-data">
<fieldset>
<table width="600" border="1">
<tr><td><h2>Pending Order</h2></td></tr>
<tr>
<th scope="col">Order ID</th>
<th scope="col">Name</th>
<th scope="col">Address</th>
<th scope="col">Product Name</th>
<th scope="col">Produt Quantity</th>
<th scope="col">Price</th>
<th scope="col">Order status</th>
</tr>
<?php
while ($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><input type="text" value='<?=$row['virtuemart_order_id']?>' name="orderid" id="virtuemart_order_id"></td>
<td><?=$row['first_name']?></td>
<td><?=$row['address_1']?></td>
<td><?=$row['order_item_name']?></td>
<td><?=$row['product_quantity']?></td>
<td><?=$row['product_final_price'] ?></td>
<td><select name="change">
<?php
while ($row2 = mysqli_fetch_array($result2)) {
?>
<option value="<?=$row2['order_status_code'] ?>"><?=$row2['order_status_name'] ?></option>
<?php
} //end inner while
?>
</td>
</tr>
<?php
} //end outer while
?>
</table>
</fieldset>
<fieldset>
<table>
<tr>
<td><input type="submit" value="Update status" name="update status"> </td>
</tr>
</table>
</fieldset>
</form>
If you want to get the same rows every time in the inner while you will have to reset the internal pointer of the mysqli result set. For this, you can use the mysqli_data_seek()
You will end up with something like this:
while ($row = mysqli_fetch_array($result)) {
// snip ...
while ($row2 = mysqli_fetch_array($result2)) {
// snip
}
mysqli_data_seek($result2, 0);
// snip
}
Please also ensure the <select> has unique name, otherwise all your status will be captured wrongly.
<select name="change_<?=$row['row_id']?>">
Something like that.

Tablesorter plugin not sorting my tables when data comes from database

I have the following code:
<table id="box-table-a" class="tablesorter">
<thead>
<tr>
<th scope="col">B-House/Dorm Name</th>
<th scope="col">Address</th>
<th scope="col">Price Range</th>
<th scope="col">Date Added</th>
<th scope="col">Status</th>
</tr>
</thead>
<?php
$q=mysql_query("select * from property");
while( $f=mysql_fetch_array($q, MYSQL_ASSOC))
{ $p_id=$f["p_id"];
echo"
<tbody>
<tr>
<td onblurr='hover2()' onmouseover='hover(".$p_id.")' onclick='showUser(".$p_id.")'>
<span style='cursor:pointer'>".$f['p_name']."</span></td>
<td id='pretty'>".$f['address']."</td>
<td>".$f['p_name']."</td> <td>".$f['payment_type']."</td> <td>".$status."</td> </tr>
</tbody>
";
}
?>
</table>
Any idea what may be wrong here?
Don't add <tbody></tbody> to every loop in the while! Tablesorter is very sensitive.
You did'nt sort your DB :
$q=mysql_query("select * from property ORDER BY p_name");

Categories