Nested Loop in table PHP - php

I am new to PHP and I am trying to make a table using two foreach and I am not getting the output I want.
<html>
<head>
<title>Didier Test</title>
</head>
<body>
<h1>Yesso!</h1>
</body>
<table border="1">
<tr>
<th>Books</th>
<th>Price</th>
</tr>
<tr>
<?php foreach($name as $item): ?>
<td><?=$item?></td>
<?php foreach($price as $item2): ?>
<td><?=$item2?></td>
</tr>
<?php endforeach; ?>
<?php endforeach; ?>
</table>
</body>
</html>
I know I am having an issue with my inner foreach, but I don't know how to correct it.
Please let me know.

Well for one thing you are closing your body tag on line 7, before you even output your table.
<body>
<h1>Yesso!</h1>
</body>
I also don't know why you are doing a nested loop on seemingly non-related data. Other than that we would need to see your query.

Since you start the row with <tr> before the first loop, you need to end the row with </tr> after it:
<tr>
<?php foreach($name as $item): ?>
<td><?=$item?></td>
<?php foreach($price as $item2): ?>
<td><?=$item2?></td>
<?php endforeach; ?>
<?php endforeach; ?>
</tr>

<?php
for($i = 0; $i< count($name); $i++) {
$item = $name[$i];
$item2 = $price[$i];
?>
<tr>
<td><?=$item?></td>
<td><?=$item2?></td>
</tr>
<?php
}
?>

Try changing your shorthand php output notation <?= with full <?php echo
<html>
<head>
<title>Didier Test</title>
</head>
<body>
<h1>Yesso!</h1>
</body>
<table border="1">
<tr>
<th>Books</th>
<th>Price</th>
</tr>
<tr>
<?php foreach($name as $item): ?>
<td><?php echo $item?></td>
<?php foreach($price as $item2): ?>
<td><?php echo $item2?></td>
</tr>
<?php endforeach; ?>
<?php endforeach; ?>
</table>
</body>
</html>
You probably dont get any output because your php.ini has set not to allow shorthand php '
Oh, and yes, like mentioned by Barmer and Revent, you have some HTML tag-nesting issues as well. Welcome to the wonderful world of PHP & Good luck :)

Related

PHP HTML foreach loop

How would I be able to make a table from php using html with a foreach loop?
My goal is to have a table with href tags for each of the sites. It's just that I'm a newbie to php
<?php
$baseConf = ['site' => [abc.com],[abc.org]];
?>
<!DOCTYPE html>
<html>
<head>
<title>Default Page</title>
</head>
<body>
<table>
<!--<?php //$site; ?>-->
<?php
foreach($site as $value ) {
printf $value;
}
?>
</table>
</body>
</html>
You can use a simple foreach loop to put <a> tag with href in table.
<table>
<?php
$sites=$baseConf['site'];
foreach($sites as $item) {
?>
<tr>
<td>
<a href="<?= $item ?>">
<?=$item?>
</a>
</td>
</tr>
<?php
}
?>
</table>
It is very simple for loop and you can grab it from SO. Still I am solving it for you.
I assume you have a multidimensional array. Let's simplify it.
$baseConf = ['site' => [example.com],[example.org]];
$sites = $baseConf['site']; // we will loop sites now
Now Use the html like this
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Links</th>
</tr>
</thead>
<tbody>
<?php for ($i = 0; $i < count($site); $i++){?>
<tr>
<th scope="row"><?php echo $i?></th>
<td><?php echo $site[$i]?></td>
</tr>
<?php}?>
</tbody>
</table>
I have fixed my code a bit and this is what worked out:
$baseConf = array(
'site' => array("abc.com","abc.org"),
);
?>
<!DOCTYPE html>
<html>
<head>
<title>Default Page</title>
</head>
<body>
<!--<?php //$site; ?>-->
<?php
foreach ($baseConf as $value) {
//echo "$value\n";
print_r($value);
}
?>
</body>
</html>

mysqli_fetch_assoc() not working, unable to enter loop

