garry Posted May 9, 2008 Share Posted May 9, 2008 Okay, so I'm trying to write a music review website using php. I want to set it up so that it goes www.mysite.com/artists.php?id=1 for example, and then it would check the id against the database and get the row for the artist id that is 1. I'm trying to do this using a switch and keep getting errors with variables not being defined and such and don't know how to do it. Can someone please have a look at my code and help? <?php add_header(); db_connect(); $query =" SELECT artist_id, artist, description FROM artists WHERE artist_id = '$artistid' "; $result = mysql_query($query); $row = mysql_fetch_array($result); $artistid = ???? switch ($id) { case ($artistid): echo 'artist info'; break; default: echo "Welcome to the artists page!"; break; } ?> I am only very new to php and still learning. Thankyou for any help! Link to comment https://forums.phpfreaks.com/topic/104848-using-switch-with-php/ Share on other sites More sharing options...
Spaceman-Spiff Posted May 9, 2008 Share Posted May 9, 2008 The variable $id has never been defined. Perhaps you can use if, else method instead, it's simpler. <?php add_header(); db_connect(); $query =" SELECT artist_id, artist, description FROM artists WHERE artist_id = '$artistid' "; $result = mysql_query($query); $row = mysql_fetch_array($result); if (!empty($row['artist'])) { echo 'Welcome to the page of ' . $row['artist']; } else { echo 'Artist ID not found'; } ?> Link to comment https://forums.phpfreaks.com/topic/104848-using-switch-with-php/#findComment-537040 Share on other sites More sharing options...
garry Posted May 9, 2008 Author Share Posted May 9, 2008 Thanks for your reply! Unfortunately that wouldn't work as it wouldn't be able to provide the .php?id=x ability. After playing around a bit, I've got it all working except one bit. <?php add_header(); db_connect(); $id = $_GET['id']; $query =" SELECT artist_id, artist, description FROM artists WHERE artist_id = '$id' "; $result = mysql_query($query); $row = mysql_fetch_array($result); switch ($id) { case ($id): echo "<b>Artist:</b> " . $row['artist'] . "<br /><br /><b>Description:</b> <br />" . $row['description']; break; default: echo "Welcome to the artists page!"; break; } ?> Now this code works fine when you navigate to a .php?id= page and will show the relevant information for the artist. However if you just go to the artists.php page (the default case) I get the following page: Notice: Undefined index: id in /Applications/MAMP/htdocs/dev/artists.php on line 12 Artist: Description: For some reason, it is still using the data from the $id case and not using the default case that I put. Can anybody suggest a solution that will let me keep the .php?id= function and also execute different code when the user simply vists artists.php. Thank you! Link to comment https://forums.phpfreaks.com/topic/104848-using-switch-with-php/#findComment-537128 Share on other sites More sharing options...
DarkWater Posted May 9, 2008 Share Posted May 9, 2008 Horrible usage of switch statements depress me. Here: <?php add_header(); db_connect(); if (isset($_GET['id'])) { $id = (int) $_GET['id']; $query =" SELECT artist_id, artist, description FROM artists WHERE artist_id = '$id' "; $result = mysql_query($query); if (mysql_num_row($result) > 0) { $row = mysql_fetch_array($result); echo "<b>Artist:</b> " . $row['artist'] . "<br /><br /><b>Description:</b> <br />" . $row['description']; } } } else { echo "Welcome to the artists page!"; } ?> *sobs* Link to comment https://forums.phpfreaks.com/topic/104848-using-switch-with-php/#findComment-537133 Share on other sites More sharing options...
radar Posted May 9, 2008 Share Posted May 9, 2008 no fear. i use switches for my site as well there is one thing you need to replace.. let me get dreamweaver open and get the code for you.. <-- 2 minutes later --> replace: $id = $_GET['id']; with: $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : ''; That should work.. Link to comment https://forums.phpfreaks.com/topic/104848-using-switch-with-php/#findComment-537141 Share on other sites More sharing options...
DarkWater Posted May 9, 2008 Share Posted May 9, 2008 NO NO NO. RADAR, NEVER USE REQUEST. =/ I absolutely cannot fathom why someone would use REQUEST. My code works, and yours will NOT work at all with his current set up. 1) Read the code next time. 2) Don't use Dreamweaver...write your own code. 3) Don't use $_REQUEST. Link to comment https://forums.phpfreaks.com/topic/104848-using-switch-with-php/#findComment-537145 Share on other sites More sharing options...
radar Posted May 9, 2008 Share Posted May 9, 2008 1) i read the code 2) dreamweaver what? notepad ftw.. 3) $_REQUEST does EVERYTHING i need it to.. $_GET never seems to work for me.. so there ya go... Link to comment https://forums.phpfreaks.com/topic/104848-using-switch-with-php/#findComment-537149 Share on other sites More sharing options...
DarkWater Posted May 9, 2008 Share Posted May 9, 2008 1) Doesn't seem like it. 2) "let me open up dreamweaver" 3) Have fun getting your site effed up by someone who actually knows what they're doing because you thought it'd be a good idea to use $_REQUEST. Link to comment https://forums.phpfreaks.com/topic/104848-using-switch-with-php/#findComment-537151 Share on other sites More sharing options...
radar Posted May 9, 2008 Share Posted May 9, 2008 Dreamweaver was easier than waiting for my very large file to open in notepad... took me 30 seconds as apposed to waiting for notepad to open and parse 18K lines of code... $_REQUEST isn't really that bad. Yeah it can get data contamination from the $_GET, $_POST, and $_COOKIE but that is where print_r comes in handy to verify data.. and if someone wants to screw up my site, let them go ahead and try... The method in which I use $_REQUEST doesn't really matter as it is ONLY used to get data from the address bar for navigation.. For forms i still use the $_POST, and if i feel like using cookies i still use $_COOKIE... $_REQUEST does have drawbacks, but to me, the good that it has, far outweighs the cons. Link to comment https://forums.phpfreaks.com/topic/104848-using-switch-with-php/#findComment-537155 Share on other sites More sharing options...
garry Posted May 9, 2008 Author Share Posted May 9, 2008 Thankyou for your replys everyone. Darkwater, I'm only new at php which is why I tried to use a switch to do what I wanted as I wasn't sure exactly how. I tried your code and the default page now works but when i go to .php?id=1 i get the following error: Fatal error: Call to undefined function mysql_num_row() in /htdocs/dev/artists.php on line 22 Not sure why this is happening? Note - I also removed one of your '}'s from just before the else statement as I was getting a syntax error. Link to comment https://forums.phpfreaks.com/topic/104848-using-switch-with-php/#findComment-537157 Share on other sites More sharing options...
radar Posted May 9, 2008 Share Posted May 9, 2008 mysql_num_rows -- just add the s but switch statements are key.. they help so much.. using if, else statements just take too much crap and leave too much room for error. Link to comment https://forums.phpfreaks.com/topic/104848-using-switch-with-php/#findComment-537158 Share on other sites More sharing options...
garry Posted May 9, 2008 Author Share Posted May 9, 2008 Ah thank you very much for your help! I am very new to PHP and still trying to learn the basics Thanks again! Link to comment https://forums.phpfreaks.com/topic/104848-using-switch-with-php/#findComment-537164 Share on other sites More sharing options...
radar Posted May 9, 2008 Share Posted May 9, 2008 np if you have a question of me let me know.. you can email me straight across @ [email protected] and i'll try and help you out... Link to comment https://forums.phpfreaks.com/topic/104848-using-switch-with-php/#findComment-537166 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.