the problem with the listing. nested loops? - php

I am here to open a new line after every 6 from the data that I want to draw the line data.
When the 6-like following code prints the data 36 times.
that this function is checking how many games. If I mention below, but as there are now 36 with 6 printing units. Each one of the prints 6 times.
for($i = 0; $i < $db->oyunSayisi(); $i++)
{
if ($i % 6 == 0)
{
echo "<tr>";
}
?>
<br/>
<?php
foreach($db->oyunCek() as $oyun)
{
?>
<td width="224" height="115"><img height="115;110" src="<?=$db->siteAdres()?>/resimler/<?=$oyun['o_resim']?>" title="<?=$oyun['o_baslik']?> oyna" alt="<?=$oyun['o_baslik']?> oyna" /></td>
<?php
}
if ($i % 6 == 0)
{
echo "</tr>";
}
}

I think I see what may be causing the problem.
For every sixth item returned from oyunSayisi(), you want to create a table row that shows the data from oyunCek(). The problem is that the first modulus just outputs <tr>, then every line runs the foreach loop. Finally, the second modulus outputs </tr>. I think you want to combine everything into just one modulus, like this:
for($i = 0; $i < $db->oyunSayisi(); $i++)
{
if ($i % 6 == 0)
{
echo "<tr><br/>\n";
foreach($db->oyunCek() as $oyun)
{
?>
<td width="224" height="115"><img height="115;110" src="<?=$db->siteAdres()?>/resimler/<?=$oyun['o_resim']?>" title="<?=$oyun['o_baslik']?> oyna" alt="<?=$oyun['o_baslik']?> oyna" /></td>
<?php
}
echo "</tr>\n";
}
}
EDIT:
Upon further pondering, it did not make sense that you would want to only echo every sixth line of data... so it occurred to me that you are probably trying to create a new table row every sixth line, rather than skipping any of the inner foreach loops. Here is the modified code to do that:
echo "<tr>\n";
for($i = 0; $i < $db->oyunSayisi(); $i++)
{
foreach($db->oyunCek() as $oyun)
{
?>
<td width="224" height="115"><img height="115;110" src="<?=$db->siteAdres()?>/resimler/<?=$oyun['o_resim']?>" title="<?=$oyun['o_baslik']?> oyna" alt="<?=$oyun['o_baslik']?> oyna" /></td>
<?php
}
if (($i + 1) % 6 == 0)
{
echo "</tr>\n<tr>\n";
}
}

Related

Number counter game

Im trying to make a random number counter game that uses a for loop to print out 6 random numbers 1 - 6. I want to make it so the code can say how many times the number 6 shows in the loop.
At the moment I have the code it prints out for a loop of 6 random numbers but it only counts the numbers printed out.
For example
Welcome to the Dice Game!
How many sixes will you roll?
4 2 4 6 4 6
You rolled 2 six(es)!
<?php
echo"<h1>Welcome to the guess game thing guess how many 6s!</h1>";
$counter = 0;
for ($i=0; $i <=6;$i++) {
$randomNum = rand(1,6);
if ($randomNum <= 6) {
echo "<br> $randomNum";
$counter++;
}
else
{
echo"$randomNum <br>";
}
}
echo"<br>You rolled $counter sixes";
Some minor changes but you were almost there. Being consistent with your line breaks and verifying you check specifically for 6
$numberToMatch = 6;
for ($i = 0; $i <= 6; $i++) {
$randomNum = rand(1,6);
if ($randomNum == $numberToMatch) {
$counter++;
}
echo "$randomNum <br>";
}
You can do it like this:
$num = $_POST["num"];
for ($i=0; $i <=100;$i++) {
$randomNum = rand(1,10);
if ($randomNum == $num) {
echo $randomNum;
break;
}
echo $randomNum;
}
echo"<h2> there are $i</h2>";

How to split a PHP loop in 2 parts?

