I need to identify a number stored in array - php

I have an array that storing numbers between 1 and 31. The numbers need to be echoed as a select drop down options.
$number = array();
echo '<select id=\'day\'>';
for ($i = 0; $i < 31; $i++)
{
$number[] = $i;
echo '<option value=\'{$number[]}\'>{$number[]}</option>';
}
echo '</select>';

You don't even need to build an array:
for ($i = 1; $i < 32; $i++) echo '<option value="'.$i.'">'.$i.'</option>';

You can use array_fill() to make the array (http://ca.php.net/array_fill)
Or just echo out the numbers as you iterate from 1->31.
Beyond that, wrapping your number in the appropriate HTML for a select box should work. Could you expand on the question?

<?php
print '<select name="foo">';
foreach($number as $n){
print "<option value=\"$n\">$n</option>;
}
print '</select>';
?>
Though as other posters pointed out, why bother building an array when you could just for($i = 1; $i < 32; $i++){ print "<option value=\"$n\">$n</option>"; }?

Related

How I can make pattern Pyramid number php like this

How I can make pyramid number like this
1
212
32123
Code:
<?php
for($i=2; $i<=5; $i++){
for($x=$i; $x>=2; $x--){
echo "$x ";
}
echo "<br/>";
}
for($i=1; $i<=5; $i++){
for($x=1; $x<=$i; $x++){
echo "$x &nbsp";
}
echo "<br/>";
}
You seem to printing numbers in descending order, adding a new line and then go to print numbers in ascending order in another line. This makes it too late to do so since you already lost the current line.
Instead, for every row, go from row number till 1 and print numbers and repeat the same in the same line from 2 till row number and then add a new line like below:
<?php
function printPyramid($rows){
for($i = 1; $i <= $rows; ++$i){
echo str_repeat(" ", $rows - $i);
for($j = $i; $j >= 1; --$j){
echo $j;
}
for($j = 2; $j <= $i; ++$j){
echo $j;
}
echo PHP_EOL; // or echo "<br/>";
}
}
printPyramid(7);
Online Demo (view the output in HTML format by clicking on the eye icon)

how to fix php for loop output displaying 1?

Here is my php code given below when I run it in my browser its display 1 continuously. My question is why its show like this? After execution its displays a fatal error : maximum execution time is exceeded. what is that?
<?php
for ($i=1; $i<=5; $i+1) {
echo $i." ";
}
?>
Give me proper answer. Make me sure that what is the execution time of php code?
TIA
$i+1 does not increment the value of $i. It only adds 1 to what is in $i but it does not assign it back to $i. Your loop does this:
$i = 1
while ($i<=5) {
echo $i." ";
$i+1;
}
$i+1 on it's own doesn't do anything.
You need something like $i = $i + 1. Or for short $i += 1. Or even shorter and better: $i++.
for ($i=1; $i<=5; $i++) {
echo $i . " ";
}
It is because of $i+1 in for loop. This is basically an expression and it produces a result but you never assign this result to $i. Therefore you would rather do something like $i = $i + 1 or, in real life, use incrementation $i++. So the final code will looks like:
for ($i = 1; $i <= 5; $i++) {
echo $i." ";
}
use $i++ not $i+1, $i++ is $i=$i+1
Change your code to
<?php
for ($i=1; $i<=5; $i++) {
echo $i." ";
}
?>
because first you must set value for $i
You need to do
<?php
for( $i = 1; $i <= 5; $i++) {
echo $i." ";
}
?>
Now you just say 1 + 1 but you don't assign it to anything. You could use $i = $i + 1 but it's the same as $i++
<?php
for ($i=1; $i<=5; $i++) {
echo $i." ";
}
?>
in a for loop, every turn you need to increase value of $i. but you forgot to increase value of $i. You wrote $i +1 but it needs to be assigned with new value of $i.
In short, you should change $i +1 to $i = $i +1 or $i ++
the right code:
<?php
for ($i=1; $i<=5; $i = $i+1) {
echo $i." ";
}
?>
You are not changing the value of $i in the loop. Either $i =$i +1 or $i++ instead of $i + 1 will do.
You've problem in printing the line. It should look like
<?php
for ($i=1; $i<=5; $i++) {
echo $i;
}
?>

How to get a total value from a loop result for integers in php. without Javascript of Jquery

how to add a total of the numbers displayed as an integer from a loop. the loop result is not an increment. here is the problem.
<?php
$i = 0;
$num =1;
while($i<=4){
echo $num;
$i++;
}
echo $num;
?>
So the result is something like this.
1 1 1 1
so my problem is how can i total the result which should be 4, and save it in a variable without incrementing. and even more confusing is. how to do the same when the value of $num is dynamic. Thank you very much. in advance
Just make an array then sum them:
<?php
$i = 0;
$num =1;
while($i<=4){
$nums[] = $num;
$i++;
}
echo array_sum($nums); //Outputs 5
?>
This assumes that $num is always numeric.
Alternatively, you could just iterate an output variable, based on the value of $num. Like so:
<?php
$num = 1; // or 2, or 3, or 4 or whatever
$output = 0;
for ($i=0; $i<=4; $i++) {
$output += $num;
}
echo $output;
?>

How to prepend a decremented number to a string in a loop?

I am trying to make a triangular-shaped set of lines of decreasing numbers like this :
5
45
345
2345
12345
I tried this :
for($i=1;$i<=5;$i++)
{
for($j=1;$j<=$i;$j++)
{
echo $j;
}
echo "<br>";
}
But it is printing the low number first and appending increasing numbers like this :
1
12
123
1234
12345
The inner loop needs to count down instead of up.
You can either subtract the outer loop's variable from the limit to get the starting point and count down:
for ($i = 0; $i < 5; $i++)
{
for ($j = 5 - $i; $j > 0; $j--)
{
echo $j;
}
echo "<br>";
}
or change the outer loop to count down from the limit as well.
for ($i = 5; $i >= 1; $i--)
{
for ($j = $i; $j >= 1; $j--)
{
echo $j;
}
echo "<br>";
}
This is pretty straightforward:
$max = 5;
echo "<pre>";
for($line=0; $line<$max; $line++) {
$min_this_line = $max-$line;
for($num = $min_this_line; $num <= $max; $num++) {
echo $num;
}
echo "\n";
}
echo "</pre>";
Output:
5
45
345
2345
12345
I think I would declare the $peak value, then use a for() loop to decrement the counter down to 1, and use implode() and range() to build the respective strings inside the loop.
This isn't going to outperform two for() loops, but for relatively small $peak values, no one is going to notice any performance hit.
Code: (Demo)
$peak = 5;
for ($i = $peak; $i; --$i) {
echo implode(range($i, $peak)) , "\n";
}
or with two loops: (Demo)
Decrement the outer loop and increment the inner loop.
$peak = 5;
for ($i = $peak; $i; --$i) {
for ($n = $i; $n <= $peak; ++$n) {
echo $n;
}
echo "\n";
}
Both output:
5
45
345
2345
12345

php itertation loop in hidden input not iterating

I've got one nagging little bug in this script. I'm going through my cart items and passing them into hidden inputs. The cart_id ($obj->id) is working fine into the value="" but my iteration loop that gives each value a unique name="" (cart_id_1, cart_id_2 etc) is NOT iterating.
<?php
$pass_cart_q = "SELECT c.id FROM carts AS c WHERE c.user_session_id='$sid'";
$result = $mysqli->query($pass_cart_q);
$i = 1;
while ($obj = $result->fetch_object()) {
echo "<input type=\"hidden\" name=\"cart_id_".$i."\" value=\" .$obj->id. \"><br>";
$i = $i++;
}
mysqli_close();?>
Each name field is coming through as cart_id_1
$i=$i++;
That's the problem just do:
$i++
Please replace $i = $i++; with just $i++.
$i = 1;
$i = $i++;
echo $i, "\n"; // 1
$i = 1;
$i = ++$i;
echo $i, "\n"; // 2
$i = 1;
$i++;
echo $i, "\n"; // 2
$i = 1;
++$i;
echo $i, "\n"; // 2
What $i = $i++ will cause it literally this: "make $i equal to $i and then increase it by one", but the $i will still remain the same. To solve this, simply replace $i = $i++; with $i++.
Manual Entry
you are assigning the incremented value to $i variable. and hence it is not able to iterate. instead you should remove that assignment variable $i and it should only be $i++

Categories