Jump to content

using switch with php


garry

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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