While loop in php stops content - php

The issue im having is when running a while loop inside of a while loop in php the html content after the second while loop is not being rendered
for a live demonstration http://naturalflame.altervista.org/home.php
I originally didnt use a continue after the second loop but then none of the other rows populate because it just stops after the second loop after adding the continue after the second loop the other rows populate but the description and action field are left out
<div id="main">
<h1>Product List</h1>
<table>
<tr>
<th>Size</th>
<th>Price</th>
<th>Flavor Selection</th>
<th>Flavor Description</th>
<th>Blend Selection</th>
<th>Action</th>
</tr>
<?
$sql="SELECT * FROM products ORDER BY price ASC";
$query=mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_object($query)){
$id = $row->id;
$size = $row->size;
$price = $row->price;
?>
<tr>
<td><? echo $size; ?></td>
<td>$<? echo $price; ?></td>
<td>
<select onchange="ChangeDescription(<? echo $id; ?>)" id="dSelection<? echo $id; ?>">
<option value="Select a flavor">Select a flavor</option>
<?
$sql2 = "SELECT * FROM flavors ORDER BY id ASC";
$query2 = mysql_query($sql2) or die(mysql_error());
while($row2 = mysql_fetch_object($query2)){
$name = $row2->name;
echo "
<option value='$name'>$name</option>
";
}
?>
</select>
</td>
<td id="description<? echo $id; ?>">Flavor Description</td>
<td>Add to cart</td>
</tr>
<?}?>
</table>
</div><!--end main-->

You seem to be using an object as an array causing an error on your page.
This code: $row['id'] is probably just supposed to be $id
If you hadn't of already put it in a placeholder it would be $row->id

Related

How to fix a table causing "502: Bad Gateway" error in PHP

I am creating a simple database that can do basic CRUD operations (create, read, update, delete) using php. I am able to complete the create, and able to see the results if I directly query the mySQL DB in the back end. But, I am having trouble getting the table to display on the webpage. It is instead displaying a "Bad Gateway" error if I attempt to display the database entries.
I tried removing the reference to the table, specifically
<?php while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row['name']; ?></td>...
and the web page on front end works fine. Albeit can only see the data if I query the backend.
<?php include('php_code.php'); ?>
...
...
...
<?php $results = mysqli_query($db, "SELECT * FROM info"); ?>
<table>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th colspan="2">Action</th>
</tr>
</thead>
<?php while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['address']; ?></td>
<td><?php echo $row['city']; ?></td>
<td>
<a href="test.php?edit=<?php echo $row['id']; ?>"
class="edit_btn" >Edit</a>
</td>
<td>
<a href="php_code.php?del=<?php echo $row['id']; ?>"
class="del_btn">Delete</a>
</td>
</tr>
<?php } ?>
</table>
<!--in php_code.php-->
//to retrieve records
$select_query = "SELECT * FROM info";
$result = mysqli_query($db, $select_query);
I should be able to see the table with data containing name, address and city. But I am getting a 502 error instead.
Try this
<?php
include('php_code.php');
$results = mysqli_query($db, "SELECT * FROM `info` ");
$return = <<<HTML
<table>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th colspan="2">Action</th>
</tr>
</thead>
<tbody>
HTML;
while ($row = mysqli_fetch_array($results)) {
$return .= <<<HTML
<tr>
<td>{$row['name']}</td>
<td>{$row['address']}</td>
<td>{$row['city']}</td>
<td><a href="test.php?edit={$row['id']}" class="edit_btn" >Edit</a></td>
<td>Delete</td>
</tr>
HTML;
}
$return .= <<<HTML
</tbody>
</table>
HTML;
echo $return;
?>
your php_code.php should really only have the database config...
you are closing the php statements so you cannot retrieve the result of your query. Don't split php parts and just echo html like this
<?php
echo " <table>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th colspan='2'>Action</th>
</tr>
</thead> ";
while ($row = mysqli_fetch_array($results)) {
echo "<tr>
<td>$row['name']</td>
<td>$row['address']</td>
<td> $row['city']</td>
<td>
<a href='test.php?edit=$row['id']'
class='edit_btn' >Edit</a>
</td>
<td>
<a href='php_code.php?del=$row['id']'
class='del_btn'>Delete</a>
</td>
</tr>";
}
echo "</table>";
?>

How can I make the data that is displayed on the table change with value chosen in a select box?

