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.
Related
I have a code that generates a table.
while ($i <= 30) {
echo "<tr id=\"Row{$i}\">";
while ($d <= 90) {
$d = $d + 1;
echo '<td id="C' . $i .''. $d . '">'.$i.''.$d.';</td>';
}
$d = 0;
echo '</tr>';
$i = $i + 1;
}
As you can see I've given each table cell a unique ID. Now, I want to be able to target that ID for a certain table-cell further down in the code and say give it a background-color: yellow;
How do I do that? Because If I echo it will create a new table cell instead of hitting hte already existing one. Would like help. Thank you.
You have to set a string variable with id value you need to target for background-color
if you know the row and column number for this specific cell let's say row 3 cell 45 then you can proceed like this
while ($i <= 30) {
echo "<tr id=\"Row{$i}\">";
while ($d <= 90) {
if($i == 3 && $d== 45)
{
$d = $d + 1;
echo '<td class =\'yourclass\' id="C' . $i .''. $d . '">'.$i.''.$d.';</td>';
}
else
{
$d = $d + 1;
echo '<td id="C' . $i .''. $d . '">'.$i.''.$d.';</td>';
}
}
$d = 0;
echo '</tr>';
$i = $i + 1;
}
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.
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>";
}
?>
I have a while/for loop with a foreach loop inside. Reading the code will make it self explanatory as to what I am trying to achieve in terms of logic.
In layman's terms, I am trying to create a calendar in PHP. The foreach statement creates 35 cells, for the calendar interface, 4 weeks, 7 days, 5 x 7 = 35.
The while loop has taken the number of days in April (30) and trying to each the range 1-30 in each cell.
However, when implementing the code below, each number gets iterated 30 times.
$i = 1;
while ($i <= 30){
foreach ($this->cells as $cells){
foreach ($cells as $key => $object){
echo "<td>" . $i . "</td>";
if($object % 7 == 0){
echo "</tr><tr>";
}
}
}
$i++;
}
I tried this:
$i = 1;
while ($i <= 30){
foreach ($this->cells as $cells){
foreach ($cells as $key => $object){
echo "<td>" . $i . "</td>"; $i++;
if($object % 7 == 0){
echo "</tr><tr>";
}
}
}
}
Which instead of outputting 1-30 many times each, it outputs 1-35, when I only want 1-30.
I have tried a for loop with the result it prints many of each number. For loop code:
for ($i = 1; $i <= 30; $i++){
foreach ($this->cells as $cells){
foreach ($cells as $key => $object){
echo "<td>" . $i . "</td>";
if($object % 7 == 0){
echo "</tr><tr>";
}
}
}
}
$this->cells is a range 1-35 in an array
$i=0;
foreach ($this->cells as $cells){
foreach ($cells as $key => $object){
if($i<=30)
echo "<td>" . $i . "</td>";
$i++;
if($object % 7 == 0){
echo "</tr><tr>";
}
}
}
Try this code
$i=0;
while ($i++ < 35){
echo '<td>' . ($i <= 30 ? $i : ' ') . '</td>';
if($i % 7 == 0) {
echo '</tr><tr>';
}
}
I'm not really sure why you need to do two loops, you could do it all with one (unless you do need to loop over $this->cells, in which case ignore this answer :p)