I have a weather json from where I get a wether forecast for 8 days. My problem is that I want to show the result in a table of 2 rows of 4 days each.
I managed to do it but in a very odd way that was pure begginer's luck during a trial-error-trial attempt :-) I don't even understand quite well why it is splitting the result in 2 rows... but it works!!! The only thing I could not do was to include a "hr" line between the 2 rows, to make the table easier to read.
You can see the result here http://www.meteocaldas.com/previsao_ds.php
With current code I am displaying each day forecasted values inside the same "td" in different lines using "br". I have been reading that it is not correct to use "br" inside "td" so I am not sure that I am doing the righ thing. Wouldn't it be better to use a table with 4 columns (one for each day) and have different rows for each of the values?
Is there any way to rewrite this code to make it more efficient and look less "childish"? :-) Thanks in advance!
<?php
(...)
$decoded = json_decode($rawData, true);
?>
<table>
<?php for($k=0;$k<8;$k++){
$dailyvalue = $decoded['daily']['data'][$k];
$dailyTime = $dailyvalue['time'];
$dailyIcon = $dailyvalue['icon'];
$dailyTempMax = round($dailyvalue['temperatureMax'],0);
$dailyTempMin = round($dailyvalue['temperatureMin'],0);
(...)
?>
<!-- table for 8 day's forecast (2 rows/4 days each) -->
<td>
<?php echo strftime("%a %d",$dailyTime) ?>
<br>
<?php echo '<img src="path/'.$dailyIcon.'.png">' ?>
<br>
<?php echo $dailyTempMin.'º' ?> </span>
<br>
<?php echo $dailyTempMax.'º' ?></span>
<br>
(...)
<?php if ($k == 3) {
echo '</td></tr>';
} ?>
<?php
}
?>
</td></tr><table>
As per your comment-request, here's some sample-pseudo code, based on your code above:
<?php
(...)
$decoded = json_decode($rawData, true);
for($k=0;$k<8;$k++){
$dailyvalue = $decoded['daily']['data'][$k];
$dailyTime[$k] = $dailyvalue['time'];
$dailyIcon[$k] = $dailyvalue['icon'];
$dailyTempMax[$k] = round($dailyvalue['temperatureMax'],0);
$dailyTempMin[$k] = round($dailyvalue['temperatureMin'],0);
}
?>
<table>
<?php
echo "<tr>";
for($k = 0; $k <= 3; $k++){
echo "<td>".strftime("%a %d",$dailyTime[$k])."</td>"; // this will make the "datetime" row
}
echo "</tr><tr>";
for($k = 0; $k <= 3; $k++){
echo "<td><img src=path/".$dailyIcon[$k].".png></td>"; // this will make the "icon" row
}
echo "</tr><tr>";
for($k = 0; $k <= 3; $k++){
echo "<td>".$dailyTempMin[$k]."º</td>"; // this will make the "MinTemp" row
}
echo "</tr><tr>";
for($k = 0; $k <= 3; $k++){
echo "<td>".$dailyTempMax[$k]."º</td>"; // this will make the "MaxTemp" row
}
echo "</tr>";
// put stuff you want between the tables here
echo "<tr>";
for($k = 4; $k <= 7; $k++){
// proceed to do the same as above
Mind you, there are further ways you can reduce the screen clutter (like moving the table-drawing for loops into a function), but this is the general gist of it
Why don't you simply stack two different table on each other ?

Is my second for loop able to iterate using the increment from my first loop?

What I want is the first loop iterating from 1 to 4 and the second loop from 5 to 6.
Here is my code:
<?php
for ($i = 1 ; $i <= 4 ; $i++)
{
echo $i . "<br>";
}
?>
<hr>
<?php
for ($i = 1 ; $i <= 2 ; $i++)
{
echo $i . "<br>";
}
?>
The loops you've given are:
1st loop: from 1 to 4
2nd loop: from 1 to 2
First loop is ok, but seconds needs to be modified. Use $i<=6 and don't initialize $i variable.
This will give you:
1st loop: from 1 to 4
2nd loop: from (value that 1st loop have ended)+1 to 6, so (4+1) to 6, 5 to 6
<?php
$i = 0; // be sure 'i' is visible in both loops
for ($i=1; $i<=4; $i++) // form 1 to 4
{
echo $i . "<br>";
}
?>
<hr>
<?php
$i++; // start from 5, not 4
for (; $i<=6; $i++) // from the previous value to 6
{
echo $i . "<br>";
}
?>
Your problem
The second for loop resets your $i variable to 1:
for ($i = 1 ; $i <= 2 ; $i++)
Solution
You can use a while loop instead of your second for loop:
<?php
for ($i = 1; $i <= 4; $i++)
{
echo $i . "<br>";
}
?>
<hr>
<?php
while ($i <= 6) // `<= 6` instead of `<= 2`, since we keep $1's value
{
echo $i . "<br>";
$i++;
}
?>
Rather than using two loops for this, why not just output the <hr> tag at the appropriate point within the same one? If you carry on with adding extra loops, first of all you'll run into confusing problems like this about (re-)initialising variables, and you'll also quickly end up with a lot of unnecessary duplicated code.
You can use the PHP modulo operator (%) to output the <hr> tag after every fourth element, which will both reduce the complexity and be a lot more extensible if you later add more elements:
for ($i=1; $i<=6; $i++) {
echo $i . "<br>";
if ($i % 4 === 0) {
echo "<hr>";
}
}
See https://eval.in/976102

creating a table from 1 to 100 in php nested in html

This is a practice question I am having a problem with. So far, what I got to create is a table using the following code. I need help to fix it, with explanation on what my mistakes are.
What I am trying to to do is pretty simple: generating numbers from 1 to 100 in a table using php nested in an html code
Each 10 numbers should be in an aligned row, then break after 10
| 1 | 2 | . . . | 10 |
|11 | 12| .... | 20 |
|21 | 22| .... | 30 |
.
.
.
|91| 92 | ... |100|
<table border="1">
<?php
for ($x=1; $x <= 100; $x++){
if($x%11 ==0) {
echo "<tr><td>" . "<br>" . "</tr></td>";
}
echo "<tc><td>" . $x . "</td></tc>" ;
}
?>
</table>
Also using a for statement for the loop with if statement nested in it for the break the row
Could someone point out my mistakes in this code please.
all right the correct code should be like this:
<table border="1">
<?php
for ($x=1; $x <= 100; $x++){
if($x%10 ==1) {
echo "<tr>";
}
echo "<td>" . $x . "</td>" ;
}
?>
</table>
Thank you all for the help
We need to first understand the requirement.
Requirement:
Need incremental counter horizontally.
Every row should contain a whole 10 numbers starting from say 1 to 10, 11 to 20 etc.
So, if we consider the output as HTML table, <tr> increments by 11 and <td> increments by 1.
So, we need two loops: once incrementing by 10: the outer loop and other incrementing by 1: the inner loop.
So, write outer loop and inside it, add inner loop, both have step: 1
So, outer loop will increment the counter and inside it, inner loop will also run.
For every outer loop, the inner loop will run 10 times.
Thus, we get exact rows: 1-10, 11-20
Try this:
<table>
<?php
$k=0;
for ($i=0; $i<10; $i++) {
?>
<tr>
<?php
for ($j=0; $j<10; $j++) {
++$k;
?>
<td><?php print $k;?></td>
<?php
}
?>
</tr>
<?php
}
?>
</table>
Try this:
<?php
echo "<table border =\"1\" style='border-collapse: collapse'>";
for ($row=1; $row <= 10; $row++) {
echo "<tr> \n";
for ($col=1; $col <= 10; $col++) {
$p = $col * $row;
echo "<td>$p</td> \n";
}
echo "</tr>";
}
echo "</table>";
?>
<table>
<?php
for ($x=1; $x <= 100; $x++)
{
if($x%10 ==1)
**{
echo "<tr><td>" . "<br>" . "</tr></td>";**
}
echo "<tc><td>" . $x . "</td></tc>" ;
}
?>
</table>

increment a variable based on loop count

I have a PHP function where an undefined number of images in a directory are being output to the browser. The images are being read in to an object. The issue I'm having is how the images are presented. I want to put them in a table, four <td> cells in a row. Here is the code:
function displayThumbList(){
$tlist = $this->getThumbList();
$i = 0;
$e = 3;
echo '<table width="400px" border=1><tr>';
foreach ($tlist as $value) {
echo "<td width=\"90px\" height=\"50px\"><img class=\"timg\" src=\"thumbnail/".$value."\" alt=\"a\" /></td>";
$_GET['imagefocus'] = $this->getBaseName($value,$this->thumbPrefix);
//here is where the condition for adding a <tr> tag is evaluated
if ($i == $e){
echo '<tr>';
}
$i++; //increments by 1 with each foreach loop
}
echo '</table>';
}
The first time $i(third time through foreach loop) is equal to $e, the process adds the as expected. I need $e to increment by 3 AFTER each time the condition is met.
The number of images are undefined. If there are 21 images in the directory, $i would increment 21 times and $e should increment 7 times adding 3 with each increment (3,6,9,12,15 etc).
I guess I'm looking for an increment based on another loop condition (every time equality is reached). Any thoughts?
rwhite35
if ($i == $e){
echo '<tr>';
$e = $e + 3;
}
Alternatively, use modulo, something like
if ($i % 3 == 0)
You want to update $e when $i == $e. That condition is already being used in your if statement. Just add
$e += 3;
and you are done.
if ($i == $e){
echo '<tr>';
$e += 3;
}

Categories