Closed Thread Icon

Preserved Topic: while (new to php) { $efforts='disaster'; } Pages that link to <a href="https://ozoneasylum.com/backlink?for=21095" title="Pages that link to Preserved Topic: while (new to php) { $efforts=&amp;#039;disaster&amp;#039;; }" rel="nofollow" >Preserved Topic: while (new to php) { $efforts=&#039;disaster&#039;; }\

 
Author Thread
Mormegil
Nervous Wreck (II) Inmate

From: Everywhere and Nowhere
Insane since: Sep 2002

posted posted 09-19-2002 15:27

($while) {
yada yada;
}

when do you use ($while) in order to create a loop. I've tried it with this:

<? $a[0]['price']="2.98<br>";
$a[1]['price']="3.98<br>";
$a[2]['price']="4.98<br>";
$a[3]['price']="5.98<br>";
$a[4]['price']="6.98<br>";
$row=0;
while ($a) {
echo $a[$row]['price'];
$row++;
}
?>
and it gives me this:

Fatal error: Maximum execution time of 30 seconds exceeded in /home/gumpscoo/public_html/test.php on line 8


What's the story?

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 09-19-2002 15:43

Well you are on the right track but when dealing with Arrays it is normally a better idea to use a for loop.

With you chunk of code maybe it would be better to write it as:

code:
<? 
$price[0] = "2.98<br>";
$price[1] = "3.98<br>";
$price[2] = "4.98<br>";
$price[3] = "5.98<br>";
$price[4] = "6.98<br>";

for ($i = 0 ; $i < count($price); $i++)
echo $price[$i];
}
?>



Typically you will not be using a while loop on an array, since is easier to use a for loop than initializing a variable above the while loop and making the incrementor in the loop. The for loop handles it all in the same line which makes it easier to change or view.

A while loop in usually used in a situation which it would be hard to get the length of the input unless you are actually processing it. Such situations would be when reading rows from a database. It would be double the work if you were to get the size via and then print it out in a for loop, instead you could use a while loop which would handle it.

In your example your code would run in a infinate loop, thus the time out on it. Seeing as you are using row as your delimited you would want while($row < count($a))

Php is not a strongly typed language so you would get, with your code:

2.98<br>3.98<br>4.98<br>5.98<br>6.98<br>nullnullnullnullnull... continuing until you get an over flow or in your case a timeout on script processing occures.



[This message has been edited by WarMage (edited 09-19-2002).]

Emperor
Maniac (V) Mad Scientist with Finglongers

From: Cell 53, East Wing
Insane since: Jul 2001

posted posted 09-19-2002 15:43

Mormegil: Welcome. Here is what I'd do:

code:
<?php

$a[0]['price']="2.98<br>";
$a[1]['price']="3.98<br>";
$a[2]['price']="4.98<br>";
$a[3]['price']="5.98<br>";
$a[4]['price']="6.98<br>";

for ($i=0; $i<5; $i++) {
echo $a[$i]['price'];
}
?>



Hope that helps.

[edit: Damn all his talking like a pirate (which I failed to do above) has made me slow arrrrr]

___________________
Emps

FAQs: Emperor

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 09-19-2002 15:44

Bwahahaha I beat you to it Emps, this is a grand day!

But on the same level Emps shows you how to use a key with your input as mine deletes the key and uses the variable name as the key.



[This message has been edited by WarMage (edited 09-19-2002).]

Mormegil
Nervous Wreck (II) Inmate

From: Everywhere and Nowhere
Insane since: Sep 2002

posted posted 09-19-2002 16:07

Ok, what if I were querying a database, though, and wanted it to print for as many arrays as there are in the database? It doesn't seem like that is something that I can hardcode every time I change the amount of available prices that there are. Does that sentence make sense?

I forgot to ask... is count a function that you can use as a value as to how many arrays there are or am I misinterpreting what you said above?



[This message has been edited by Mormegil (edited 09-19-2002).]

Emperor
Maniac (V) Mad Scientist with Finglongers

From: Cell 53, East Wing
Insane since: Jul 2001

posted posted 09-19-2002 16:22

WarMage: You're mean

Mormegil: I'm not sure what you mean - is this what you are after:

code:
$db = mysql_connect("localhost", "USERNAME", "PASSWORD") or die ("Couldn't connect to server");   

mysql_select_db (DB_NAME) or die ("Couldn't select database");

$result = mysql_query ($sql) or die ("Couldn't execute query");

while ($row = mysql_fetch_array($result)) {

OUTPUT RESULTS

$stuff = $row['stuff'];

}



count():
www.php.net/count

is the same as sizeof():
www.php.net/sizeof

and returns the number of elements in an array.

___________________
Emps

FAQs: Emperor

Mormegil
Nervous Wreck (II) Inmate

From: Everywhere and Nowhere
Insane since: Sep 2002

posted posted 09-19-2002 16:28

I think that may work, but I did try while($row < count($a)) and it worked just fine. Thanks for all of your help. But I have one last question: In looking through other peoples code, I have seen while ($somevariable) and I am interested to know when you would want to use a lone variable as an expression. I looked through phpbuilders definition of while and even phpnet, but could find no example of this. Hope I'm not pestering you. That will be the last question, I swear.


bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 09-19-2002 17:39

2 things first a foreach loop's easier than a for loop in php

foreach($array as $elm) {
// Now each element of the array becomes $elm in the loop.
// This is easier than using a for loop with an index
echo $elm;

}


also you can use a while loop but you need to use this method

while ($elm = $array) {
echo $elm;
}

also foreach loops kick ass with associative arrays. like so

$array['name'] = "mike";
$array['color'] = "blue";
$array['age'] = 29;

foreach($array as $key => $value) {
echo $key." - ".$value."<br>";
}

this will output
name - mike
color - blue
age - 29




.:[ Never resist a perfect moment ]:.

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 09-19-2002 18:49

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 09-19-2002 19:59

When you have something that will return a boolean value you might use while($variable)

for exampe:

code:
$variable = true;

while ($variable)
{
//do some computation
$variable = false;
}



That is the basic idea. You have have some function or variable that can return either a true or false value (1 or 0). For these cases you can do the simple while.

Like the example: while($row = @mysql_fetch_array($results))

mysql_fetch_array will return false when there are no more elements. So it is a usable function in that instance.


Mormegil
Nervous Wreck (II) Inmate

From: Everywhere and Nowhere
Insane since: Sep 2002

posted posted 09-19-2002 20:10

Thank you for your help, all. Very good stuff. I quite understand alot more now than when I started, and everything you said made sense to me.

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 09-19-2002 20:50

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

« BackwardsOnwards »

Show Forum Drop Down Menu