Invalid argument supplied foreach php mysqli [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm trying to display products from cart but I receive following errors (see below). I really don't understand what I'm doing wrong... A detailed answer with explanations would be nice. I tried to find solutions to my problem without success. I do understand what an undefined variable means but I don't know how to fix it in this particular case.
Notice: Undefined variable: row
Warning: Invalid argument supplied for foreach()
<html>
<body>
<table>
<tbody>
<?php
require_once 'core/config.php';
$cartQ = $db->query("SELECT * FROM cart");
$result = mysqli_fetch_assoc($cartQ);
foreach ($row as $product) {
$productQuery = $db->query("SELECT * FROM product INNER JOIN cart ON product.id=cart.product_id ");
$product = mysqli_fetch_assoc($productQuery);
while ($row = mysqli_fetch_assoc($cartQ)) {
}
?>
<tr class="p">
<td class="image"><img src="<?= $product['image_1']; ?>" /></td>
<td class="name"><?= $product['prod_name']; ?></td>
<td class="price"><?= money($product['price']); ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</body>
</html>

Get rid of the first query and the foreach loop, they're not needed. The JOIN query returns all the information you need to display.
<html>
<body>
<table>
<tbody>
<?php
require_once 'core/config.php';
$productQuery = $db->query("SELECT * FROM product INNER JOIN cart ON product.id=cart.product_id ");
while ($product = mysqli_fetch_assoc($productQuery)) {
?>
<tr class="p">
<td class="image"><img src="<?= $product['image_1']; ?>" /></td>
<td class="name"><?= $product['prod_name']; ?></td>
<td class="price"><?= money($product['price']); ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</body>
</html>

Related

Add Numbers to MySQL, HTML table in order [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I will try my best to explain what I am trying to do. Hopefully you can help me out. I have a database that holds links, these links are displayed in a table. I have the entries output properly in order from Points, but I am trying to add a rank number to the side so it says 1, 2, 3, 4, etc going down the page per entry.
Here is my attempt.
<table width = "1000" style='table-layout:fixed;'>
<tr>
// These are the numbers I need to add
<th>Rank</th>
// All these work fine.
<th>Host</th>
<th>Location</th>
<th>Points</th>
</tr>
<?php while($row1 = mysqli_fetch_array($result1)):;?>
<tr>
<td><?php echo $row1[0]; ?></td>
<td><?php echo $row1[1]; ?></td>
<td><?php echo $row1[2]; ?></td>
</tr>
<?php endwhile; ?>
</table>
Hopefully you understand what I am trying to do, if you need any more information let me know.
Thanks for your time.
you mean? :
<?php $rankId=0; while($row1 = mysqli_fetch_array($result1)){ ?>
<tr>
<td><?=$rankId++ ?></td>
<td><?=$row1[0] ?></td>
<td><?= $row1[1] ?></td>
<td><?=$row1[2] ?></td>
</tr>
<?php } ?>

html php not properly aligning for each column [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I m trying to run two loops the first query in retrieves the column .The second query takes the primary id of the first table and executes the query.
Final result should be like given in the image
Im not able to give the proper alignment
Below is the code
<table width="100%" border="1" cellspacing="1" cellpadding="1" >
<tr>
<td>Col1</td>
<td>col2</td>
<td>col3</td>
<td>col4/td>
</tr>
<?php
$mast = mysql_query("select * from table1 where av_master_id='".$_REQUEST['id']."'");
while($res_mas= mysql_fetch_assoc($mast))
{
?>
<tr>
<td><?php echo $res_mas['col1'];?></td>
<?php
$room= mysql_query("SELECT * FROM `table2` WHERE av_room_id='".$res_mas['av_room_id']."'");
while($res_room= mysql_fetch_assoc($room))
{
?>
<td><?php echo $res_room['col2'];?></td>
<td><?php echo $res_room['col3'];?></td>
<td><?php echo $res_room['col4'];?></td>
</td>
</tr><tr>
<?php }?>
</tr>
<?php } ?>
</table>
This should do it:
<table width="100%" border="1" cellspacing="1" cellpadding="1" >
<tr>
<td>Col1</td>
<td>col2</td>
<td>col3</td>
<td>col4/td>
</tr>
<?php
$mast = mysql_query("select * from table1 where av_master_id='".$_REQUEST['id']."'");
while($res_mas = mysql_fetch_assoc($mast)) {
$room = mysql_query("SELECT * FROM `table2` WHERE av_room_id='".$res_mas['av_room_id']."'");
$count = 0;
while($res_room= mysql_fetch_assoc($room)) {
$count += 1;
?>
<tr>
<td><?php echo $count === 1 ? $res_mas['col1'] : "";?></td>
<td><?php echo $res_room['col2'];?></td>
<td><?php echo $res_room['col3'];?></td>
<td><?php echo $res_room['col4'];?></td>
</tr>
<?php } } ?>
</table>
(untested)

Unexpected T_WHILE error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Can anybody point out what I am doing wrong in my code below? I'm getting an "UNEXPECTED T_WHILE" error.
echo '
<table border="1">
<tr>
<td>Events</td>
<td>Category</td>
</tr>
<tr>
<td>', while ($row = mysql_fetch_assoc($queryresult)) { $title = $row['eventTitle']; echo $title; }, '</td>
<td>', while ($row = mysql_fetch_assoc($queryresult)) { $category = $row['eventTitle']; echo $category; }, '</td>
</tr>
</table> ';
Try this
First off, close your PHP tags.
?>
<table border="1">
<tr>
<td>Events</td>
<td>Category</td>
</tr>
<tr>
<td>
<?php
while ($row = mysql_fetch_assoc($queryresult)) {
$title = $row['eventTitle'];
echo $title;
}
?>
</td>
<td>
<?php
while ($row = mysql_fetch_assoc($queryresult)) {
$category = $row['eventTitle'];
echo $category;
}
?>
</td>
</tr>
</table>
PHP -
$array = array();
$i = 0;
while ($row = mysql_fetch_assoc($queryresult))
{
$array[$i]['title'] = $row['eventTitle'];
$array[$i]['category'] = $row['eventCategory'];
$i++;
}
HTML -
<table border="1">
<tr>
<td>Events</td>
<td>Category</td>
</tr>
<?php
foreach($array as $arr)
{
?>
<tr>
<td><?php echo $arr['event']; ?></td>
<td><?php echo $arr['category']; ?></td>
</tr>
<?php
}
?>

Column wise sum in PHP from HTML table [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to sum of a column
<table width="240" border="1">
<tr>
<td width="94">Name</td>
<td width="130">Current Amount</td>
</tr>
<?php
do {
?>
<tr>
<td><?php echo $row_Record['name']; ?></td>
<td><?php echo $row_Record['current_amount']; ?></td>
</tr>
<?php
}
while ($row_Record = mysql_fetch_assoc($Record));
?>
<tr>
<td colspan="2">Total = </td>
</tr>
</table>
I don't know how to do this kind of sum.
Please any one help me.
Thank you.
By using Jquery, you can do something like that
$(document).ready(function(){
var sum = 0
$(".sum").each(function(){
sum += parseFloat($(this).text());
});
alert(sum);
});
Jsfiddle
Alternatively, you can sum all the values by using PHP when fetching the values.
Something like that:
$sum = 0;
while($read = mysqli_fetch_array()){
$sum += $read['row'];
}
echo $sum; //total
<table width="240" border="1">
<tr>
<td width="94">Nmae</td>
<td width="130">Current Amount</td>
</tr>
<?php
$tmp=0;
do {
$tmp+=$row_Record['current_amount'];
?>
<tr>
<td><?php echo $row_Record['name']; ?></td>
<td><?php echo $row_Record['current_amount']; ?></td>
</tr>
<?php } while ($row_Record = mysql_fetch_assoc($Record)); ?>
<tr>
<td colspan="2">Total = <?php echo $tmp; ?></td>
</tr>
</table>

foreach in table format using codeigniter [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
<tbody>
<?php foreach($result as $row){ ?>
<?php foreach($result1 as $row1) {?>
<tr>
<td >
<span><?php echo $row->state; ?></span>
</td>
<td >
<span><?php echo $row->breakdown_grants; ?></span>
</td>
<td >
<span><?php echo $row1->breakdown_grants; ?></span>
</td>
</tr>
<?php } ?>
<?php } ?>
</tbody>
I need to display my coding in table format.in that first foreach contain state and breakdown_grants value.
state actual provisional
$row->state $row->breakdown_grants $row1->breakdown_grants
this is my table format.what i need to change in my foreach.in $result and $result1 i'll get different data.
Better you use for loop like
<?php for($i=0;$i<count($result);$i++) { ?>
<tr>
<td>
<span><?php echo $result[$i]->state; ?></span>
</td>
<td>
<span><?php echo $result[$i]->breakdown_grants; ?></span>
</td>
<td>
<span><?php echo $result1[$i]->breakdown_grants; ?></span>
</td>
</tr>
<?php } ?>
Make sure that $result and $result1 are of same indexes and of same lengths.

Categories