chocopi Posted May 14, 2007 Share Posted May 14, 2007 I want to get a numerical vaule from my database and change it to a string This is code i came up with, but its not working: while($row = mysql_fetch_array($class)) { if ( $row['class'] == 1){ $class = 'A'; } else if ( $row['class'] == 2){ $class = 'B'; } else if ( $row['class'] == 3){ $class = 'C'; } } print ("You chose $class \n"); Any help would be greatly appreciated, ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/51336-solved-simple-while-problem/ Share on other sites More sharing options...
quickstopman Posted May 14, 2007 Share Posted May 14, 2007 did you previously set $class as a variable? Quote Link to comment https://forums.phpfreaks.com/topic/51336-solved-simple-while-problem/#findComment-252809 Share on other sites More sharing options...
pocobueno1388 Posted May 14, 2007 Share Posted May 14, 2007 Since you are using a while loop I am assuming you are trying to print "You chose $class" for each value from the DB? Why do you have your print outside of the while loop? Change your code to this: <?php while($row = mysql_fetch_array($class)) { if ( $row['class'] == 1){ $class = 'A'; } else if ( $row['class'] == 2){ $class = 'B'; } else if ( $row['class'] == 3){ $class = 'C'; } print "You chose". $class ."<br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/51336-solved-simple-while-problem/#findComment-252816 Share on other sites More sharing options...
kenrbnsn Posted May 14, 2007 Share Posted May 14, 2007 You're clobbering the variable $class, which is the pointer to the mysql query result, use a different variable. This can be written much more efficiently by using an array: <?php $classes = array('','A','B','C'); while($row = mysql_fetch_assoc($class)) $cls = $classes[$row['class']]; echo "You chose $cls<br>\n"; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/51336-solved-simple-while-problem/#findComment-252819 Share on other sites More sharing options...
chocopi Posted May 14, 2007 Author Share Posted May 14, 2007 Thanks for your help but that is still only showing 'You chose ' @quickstopman: Yes $class had already been set as that is the column name Quote Link to comment https://forums.phpfreaks.com/topic/51336-solved-simple-while-problem/#findComment-252827 Share on other sites More sharing options...
chocopi Posted May 14, 2007 Author Share Posted May 14, 2007 ok i found the problem and fixed. The mistake was sooooo n00bish i shall not tell you, but thanks for your help everyone. Cheers, ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/51336-solved-simple-while-problem/#findComment-252877 Share on other sites More sharing options...
kenrbnsn Posted May 14, 2007 Share Posted May 14, 2007 Please post the solution, so that if some one else has the same problem, it will save them the time. Ken Quote Link to comment https://forums.phpfreaks.com/topic/51336-solved-simple-while-problem/#findComment-252878 Share on other sites More sharing options...
chocopi Posted May 14, 2007 Author Share Posted May 14, 2007 it was not a problem with the code though, it was because i missed out: <?php $class = mysql_query("SELECT class FROM users WHERE username='$username'"); ?> so obviously no result could be posted because it was not being told where too look Quote Link to comment https://forums.phpfreaks.com/topic/51336-solved-simple-while-problem/#findComment-252880 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.