mysqli fetch_assoc with conditional statement - php

I am trying to display multiple parts of a query on my page using fetch_assoc and some conditional statements. My first while loop is displaying the correct information, but loops 2 and 3 are not displaying any information. What would be the correct way to filter my results?
require_once('inc/connection.inc.php');
//create database connection
$conn = dbConnect();
//create SQL
$sql = 'SELECT * FROM menu';
$result = $conn->query($sql) or die(mysqli_error());
while($row = $result->fetch_assoc()){
if($row['category'] == 'appetizers'){
echo $row['title'];
echo $row['price'];
}
}
while($row = $result->fetch_assoc()){
if($row['category'] == 'salads'){
echo $row['title'];
echo $row['price'];
}
}
while($row = $result->fetch_assoc()){
if($row['category'] == 'desserts'){
echo $row['title'];
echo $row['price'];
}
}

Why not do it like this:
while($row = $result->fetch_assoc()){
if($row['category'] == 'appetizers'){
echo '<h1>Appetizers</h1>';
echo $row['title'];
echo $row['price'];
}
if($row['category'] == 'salads'){
echo '<h1>Salads</h1>';
echo $row['title'];
echo $row['price'];
}
if($row['category'] == 'desserts'){
echo '<h1>Desserts</h1>';
echo $row['title'];
echo $row['price'];
}
}
i.e., use the conditions in a single while loop.
OR
You can use mysqli_data_seek to reset the pointer to the first record and re-use the same resource multiple times
while($row = $result->fetch_assoc()){
if($row['category'] == 'appetizers'){
echo $row['title'];
echo $row['price'];
}
}
mysqli_data_seek($result,0);
while($row = $result->fetch_assoc()){
if($row['category'] == 'salads'){
echo $row['title'];
echo $row['price'];
}
}
mysqli_data_seek($result,0);
while($row = $result->fetch_assoc()){
if($row['category'] == 'desserts'){
echo $row['title'];
echo $row['price'];
}
}

