Simple Case Switching for Dynamic Content

72dpi

Case switching is a neat way to make your site a bit more dynamic. it gives you the ability to show blocks of information "on the fly".

By attaching the variable name "Content" to our link, we pass that condition in the query string. Note that on the last link, our Default content will show, since we have not passed the variable.

LastMitch commented: Thanks for sharing! +11
761 Views
About the Author

Multimedia Designer

< href="<?php echo $_SERVER['PHP_SELF']; ?>?Content=one">Case One</a><br />
< href="<?php echo $_SERVER['PHP_SELF']; ?>?Content=two">Case Two</a><br />
< href="<?php echo $_SERVER['PHP_SELF']; ?>?Content=two">Case Tthree</a><br />
< href="<?php echo $_SERVER['PHP_SELF']; ?>Main</a>
<br />
<br />

<?php
switch ($Content)
{
  case one:
  echo "This is our First Case";
  break;
  
  case two:
  echo "This is our Second Case";
  break;
  
  case three:
  echo "This is our Third Case";
  break;

  default:
echo"This is our default Content in case nothing was selected!";
  }
  ?>


hope this helps a newbie!
I
Member Avatar
LastMitch

Simple Case Switching for Dynamic Content

I just updated the code a little:

<a href="<?php echo $_SERVER['PHP_SELF']; ?>">Main</a><br />
<a href="<?php echo $_SERVER['PHP_SELF']; ?>?Content=one">Case One</a><br />
<a href="<?php echo $_SERVER['PHP_SELF']; ?>?Content=two">Case Two</a><br />
<a href="<?php echo $_SERVER['PHP_SELF']; ?>?Content=three">Case Three</a><br />
<br />
<br />

<?php

$Content= "one"; // change the case #

switch ($Content){
case "one": 
echo "This is our First Case";
break;
case "two": 
echo "This is our Second Case";
break;
case "three": 
echo "This is our Third Case";
break;
default;
echo "This is our default Content in case nothing was selected!";
}
?>

I also attached how it looks like:

f7a1d9f00f7e996c88746962c4da9304

IIM 163 Master Poster

@LastMitch: It will not work properly.as you are assigning $content value to one.
Just take the value from URL and then it will work.

$Content=  $_GET['Content']; // change the case #
Member Avatar
LastMitch

@LastMitch: It will not work properly.as you are assigning $content value to one.

You are correct.

But I can't make any changes because I didn't write this code snippet.

The only person can change it is the OP.

I'm just playing around with the code snippet and post the update.

If you have an issue then you can repost it.

Member Avatar
diafol

This type of code could be useful if you were actually loading dynamic data from a db or from an include file or including variables / constants. As it is, all the data (static in this case) is already in the page (in the switch area), ie no 'dynamic' element to it, so it makes little sense to involve the server like this, when a simple bit of js could do it all without page refresh. As an example of how to route requests, it's certainly one method of how it could be done.

cereal 1,524 Nearly a Senior Poster Featured Poster

That code should not be considered as a correct snippet, as will work only if register_global is on, a condition that enables everyone to overwrite any variable with get, post, cookie call... fortunately register_globals is going to be removed.

More in general, for everyone, use superglobals as suggested by IIM: http://www.php.net/manual/en/language.variables.superglobals.php

Member Avatar
diafol

I think we should take the opportunity to thank 72dpi for the snippet regardless. We could definitely do with more snippet / tutorials for beginners to php. That said, I think a repost with alterations may be a good idea.

F-3000 commented: Reposting with alterations sounds good. +0
Member Avatar
LastMitch

I think we should take the opportunity to thank 72dpi for the snippet regardless. We could definitely do with more snippet / tutorials for beginners to php. That said, I think a repost with alterations may be a good idea.

You are right and you make a good point. I was not in a good mood so I lost track what I post.

michel.cote 0 Newbie Poster

As an exemple of how a switch/case works this is a good snipet.

But as an exemple of how to carry values over the URL this is grossly assuming that the PHP configuration is pretty permissive. In 2005 PHP allowed values to be converted automaticaly from $_GET["abc"] to $abc or $_POST["abc"] to $abc but for a few years now this is no longer the case unless PHP's defaults are overiden.

Like others I applaud 72dpi and LastMitch for their intentions in helping newcomers and for sharing their knowledge but I have to say that more care should be taken to make sure the snippets are compatible over many installations of PHP.

I can only imagine a developper new to PHP who searched for select/case information and falls on this post, it might confuse more then help unfortunately.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.