Jump to content

Recommended Posts

Morning,

 

I'm working on a web site using a switch to separate different calls from a MySQL database.  My style looks like this:

 

<?php

switch ($_GET['page']) {
     case 'news':
           echo ("NEWS HERE"); }
           break;
     case 'schedule';
          echo ("SCHEDULE HERE"); }
          break;
     default:
          echo ("MAIN PAGE HERE");
     break;
}

 

What I am trying to do, is specify that if the page is news, there can be an additional variable for id.  I attempted before and after the break; for the news case, to make a new switch, but it isn't taking.  I've looked around online, but can't find any information about this.  Can anyone offer a hand?

Link to comment
https://forums.phpfreaks.com/topic/215782-a-switch-inside-a-switch/
Share on other sites

switch ($_GET['page']) {
     case 'news':
           echo ("NEWS HERE");
           $id2 = "extra id";
           break;
     case 'schedule';
          echo ("SCHEDULE HERE");
          break;
     default:
          echo ("MAIN PAGE HERE");
     break;
}

 

You had some syntax issues there, removed the extra } from it. Also showed how / where you would put the second "id". If you need a better example, you need to provide us with more thorough information and examples.

 

<?php

switch ($_GET['page']) {
     case 'news':
           echo ("NEWS HERE"); }
           break;
     case 'schedule';
          echo ("SCHEDULE HERE"); }
          break;
     default:
          echo ("MAIN PAGE HERE");
     break;
}

 

first... you have 2 incorrect placed }'s in the first and second echo...

 

if I understood correctly your question you want to implement a nested switch ... in that case it could look like this:

<?php

switch ($_GET['page']) {
     case 'news':
           echo ("NEWS HERE");
           switch ($_GET['id']) {
              case 1:
                  echo "ID is 1";
                  break;
              case 2:
                  echo 'ID is 2';
                  break;
              default:
                  echo 'ID is unknown';
                  break;
           }
           break;
     case 'schedule';
          echo ("SCHEDULE HERE"); 
          break;
     default:
          echo ("MAIN PAGE HERE");
     break;
}

I've made attempts to implement this nested switch, but it's not working.  The code works until I add this nested switch.  Am I overlooking something?

 

<?php

