Re: [PHP] Problem with a while loop
Richard Kurth wrote: I am having a problem with a while loop if I put in a number with 0's in front of it it converts the number two the first whole number. such as 1 would be 1 02200 would be 2200. Then it starts the count down starting with that number. How can I make it not remove the 0's in front of the number these are needed to search a certain database . The following script would look like this when run 1 2 3 4 I needed it to look like this 1 2 3 $i = $startnumber; echo $i; while ($i <= $stopnumber): echo "" ; echo $i; ++$i; endwhile; ?> Here's the problem. You set the number originally as a string. However, as soon as you tell PHP to add the number, it becomes an integer. It must. You can't add strings. If you need to use the number as a string inside the loop, you will have to convert it back to a string. You can do this with sprintf, as in: $str_num = sprintf("%05.0f",$i); You can use var_dump($varname) to see what kind of data is stored in $varname. Janet -- Janet Valade -- janet.valade.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Problem with a while loop
> The following script would look like this when run > 1 > 2 > 3 > 4 > I needed it to look like this > 1 > 2 > 3 You're going to want to use printf or sprintf to format the numbers. example: printf( "%05d", $i ); this would print out your number, then left fill it will the zeros you want. -Dan Joseph -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] problem with a while loop
From: "John Nichel" <[EMAIL PROTECTED]> > > Same thing, really. It's a little more efficient than keeping a count, but > > not the cause of any of the problems. :) > > > > ---John Holmes... > > Chris! John is picking on me! I thought we agreed that Chris is the one that cannot be trusted?? ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] problem with a while loop
John W. Holmes wrote: Same thing, really. It's a little more efficient than keeping a count, but not the cause of any of the problems. :) ---John Holmes... Chris! John is picking on me! ;) -- John C. Nichel KegWorks.com 716.856.9675 [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] problem with a while loop
From: "John Nichel" <[EMAIL PROTECTED]> > > $i =0; > > > > while ($i < $num_results) { > > > > $row = mysql_fetch_array($result); > > > > $producer = $row["PRODUCE"]; > > > Try changing the above from > > while ($i < $num_results) { > > $row = mysql_fetch_array($result); > > To > > while ( $row = mysql_fetch_array($result) ) { Same thing, really. It's a little more efficient than keeping a count, but not the cause of any of the problems. :) ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] problem with a while loop
From: "Jessica Mays" <[EMAIL PROTECTED]> > I am new to php (and mySQL) and I am working on a php page that links > up to a mySQL database. My current code works up to a point, BUT what > happens is that I try to have a while loop and check a variable with an > inner while loop but it only catches after the first complete cycle. > >while ($i < $num_results) { > > $row = mysql_fetch_array($result); > > $producer = $row["PRODUCE"]; [snip] > while ( $producer == $row["PRODUCE"]) { > > $row = mysql_fetch_array($result); [snip] > ++$i; > } > > ++$i; >} You posted too much code to go through easily. It took me a while to figure out what you were doing. The code above is what it really could have been condensed to... Anyhow, the first time through, you're inner while() loop is always going to execute and keep executing until you pull a new "produce" value from the result set. At that time, the new "produce" row is currently in $row, the inner while() loop fails and control passes back to your outer loop. However, at that time you call mysql_fetch_array() again in your outer loop and you just lost the row that cause the inner loop to teminate. What you want, if I can read right, is to only display the part in your outer while() loop when "produce" changes. $producer = ''; while($row = mysql_fetch_array($result)) { if($row['produce'] != $producer) { //display outer while() loop HTML here $producer = $row['produce']; } //display inner while() loop HTML here. } Hope that helps. ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] problem with a while loop
Jessica Mays wrote: From: [EMAIL PROTECTED] Subject: problem with a while loop Date: May 6, 2004 11:25:28 AM EDT To: [EMAIL PROTECTED] Hey everybody! I am new to php (and mySQL) and I am working on a php page that links up to a mySQL database. My current code works up to a point, BUT what happens is that I try to have a while loop and check a variable with an inner while loop but it only catches after the first complete cycle. Basically, some producers will only call up one row, but you always get 2 at the minimum with this code below. I have tried lots of different things which all lead to the second row endlessly repeating or the page not loading at all. Any ideas? Please email me directly since I am on the digest. Thanks Jessica (Also, I am new to this list and if this email is in the wrong format I apologize.) CODE : $query = "select * from wine where REGION like 'California' order by PRODUCE,DESCRIPT,VINTAGE"; $result = mysql_query($query); $num_results = mysql_num_rows($result); echo "Number of wines found: ".$num_results.""; $i =0; while ($i < $num_results) { $row = mysql_fetch_array($result); $producer = $row["PRODUCE"]; Try changing the above from while ($i < $num_results) { $row = mysql_fetch_array($result); To while ( $row = mysql_fetch_array($result) ) { -- John C. Nichel KegWorks.com 716.856.9675 [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] problem with a while loop
Or you can do something like: $query = "SELECT * FROM members Where Company LIKE '%$search1%'"; $result = mysql_db_query($dbName, $query); $numRows = mysql_num_rows($result); for ($i=0; $i<$numRows; $i++) { $row = mysql_fetch_array($result); $email[] = $row[email]; } OR while ($row = mysql_fetch_array($result)) { $email[] = $row[email]; } This just puts all your emails into an array. You can do whatever you want within either loop and it'll do it to every element. It's pretty limitless, or like I did you can stick everything you want into an array to use later.. whatever your fancy :) The reason you only get one email when you echo it outside the loop is that mysql_fetch_array only grabs one row, so you loop through and if you echo inside the loop you'll see every row outputted, while outside the loop you'll only get the last row outputted. (not sure if outputted is a word :) Anyways so now that you understand it a little more, you can do whatever you want to each element in the row inside the loop... I hope that helps! Rick > your while loop is working fine -- it's just that you're re-setting the value > of $email every time it loops through. > > I'm not sure what you're trying to accomplish, but you might try something > like: > > $query = "SELECT * FROM members Where Company LIKE '%$search1%'"; > $result=mysql_db_query($dbName,$query); > while ($row = mysql_fetch_array($result)) { > $email[$row['lname']] = $row["E_mail_1"] > } //untested code -- probably missing some quotes somewhere. > > > that will give you an array with lname as the key and e_mail_1 as your value. > You can then loop through it and format it however you want. > > hth > > --kurt > > On Friday 26 October 2001 00:10, Richard Kurth wrote: >> I am trying to get the data out of the while loop if I echo $email >> inside the } it gives me all of the data but if I echo it out side of >> the loop it only gives me one record even though I know there is >> more. How can I get this to work >> >> $query = "SELECT * FROM members Where Company LIKE '%$search1%'"; >> $result=mysql_db_query($dbName,$query); >> while ($row = mysql_fetch_array($result)) { >> $email= $row["lname"] . " " . "<" . $row["E_mail_1"] . ">" . ";"; >> } >> >> >> echo $email; > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > To contact the list administrators, e-mail: [EMAIL PROTECTED] > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] problem with a while loop
On Fri, 26 Oct 2001 16:40, Richard Kurth wrote: > I am trying to get the data out of the while loop if I echo $email > inside the } it gives me all of the data but if I echo it out side of > the loop it only gives me one record even though I know there is > more. How can I get this to work > > $query = "SELECT * FROM members Where Company LIKE '%$search1%'"; > $result=mysql_db_query($dbName,$query); > while ($row = mysql_fetch_array($result)) { > $email= $row["lname"] . " " . "<" . $row["E_mail_1"] . ">" . ";"; > } > > > echo $email; You would probably want to append the values to $email in your loop, then. As it is, you are overwriting the value in $email each pass through the loop. -- David Robley Techno-JoaT, Web Maintainer, Mail List Admin, etc CENTRE FOR INJURY STUDIES Flinders University, SOUTH AUSTRALIA I'm fascinated by the way memory diffuses fact. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] problem with a while loop
your while loop is working fine -- it's just that you're re-setting the value of $email every time it loops through. I'm not sure what you're trying to accomplish, but you might try something like: $query = "SELECT * FROM members Where Company LIKE '%$search1%'"; $result=mysql_db_query($dbName,$query); while ($row = mysql_fetch_array($result)) { $email[$row['lname']] = $row["E_mail_1"] } //untested code -- probably missing some quotes somewhere. that will give you an array with lname as the key and e_mail_1 as your value. You can then loop through it and format it however you want. hth --kurt On Friday 26 October 2001 00:10, Richard Kurth wrote: > I am trying to get the data out of the while loop if I echo $email > inside the } it gives me all of the data but if I echo it out side of > the loop it only gives me one record even though I know there is > more. How can I get this to work > > $query = "SELECT * FROM members Where Company LIKE '%$search1%'"; > $result=mysql_db_query($dbName,$query); > while ($row = mysql_fetch_array($result)) { > $email= $row["lname"] . " " . "<" . $row["E_mail_1"] . ">" . ";"; > } > > > echo $email; -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] problem with a while loop
What happens there is this: $email = "1. row"; $email = "2. row"; $email = "3. row"; echo $email; You really need to put echo $email inside the loop or use '.=' to set up values to $email. Niklas -Original Message- From: Richard Kurth [mailto:[EMAIL PROTECTED]] Sent: 26. lokakuuta 2001 10:11 To: php Subject: [PHP] problem with a while loop I am trying to get the data out of the while loop if I echo $email inside the } it gives me all of the data but if I echo it out side of the loop it only gives me one record even though I know there is more. How can I get this to work $query = "SELECT * FROM members Where Company LIKE '%$search1%'"; $result=mysql_db_query($dbName,$query); while ($row = mysql_fetch_array($result)) { $email= $row["lname"] . " " . "<" . $row["E_mail_1"] . ">" . ";"; } echo $email; -- Best regards, Richard mailto:[EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]