Based on the user that is chosen within the combo box, I want the table that is displaying user data from the database to only show the data corresponding to the user selected in the combo box.
I mainly tried using an array to store values but I couldn't get that working.
Combo Box that displays the name to pick
<select>
<?php
$res = mysqli_query($db, "SELECT * FROM shifts");
while ($row = mysqli_fetch_array($res))
{
?>
<option><?php echo $row ["name"]; ?></option>
<?php
}
?>
<button class="btn-primary rounded">Find</button>
</select>
</form>
Table that shows the data from the database.
<table class="table table-hover">
<thead class="thead-dark"></thead>
<tr>
<th scope="col">Shift ID</th>
<th scope="col">Name</th>
<th scope="col">Origin</th>
<th scope="col">Destination</th>
<th scope="col">Date</th>
</tr>
</thead>
<?php
global $result;
//Fetch Data form database
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
?>
<tbody>
<tr>
<td><?php echo $row['shift_id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['origin']; ?></td>
<td><?php echo $row['destination']; ?></td>
<td><?php echo $row['date']; ?></td>
</tr>
</tbody>
</table>
I'm wondering if by using the form and doing a function that on pressing the Find button it looks up the user and displays only it's data. Thanks
Check my code here, there are some thing you must add, like a WHERE statement to your query, when fetching data to only show results with the selected name in the form
<!-- Create a form with a select for all the names -->
<form method="POST" action="">
<select name="name">
<?php
$res = mysqli_query($db, "SELECT * FROM shifts");
while ($row = mysqli_fetch_array($res))
{
?>
<option><?php echo $row ["name"]; ?></option>
<?php
}
?>
<button class="btn-primary rounded" name="find_info">Find</button>
</select>
</form>
<?php
if(isset($_POST['find_info'])){ //If find button is pressed, show this:
?>
<table class="table table-hover">
<thead class="thead-dark"></thead>
<tr>
<th scope="col">Shift ID</th>
<th scope="col">Name</th>
<th scope="col">Origin</th>
<th scope="col">Destination</th>
<th scope="col">Date</th>
</tr>
</thead>
<?php
global $result;
$nameSelected = strip_tags(mysqli_real_escape_string(htmlspecialchars($_POST['name'])));
// Use WHERE in your mysqli query to fetch data where name in database is equal to $nameSelected
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
?>
<tbody>
<tr>
<td><?php echo $row['shift_id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['origin']; ?></td>
<td><?php echo $row['destination']; ?></td>
<td><?php echo $row['date']; ?></td>
</tr>
</tbody>
</table>
<?php
}
?>

PHP and Mysql while loop

I have a mysql table with orders. I am trying to loop through the orders table and select individual client orders according to their user id and display the orders on their client accounts. However, my code below just prints the first row and repeats it endlessly jaming my browser every time.
what is wrong with this and how to i solve it
<?php
$records = $conn->prepare('SELECT * FROM orders WHERE user_id= :id');
$records-> bindParam(':id', $_SESSION['user_id']);
$records->execute();
$results=$records->fetch(PDO::FETCH_ASSOC);
$i=0;
while($i<=$results):
$i++;
?>
<h3>Your Orders</h3>
<table >
<tr >
<th>Order Number</th><th>Academic Level</th><th>Order Details</th>Manage Order</th>
</tr>
<tr>
<td>#SJ<?=$results['id']; ?> </td><td><?=$results['academic_level']; ?></td><td ><?=$results['details']; ?></td>
</tr>
</table>
<?php
endwhile;
?>
Remove all $i related code. Just move your fetch statement to while condition, like the following:
while( $results=$records->fetch(PDO::FETCH_ASSOC))
You need to include the html code in the while loop because you want to display all of the orders.
I'm using this method, try this out.
Start while in first php section
<?php
$username = $_SESSION['username'];
$sql = $db->prepare("SELECT * FROM tableName WHERE username = '$username' ");
$sql->execute();
while($results=$sql->fetch(PDO::FETCH_ASSOC)){
?>
After that the html section coming:
<h3>Your Orders</h3>
<table >
<tr >
<th>Order Number</th><th>Academic Level</th><th>Order Details</th>Manage Order</th>
</tr>
<tr>
<td><?php echo $results['id']; ?> </td><td><?php echo $results['academic_level']; ?></td><td ><?php echo $results['details']; ?></td>
</tr>
</table>
and here the ending
<?php
}
?>

Cannot generate the report

I want to generate the report that click and choose from the category drop down list, then it will show that the order which product in the category that you select and how many of them sold. But it cannot come out, I have been find many times the errors, and I also tried the query in SQL as it showed correct. I don't know why. Please help me.
<?php
session_start();
include_once 'header_admin.php';
?>
<div class="report">
<h2>Sales Report</h2>
<div class="sales_report">
<form action="" method="POST">
<select name="category" class="category">
<option>Category</option>
<?php
include_once 'includes/dbh.php';
$query1 = "SELECT * from category";
$result1 = mysqli_query($conn, $query1);
while ($row1 = mysqli_fetch_assoc($result1)) {
$CategoryID = $row1['CategoryID'];
$rowData1 = $row1['CategoryName'];
?>
<option value="<?php echo $CategoryID; ?>"><?php echo $rowData1; ?></option>
<?php
}
?>
</select>
<button class="generate" type="submit" name="generate">Generate</button>
<table class="genreport">
<tr>
<th>Furniture ID</th>
<th>Furniture Name</th>
<th>Category Name</th>
<th>Order Date</th>
<th>Quantity</th>
<th>Total Amount</th>
</tr>
<?php
if (isset($_POST['generate'])) {
$catName = mysqli_real_escape_string($conn, $_POST['category']);
$query2 = "SELECT D.FurCode, F.FurName, C.CategoryName, O.OrderDate, D.quantity, COUNT(D.quantity * D.PriceEach)
FROM orderdetail D, furniture F, category C, ordertab O
WHERE C.CategoryName = '$catName' GROUP BY C.$catName ORDER BY C.$catName ";
$result2 = mysqli_query($conn, $query2);
if ($result2) {
while ($row2 = mysqli_fetch_array($result2)) {
$furCode = $row2['FurCode'];
$furName = $row2['FurName'];
$orderDate = $row2['OrderDate'];
$quantity = $row2['quantity'];
$priceEach = $row2['PriceEach'];
$total = $quantity * $priceEach;
?>
<tr>
<td><?php echo $furCode; ?></td>
<td><?php echo $furName; ?></td>
<td><?php echo $catName; ?></td>
<td><?php echo $orderDate; ?></td>
<td><?php echo $quantity; ?></td>
<td><?php echo $total; ?></td>
</tr>
<?php
}
}
} else {
echo '<tr><td>No Result!</td><td></td><td></td><td></td><td></td><td></td></tr>';
}
?>
</table>
</form>
</div>
</div>
</div>
</section>
</body>
</html>

PHP table data filtering

I want to filter data in the table use dropdown menu (e.g. filter by name so i can see all data with name 'Jane'). I don't want to move to another page (use ajax or anything else if can). Any idea what must i do ?
This is the dropdown menu and table code :
<!-- Dropdown menu -->
<div class="col-md-2">
<select class="form-control selectpicker">
<option value="">Name</option>
<?php
// print all name value from $administratorProvider
foreach($administratorProvider as $administrator){
?>
<option value="<?php $administrator->first_name ?>"><?php echo $administrator->first_name; ?></option>
<?php
}
?>
</select>
</div>
<table>
<!-- Table heading -->
<thead>
<tr>
<th class="center">No.</th>
<th>Name</th>
<th>Email</th>
<th>Join</th>
<th>Last Login</th>
</tr>
</thead>
<!-- Table body -->
<tbody>
<?php
$i=1;
foreach ($dataProvider as $data){
?>
<tr>
<div>
<td class="center"><?php echo $i; ?></td>
<td><?php echo $data->name; ?></td>
<td><?php echo $data->email; ?></td>
<td><?php echo $data->join; ?></td>
<td><?php echo $data->last_login; ?></td>
</div>
</tr>
<?php $i++; } ?>
</tbody>
<!-- // Table body END -->
</table>
Thanks for any advice.
Regards
You can use jQuery to achieve this effect quite easily. Make two files:
1) One that includes the table.
2) One that has the select tag that will reload the first file upon change of the <select> tag.
Let's call the first file select.php
<script>
// Load the div with the contents of the table.php file with no GET parameter
$('div').load('table.php');
$('select').change(function() {
var name = $(this).val();
var data = 'name='+ name;
// Make sure that the table's contents don't change if the first option tag
// is selected.
if(name != '') {
$('div').load('table.php', data);
}
});
</script>
<div class="col-md-2">
<select class="form-control selectpicker">
<option value="">Name</option>
<?php
// print all name value from $administratorProvider
foreach($administratorProvider as $administrator){
?>
<option value="<?php $administrator->first_name ?>"><?php echo $administrator->first_name; ?></option>
<?php
}
?>
</select>
</div>
File two can be called table.php
$sql = "SELECT * FROM table WHERE name = '".$name."'";
$query = mysql_query($sql)or die(mysql_error());
$num = mysql_num_rows($query);
$i = 0;
while($row = mysql_fetch_array($query)) {
// Save your info as variables
$name[$i] = $row['name'];
$email[$i] = $row['email'];
$join[$i] = $row['join'];
$login[$i] = $row['last_login'];
}
?>
<div>
<table>
<thead>
<tr>
<th class="center">No.</th>
<th>Name</th>
<th>Email</th>
<th>Join</th>
<th>Last Login</th>
</tr>
</thead>
<tbody>
<?php
for($i=0;$i<$num;$i++) {
?>
<tr>
<div>
<td class="center"><?php echo $i; ?></td>
<td><?php echo $name[$i]; ?></td>
<td><?php echo $email[$i]; ?></td>
<td><?php echo $join[$i]; ?></td>
<td><?php echo $login[$i]; ?></td>
</div>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
Upon change of the select tag, the second file will be loaded with the value of the selected option tag sent to that file as a GET variable. You might wanna take the SQL part with a grain of salt as I'm not sure how you're fetching your relevant data.

Categories