you are running query result one time and fetching it three time so need to query result three time
$result = $conn->query($sql) or die(mysqli_error());
$result1 = $conn->query($sql) or die(mysqli_error());
$result2 = $conn->query($sql) or die(mysqli_error());
then loop
while($row = $result->fetch_assoc()){
while($row1 = $result1->fetch_assoc()){
while($row2 = $result2->fetch_assoc()){
Also you can use mutiple if() in loop like:-
while($row = $result->fetch_assoc()){
if($row['category'] == 'appetizers'){
echo $row['title'];
echo $row['price'];
}
if($row['category'] == 'salads'){
echo $row['title'];
echo $row['price'];
}
if($row['category'] == 'desserts'){
echo $row['title'];
echo $row['price'];
}
}

while($row = $result->fetch_assoc())
{
if($row['category'] == 'appetizers'){
echo $row['title'];
echo $row['price'];
}
if($row['category'] == 'salads'){
echo $row['title'];
echo $row['price'];
}
if($row['category'] == 'desserts'){
echo $row['title'];
echo $row['price'];
}
}

I think what you're after is something like this...
$categories = ['appetizers', 'salads', 'desserts'];
if (!$stmt = $conn->prepare('SELECT title, price FROM menu WHERE category = ?')) {
throw new Exception($con->error, $con->errno);
}
$stmt->bind_param('s', $category);
foreach ($categories as $category) {
if (!$stmt->execute()) {
throw new Exception($stmt->error, $stmt->errno);
}
$stmt->bind_result($title, $price);
while($stmt->fetch()) {
echo $title, $price;
}
}
What I've done is execute a prepared statement for each category in the $categories array. This will group your menu rows together under each category in the order you want.

Related

PHP: I have failed to select all data i inserted in the table although, some one help me

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!');
}
?>

How To Fix While Loop output results

I just want to get the results of while loop which is within a php in different lines
if($count == 0){
$output = 'THERE WAS NO RESULTS';
}
else{
while($row = mysqli_fetch_array($query)){
$des = $row['description'];
$file = $row['name'];
$output ='<div>'.$des. '</div>';
echo "<td>";
echo "$output";
echo "<a href='".$file."'>Download</a></td>";
}
}
}
echo "</table>";
?>
I am getting all the results in the same line itself.
Are you missing the table row tag? <tr>
if($count == 0){
$output = 'THERE WAS NO RESULTS';
}
else{
echo '<table>';
while($row = mysqli_fetch_array($query)){
$des = $row['description'];
$file = $row['name'];
echo '<tr>';
echo '<td><div>'. $des .'</div></td>';
echo '<td><a href='".$file."'>Download</a></td>';
echo '</tr>';
}
echo '</table>';
}

get the whole row of the same ID from drop down menu

Okay I just changed it to $_POST and it's now working. I'm not sure if this is the shortcut method. At least it's working now. You can help me shrink the code if you want to help me. thanks
<?php
$conn = new mysqli('localhost', 'root', 'jared17', 'hbadb')
or die ('Cannot connect to db');
$result = $conn->query("select * from english");
echo "<html>";
echo "<body>";
echo "<form method = POST>";
echo "<select name = 'Students'>";
while ($row = $result->fetch_assoc()) {
$LRN = $row['LRN'];
$Last = $row['Last_Name'];
$First = $row['First_Name'];
$Lvl = $row['Level'];
$Q1 = $row['Q1'];
$Q2 = $row['Q2'];
$Q3 = $row['Q3'];
$Q4 = $row['Q4'];
$Final = $row['FINAL'];
echo '<option value="'.$LRN.'|'.$Last.', '.$First.'|'.$Lvl.'|'.$Q1.'|'.$Q2.'|'.$Q3.'|'.$Q4.'|'. $Final.'">'.$Last.', '.$First.'</option>';
}
echo "</select>";
echo "<input type='submit' name='submit' value='Show'>";
echo "</form>";
$show = $_POST['Students'];
$show_explode = explode('|', $show);
echo "<table><tr><th>LRN</th><th>Name</th><th>Level</th><th>Q1</th><th>Q2</th><th>Q3</th><th>Q4</th><th>Final</th></tr>";
echo "<tr><td>". $show_explode[0]."</td><td>". $show_explode[1]."</td><td>". $show_explode[2]."</td><td>". $show_explode[3]."</td><td>". $show_explode[4]."</td><td>". $show_explode[5]."</td><td>". $show_explode[6]."</td><td>". $show_explode[7]."</td></tr>";
echo "</table>";
echo "</body>";
echo "</html>";
?>
Don't put all the details in the option value like that. Just put the ID in the value.
echo "<select name = 'Students'>";
while ($row = $result->fetch_assoc()) {
$LRN = $row['LRN'];
$Last = $row['Last_Name'];
$First = $row['First_Name'];
echo '<option value="'.$LRN.'">'.$Last.', '.$First.'</option>';
}
echo "</select>";
Then look it up in the database when the form is submitted.
if (isset($_POST['Students'])) {
$lrn = $_POST['Students'];
$stmt = $conn->prepare("SELECT Last_Name, First_Name, Level, Q1, Q2, Q3, Q4, FINAL FROM english WHERE LRN = ?");
$stmt->bind_param('i', $lrn);
$stmt->execute();
$stmt->bind_result($last, $first, $level, $q1, $q2, $q3, $q4, $final);
$stmt->fetch();
echo "<table><tr><th>LRN</th><th>Name</th><th>Level</th><th>Q1</th><th>Q2</th><th>Q3</th><th>Q4</th><th>Final</th></tr>";
echo "<tr><td>$lrn</td><td>$last, $first</td><td>$level</td><td>$q1</td><td>$q2</td><td>$q3</td><td>$q4</td><td>$final</td></tr></table";
}
You can use $foreach for minimum code when deal with Array. Here code goes
if(isset($_POST['submit'])){
// after post a form ur code goes here
$show = $_POST['Students']; $show_explode = explode('|', $show);
echo "<table><tr>
<th>LRN</th>
<th>Name</th>
<th>Level</th>
<th>Q1</th>
<th>Q2</th>
<th>Q3</th>
<th>Q4</th>
<th>Final</th>
</tr>";
echo "<tr>";
foreach($show_explode as $value){
echo "<td>".$value."</td>";
}
echo "</tr></table>
}

PHP/SQL - Query not showing all data

Here's the code:
<?php
if(isset($_POST['results']) && $_POST['results'] != -1) {
$db = new PDO('mysql:host=localhost;dbname=;charset=utf8', '', '');
echo "<table border='1'>
<tr>
<th>Courses</th>
</tr>";
$stmt = $db->prepare("SELECT title FROM course WHERE `subject_id`=?");
$stmt->execute(array($_POST['results']));
if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>";
echo "<td>", $row['title'], "</td>";
echo "</tr>";
}
} else {
echo "No results found";
} echo "</table>";
}
?>
This is just returning one result into the table, when there are more results to show.
Where am I going wrong?
remove the if, because it's change the pointer of array(fetch)
if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { // get the first record, remove this if
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {// get the second record and reset $row
you can change to:
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
$total_rows = $stmt->rowCount();
if($total_rows > 0){
foreach($row as $item){
echo '<td>'. $item['title'] .'</td>';
}
}else{
echo 'no results';
}

PHP - Adding a separator in a Loop

I asked this question yesterday but no one replied after I tried to get further help. I've been trying to figure out the problem in the code I was given in my previous post but it's not giving me the results I want. Right now what it does is it writes a separator after each row but what I really want is if the name of the manager from the previous row is different then write a separator and keep going on with loop.
while ($row = mysqli_fetch_array($results)) {
$team = $row[0]['Manager'];
foreach($row as $rows){
if($rows['Manager'] !== $team){
echo "<tr><td colspan=\"3\">Separator</td></tr>";
}
echo "<tr>";
echo "<td>".$row['Manager']."</td>";
echo "<td>".$row['Employee_ID']."</td>";
echo "<td>".$row['Name']."</td>";
echo "</tr>";
}
}
You're mixing $row and $rows, you would be okay doing this:
$team = "";
while ($row = mysqli_fetch_array($results)) {
if($team && ($row['Manager'] != $team)){ // skip first separator
echo "<tr><td colspan=\"3\">Separator</td></tr>";
}
$team = $row['Manager'];
echo "<tr>";
echo "<td>".$row['Manager']."</td>";
echo "<td>".$row['Employee_ID']."</td>";
echo "<td>".$row['Name']."</td>";
echo "</tr>";
}
Am I missing something, or can you simply display the data, then assign it to the variable further in the loop?
while ($row = mysqli_fetch_array($results)) {
$team = $row[0]['Manager'];
foreach($row as $rows){
if($row['Manager'] !== $team){
echo "<tr><td colspan=\"3\">".$team."</td></tr>";
}
echo "<tr>";
echo "<td>".$row['Manager']."</td>";
echo "<td>".$row['Employee_ID']."</td>";
echo "<td>".$row['Name']."</td>";
echo "</tr>";
$team = $row[0]['Manager'];
}
}
What is $rowS['Manager'] ? $rowS is unknown. use $row['Manager'] like:
if($row['Manager'] !== $team){
echo "<tr><td colspan=\"3\">Separator</td></tr>";
}
while ($row = mysqli_fetch_array($results)) {
$team = $row[0]['Manager'];
foreach($row as $rows){
if($rows['Manager'] !== $team) {
$team = $row['Manager'];
echo "<tr><td colspan=\"3\">Separator</td></tr>";
}
echo "<tr>";
echo "<td>".$row['Manager']."</td>";
echo "<td>".$row['Employee_ID']."</td>";
echo "<td>".$row['Name']."</td>";
echo "</tr>";
}
}
Just move the value allocation $team = $rows['Manager']; in the if condition.
while ($row = mysqli_fetch_array($results)) {
$team = "";
foreach($row as $rows){
if($rows['Manager'] != $team){
echo "<tr><td colspan=\"3\">Separator</td></tr>";
$team = $rows['Manager'];
}
echo "<tr>";
echo "<td>".$rows['Manager']."</td>";
echo "<td>".$rows['Employee_ID']."</td>";
echo "<td>".$rows['Name']."</td>";
echo "</tr>";
}
}

Categories