switch ($_GET['page']) {
	// NEWS CASE
	case 'news';
		$link = mysql_connect ($host, $user, $pass);
 		 mysql_select_db ($db, $link);
  		$query = "SELECT * from news ORDER BY date DESC LIMIT 5";
  		$result = mysql_db_query ($db, $query, $link);
  		while ($row = mysql_fetch_array($result)) {
		echo ("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
        	<tr>
          		<td colspan=\"2\" class=\"Main-Header\"><a href=\"index.php?page=news&id=".$row['id'] . "\">$row[title]</a></td>
       			 </tr>
        		<tr>
         		 <td colspan=\"2\" class=\"Main-PostInfo\">Posted By $row[author] on $row[date]</td>
        		</tr>
        		<tr>
          		<td colspan=\"2\" class=\"Main-Content\">$row[author]</td>
       		 </tr>
        		<tr>
        		  <td> </td>
        		  <td class=\"Main-Comment\">Comment | (0) Comments</td>
                  
   		      </tr>
     		 </table><br />
		 \n"); 
		switch ($_GET['id']) {
			$link = mysql_connect ($host, $user, $pass);
 		 	mysql_select_db ($db, $link);
  			$query = "SELECT * from news WHERE $id = id";
  			$result = mysql_db_query ($db, $query, $link);
  			while ($row = mysql_fetch_array($result)) {
			$id = $_GET['id'];
			case '$id';
				echo ("NEWS POST $row['id']");
				break; 
			default:
				$link = mysql_connect ($host, $user, $pass);
 		 		mysql_select_db ($db, $link);
  				$query = "SELECT * from news ORDER BY date DESC LIMIT 5";
  				$result = mysql_db_query ($db, $query, $link);
  				while ($row = mysql_fetch_array($result)) {
				echo ("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
        			<tr>
          				<td colspan=\"2\" class=\"Main-Header\"><a href=\"index.php?page=news&id=".$row['id'] . "\">$row[title]</a></td>
       			 		</tr>
        				<tr>
         		 		<td colspan=\"2\" class=\"Main-PostInfo\">Posted By $row[author] on $row[date]</td>
        				</tr>
        				<tr>
          				<td colspan=\"2\" class=\"Main-Content\">$row[author]</td>
       		 		</tr>
        				<tr>
        		 		 <td> </td>
        		  		<td class=\"Main-Comment\">Comment | (0) Comments</td>
                  
   		      		</tr>
     		 		</table><br />
				 \n");
			}
		break;

	case 'about';
		$link = mysql_connect ($host, $user, $pass);
 		 mysql_select_db ($db, $link);
  		$query = "SELECT * from aboutvow";
  		$result = mysql_db_query ($db, $query, $link);
  		while ($row = mysql_fetch_array($result)) {
		echo ("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
        	<tr>
          		<td class=\"Main-Header\">$row[title]</td>
       			 </tr>
        		<tr>
          		<td class=\"Main-Content\">$row[content]</td>
       		 </tr>
        		
     		 </table><br />
		 \n"); }
		break;

	default:
		$link = mysql_connect ($host, $user, $pass);
 		 mysql_select_db ($db, $link);
  		$query = "SELECT * from news ORDER BY date DESC LIMIT 5";
		$result = mysql_db_query ($db, $query, $link);
		while ($row = mysql_fetch_array($result)) {
		echo ("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
        	<tr>
          		<td colspan=\"2\" class=\"Main-Header\"><a href=\"index.php?page=news&id=".$row['id'] . "\">$row[title]</a></td>
       			 </tr>
        		<tr>
         		 <td colspan=\"2\" class=\"Main-PostInfo\">Posted By $row[author] on $row[date]</td>
        		</tr>
        		<tr>
          		<td colspan=\"2\" class=\"Main-Content\">$row[author]</td>
       		 </tr>
        		<tr>
        		  <td> </td>
        		  <td class=\"Main-Comment\"><a href=\"index.php?page=news&id=".$row['id'] . "&action=comment\">Comment</a> | <a href=\"index.php?page=news&id=".$row['id'] . "\"> View Comments</a></td>
                  
   		      </tr>
     		 </table><br />
		 \n"); }
		break;
}

?>

compare this lines that you have :

			switch ($_GET['id']) {
			$link = mysql_connect ($host, $user, $pass);
 		 	mysql_select_db ($db, $link);
  			$query = "SELECT * from news WHERE $id = id";
  			$result = mysql_db_query ($db, $query, $link);
  			while ($row = mysql_fetch_array($result)) {
			$id = $_GET['id'];
			case '$id';
				echo ("NEWS POST $row['id']");
				break; 
			default:

 

with the first switch in your code and try to catch the differences... if not... read about the switch proper format here

http://php.net/manual/en/control-structures.switch.php

The "case" have to go directly inside the switch.  You can't have code inbetween the switch and the case, and that especially applies to "while" loops.  Also the syntax for case requires ":", not ";"

 

I recommend you start with writing simple switches, which do something basic like printing out "This is case 4".  Once you have mastered those, try adding more code and building up to something bigger.

Am I overlooking something?

 

The basics of clean and tidy code, perhaps. I don't mean that in any condescending way; it is a valuable skill to learn, to craft code that you (and others) can work with (and that gets the job done).  Why not make life easier, for yourself in the long run, if you stand back and refactor your code into something usable?  There are repeated elements which don't need to be repeated: why use the same code in multiple places to, for example, connect to the database, to issue a query and fetch the results, and even the HTML snippets are fairly repetitive.

 

It would be much nicer if you could get the [samp]switch[/samp] itself down to a similar size and complexity as that in the first post here.  :)

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.