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 2 years ago.
Improve this question
I have a foreach loop but i want level only print once like example below.
https://prnt.sc/wgm877
But when i try foreach loop it print again and again.
https://prnt.sc/wgmazh
<?php foreach($rooms as $room) { ?>
<tr>
<td><?php echo $room->Level; ?></td>
<td><?php echo $room->Type; ?></td>
<td><?php echo $room->Dimension; ?></td>
</tr>
<?php } ?>
I would solve it like this. I.e. "remember" the last room level for each iteration. If it's changed, then print the new room level, otherwise print an empty string:
<?php
$lastlevel = "";
foreach($rooms as $room) {
if ($lastlevel != $room->Level) {
$level = $room->Level;
$lastlevel = $level;
} else {
$level = " ";
}
$nextrow = <<<EOD
<tr>
<td>{$level}</td>
<td>{$room->Type}</td>
<td>{$room->Dimension}</td>
</tr>
EOD;
echo $nextrow;
}
?>
You can either change the structure and do a foreach on the levels instead of the rooms, or use a new variable to know if the level has already been displayed
<? foreach ($actions as $action): ?>
<option value="<?= $action ?>"><?= $action ?>
<? endforeach; ?>
If you want to implement php on the frontend with html, I recommend doing it in the following way.
Related
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 5 years ago.
Improve this question
Let me start by saying that i'm sorry if this is a complete noob question.
while using phpStorm, I have an array of $teams and by invoking foreach loop I tried to put each of the array section into an HTML table
here is what I did so far
I am getting cannot use [] for reading, I can't find an answer how to do that properly, thx in advance.
<table>
<tr>
<th>Team</th>
<th>ID</th>
</tr>
<?php
include dirname(__FILE__) . "/stats.php";
global $teams;
foreach ($teams['data'] as $team) {
echo "<tr>" ;
echo "<td> $team['name']</td>" ;
echo "<td> $team ['id']</td> " ;
echo "</tr>";
}
?>
</table>
To get the string substitution working correct, you have to suround it with {} if it's something more complex than a simple var - especially array access.
echo "<td> {$team['name']}</td>";
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
Maybe this question is really simple but I can't find any answers on internet and I'm a PHP noob.
how can I print this:
[{"1":{"key":"Aansturing","value":"AAN\/UIT"}},{"1":{"key":"Maximale output","value":"6 W"}},{"1":{"key":"Product categorie","value":"Selecteerbare I OUT"}},{"1":{"key":"Output stroom","value":"350 mA \/ 700 mA"}}]
In
<?php
$jsonString = '[{"1":{"key":"Aansturing","value":"AAN\/UIT"}},{"1":{"key":"Maximale output","value":"6 W"}},{"1":{"key":"Product categorie","value":"Selecteerbare I OUT"}},{"1":{"key":"Output stroom","value":"350 mA \/ 700 mA"}}]';
$jsonData = json_decode($jsonString, TRUE);
?>
<table border>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
<?php foreach ($jsonData as $oneRowData): ?>
<?php foreach ($oneRowData as $oneRow): ?>
<tr>
<td><?php echo $oneRow['key'] ?></td>
<td><?php echo $oneRow['value'] ?></td>
</tr>
<?php endforeach; ?>
<?php endforeach; ?>
</table>
By print, I am assuming you want to be able to echo individual items in that JSON dataset. If so, you need to use json_decode
$json = '[{"1":{"key":"Aansturing","value":"AAN\/UIT"}},{"1":{"key":"Maximale output","value":"6 W"}},{"1":{"key":"Product categorie","value":"Selecteerbare I OUT"}},{"1":{"key":"Output stroom","value":"350 mA \/ 700 mA"}}]'
$ar = json_decode($json);
You can then access your elements like this:
echo $ar[0];
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
so i have got this string:
<?php
$elements = "first, second, third, fourth, fifth";
?>
And i'd like it to be echoed out like this:
<ul>
<li>first</li>
<li>second</li>
<li>third</li>
<li>fourth</li>
<li>fifth</li>
</ul>
Try this, it may help you issue;
<?php
$elements = "first, second, third, fourth, fifth";
$numbers = explode(", ", $elements);
echo "<ul>";
foreach ($numbers as $number) {
echo "<li>{$number}</li>";
}
echo "</ul>";
?>
$elements = "first, second, third, fourth, fifth";
$arrayval=explode(',',$elements);
print_r($array);?>
<ul><?php
foreach($arrayval as $value){ ?>
<li><?php echo $value; ?></li>
<?php } ?>
</ul>
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I would need some help, since this code does not work (I have lack of knowledge, but I need to have it in that format)
$user_goals is array()
$user_count is just a number like 15
<?php foreach ($user_goals as $number): ?>
<?php if ($number < $user_count): ?>
<h5 class="text-center"><strike><?php echo htmlspecialchars($number); ?> users</strike></h5>
<?php else: ?>
<h5 class="text-center"><?php echo htmlspecialchars($number); ?> users</h5>
<?php endif; ?>
<?php endforeach; ?>
Any help will be apreciated :)
result that I want:
$user_count = 15;
$user_goals = array(
10,
15,
20,
etc,
);
so I want to get:
<strike>10</strike><br>
<strike>15</strike><br>
20
Solved it myself
<?php
foreach ($user_goals as $number) {
if ($number <= $users_count) {
echo "<h5 class='text-center'><strike>$number users</strike></h5>";
}else{
echo "<h5 class='text-center'>$number users</h5>";
}
}
?>
If you are looking to add a strike through when the goal is less than or equal to, you need to use that comparison operator.
if ($number <= $user_count)
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 so new to PHP, now i am trying to print the not null values. I have following php code which throws me all values including null and not nulls. In my website I need only not null values.
<tr>
<td><?php echo $stock['stsymbol']?><td>
<td><?php echo $stock['noshares']?><td>
<td><?php echo $stock['purchaseprice']?><td>
<td><?php echo $stock['datepurchased']?><td>
<td><?php echo $stock['Original Value']?><td>
<td><?php echo $stock['Current Price']?><td>
<td><?php echo $stock['Current Value']?><td>
</tr>
<?php } // end foreach ?>
change the all td like this example :
<?php if(!is_null($stock['stsymbol'])){ ?>
<td><?php echo $stock['stsymbol']; ?><td>
<?php } ?>
try using this
<td><?php if(!empty($stock['stsymbol'])){ echo $stock['stsymbol']; }?><td>
php has is_null clearly for situation like this. use
if (!is_null($varname)) echo $varname;
The basic pattern you want to follow to conditionally print a value is
<?php
if (!empty($stock['stsymbol'])) {
echo $stock['stsymbol'];
}
?>
This can be shortened by using a ternary if
<?php echo (!empty($stock["stsymbol"]))?$stock["stsymbol"]:"" ?>
UPDATE
Much discussion has gone on in the comments regarding the mechanism used to test for a null value. I thought it might be good to recap the merits of each.
is_null - returns true if the variable is strictly null or undefined (will issue a notice if it is undefined)
empty - returns true if the variable is falsy or undefined (will not issue a notice)
!$val - same as empty but with a notice with undefined values.
You can avoid the problem altogether and use another foreach loop in your current foreach.
It goes like this:
foreach ($stock as $key=>$value) {
echo "<td>$value</td>";
}