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);
if(mysql_num_rows($result)==1)
{
$thestyle = $row['styletype'];
}
else
{
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
Help!