Include if statement in HTML table - php

I want to include an if statement in my PHP code that renders an HTML table. The output should be in a table row, too.
echo '<table style="border:1px solid red;">',
'<tr align="center" ><td><div class="item-font1">', $mounts['name'], '</div></td>
</tr>',
if ($itembind == 1) {
echo '<tr align="center" ><td><div class="item-font1">Text1</div></td>
</tr>',
} elseif ($itembind== 2) {
echo '<tr align="center" ><td><div class="item-font1">Text2</div></td>
</tr>',
echo "\t";
}
'<tr align="center" ><td><div class="item-font1">', $mounts['name'], '</div></td>
</tr>
</table>';
?>
I tried it like this, but there are some errors.

With echo, the strings can be separated by a comma (i.e. ,). However, when additional logic is needed, the echo statement must be terminated. Use a semi-colon (i.e. ;) for that.
echo '<table style="border:1px solid red;">',
'<tr align="center" ><td><div class="item-font1">', $mounts['name'],
'</div></td>
</tr>'; //use a semi-colon here to end the call to echo
if ($itembind == 1) {
As in C or Perl, PHP requires instructions to be terminated with a semicolon at the end of each statement. The closing tag of a block of PHP code automatically implies a semicolon; you do not need to have a semicolon terminating the last line of a PHP block.
1
While it is acceptable to pass multiple strings to echo, the strings could be concatenated into one. For that, use a dot operator.
So update this line (which is missing an echo at the beginning) :
'<tr align="center" ><td><div class="item-font1">', $mounts['name'], '</div>
</td></tr></table>';
To
echo '<tr align="center" ><td><div class="item-font1">'. $mounts['name']. '</div>
</td></tr></table>';
Final output:
<?php
$mounts = ['name'=>'cat'];
$itembind = 1;
echo '<table style="border:1px solid red;">',
'<tr align="center" ><td><div class="item-font1">', $mounts['name'], '</div></td>
</tr>';
if ($itembind == 1) {
echo '<tr align="center" ><td><div class="item-font1">Text1</div></td>
</tr>';
} elseif ($itembind== 2) {
echo '<tr align="center" ><td><div class="item-font1">Text2</div></td>
</tr>';
echo "\t";
}
echo '<tr align="center" ><td><div class="item-font1">', $mounts['name'], '</div></td>
</tr>
</table>';
?>
See a demonstration of the updated code in this playground example.
1http://php.net/manual/en/language.basic-syntax.instruction-separation.php

Use . operator while concatenation. You are using comma , operator. See your code :-
echo '<table style="border:1px solid red;">','<tr align="center" ><td><div class="item-font1">', $mounts['name'], '</div></td></tr>',
should be
echo '<table style="border:1px solid red;">'.'<tr align="center" ><td><div class="item-font1">'. $mounts['name']. '</div></td></tr>'.
and same for rest of the code.

Your syntax is wrong. You should use . to concatenate strings and ; to terminate lines.
You can also use double quotes (") and PHP will parse the string looking for variables. For example:
$myvar = 'example';
$singleQuote = 'This is an ' . $myvar;
$doubleQuote = "This is an {$myvar}";
I usually find double quoting strings easier when you are going to be using variables.
echo '<table style="border:1px solid red;">';
echo '<tr align="center"><td><div class="item-font1">' . $mounts['name'] . '</div></td></tr>';
if ($itembind == 1) {
echo '<tr align="center" ><td><div class="item-font1">Text1</div></td></tr>';
} elseif ($itembind== 2) {
echo '<tr align="center" ><td><div class="item-font1">Text2</div></td></tr>';
echo "\t";
}
'<tr align="center" ><td><div class="item-font1">' . $mounts['name'] . '</div></td></tr></table>';

It would be a better idea to make the conditional statement on the value that's being inserted, but doing this your way looks like:
echo '<table style="border:1px solid red;">',
'<tr align="center" ><td><div class="item-font1">'. $mounts['name']. '</div></td>
</tr>';
if ($itembind == 1) {
echo '<tr align="center" ><td><div class="item-font1">Text1</div></td>
</tr>';
} elseif ($itembind== 2) {
echo '<tr align="center" ><td><div class="item-font1">Text2</div></td></tr>';
} else {
echo "\t";
}
echo '<tr align="center" ><td><div class="item-font1">'. $mounts['name']. '</div></td>
</tr>
</table>';
It wouldn't be a bad idea to practice a tutorial or two before posting here.
Something that some new people don't realize is that you can pull the html out of the php a little. This same thing could be written:
<?php
if ($itembind == 1) $text = 'text1';
elseif ($itembind== 2) $text = 'text2';
if(isset($text)){
$row = '<tr align="center" >
<td>
<div class="item-font1">'.$text.</div>
</td>
</tr>
} else {
$row = '';
}
?>
<table style="border:1px solid red;">
<tr align="center">
<td>
<div class="item-font1"><?=$mounts['name']?></div>
</td>
</tr>
<?=$row?>
<tr align="center" >
<td>
<div class="item-font1">'<?=$mounts['name']?></div>
</td>
</tr>
</table>

You are using , instead of .. Also need to add ; at your echo end.
<?php
$mounts['name'] = 'yyy';
$itembind = 1;
echo '<table style="border:1px solid red;">',
'<tr align="center" ><td><div class="item-font1">'. $mounts['name']. '</div></td>
</tr>';
if ($itembind == 1) {
echo '<tr align="center" ><td><div class="item-font1">Text1</div></td>
</tr>';
} elseif ($itembind== 2) {
echo '<tr align="center" ><td><div class="item-font1">Text2</div></td>
</tr>';
echo "\t";
}
'<tr align="center" ><td><div class="item-font1">'.$mounts['name'].'</div></td>
</tr>
</table>';
?>

$output = "<table style=\"border:1px solid red;\">";
$output .= "<tr align=\"center\"><td><div class=\"item-font1\">";
$output .= $mounts['name'];
$output .= "</div></td>";
$output .= "</tr>";
if ($itembind == 1) {
$output .= "<tr align=\"center\"><td><div class=\"item-font1\">Text1</div></td></tr>";
} else if ($itembind== 2) {
$output .= "<tr align=\"center\"><td><div class=\"item-font1\">Text2</div></td></tr>\t";
}
$output .= "<tr align=\"center\"><td><div class=\"item-font1\">";
$output .= $mounts['name'];
$output .= "</div></td></tr>";
$output .= "</table>";
echo $output;

No need of write table within PHP. it will make complexity of the code. try this:
<?php if(condition) { ?>
<tr> <td>some content</td></tr>
<?php } elseif(condition) { ?>
<tr><td>some content</td></tr>
<?php } else { ?>
<tr><td>some content</td></tr>
<?php } ?>

Related

Building an edit/update database form - adding column

I am working on an update database record form. The data is fetched from db table and populated into an html table on the webpage. I am stuck now how to add another column (5th) in the table that will contains 'edit' href link for updating database script.
Like:
|Col1|Col2|Col3|Col4|Edit|
| -- | -- | -- | -- |href|
I know this is very basic question, but so far I am not able to solve it.
It will be very helpful if someone can help me to work it.
Here is my code:
<div id="update" class="col mb-4">
<?php
class TableRows extends RecursiveIteratorIterator {
function __construct($it) {
parent::__construct($it, self::LEAVES_ONLY);
}
function current() {
return "<td style='width:150px;border:1px solid grey;'>" . parent::current(). "</td>";
}
function beginChildren() {
echo "<tr>";
}
function endChildren() {
echo "</tr>" . "\n";
}
}
echo "<table class='table-bordered'>";
echo "<thead class='table-dark'>";
echo "<tr><th>Col1</th><th>Col2<th>Col3</th><th>xCol4</th></tr>";
echo "</thead>";
echo "<tbody>";
try {
$stmt = $conn->prepare("SELECT Col1, Col2, Col3, Col14 FROM table");
$stmt->execute();
// set the resulting array to associative
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
echo $v;
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
// $conn = null;
echo "</tbody>";
echo "</table>";
?>
</div>
Your code is very overcomplicated, which is what's making it hard to modify.
It seems like you just want to do a simple table with data from a database. You could basically just do:
<div id="update" class="col mb-4">
<table class='table-bordered'>
<thead class='table-dark'>
<tr>
<th>Col1</th>
<th>Col2</th>
<th>Col3</th>
<th>Col4</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
<?php
try {
$stmt = $conn->prepare("SELECT Col1, Col2, Col3, Col14 FROM table");
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach($stmt->fetchAll() as $row) {
?>
<tr>
<td style='width:150px;border:1px solid grey;'><?= $row['Col1'] ?></td>
<td style='width:150px;border:1px solid grey;'><?= $row['Col2'] ?></td>
<td style='width:150px;border:1px solid grey;'><?= $row['Col3'] ?></td>
<td style='width:150px;border:1px solid grey;'><?= $row['Col4'] ?></td>
<td style='width:150px;border:1px solid grey;'>Edit</td>
</tr>
<?php
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
?>
</tbody>
</table>
</div>
I would also recommending moving the database query away from the view into a class or something. Then the view would be much cleaner (no try/catch etc). Then all the PHP in the view would be something like:
<?php foreach ($myClass->getTheData() as $row) : ?>
<tr>
<td style='width:150px;border:1px solid grey;'><?= $row['Col1'] ?></td>
<td style='width:150px;border:1px solid grey;'><?= $row['Col2'] ?></td>
<td style='width:150px;border:1px solid grey;'><?= $row['Col3'] ?></td>
<td style='width:150px;border:1px solid grey;'><?= $row['Col4'] ?></td>
<td style='width:150px;border:1px solid grey;'>Edit</td>
</tr>
<?php endforeach ?>
Now it would be much easier to change the HTML.

how to echoing value but i want the row on it heading place

I have a table like this.
but I want the data on the 'debit' column is on it own place.
this is what Iwant :
this is my current code :
<?php
foreach ($jurnal->result_array() as $data){
echo "
<tr>
<td>".$data['no_akun']."</td>
<td>".$data['nm_akun']."</td>";
if ($data['nm_akun'] == 'kredit'){
echo "
<td></td>
<td>".$data['posisi_dr_cr']."</td>
";
}else{
echo "
<td>".$data['posisi_dr_cr']."</td>
<td></td>
";
}
"</tr>
";
}
?>
td tag width is the same of the content that it envelopes, so an empty td will be 0px width.
Try adding width="25%" to your td tags, like this:
EDITED:
foreach ($jurnal->result_array() as $data){
echo "
<tr>
<td width=\"25%\">".$data['no_akun']."</td>
<td width=\"25%\">".$data['nm_akun']."</td>";
if ($data['nm_akun'] == 'kredit'){
echo "
<td width=\"25%\"></td>
<td width=\"25%\">".$data['posisi_dr_cr']."</td>
";
}else{
echo "
<td width=\"25%\">".$data['posisi_dr_cr']."</td>
<td width=\"25%\"></td>
";
}
"</tr>
";
}
?>
Notice that width="25%" is typed inside a String, so double quote characters must have \ characters in order to be escaped.
Try this way. if you are using bootstrap css, you can use text-left in the class.
<?php
foreach ($jurnal->result_array() as $data){
echo "
<tr>
<td class='text-left'>".$data['no_akun']."</td>
<td class='text-left'>".$data['nm_akun']."</td>";
if ($data['nm_akun'] == 'kredit'){
echo "
<td class='text-left'></td>
<td class='text-left'>".$data['posisi_dr_cr']."</td>
";
}else{
echo "
<td class='text-left'>".$data['posisi_dr_cr']."</td>
<td class='text-left'></td>
";
}
"</tr>
";
}
?>
use the following code:
<?php
foreach ($jurnal->result_array() as $data){
echo "
<tr>
<td>".$data['no_akun']."</td>
<td>".$data['nm_akun']."</td>";
if ($data['nm_akun'] == 'kredit'){
echo "
<td>&nbsp</td>
<td>".$data['posisi_dr_cr']."</td>
";
}else{
echo "
<td>".$data['posisi_dr_cr']."</td>
<td>&nbsp</td>
";
}
"</tr>
";
}
?>

How do I concatenate html and php?

I currently have this:
$output .= '
<tr>
<th style="text-align: left">'.(__("Item", "WSPSC")).'</th><th>'.(__("Quantity", "WSPSC")).'</th><th>'.(__("Price", "WSPSC")).'</th><th></th>
</tr>';
But I need to replace Item with a piece of php like:
$output .= '
<tr>
<th style="text-align: left">'.(__("
<?php if(ICL_LANGUAGE_CODE=='en'); ?>
Item
<?php elseif(ICL_LANGUAGE_CODE=='it'); ?>
Products
<?php endif; ?>
", "WSPSC")).'</th><th>'.(__("Quantity", "WSPSC")).'</th><th>'.(__("Price", "WSPSC")).'</th><th></th>
</tr>';
The issue I am having is that this is obviously wrong but I can't get my head around the correct concatenation of html and php
If I right understand you need something like this:
$output .= '
<tr>
<th style="text-align: left">'.
(
(ICL_LANGUAGE_CODE=='en')?
'Item':
( (ICL_LANGUAGE_CODE=='it')? 'Products': '' ),
"WSPSC"
)
.'</th><th>'.(__("Quantity", "WSPSC")).'</th><th>'.(__("Price", "WSPSC")).'</th><th></th>
</tr>';
You should not use it in that way. Look at this pseudo code:
$output .= '
<tr>
<th style="text-align: left">';
if (something...) {
$output.= 'sss';
}
elseif (something...) {
$output.= 'ddd';
}
That's the way you should do it.
Your replies helped me thanks, this worked:
$output .= '
<tr>
<th style="text-align: left">';
if (ICL_LANGUAGE_CODE=='en') {
$output .= (__("Item", "WSPSC"));
} elseif (ICL_LANGUAGE_CODE=='it') {
$output .= (__("PRODOTTO", "WSPSC"));
}
$output .= '</th><th>'.(__("Quantity", "WSPSC")).'</th><th>'.(__("Price", "WSPSC")).'</th><th></th>
</tr>';

foreach td loop values

I have the following within my foreach....this works but there are 2 sets of values per $value and it only shows the first
<?php
$info = simplexml_load_file("https://api.website.co.za/ACCESS_GetAccountSessions?");
echo "<ul info>";
foreach ($info->sessions as $sessions):
$count = $sessions->{'session-count'};
$ip = $sessions->session->{'ip-address'};
$nas = $sessions->session->{'nas-ip-address'};
$port = $sessions->session->{'nas-port'};
$phone = 'N/A';
?>
<?php
echo '<tr>',
'<td class="blockcontentwhite sessionicon">',
'<td class="blockcontentwhite center">' . ("$ip") . '</td>',
'<td class="blockcontentwhite center">' . ("$nas") . '</td>',
'<td class="blockcontentwhite center">' . ("$port") . '</td>',
'<td class="blockcontentwhite center">' . ("$phone") . '</td>',
'</tr>';
endforeach;
?>
so basically within a table on 1st row its showing the $ip , $nas, $port and $phone BUT it is not showing the second values on the 2nd row , any ideas?
Thanks Guys
You are trying to access an object as an array. $info->sessions is of type SimpleXMLElement Object which contains (as a property) the array you want to use in the foreach.
Change your foreach ( foreach ($info->sessions as $sessions): ) to foreach ($info->sessions->session as $sessions): and:
$count = $sessions->{'session-count'};
$ip = $sessions->session->{'ip-address'};
$nas = $sessions->session->{'nas-ip-address'};
$port = $sessions->session->{'nas-port'};
$phone = 'N/A';
to:
$count = $info->sessions->{'session-count'};
$ip = $sessions->{'ip-address'};
$nas = $sessions->{'nas-ip-address'};
$port = $sessions->{'nas-port'};
$phone = 'N/A';
I think Jueecy is correct regarding the question you asked about.
But I noticed something that looks like it might be a problem besides that. It looks like the code will generate multiple tables, but I think you just want multiple rows in the same table.
I would suggest moving your foreach down to where you are generating the rows:
<?php
$info = simplexml_load_file("https://api.website.co.za/ACCESS_GetAccountSessions?");
echo "<ul info>";
?>
<input type="hidden" name="ctl00$ctl00$contentDefault$contentControlPanel$hdnIsSecure" id="ctl00_ctl00_contentDefault_contentControlPanel_hdnIsSecure" value="false" />
<table id="active_sessions_table" class="blocktable centered" cellpadding="0" cellspacing="0">
<tr class="blockheader">
<td id="active_sessions_title" class="left" colspan="2">
<label class="floatleft">Current connections</label></td>
</tr>
<tr id="trconnections">
<td class="blockcellnopadding" colspan="2">
<table cellpadding="4" cellspacing="0" width="100%" style="border-bottom:solid 1px #bfbfbf;">
<tr>
<td class="columntitle center"> </td>
<td class="columntitle center">IP Address</td>
<td class="columntitle center">NAS IP Address</td>
<td class="columntitle center">Line Port</td>
<td class="columntitle center">Telephone Number</td>
</tr>
<?php
foreach ($info->sessions->session as $session) {
$ip = $session->{'ip-address'};
$nas = $session->{'nas-ip-address'};
$port = $session->{'nas-port'};
$phone = 'N/A';
echo '<tr>',
'<td class="blockcontentwhite sessionicon">',
'<td class="blockcontentwhite center">' . ("$ip") . '</td>',
'<td class="blockcontentwhite center">' . ("$nas") . '</td>',
'<td class="blockcontentwhite center">' . ("$port") . '</td>',
'<td class="blockcontentwhite center">' . ("$phone") . '</td>',
'</tr>';
};
?>

PHP: working with tr and td with while()

<table border="0" cellspacing="0" cellpadding="0" >
<tr>
<?php
while ($pF = mysql_fetch_array($stringVisits)) {
?>
<td class="viewercell" style="color: #CCC; font-size: 10px;">
<?php
echo "<a href='profil.php?id=".$BuID."'>";
echo "<img style='margin-right: 5px; width: 61px; height: 80px;'";
if (checkStatus($BuID) == 1) {
echo 'class="onlineBorder" ';
} else {
echo 'class="image-xxsmall-border" ';
}
echo " src='images/profilePhoto/thumbs/";
if (!empty($getByProfile["photo_thumb"])) {
echo $getByProfile["photo_thumb"];
} else {
echo "noPhoto_thumb.jpg";
}
echo "'>";
?>
</a>
</td>
<?php } ?>
</tr>
</table>
This is what i have right now it displays the visiterĀ“s profileimage. Now i would like to have their name under the image too.. but then i need to create another <tr> after this one, and add their name in each <td>. But how can i do that, if i have this while? Should i run another while, to get the names or can i do something smart with this?
Why not just stick the name in the same cell as the image? After you close the image tag, just echo $getByProfile["username"], or whatever?
<table border="0" cellspacing="0" cellpadding="0" >
<tr>
<?php
while($pF = mysql_fetch_array($stringVisits)){
?>
<td class="viewercell" style="color: #CCC; font-size: 10px;">
<?php
echo "<a href='profil.php?id=".$BuID."'>";
echo "<img style='margin-right: 5px; width: 61px; height: 80px;'";
if(checkStatus($BuID) == 1){
echo 'class="onlineBorder" ';
} else {
echo 'class="image-xxsmall-border" ';
}
echo " src='images/profilePhoto/thumbs/";
if(!empty($getByProfile["photo_thumb"])) { echo $getByProfile["photo_thumb"]; }else{
echo "noPhoto_thumb.jpg";
}
echo "'><br/>";
?>
</a>
<br/><?php echo $getByProfile['username']; ?>
</td>
<?php } ?>
</tr>
</table>
Here's a way to do it with minimal changes to your existing code:
<?php
<table border="0" cellspacing="0" cellpadding="0" >
<tr>
<?php
$names = array(); // ADDITION #1
while($pF = mysql_fetch_array($stringVisits)){
$names[] = // ADDITION #2 - ADD NEW USER NAME HERE;
?>
<td class="viewercell" style="color: #CCC; font-size: 10px;">
<?php
echo "<a href='profil.php?id=".$BuID."'>";
echo "<img style='margin-right: 5px; width: 61px; height: 80px;'";
if(checkStatus($BuID) == 1){
echo 'class="onlineBorder" ';
} else {
echo 'class="image-xxsmall-border" ';
}
echo " src='images/profilePhoto/thumbs/";
if(!empty($getByProfile["photo_thumb"])) { echo $getByProfile["photo_thumb"]; }else{
echo "noPhoto_thumb.jpg";
}
echo "'>";
?>
</a>
</td>
<?php }
// ADDITION #3:
echo "</tr>
<tr>";
foreach ($names as $username)
echo "<td class=\"username\"><p>$username</p></td>";
?>
</tr>
</table>
Also note that the way you had your code, the </tr> was inside the while loop.
this might not look nice but it should work why do you want 2 loops?
<?php
echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" >";
while($pF = mysql_fetch_array($stringVisits)){
echo "
<tr>
<td class="viewercell" style="color: #CCC; font-size: 10px;">
<a href='profil.php?id=".$BuID."'> <img /> </a>
</td>
</tr>
<tr><td> " . $getByProfile['username'] . " </td></tr>";
}
echo "</table>";
?>

Categories