PHP/MySQL array help need - php

<?php
include 'db.php';
$i=0;
$result15=mysql_query("select c.dishes from c");
while($row=mysql_fetch_array($result15))
{
if($row['dishes']!=NULL)
{
$dish[$i]=$row['dishes'];
$i++;
}
}
mysql_close();
$j=0;
while($j<=$i)
{
echo $dish[$j];
$j++;
}
?>
Getting notice: Undefined offset: 2 in F:\xampp\htdocs....on line 18

You mean while($j<$i) there.
Remember, you incremented $i after the last insert. This means that $i will be higher than the maximum key of $dish.
Some thoughts:
Any time you're testing for equality with null, you should consider using is_null (or !is_null). It is more accurate.
This:
$dish[$i]=$row['dishes'];
$i++;
Would be better as:
// obviously instead of $i you would use count($dish) later (or use foreach)
$dish[]=$row['dishes'];
That final while loop would be better as a foreach:
foreach($dish as $val)
{
echo $val;
}

You need to make sure the index exists before echoing it, as so:
while($j<=$i)
{
if (isset($dish[$j])) echo $dish[$j];
$j++;
}

Your second while loop is going to far. You only define dishes up to i-1 and then you loop through it up to and including i. Change it to:
while($j<$i)

Related

Else break deletes variable

I am going through a series of values checking if they are the same or different, I want to how many values at the top of the list are the same.
$counter=0;
while ($value<$numberOfValues){
if($valueA == $valueB){
$counter++;
}else{
break;
}
}
echo $counter;
Why does $counter always equals 0 after the break?
Thanks in advance!
Because value and numberOfValues should have a $ in front of them ($value and $numberOfValues), otherwise the while() loop will not run at all and $counter will still be 0.

php for loop for an array that takes values from function

I have a function which is returning some values. I want to put those values in an array after checking if the current value exists. I 've written the following code:
$return[0]=myexec_proc($varsearch,$get_input1);
if (isset($return[0])){
$return[1]=myexec_proc($varsearch,$return[0]);
}
if (isset($return[1])){
$return[2]=myexec_proc($varsearch,$return[1]);
}
if (isset($return[2])){
$return[3]=myexec_proc($varsearch,$return[2]);
}
if (isset($return[3])){
$return[4]=myexec_proc($varsearch,$return[3]);
}
which works as I want to but I need to do it with a for loop.
I've tried this:
$return=array();
for($i=0; $i=3; $i++){
if (isset($return[$i])){
$return[$i+1]=myexec_proc($varsearch,$return[$i]);
}}
but I get no data and after a while I get a php fatal error "Maximum execution time of 30 seconds exceeded".
Any tips on what I am doing wrong would be appreciated.
The second condition of your for loop is incorrect. You are assigning $i to 3 instead of checking it as a conditional.
It should be something like this:
for($i=0; $i<=3; $i++){
if (isset($return[$i])){
$return[$i+1]=myexec_proc($varsearch,$return[$i]);
}
}
For loops require a condition which they loop up until. Add a less than operator to make the for run correctly.
for ($i=0; $i<=3; $i++) {
if (isset($return[$i])) {
$return[$i+1]=myexec_proc($varsearch,$return[$i]);
}
}

Why This Loop Always Produce N+1 Output?

