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.
Related
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.
I have a loop that creates hexagons 3 in a row, but there is a small bug in the code if 3 are not used per row. To get around this, I have been applying a negative position using conditional code, but it's obviously not going to work on going without adding conditions to each step.
Does anyone know of a way of simplifying the following for future proofing? The condition just checks in increments of 3.
if($i == 3) {
echo ".hex-2 { left: -24.7%;}";
} else if($i == 6) {
echo ".hex-5 { left: -24.7%;}";
} else if($i == 9) {
echo ".hex-8 { left: -24.7%;}";
}
I'd like to do something for this array too, which counts in 2 then 1, then 2 and so on;
if(in_array($i, array(1,3,4,6,7,9,10,12,13,15,16,18,19,21,22,24,25,27,28,30,31))) { echo "</div><div class='hex-wrap'>"; };
Is this possible?
This should do it, though it definitely can be beautified somehow:
$numbers = [];
$n = 1;
$numbers[] = $n;
while ($n < 29) // Or whichever is the allowed maximum number
{
$n += 2;
$numbers[] = $n;
$n += 1;
$numbers[] = $n;
}
And then:
if (in_array($i, numbers)) { echo "</div><div class='hex-wrap'>"; };
The code below basically helps in finding out if a number is a Palindromic Number or not. Although I get my execution done with the output, I just can seem to handle all the "screams" and fatal errors that I get. How do I handle this. Just a beginner and trust you can explain in a way that I may be able to understand..
<?php
for ($num = 1; $num <= 20; ++$num){
$_array1 = str_split($num);
//print_r($_array1);
//echo "<br/>";
$_array2 = array_reverse($_array1);
//print_r($_array2);
//echo "<br/>";
$i = 0;
$j = 0;
while ($i < sizeof($_array1) && $j < sizeof($_array2)){
if ($_array1[$i] == $_array2[$j]){
++$i;
++$j;
}
}
if ($_array1[$i] == $_array2[$j]){
echo "The number $num is a Palindrome Number";
}
}
?>
You get to the size of elements, which is 1. However, if your array has only one element, which is the case for 1-digit numbers, then sizeof($_array) === 1. Which means that the biggest possible index you can use is 0. You need to change your code to something like this:
<?php
for ($num = 1; $num <= 20; ++$num){
$_array1 = str_split($num);
//print_r($_array1);
//echo "<br/>";
$_array2 = array_reverse($_array1);
//print_r($_array2);
//echo "<br/>";
$i = 0;
$j = 0;
$different = false;
while ((!$different) && ($i < sizeof($_array1))){
if ($_array1[$i] == $_array2[$j]){
++$i;
++$j;
} else {
$different = true;
}
}
if (!$different){
echo "The number $num is a Palindrome Number";
}
}
?>
But you are inversing the array without a need to do so and you are looping for unnecessarily long. I propose this function to determine whether an array is a palindrome:
function isPalindrome($input) {
$size = count($input);
for ($index = 0; $index < $size / 2; $index++) {
if ($input[$index] != $input[$size - $index - 1]) {
return false;
}
}
return true;
}
Note, that:
the function assumes that the keys of the array are numbers
the function uses a single array
the size of the array is stored into a local variable to not calculate it repeatedly
the cycle cycles until half of the array, since going beyond that is unnecessary, due to the symmetrical nature of the != operator
the function returns false when the first difference is found, to further optimize the checking
if there were no differences, the function returns true, representing that the input is a palindrome
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);
I would like to optimize the script a little and save cpu some work, so I have to create for loop. In this loop I will work with some data from database and below is my current code:
$result = $this->Model->function();
for ($i = 0; $i < $result->num_rows(); $i++)
{
echo $result->row_array($i)['row'];
}
What do I need here is to check if the next row exists. This one would be on the top of all code in for loop in if statement, but there is some problem. If I type $result->num_rows()+1 in for loop and echo the last row (which doesn't exist) out, the value of it isn't null, but it's same as row before.
How do I check if current row is null?
Or rather than have the boolean checked every time, plus you don't call the num_rows method a second time:
$result = $this->Model->function();
$y = $result->num_rows();
for ($i = 0; $i < $y-1; $i++)
{
echo $result->row_array($i)['row'];
}
echo 'last row';
echo $result->row_array($y-1)['row'];
You can do this for example:
$result = $this->Model->function();
$y = $result->num_rows();
$y--;
for ($i = 0; $i < $result->num_rows(); $i++)
{
if ($i == $y)(
echo 'last row';
)
echo $result->row_array($i)['row'];
}