Jump to content

Recommended Posts

Posted (edited)

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
Posted

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.

Posted

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!

Posted

If all you're doing is stating the number of pages, then why not:

 

$c = $_POST['page_count'];

 

if ($c==1) {

echo "There is 1 page.";

}

else {

echo "There are $c pages.";

}

Posted

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!

Posted

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...