Jump to content

Using a database to fill in a switch statement


floridaflatlander

Recommended Posts

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

Link to comment
Share on other sites

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
}
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.