floridaflatlander Posted April 22, 2011 Share Posted April 22, 2011 I have database that is a list of categories that I would like to insert into a switch statement. This is the only way that I know how to do it right off hand and of course it doesn't work. I've looked on the internet and found examples but they are slightly over my head as ways to do it. Depending on $group which comes from the url a title an h1 would be assigned. $q = "SELECT * FROM categories"; $r = @mysqli_query ($dbc, $q); switch ($group) { if ($r) { //If $r ran OK while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)){ case '$row['id_cat']': $h1 = '.$row['group']'; break; } } } Thanks S Quote Link to comment https://forums.phpfreaks.com/topic/234437-using-a-database-to-fill-in-a-switch-statement/ Share on other sites More sharing options...
PFMaBiSmAd Posted April 22, 2011 Share Posted April 22, 2011 You wouldn't use a switch/case for this. You would use simple conditional logic to test the value as you loop through the result set - $q = "SELECT * FROM categories"; $r = mysqli_query ($dbc, $q); if($r){ //If $r ran OK while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)){ if($row['id_cat'] == $group){ $h1 = $row['group']; } // the rest of your code in the loop } } Quote Link to comment https://forums.phpfreaks.com/topic/234437-using-a-database-to-fill-in-a-switch-statement/#findComment-1204828 Share on other sites More sharing options...
floridaflatlander Posted April 22, 2011 Author Share Posted April 22, 2011 Thanks That works Quote Link to comment https://forums.phpfreaks.com/topic/234437-using-a-database-to-fill-in-a-switch-statement/#findComment-1204831 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.