I have been trying to display the data from a table onto a page. The table is called events and the database is school.
As far as I know, I'm doing everything right, I've stored the query in a string, run it using mysqli_query() and later put the echo statements in a while loop while placing $row=mysqli_fetch_assoc($result) inside the parenthesis. Yet when I run it not even a single entry is displayed on the page.
Here's my entire code:
<?php
require("includes/common.php");
$query = "SELECT name,place,date FROM school.events";
$result = mysqli_query($con, $query)or die(mysqli_error($con));
?>
<!DOCTYPE html>
<!--
Calendar Page for Trinity High School
-->
<html>
<head>
<title>Events Calendar</title>
<?php
require 'includes/external.php';
?>
</head>
<body>
<div class="content">
<?php
include 'includes/header.php';
?>
<div class="container">
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Event</th>
<th>Place</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['place']; ?></td>
<td><?php echo $row['date']; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div>
</body>
</html>
PS: Using some trial and error I found out that the program simply doesn't enter the while loop. Perhaps there's something wrong with the condition?

php/datatables generate table using foreach but need an additional <td></td> at end

I am using datatables and generating my table using a foreach, but I need one last to contain the options (edit, etc). I cannot think how to most efficiently do this, I can think to do it using a counter and then when the output has ended place an if statement (rows = rows then) to put these last td's in... I am hoping someone has a better way using my current syntax?
<?php foreach($results as $row):?>
<tr>
<?php foreach($row as $cell): ?>
<td>
<?php echo $cell ?>
</td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
you can put then after internal for like this:
<?php foreach($results as $row):?>
<tr>
<?php foreach($row as $cell): ?>
<td>
<?php echo $cell ?>
</td>
<?php endforeach; ?>
<td>edit </td> <td>show</td><td>delete</td>
</tr>
<?php endforeach; ?>

PHP html logic error If condition works backwards

I have a form where I'm using a nested pair of foreach loops. I can't figure out why the continue statement executes when the condition is false instead of true: Here's the code I'm using
<?php $array =[9,10,11,12,13,14,15,16,17,18,19,20,21] ; $i = 0;?>
<?php foreach ($reservations as $reservation): ?>
<table class="tabsize1" style ="margin-top:0%;margin-bottom:0%;">
<tbody>
<tr >
<td class="nextsail"><?=$reservation["date"] ?></td>
<td class="nextsail"><?=$reservation["boat"] ?></td>
<td class="nextsail"><?=$reservation["start_res"]?></td>
<td class="nextsail"><?=$reservation["end_res"] ?></td>
</tr>
</tbody>
</table>
<?php if($weather[$i]['cnt'] == 24) continue ; ?> <!-- this works if i use != but $weather[$i]['cnt'] = 24 -->
<table >
<tbody>
<tr >
<?php foreach ($array as $value): ?>
<td> <?= $weather[$i][$value]['HOUR']?></td>
<?php endforeach ?>
</tr>
</tbody>
</table>
<?php $i++; ?>
<?php endforeach ?>
You are missing ; after endforeach

how to store json_encode results in a table

I have a database which works by displaying first and last_names of all employees in the database, i can display it but when it displays, it's not formatted. I want to try and put the results in a table but I'm not sure how I would.
I thought I would have to echo json_encode(echo.<td>$posts</td>) or something like that
<?php foreach($query as $row): ?>
<tr>
<td>
<?php $arr = array(
'first_name' => $row->first_name,
'last_name' => $row->last_name,
); ?>
<?php $posts[] = $arr;?>
</tr>
<?php endforeach; ?>
<?php echo json_encode($posts);?>
This is how its displayed now
[{"first_name":"Georgi","last_name":"Facello"},
{"first_name":"Georgi","last_name":"Atchley"}]
Nothing is written between your <tr> and such .. you are just assigning to posts and then printing it out as JSON after the fact which makes no sense.
<?php foreach... ?>
<tr>
<td>
<?php echo $row->first_name ?>
</td>
<td>
<? php echo $row->last_name ?>
</td>
</tr>
<?php endforeach ?>
You're gonna have to build the table 'by hand'. Like this:
<table>
<?php foreach($query as $row): ?>
<tr>
<td>
<?php echo $row->first_name; ?>
</td>
<td>
<?php echo $row->last_name; ?>
</td>
</tr>
<?php endforeach; ?>
</table>
<table>
<thead>
<th> First Name </th>
<th> Last Name </th>
</thead>
<tbody>
<?php foreach($query as $row): ?>
<tr>
<td> <?php echo $row->first_name ?> </td>
<td> <?php echo $row->last_name ?> </td>
</tr>
<?php endforeach; ?>
</tbody>
</table>

Categories