MySQL Forums
Forum List  »  General

loop doesnt seem to run
Posted by: michael poxon
Date: November 03, 2010 09:03AM

Hi all,
I have an if-else clause for retrieving data. Below, some clubs teach one style but others teach several:
$sql = "select styletype from styles where club = '$theclub'";
$result= mysql_query($sql);

$thestyle = $row['styletype'];

	while ($row = mysql_fetch_assoc($result))
	$thestyle = $row['styletype'].", ";	
When this runs, there is no problem with those clubs that only teach one style. That gets put into the "$thestyle" variable and is echoed into a text box on a form where the user can manipulate it farther if they wish (for example, by adding another style).

However, what I want is where a club teaches several styles to have these echoed to the text box, but with a comma after each one. But when the 'else' clause runs, only the first style-plus-comma is written to the box. It's as though the 'while' loop is only running once!
Ideally, I also want the last style to lose its comma, so instead of having the text box saying
"karate, kung fu," I want it to say
"karate, kung fu"
(i.e., without the final comma in the second example)
If I try and get round this problem by writing:-
$thestyle = $row['styletype'].", ".$thestyle;
then the full list is indeed echoed to the text box, with commas after every entry, including the last one (i.e., "karate,kung fu,"). Great - getting there!
However, when I try and delete the last character in this string (which I take to be the comma after the last style) what I get is not
karate, kung fu
but instead
karate, kung f

Options: ReplyQuote

Written By
loop doesnt seem to run
November 03, 2010 09:03AM
November 04, 2010 01:29PM
November 04, 2010 06:55PM
November 05, 2010 06:05AM

Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.