PHP parse error T_FOREACH [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 8 years ago.
Improve this question
I am trying to create table in PHP using array but I am getting parse error in my foreach line. Don't know why its giving me syntax error.
$message = '<table border=1>
<tr>
<th><b>Order Details</b></th>
</tr>
'.foreach ($json_array->request->list as $value) {
$wineId = $value->wineId;
$wineName = $value->wineName;
$noOfDrinks = $value->noOfDrinks;
.'
<tr>
<td>ID</td><td>'.$wineId.'</td>
</tr>
<tr>
<td>Name</td><td>'.$wineName.'</td>
</tr>
<tr>
<td>Quantity</td><td>'.$noOfDrinks.'</td>
</tr>
'. } .'
</table>';

Try this, use string concatenation except php code
$message = '<table border=1>
<tr>
<th><b>Order Details</b></th>
</tr>';
foreach ($json_array->request->list as $value) {
$wineId = $value->wineId;
$wineName = $value->wineName;
$noOfDrinks = $value->noOfDrinks;
$message .= '<tr>
<td>ID</td><td>'.$wineId.'</td>
</tr>
<tr>
<td>Name</td><td>'.$wineName.'</td>
</tr>
<tr>
<td>Quantity</td><td>'.$noOfDrinks.'</td>
</tr>';
}
$message .='</table>';

Related

Invalid argument supplied foreach php mysqli [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 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>

Display table in PHP if statement [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 7 years ago.
Improve this question
Can anyone help me how to display table inside PHP if statement.
Here's my code:
if( empty($errors))
{
ACTIVITIES \n
echo ' <table>
<thead>
<tr>
<th>Activity</th>
<th>Price</th>
<th>Quantity</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>Chicken Feeding</td>
<td>$price_chicken</td>
<td>$num_chicken</td>
<td>$total_chicken</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Fish Feeding</td>
<td>$price_fish</td>
<td>$num_fish</td>
<td>$total_fish</td>
</tr>
</tbody>
.................
</table> ';
}
try this
<?php if( empty($errors)): ?>
ACTIVITIES \n
<table>
<thead>
<tr>
<th>Activity</th>
<th>Price</th>
<th>Quantity</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>Chicken Feeding</td>
<td><?=$price_chicken;?></td>
<td><?=$num_chicken;?></td>
<td><?=$total_chicken;?></td>
</tr>
</tbody>
<tbody>
<tr>
<td>Fish Feeding</td>
<td><?=$price_fish;?></td>
<td><?=$num_fish;?></td>
<td><?=$total_fish;?></td>
</tr>
</tbody>
.................
</table>
<?php endif; ?>
You have the table code wrapped in single quotes, but there are PHP variables inside. PHP will not translate those variables unless you wrap the table in double quotes.
If this doesn't help, give us an idea of what the output looks like.
Try using a 2-dimensional array and looping through it to create a proper HTML table. This way, there's no hard-coding, so you can make as many rows/columns as you want. Here's the code, just change the array:
<table>
<tbody>
<?php
$tableArray = [["Chicken Feeding", $price_chicken, $num_chicken],
["Fish Feeding", $price_fish, $num_fish]];
foreach ($tableArray as $tableRow) {
echo "<tr>";
foreach ($tableRow as $tableCell) {
echo "<td>$tableCell</td>";
}
echo "</tr>";
}
?>
</tbody>
</table>

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
}
?>

Why not close row? [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 8 years ago.
Improve this question
I output data from atabase in table.
In result table created with next code:
while($i=$res2->fetch_assoc()) {
$a++;
$t2.='
<tr>
<td>'.$a.'</td>
<td>'.date_format(new DateTime($i['date']),'d.m.Y').'</td>
';
if($valid!='id'){
$t2.='
<td>'.$partner.'</td>
';
}
$t2.='
<td>'.$http_referer.'</td>
</tr>';
}
$t1='
<table class="table table-hover table-bordered">
<thead>
<tr>
<th class="column_th_number">№</th>
<th>Date</th>
';
if($valid!='id'){
$t1.='
<th>Partner</th>
';
}
$t1.='
<th></th>
</tr>
<tr>
<td colspane="2">
</td>
</tr>
</thead>
<tbody>
';
$t3='
</tbody>
</table>
';
echo $t1.$t2.$t3;
but in the result I see that last row was not closed (see image):
Tell me please why last row was not closed?
And how can I make this right?
<td>'.date_format(new DateTime($i['date']),'d.m.Y').'</td>
if($valid!='id'){
replace with
<td>'.date_format(new DateTime($i['date']),'d.m.Y').'</td>'; <--- end the string here
if($valid!='id'){
you missed ';
<td>'.$a.'</td>
<td>'.date_format(new DateTime($i['date']),'d.m.Y').'</td>';
if($valid!='id'){
Thanks ALL.
Error was with colspan(value does not match the number of columns).
Thanks all for help

Retrieving records from mysql [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 8 years ago.
Improve this question
Just looking for better solution.I'm retrieving all the records from mysql. Then I will send this data by email. Its working fine. Is there any better solution? thanks
$query = "SELECT * FROM order_details WHERE order_id = '".$data['order_id']."'";
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$message_admin .= "<table style='text-align:center;' border='1' cellspacing='0' cellpadding='7'>
<tr>
<td>Source</td>
<td>".$data['source']."</td>
</tr>
<tr>
<td>Email</td>
<td>".$data['email']."</td>
</tr>
<tr>
<td>Message</td>
<td>".$data['message']."</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</table>";
}
You would benefit from: http://us.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
Instead of the complicated string manipulation you are doing now.
You are setting $row, but using $data?
I would take a look at the following to improve your code.
mysql-real-escape-string
SELECT * vs SELECT column

Categories