Okay so I still consider myself fairly new to php but I am making good progress. I have a few questions about the switch statement. I want to pull different cases from a database but I am having trouble getting the syntax right..or maybe this is just not the proper way to do it?? Basically I have a switch statment that analyzes the "$trigger" variable...but I have several different cases that the "$trigger" variable could be...rather than writing each line of code in, I was hoping I could just pull all the different cases from a database. Could someone give me an example or get me headed in the proper direction with perhaps a different way to code the whole situation?
It is tricky. Switch case will not allow loop within switch. You will need to use else if and if else instead of switch that way u can have a loop. Thats the only workaround. However, let me see if someone comes up with a brilliant solution altogether!
I suppose youve got a database with two collums, one with the case, and one with the code? If so, you could just select the right case from the database, right?
Give an example of what you're trying to accomplish with the switch statement. Partial code will help.
Yes that is exactly what I am trying to do, but I haven't gotten it to work yet. What I currently have: switch($trigger){ case 'a': case 'b': case 'c': } PHP: What I would like to do: while ($row = mysqli_fetch_array($data)) { //maybe place the values in an array or something???? switch($trigger){ case 'somehow get all the values from the array or the database to here': } PHP: The reason I want to do this is because I have a backend where I can add a new product type...Well rather than going in and hardcoding in the new product type at all the various points, I am trying to streamline my system so that it just pulls the product types from the database for my switch statments or anywhere eles on the site where I need to access a product code or some other identifiying factor.
Okay okay...so something like this: while ($row = mysqli_fetch_array($data)) { if ($trigger == $row['name']) { //do whatever I usually had the switch statment do } else{ //keep trying } } PHP:
Thanks for the help guys...I have this all figured out. Basically when I first started this website I had all the different cases hard coded in...I eventually got around to making a database to hold the cases. Simple query and the if statement were all I eventually needed..again thanks for pointing me in the right direction.
Oh I told that earlier but, I was 'silently' expecting someone to come up with an extra-ordinary reply. Nevertheless, problem solved, right?
Yeah your way was the way to do it the whole time...and the problem is absolutely solved! Now I enter a new product type in the backend and I dont have to go change five different pages of stuff to get the system to work with the new product type..pretty nice actually.
Are you trying to switch($trigger) { case $dbresult1: case $dbresult2: ? Code (markup): Then why won't you just while ($obj = mysql_fetch_object($res)) { if ($trigger == $obj->fieldname) { echo "you made it?"; } } Code (markup):