Display table in PHP if statement [closed] - php

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>

Related

Php variable in a html table

I'm trying to display an html table in which I'd like to show some php variables.
My code is the following:
<?php
$title = the_title();
?>
<table>
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $title?></td>
</tr>
</tbody>
</table>
In the output I can see the word "Name" as expected but it doesn't print the $title variable (there is a empty space instead).
I've already read some of the questions on the site but they doesn't help me because they suggest to write the same code that I wrote.

how to create a nested array and store it in database? [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 6 years ago.
Improve this question
i have a data like
<table>
<tr>
<th>title 1</th>
<td>para1</td>
</tr>
</table>
<table>
<tr>
<th>title 2</th>
<td>para1</td>
<td>para2</td>
<td>para3</td>
</tr>
</table>
<table>
<tr>
<th>title 3</th>
<td>para1</td>
<td>para2</td>
</tr>
</table>
now how can i take this data and make into an array..it will be really helpful if i can get a solution for this.
In my problem i have a table as shown above and want to store the data in a nested/ multidimensional array. All the solution above doesnot answer my question
thanks in advance
I think I know what you are looking for... It's actually pretty simple.
You want to iterate through the <table> tags first .. And subsequently iterate the children tr - tds.
I'd surround the tables with a surrounding div to make grabbing them much easier.
Then I'd use jQuery because the library makes it easy to choose children etc etc. Then for Storage into the database .. I'd "Json-ify" the array(s)
IE
$(document).ready(function() {
myHTML = $('#myDiv').html();
});
var tableNumber = $('#myDiv').children('table').length;
var items = [];
for (i = 0; i < tableNumber; i++) {
var title = $($("table tr th")[i]).html();
var paras = [];
$($("table tr")[i]).find('td').each(function() {
paras.push($(this).html());
});
items.push(title, paras);
}
var outPut = JSON.stringify(items);
$('#jsonOut').html(outPut);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="myDiv">
<table>
<tr>
<th>title 1</th>
<td>para 1-1</td>
</tr>
</table>
<table>
<tr>
<th>title 2</th>
<td>para 2-1</td>
<td>para 2-2</td>
<td>para 2-3</td>
</tr>
</table>
<table>
<tr>
<th>title 3</th>
<td>para 3-1</td>
<td>para 3-2</td>
</tr>
</table>
</div>
<br>
<pre>
<div id="jsonOut">
</div>
</pre>
You can also view the FIDDLE
Hope this helps.

display an Html table as nested array [duplicate]

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 6 years ago.
Improve this question
i have a data like
<table>
<tr>
<th>title 1</th>
<td>para1</td>
</tr>
</table>
<table>
<tr>
<th>title 2</th>
<td>para1</td>
<td>para2</td>
<td>para3</td>
</tr>
</table>
<table>
<tr>
<th>title 3</th>
<td>para1</td>
<td>para2</td>
</tr>
</table>
now how can i take this data and make into an array..it will be really helpful if i can get a solution for this.
In my problem i have a table as shown above and want to store the data in a nested/ multidimensional array. All the solution above doesnot answer my question
thanks in advance
I think I know what you are looking for... It's actually pretty simple.
You want to iterate through the <table> tags first .. And subsequently iterate the children tr - tds.
I'd surround the tables with a surrounding div to make grabbing them much easier.
Then I'd use jQuery because the library makes it easy to choose children etc etc. Then for Storage into the database .. I'd "Json-ify" the array(s)
IE
$(document).ready(function() {
myHTML = $('#myDiv').html();
});
var tableNumber = $('#myDiv').children('table').length;
var items = [];
for (i = 0; i < tableNumber; i++) {
var title = $($("table tr th")[i]).html();
var paras = [];
$($("table tr")[i]).find('td').each(function() {
paras.push($(this).html());
});
items.push(title, paras);
}
var outPut = JSON.stringify(items);
$('#jsonOut').html(outPut);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="myDiv">
<table>
<tr>
<th>title 1</th>
<td>para 1-1</td>
</tr>
</table>
<table>
<tr>
<th>title 2</th>
<td>para 2-1</td>
<td>para 2-2</td>
<td>para 2-3</td>
</tr>
</table>
<table>
<tr>
<th>title 3</th>
<td>para 3-1</td>
<td>para 3-2</td>
</tr>
</table>
</div>
<br>
<pre>
<div id="jsonOut">
</div>
</pre>
You can also view the FIDDLE
Hope this helps.

While Loop in php not working properly [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
The While Loop is Only Displaying only Last entry
<table border="5" bgcolor="white" width="300" align="center">
<tr>
<th bgcolor="grey">User ID</th>
<th bgcolor="">User Name</th>
<th bgcolor="grey">User Password</th>
</tr>
<?php
$Load = "select * from sudents";
$fetch = mysql_query($Load);
while ($rows = mysql_fetch_array($fetch))
{
$ID=$rows['id'];
$User=$rows['user'];
$Password=$rows['password'];
}
echo "<tr>
<td>$ID</td>
<td>$User</td>
<td>$Password</td>
</tr>
";
?>
</table>
Your loop isn't printing anything to the page. It's setting values to variables. And it's over-writing those variable every time. So when the loop is done, only the last values are still set.
Then you just echo that last value. Once.
Instead, echo the output inside the loop so you can have one element of output for each loop iteration:
while ($rows = mysql_fetch_array($fetch))
{
$ID=$rows['id'];
$User=$rows['user'];
$Password=$rows['password'];
echo "<tr>
<td>$ID</td>
<td>$User</td>
<td>$Password</td>
</tr>";
}
Note, however, that there are a couple of other things wrong here:
Your code is vulnerable to XSS attacks.
You are displaying user passwords. Never, ever do that. Your system shouldn't even have user passwords in a readable format. User passwords should be obscured using a 1-way hash and should never be retrievable by anybody.
As you have it, you are only printing a table row once, and using the last values of the row. All your loop does is assign values to $ID, $User, and $Password, and each loop pass just overwrites the old values. To fix this, you need to move the echo statements into the body of the loop.
This will let you print the current values over each iteration, instead of only printing the last. Here's the code that will work for what you want.
<table border="5" bgcolor="white" width="300" align="center">
<tr>
<th bgcolor="grey">User ID</th>
<th bgcolor="">User Name</th>
<th bgcolor="grey">User Password</th>
</tr>
<?php
$Load = "select * from sudents";
$fetch = mysql_query($Load);
while ($rows = mysql_fetch_array($fetch))
{
$ID=$rows['id'];
$User=$rows['user'];
$Password=$rows['password'];
echo "<tr>
<td>$ID</td>
<td>$User</td>
<td>$Password</td>
</tr>";
}
?>
</table>
Welcome to Stack Overflow #Cat I see two problems.
First: no echo, var_dump() or print found.
Second: the while loop will overwrite anything as long as it iterates, so in your case i would push to an array with each result found, like this:
<?php
$q = "SELECT id, user, password FROM sudents";
$fetch = mysql_query($q);
$final = [];
while ($row = mysql_fetch_array($fetch)) {
final[] = $row;
}
?>
<table>
<tr>
<th>User ID</th>
<th>User Name</th>
<th>User Password</th>
</tr>
<?php foreach($final as $r) {?>
<tr>
<td><?= $r['id'] ?></td>
<td><?= $r['user'] ?></td>
<td><?= $r['password'] ?></td>
</tr>
<?php } ?>
</table>

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

Categories