How can I print these values (see description) in a table? [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 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];

Related

Foreach Loop in PHP only Once [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 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.

Looking for proper way to inject HTML table into PHP code [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 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>";

Else status in php [closed]

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)

How For Loop is generating dynamic table? [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
My teacher has shown us the following script:
<table border="1" width="600" height="600" cellspacing="0" cellpadding="0">
<?php
$iNew=1;
echo '<tr>';
for ($iCounter=1;$iCounter<=15;$iCounter++)
{
echo '<td>'.$iCounter.'</td>';
if ($iNew==3) {
echo '<tr></tr>';
$iNew=0;
}
$iNew++;
}
echo '</tr>';
?>
</table>
Here, the <tr> tag is inside the <td> tag. In normal HTML this does not work because cell tags must be inside row tags, but when run in the above php, the appropriate number of columns appear. How is this script actually working?
It should be
if ($iNew==3)
{
echo '</tr><tr>';
$iNew=0;
}
$iNew++;
You had TRs reversed, creating a new row and ending it immediately instead of ending the previously created row and then starting a new one!

PHP json_parse trakt API [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 9 years ago.
Improve this question
<?php
$response = file_get_contents("http://api.trakt.tv/shows/trending.json/5d7588c188eeea0074b8d2664d12fffc");
$result = json_decode($response, true);
echo $result['title'][0];
echo "<br>";
echo $result['network'][0];
echo "<br>";
echo $result['air_day'][0];
echo "<br><img style='width:200px;' src='";
echo $result['images'][0]['poster'];
echo "'>";
?>
Ain't working. I don't know why.
I use the the trakt.tv shows API.
Write
echo $result[0]['title'];
instead of
echo $result['title'][0];
Besides, PHP's echo function will print integers and strings, but will fail with array-alike structures. You could use var_dump or var_export instead. Thanks to them, you could scan the structure and you wouldn't ask this question ;)

Categories