2

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.

Votes + Comments
Thanks for sharing!
< 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
6
Contributors
8
Replies
60
Views
12 Years
Discussion Span
Last Post by michel.cote
0

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

0

@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 #
0

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

0

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.

Edited by diafol

3

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.

Votes + Comments
Reposting with alterations sounds good.
0

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.

1

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.

Edited by michel.cote: Didn't realise that OP posted in 2005....

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.