Jump to content
Killersites Forums

Switch function


arcanepsyche
 Share

Recommended Posts

Hello all. I'm attempting to use the switch function for the first time and have a general question about the following code, which is part of a .php page I'm using to display the results of a submitted form:

 

switch ($_POST['page_count'])

 

{case "1":

echo "There is 1 page.";

break;

case "2":

echo "There are 2 pages.";

break;

case "3":

echo "There are 3 pages.";

break;

};

 

Is there a way to make the code cleaner by using some sort of "through" statement? I tried this:

 

{case "1-5":

echo "There are between 1 and 5 pages";

 

But that didn't work, which is what I thought would happen.

 

Or, is there some sort of greater than or less than symbol? My users are able to choose any number between 1 and 25 on the form and I'm not too keen on copying and pasting 25 different cases on the PHP page if there's a simpler way out there somewhere.

 

 

Thanks in advance for the help, and Happy New Year!

Zac

Edited by arcanepsyche
Link to comment
Share on other sites

You could do a switch for 1 through 5 like this:

 

switch ($_POST['page_count']) {
 case "1":
 case "2":
 case "3":
 case "4":
 case "5":
   echo "There are 1-5 pages pages.";
   break;
};

 

(no ";" is necessary after the switch statement, FYI)

 

As I said above though, if you have a lot of these, it may make more sense to do a long if / else if statement instead of switch.

Link to comment
Share on other sites

I started out using the if/else statements but then found info on switch and it just seemed like a simpler way of doing the same thing...unless I'm not able to figure out how to do the above. :P

 

Either way, thanks for experimenting for me. Don't let it eat into your champagne drinking time tonight!

Link to comment
Share on other sites

Thanks for the response. The reason I can't use that is because there is a different response for different numbers of pages so I have to add conditions.

 

I ended up using this, which worked fine:

 

 

if ($page_count >0 && $page_count <5)

{

$price_per_page=25;

}

 

if ($page_count >4 && $page_count <10)

{

$price_per_page=20;

}

 

if ($page_count >10)

{

$price_per_page=15;

}

 

And then later multiplied the $price_per_page by $page_count to create $total_page_price which will give my customers the price they will pay based on how many pages they want built.

 

Thanks for your help guys!

Link to comment
Share on other sites

Hi,

 

You change your thinking on this and use the 'default' statement for a catch all:

 

switch ($_POST['page_count'])

{
case "1":
echo "There is 1 page."
break;
case "2":
echo "There are 2 pages."
break;
case "3":
echo "There are 3 pages."
break;
default: 
echo "There are 4 to 5 pages ... or even more!"
break;
};

 

Actually, I think the other options presented above would work better, but I wanted to mention the 'default' statement.

 

;)

 

Stefan

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
 Share

×
×
  • Create New...