How to increment Row inside echo - php

Hello I would like to know if there is away and if this is valid programming to increment the value of array inside echo. Example code is:
for ($x = 1; $x <= $number; $x++) {
echo"<td>".(round(($row['day_1'])/3600))."</td>";
}
Where I would like to display $row['day_1'], $row['day_2'], $row['day_3'] etc. if there is no way this to be achieved, is there a way to increment the same predefined array with the results of $row like
$time_01[0] = $row['day_1'];
$time_01[1] = $row['day_2'];
So after that to loop through the time_01[] array ?

Least amount of changes: Use the $x you already have:
for ($x = 1; $x <= $number; $x++) {
echo"<td>".(round(($row["day_$x"])/3600))."</td>";
}
or
for ($x = 1; $x <= $number; $x++) {
echo"<td>".(round(($row['day_' . $x])/3600))."</td>";
}
or if you want to put it in another array first, for whatever reason:
for ($x = 1; $x <= $number; $x++) {
$time_01[] = $row['day_' . $x];
}
This really is basic stuff by the way. I do recommend that you follow some basic tutorials about variables, expressions, string concatenation and more.

Related

PHPWord addListItem loop corrupts the document

$cInc = json_decode($inc);
$c = count((array)$cInc);
for ($x = 0; $x < $c; $x++)
{
$section->addListItem($cInc[$x]);
}
So I want to loop the array $cInc to a List Item and somehow the loop corrupts the document.
i think you are wrong using count
The count() function returns the number of elements in an array
so you dont need array inside count
just
$cInc = json_decode($inc);
$c = count($cInc);
for ($x = 0; $x < $c; $x++)
{
$section->addListItem($cInc[$x]);
}
or u can use foreach if you dont know how many array that you have
$cInc = json_decode($inc);
foreach ($cInc as $val)
{
$section->addListItem($val);
}

Getting weird error whilst populating an array

What I'm trying to do is transfer the contents of the array difference() to the array tempStore. The error I am getting is 'Fatal error: Can't use function return value in write context in' on line 4 ($tempStore($x) = $difference($x);). What am i doing wrong here and how do i fix it?
$difference = array("","","","","","","");
for ($x = 1; $x <= 6; $x++){
$tempStore($x) = $difference($x);
}
for ($x = 1; $x <= 6; $x++){
echo $tempStore($x);
}
You just need to use [] instead of ()
for ($x = 1; $x <= 6; $x++){
$tempStore[$x] = $difference[$x];
}

How can I get increment value after a specific interval using for loop in PHP?

I want to show my output 0 to 50 after two digits interval but it's produce an infinity loop and therefore, crash my browser. How can I solve my issue?
<?php
for ($x = 0; $x < 50; $x+2) {
echo "The number is: $x <br>";
}
?>
You need to set $x:
<?php
for ($x = 0; $x < 50; $x = $x+2) {
echo "The number is: $x <br>";
}
?>
you could also use $x+=2 as a shorthand instead of $x = $x+2
Change the incremet to $x+=2 like:`
for($x=0; $x<50; $x+=2){
//do any thing... Echo
}
Let's try with do while loop.
$x = 0;
do {
echo "The number is: $x <br />";
$x = $x + 2;
} while($x < 50);

PHP for loop not getting all posted values

I have a HTML Form where i can add rows dynamically, every time a row is added the number in my input field goes up each time
each row there are 4 input fields and each one has the number on the end of the name
on my submit page i have this PHP for loop:
for($x=1;$x<=$_POST["number"];$x++)
{
}
but its only getting one posted value
i have tried changing to:
for($x=0;$x<=$_POST["number"];$x++)
{
}
but this does the same thing
for ($x = 0; $x < count($_POST['number']); $x++)
{
echo "Number: $_POST['number'][$x]";
}
// OR:
foreach ($_POST['number'] as $number)
{
echo "Number: $number";
}
You have to add the count() function and use < instead of <= or else you will get an:
index out of range error.
Try this:
for ($x = 0; $size = count($_POST['number']); $x < size; $x++)
{
echo "Number: $_POST['number'][$x]";
}
Bad practice:
for ($x = 0; $x < count($_POST['number']); $x++)
{
echo "Number: $_POST['number'][$x]";
}
Why? The array size is fetched on every iteration and this will make your code run slower.

SimpleXML Dynamic element names

echo $xml->SLOT1->Effect;
echo $xml->SLOT2->Effect;
echo $xml->SLOT3->Effect;
Is there a way to simplify this by using a for loop? I tried this but it echos nothing:
for ($x = 1; $x <= 3; $x++) {
echo $xml->SLOT[$x]->Effect;
}
You can use
$xml->{"SLOT".$x}->Effect;
for ($x = 1; $x <= 3; $x++) {
echo $xml->{SLOT.$x}->Effect;
}

Categories