Hello my first question here, I have a bad of trouble getting this code to work or better said how to proceed.
I have 2 tables, table 1 holds a string separated by commas, after exploding the array I want to compare the strings to another table that holds product prices related to the string.
<?php
function packages()
{
global $db;
$query = "SELECT * FROM product_package WHERE package_id > 1; ";
$result = mysqli_query($db, $query) or die('<h3>Query failed</h3>');
$rowcount = mysqli_num_rows($result);
if ($rowcount < 1)
{
return;
}
else
{
while ($row = mysqli_fetch_array($result))
{
$singles = explode(',', $row["product_ids"]);
$query2 = "SELECT product_price FROM products WHERE product_id = $single; ";
$result2 = mysqli_query($db, $query2);
?>
<tr>
<td><?php echo $row["package_id"] ?></td>
<td><?php echo $row["package_name"] ?></td>
<td><?php echo $row["product_ids"] ?></td>
<td>
EDIT
</td>
<td>
<?php
foreach($singles as $single)
{
echo $single . '<br />';
}
?>
</td>
</tr>
<?php
}
}
}
?>
How do I echo the prices that are overlapping with $single?
Related
I inserted the data in the table very well and it's showing in phpMyadmin but when i try to display data from the database only one single item is displayed. I need help.
Code and screenshoots below
<?php
$sql = "SELECT * FROM nw";
$result = mysqli_query($conn, $sql);
if($result == TRUE){
$count = mysqli_num_rows($result);
if($count > 0){
while($row = mysqli_fetch_assoc($result)){
$incidentId = $row['id'];
$icTypeName = $row['inctype_name'];
$addedBy = $row['added_by'];
$addedOn = $row['added_on'];
}
?>
<tr class="tableData">
<td><?php echo $incidentId;?></td>
<td><?php echo $icTypeName;?></td>
<td><?php echo $addedBy;?></td>
<td><?php echo $addedOn;?></td>
<td><i class="uil uil-edit icon editIcon"></i></td>
</tr>
<?php
} else {
$_SESSION['noData'] = 'No incidents to show!';
}
} else {
die('Failed to Connect!');
}
?>
Images below
I expect to get all the data in the database tables displayed with the "SELECT * FROM nw"
Simply move the } that ends the while loop to after the code that is using the variables. You are currently consuming all the resultset before outputting anything, so you will only see the last rows data.
<?php
$sql = "SELECT * FROM nw";
$result = mysqli_query($conn, $sql);
if($result == TRUE){
$count = mysqli_num_rows($result);
if($count > 0){
while($row = mysqli_fetch_assoc($result)){
$incidentId = $row['id'];
$icTypeName = $row['inctype_name'];
$addedBy = $row['added_by'];
$addedOn = $row['added_on'];
//} REMOVED
?>
<tr class="tableData">
<td><?php echo $incidentId;?></td>
<td><?php echo $icTypeName;?></td>
<td><?php echo $addedBy;?></td>
<td><?php echo $addedOn;?></td>
<td><i class="uil uil-edit icon editIcon"></i></td>
</tr>
<?php
} // END OF WHILE LOOP
} else {
$_SESSION['noData'] = 'No incidents to show!';
}
} else {
die('Failed to Connect!');
}
?>
I want to show multiple records from database upon some criteria in to html table, I only get 1 record, but I have 2 records in database which meet the criteria.
Please help to display all records from database which meets the criteria.
<?php
$count = 1;
$query = "SELECT * FROM career group by pno";
$result = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($result)) {
$pno = $row['pno'];
$query = "SELECT sum(duration) as total_duration FROM career where pno = $pno";
$result = mysqli_query($connection, $query);
$Value = $row = mysqli_fetch_assoc($result);
$total_value = implode(",", $Value);
if ($total_value < 54) {
continue;
}
$rank_query = "SELECT name,pno,rank,medical_category,staff_course_isc FROM user_general_info where pno = $pno and rank in ('manager','staff') and medical_category = 'Aye' and staff_course_isc = 'Yes'";
echo $rank_query;
$result = mysqli_query($connection, $rank_query);
$row = mysqli_fetch_assoc($result);
$pno = $row['pno'];
$name = $row['name'];
$rank = $row['rank'];
// $total_value= implode(",", $Value['name']);
// echo $total_value; exit;
?>
<tr>
<td><?php echo $count; ?></td>
<td><?php echo strtoupper($pno); ?></td>
<td><?php echo strtoupper($name); ?></td>
<td><?php echo strtoupper($rank); ?></td>
</tr>
<?php $count++;
} ?>
Here is my database rows
Record 1 : Pno: 2222, name: test1, rank : manger , medical_category: Aye : duration : 59, staff_course_isc: Yes
Record 2 : Pno: 4301234, name:test2, rank : staff , medical_category: Aye : duration : 122, staff_course_isc: Yes
You should name your variables differently:
<?php
$count = 1;
$query = "SELECT * FROM career group by pno";
$result1 = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($result1))
{
$pno = $row['pno'];
$query = "SELECT sum(duration) as total_duration FROM career where pno = $pno";
$result2 = mysqli_query($connection, $query);
$Value = $row = mysqli_fetch_assoc($result2);
$total_value = implode(",", $Value);
if ($total_value < 54) continue;
$rank_query = "SELECT name,pno,rank,medical_category,staff_course_isc FROM user_general_info where pno = $pno and rank in ('manager','staff') and medical_category = 'Aye' and staff_course_isc = 'Yes'";
echo $rank_query;
$result3 = mysqli_query($connection, $rank_query);
$row = mysqli_fetch_assoc($result3);
$pno = $row['pno'];
$name = $row['name'];
$rank = $row['rank'];
// $total_value= implode(",", $Value['name']);
// echo $total_value; exit;
?>
<tr>
<td><?php echo $count; ?></td>
<td><?php echo strtoupper($pno); ?></td>
<td><?php echo strtoupper($name); ?></td>
<td><?php echo strtoupper($rank); ?></td>
</tr>
<?php $count++;
} ?>
I don't know why it doesn't show up anything, I already tested my query and it is working in my phpmyadmin, but in my php code it does not work upon adding the AS keyword. My goal for this is to place a value to a variable coming from the SUM() keyword.
<?php
require_once "user-connect.php";
$user = $_SESSION['id'];
$sql = "SELECT SUM(total) AS sumz FROM cart WHERE userID = $user AND month(orderDate) = month(now()) AND day(orderDate) = day(now())";
$result = $link->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
echo $row['sumz'];
}
if (mysqli_query($link, $sql)) {
} else {
echo "Error: " . $sql . "" . mysqli_error($link);
} ?>
<table cellspacing="0">
<tbody>
<tr class="cart-subtotal">
<th>Cart Subtotal</th>
<td><span class="amount"><?php echo $row['sumz']; ?></span></td>
</tr>
You can use:
$sql = "SELECT SUM(total) FROM cart WHERE..."
And in HTML:
<td><span class="amount"><?php echo $row['SUM(total)']; ?></span></td>
Take a look at PHP fetch assoc guide.
while ($row = $result->fetch_assoc()) {
echo $row['sumz'];
}
An example of getting the SUM value
<?php
$sql="SELECT sum(amount) as total FROM table";
$result = mysqli_query($sql);
while ($row = mysqli_fetch_assoc($result)) {
echo $row['total'];
}
mysqli_close($con);
?>
I'm making a page which needs to display all the rows stored in a table named 'events' in a database named 'school'
The problem is that even though I've multiple entries/rows in the table, for some reason only a single row is displayed when I run the page.
Here's my code-
<?php
require("includes/common.php");
$query = "SELECT * FROM school.events";
$result = mysqli_query($con, $query)or die(mysqli_error($con));
$row = mysqli_fetch_array($result);
$ename = $row['name'];
$place = $row['place'];
$date = $row['date'];
?>
.
.
.
<?php
while ($row) {
?>
<tr>
<td><?php echo $date; ?></td>
<td><?php echo $ename; ?></td>
<td><?php echo $place; ?></td>
</tr>
<?php
$row = mysqli_fetch_array($result);
$name = $row['name'];
$place = $row['place'];
$date = $row['date'];
}
?>
Try instead while($row = mysqli_fetch_assoc($result)){ ... } , you're just assigning the array to $row, you can't loop an array saying while($array)
Do it this way:
while($row = mysqli_fetch_assoc($result)){
echo $row['id'];
echo $row['name'];
}
That is because $row = mysqli_fetch_array($result); will only pull one row and then move the pointer. In order to get all the rows, use a while loop
while($row = mysqli_fetch_array($result)) {
//code to populate table one row at a time
}
I have the following code which echoes out pupil info from a database automatically. However when the user searches for a specific pupil I need to repopulate the same table with relevant pupils to the search request.
All help is greatly appreciated!
if(!isset ($_POST['search'])){
$pupils = mysql_query("SELECT * FROM pupil") or die("Cant find Pupils");
$count = mysql_num_rows($pupils);
if ($count == 0 ) {
$totalpupil = "There are currently no Pupils in the system.";
} else{
while($row = mysql_fetch_array($pupils)){
?>
<tr>
<td><?php echo ''.$row['pupil_id'].''; ?></td>
<td><?php echo $row['pupil_name']?></td>
<td><?php echo $row['class_id']?></td>
<td>Edit | Delete</td>
</tr>
<?php
}
}
}
Something like this ?
if(!isset ($_POST['search'])){
$pupils = mysql_query("SELECT * FROM pupil") or die("Cant find Pupils");
}
elseif(isset($_POST['search'])){
$search_param = "%".$_POST['search']."%";
$pupils = mysql_query("SELECT * FROM pupil WHERE LOWER(pupil_name) LIKE LOWER($search_param)") or die("Cant find Pupils");
}
$count = mysql_num_rows($pupils);
if($count == 0){
$totalpupil = "There are currently no Pupils in the system.";
}
else{
while($row = mysql_fetch_array($pupils)){
?>
<tr>
<td><?php echo ''.$row['pupil_id'].''; ?></td>
<td><?php echo $row['pupil_name']?></td>
<td><?php echo $row['class_id']?></td>
<td>Edit | Delete</td>
</tr>
<?php
}
I'm assuming that you are searching in the pupil_name column.