Let's say I have this inputs :
<input type="hidden" name="block-1" value="001"/>
<input type="hidden" name="block-2" value="012"/>
<input type="hidden" name="block-3" value="002"/>
<input type="hidden" name="block-4" value="005"/>
<input type="hidden" name="block-5" value="008"/>
and I want to process those input using this PHP loop
$i = 1;
do {
$x = 'block-'.$i;
$webBlock = $_POST[$x];
//some codes here
$i++;
}
while (!empty($webBlock));
why I always have 6 outputs? and the last one is blank output. seems that loop always doing n+1. how to make correct loop based on number of inputs given? thanks!
Do you need while?
I'd go with:
$i=0;
foreach($_POST as $name => $value)
{
if( strpos($name , 'block-') !== false ) echo $i . " - " . $name . ": " . $value;
$i++;
}
Believe that should account for items named 'block-n'. The if statement basically says "if block- is anywhere in the name of the field, echo out such and such".
Let me know if you get an error and will amend.
Because you are using a repeat loop, you should use a while loop:
while (!empty($webBlock)){
$x = 'block-'.$i;
$webBlock = $_POST[$x];
//some codes here
$i++;
}
beacause do will get executed at least once what ever may be in while expression .do while is an exit control loop.
Try this:
$i = 0;
do {
$i++;
$x = 'block-'.$i;
$webBlock = $_POST[$x];
//some codes here
}
while (!empty($webBlock));
UPD: The best approach is this:
for ($i = 1; $i <= count($_POST); $i++) {
$webBlock = $_POST['block-'.$i];
//some code here
}
Because You are using Do While loop.I am not sure about your data but i must say that it will execute one more time as you expected.Let us check it by iterating
It will run first time because $webBlock is not empty and same as for 5 times.Now here you want to close the loop but your 5th iteration's condition gets true.Now it will execute once more and show nothing (because there is nothing in $webBlock)and now condition will find that $webBlock is empty.I suggest you to use while loop here.It will solve your issue
I am not a PHP developer so I will just give you general idea about what you are doing.
Actually you are using Do-while loop which is sometimes called Exit Control loop. So, in case of Do-While after executing code your condition will be checked. So, even you didn't get any value in 'webBlock' your code will be executed. So, this is your bug.
Instead of this you can use While loop, Entry control loop. You code is executed if and only if condition is true.
$i = 1;
while (i>0) {
$x = 'block-'.$i;
$webBlock = $_POST[$x];
if(empty($webBlock))
{
break;
}
$i++;
}

Unexpected behaviour with PHP array references

I'm using references to alter an array:
foreach($uNewAppointments as &$newAppointment)
{
foreach($appointments as &$appointment)
{
if($appointment == $newAppointment){
$appointment['index'] = $counter;
}
}
$newAppointment['index'] = $counter;
$newAppointments[$counter] = $newAppointment;
$counter++;
}
If I print the array contents, then I receive the expected result. When I iterate over it, all elements seem to be the same (the first).
When I remove the reference operator & in the inner array, all goes normal, except index isn't set.
Using references in foreach loops is asking for trouble :) I've done that several times, and I always rewrote that code.
You should to it as well. Like this:
foreach($uNewAppointments as $newAppointmentKey => $newAppointment)
{
foreach($appointments as $appointmentKey => $appointment)
{
if($appointment == $newAppointment){
appointments[$appointmentKey]['index'] = $counter;
}
}
$uNewAppointments[$newAppointmentKey]['index'] = $counter;
$$uNewAppointments[$newAppointmentKey][$counter] = $newAppointment;
$counter++;
}
Though I have just rewritten it "mechanically", so it will probably not work. But it's to get the idea of how to achieve the same effect, without the side effects. You are still modifying the original arrays in this loop.
If you do this, you must unset $newAppointment when you exit the loop. Here is the relevant entry.

Display text once within while loop on the first loop

<?php
$i = 0;
while(conditionals...) {
if($i == 0)
print "<p>Show this once</p>";
print "<p>display everytime</p>";
$i++;
}
?>
Would this only show "Show this once" the first time and only that time, and show the "display everytime" as long as the while loop goes thru?
Yes, indeed.
You can also combine the if and the increment, so you won't forget to increment:
if (!$i++) echo "Show once.";
Rather than incrementing it every time the loop runs and wasting useless resource, what you can do is, if the value is 0 for the first time, then print the statement and make the value of the variable as non-zero. Just like a flag. Condition, you are not changing the value of the variable in between the loop somewhere. Something like this:
<?php
$i = 0;
while(conditionals...) {
if($i == 0){
print "<p>Show this once</p>";
$i=1;
}
print "<p>display everytime</p>";
}
?>
Yes, as long as nothing in the loop sets $i back to 0
Yes it will, unless the conditions are false from the start or $i was set to 0 inside the loop

Categories