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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.