For some reason this loop only does one iteration and makes only 1 row. Can anyone spot an error?
for($i = 0; $i < 7; $i++){
echo "<tr>";
echo "<td>" . ($min + ($i * 5)) . "</td>";
for($i = 1; $i < 6; $i++){
echo "<td>" . $min . "</td>";
}
echo "</tr>";
}
You are using same variable in both loops ($i). Use another variable in second loop.
Related
I'm try to create a simple calendar and replace the hour by the event in the database, but only the first event show up, the other one are not display. I use a fetch_all and a loop to go through and convert the date to match the one in the table. When I do a var_dump after the loop, all the item appear, but when I do it in my if conditions only the first one is shown.
My code:
<tbody>
<?php
for ($b = 0; isset($row[$b]); $b++) {
$event = date('d M Y H:i', strtotime($row[$b][1]));
while ($hour <= 19) {
echo "<tr>";
for ($j = 0; $j <= 0; $j++) {
$time1 = $hour . ":00";
echo "<td>" . $time1 . "</td>";
}
for ($k = 0; isset($five_days[$k]); $k++) {
$date_match = $five_days[$k].' '.$time1;
if ($date_match === $event) {
echo "<td>" . $row[$b][0]."</td>";
} else {
echo "<td>" . $time1 . "</td>";
}
}
for ($j = 5; $j <= 6; $j++) {
echo "<td>" . "Indisponible" . "</td>";
}
echo "</tr>";
$hour++;
}
}
?>
</tbody>
I try to move the for loop who give me trouble, and when I do var_dump I get an array with a first date, another the first and second date, and a third one with all 3 date, but they all have the same a 0 index. Someone advised me to use DateTime instead of Date/strtotime, but it's a bit complicated to use for me.
I have a "standings" table which ranks teams from 1-42 through a mysql query. What I am trying to do is retrieve the rank from the first page to display on each team's page via php. I was able to get this to work using a second GET method through the team.php link, but it just doesn't seem right having the rank in the url along with the team id.
Here is my current code on the standings page:
for($i = 0; $i < $rows; $i++) {
$result->data_seek($i);
$row = $result->fetch_array(MYSQLI_NUM);
$rank = $row[12];
$teamid = $row[0];
echo "<tr>";
echo "<td class='rank'>" . $rank . "</td>";
echo "<td class='left'><a href='team.php?team_id=$teamid&rank=$rank'>$row[1]</a></td>";
for($j = 2; $j < 11; $j++) echo "<td>$row[$j]</td>";
echo "</tr>";
}
And then the individual team's page:
$teamid = $_GET['team_id'];
$rank = $_GET["rank"];
I then just echo the $rank variable to display that team's rank, which works fine. However, is there a better way to retrieve the rank variable without sending it through the href? So I could change this:
echo "<td class='left'><a href='team.php?team_id=$teamid&rank=$rank'>$row[1]</a></td>";
Back to:
echo "<td class='left'><a href='team.php?team_id=$teamid'>$row[1]</a></td>";
While retrieving the rank in a different way?
I should note I also attempted to use a SESSION variable to retrieve the rank from the first page. However, each user's page displays the rank of '42', as the for loop overwrites the SESSION variable until the last row. As shown below:
session_start();
for($i = 0; $i < $rows; $i++) {
$result->data_seek($i);
$row = $result->fetch_array(MYSQLI_NUM);
$_SESSION["rank"] = $row[12];
$teamid = $row[0];
echo "<tr>";
echo "<td class='rank'>" . $_SESSION["rank"] . "</td>";
echo "<td class='left'><a href='team.php?team_id=$teamid'>$row[1]</a></td>";
for($j = 2; $j < 11; $j++) echo "<td>$row[$j]</td>";
echo "</tr>";
}
Actually, the way you tried is correct.. But, if you're still looking for an alternative, one method would be store each team rank in a separate session variable... something like $_SESSION[$teamid] = $rank;
for($i = 0; $i < $rows; $i++) {
$result->data_seek($i);
$row = $result->fetch_array(MYSQLI_NUM);
$rank = $row[12];
$teamid = $row[0];
$_SESSION[$teamid] = $rank;
echo "<tr>";
echo "<td class='rank'>" . $rank . "</td>";
echo "<td class='left'><a href='team.php?team_id=$teamid'>$row[1]</a></td>";
for($j = 2; $j < 11; $j++) echo "<td>$row[$j]</td>";
echo "</tr>";
}
Then on you individual page you can try something like:
$teamid = $_GET['team_id'];
$rank = $_SESSION[$teamid];
I want to iterate an array of data using foreach loop.
However, I want the first 4 results to display two items per row.Thereafter I want the rest of the data to display I item per row
i found a possible solution here
$data = range(1, 30);
for($count = 0; $count < count($data);)
{
echo "<tr>\n";
for($i = 0; $count < count($data) && $i < 2; $count++, $i++) {
echo "\t<td>$data[$count]</td>\n";
}
for(; $i < 2; $i++) {
echo "\t<td>-</td>\n";
}
echo "</tr>\n";
}
how the problem with this code is that it displays all the data in rows of two.i however only want the first 4 results to display that way.
You can do something like this:
$data = range(1, 30);
for($count = 0; $count < count($data); ++$count){
if($count < 4){
if($count % 2 == 0){
echo "<tr><td>" . $data[$count] . "</td>";
}else{
echo "<td>" . $data[$count] . "</td></tr>";
}
}else{
echo "<tr><td>" . $data[$count] . "</td></tr>";
}
}
Here's the demo
You're using the already incremented $i variable twice.
$i is never < 2
Add this instead
for($j = 0; $j < 2; $j++) {
echo "\t<td>-</td>\n";
}
Test here
Try with -
$data = range(1, 30);
for($count = 0; $count < count($data); $count++)
{
echo "<tr>";
if($count < 2) {
for($i = 1; $i <= 2; $i++) {
echo "<td>" . $data[$count] . "</td>";
if($i == 1) {
echo "</tr><tr>";
}
}
} else {
echo "<td>" . $data[$count] . "</td>";
}
echo "</tr>";
}
What about something like this?
echo '<table border=1>';
$data = range(1, 30);
for($count = 0; $count < count($data); $count++){
echo "<tr>\n";
if($count < 4) {
echo "\t<td>$data[$count]</td>\n";
$count++;
echo "\t<td>$data[$count]</td>\n";
} else {
echo "\t<td colspan=2>$data[$count]</td>\n";
}
echo "</tr>\n";
}
echo '</table>';
Is this possible to do?
i'm looking to build a grid of values, but I was wondering if this is possible
$section_array = array(
$array_1[$i],
$array_2[$i],
$array_3[$i]
);
for ($i = 1; $i <= 31; $i++) {
echo "<tr>";
for ($j = 0; $j < 3; $j++) {
echo "<td>" . $section_array[$j] . "</td>"; // 3
}
echo "</tr>";
}
This is basically the idea, I want to print out 1 value of each section (array_1, array_2) etc
then move to the next day, and print out the values of day 2 and so on. is it possible without having to just list every single one in the 2nd for loop?
Are you looking for something like this? Loop through your array of arrays and print 2 values from the sub array.
$arr = //insert data here
foreach($arr as $subarr) {
echo '<tr><td>' . $subarr['myval1'] . '</td><td>' . $subarr['myval2'] . '</td></tr>';
}
$secname_array = array($val1, $val2, $val3);
$daycount = 31;
for ($i = 1; $i <= $daycount; $i++){
echo"<tr>
<td>$i</td>";
foreach ($secname_array as $v)
{
echo "<td>". $v[$j]. "</td>";
}
$j++;
echo"</tr>";
}
?>
I am trying to create a simple multiplication grid in PHP
It should be of the format for example for a 2x2 grid:
0 1 2
1 1 2
2 2 4
My issue is getting it to start from 0.
This is my nested for loop so far:
for($i=0;$i<=$_POST['rows'];$i++)
{
echo "<tr>";
for($j=0;$j<=$_POST['columns'];$j++)
{
if($i==0)
{
echo "<td>" . 1*$j . "</td>";
}
else
{
$mult = $i * $j;
echo "<td> $mult </td>";
}
}
echo "</tr>";
}
But it gives the output:
0 1 2
0 1 2
0 2 4
I need the column of 0's to be appropriate.
The way you're getting the top row of 0 1 2 3 is by that special-case on the X-axis. Do a similar special-case for the Y-axis ($j):
if ($i == 0) {
... 1 * $j ...
}
else if ($j == 0) {
... $i * 1 ...
}
else {
... $i * $j ...
}
You not only have $i==0 as special case but also $j==0:
if($i==0)
{
echo "<td>" . 1*$j . "</td>";
}
elseif($j==0)
{
echo "<td>" . $i*1 . "</td>";
}
else
{
$mult = $i * $j;
echo "<td> $mult </td>";
}
I don't understand the way you construct your grid. All you need is a row indicator and the number for the multiplication not a nested loop. Second: Why don't you start with 1 instead of catching the case inside the loop. This would be my variant of the multiplication “grid”
<?php
$rows = $_POST['rows'];
$number = $_POST['columns'];
for( $i=1; $i <= $rows; $i++) {
$mult = $i * $number;
echo "<tr>
<td>" . $i.'*'. $j . "</td>
<td>".$mult."</td>
</tr>";
}
?>
This would to a simple grid (x * y) = result. If you want a complete multiplication table it would be something like this:
<?php
$rows = $_POST['rows'];
$number = $_POST['columns'];
echo "<tr><th></th>";
for( $j=1; $j <= $number; $j++) {
echo "<th>".$j."</th>";
}
echo "</tr>";
for( $i=1; $i <= $rows; $i++) {
echo "<tr>";
echo "<th>".$i."</th>";
for( $j=1; $j <= $number; $j++) {
$mult = $i * $j;
echo "<td>".$mult."</td>";
}
echo "</tr>";
}
?>