• 0

How to make a url ?action=lala


Question

Can anyone give me a snippet of a php code for switch functions. I want to make a url so that it appears like ?action=support or ?index=lalala or something like that. I don't want a template that says 0 equals 1 or 1 equals with something like that (I seriously forgot what that switch function template looked like.). If so please include the mention of php files in your code snippet. *Thanks* xD

Link to comment
Share on other sites

13 answers to this question

Recommended Posts

  • 0

<php

$what = 'foobar';

$url = 'whatever.php';

switch($what)
{
	case 'foobar':
		$url .= '?foo=bar';
		break;

	case 'support':
		$url .= '?action=support';
		break;

	case 'lalala':
		$url .= '?index=lalala';
		break;

	default:
		break;
}

?>

?

Link to comment
Share on other sites

  • 0

Im no coder but when i was after what you wanted i used if statements. Below is an example if it works is another thing because ive lost the original :)

<div id="content">
<? 
 if($action=='all'){
 $missions = mysql_query("select * from missions");
 echo "<table border='1' width='750px'>";
 echo "<tr><td colspan='9' align='center'>Missions</td></tr>";
 echo "<tr><td>Mission</td><td>Level</td><td>Location</td><td>Loot</td><td>Total Ngy</td><td>bronze</td><td>bronze</td><td>bronze</td><td>bronze</td></tr>";
 while($row = mysql_fetch_array( $missions )) {
 echo "<tr><td>".$row['mission']."</td><td>".$row['level']."</td><td>".$row['location']."</td><td>".$row['loot']."</td><td>".$row['energy1']."</td><td>".$row['bronze']."</td><td>".$row['silver']."</td><td>".$row['gold']."</td><td>".$row['platinum']."</td></tr>";
 }
 echo "</table>";
 }
 if($action=='bronx'){
 $missions = mysql_query("select * from missions where location='bronx'");
 echo "<table border='1' width='750px'>";
 echo "<tr><td colspan='9' align='center'>Missions</td></tr>";
 echo "<tr><td>Mission</td><td>Level</td><td>Location</td><td>Loot</td><td>Total Ngy</td><td>bronze</td><td>bronze</td><td>bronze</td><td>bronze</td></tr>";
 while($row = mysql_fetch_array( $missions )) {
 echo "<tr><td>".$row['mission']."</td><td>".$row['level']."</td><td>".$row['location']."</td><td>".$row['loot']."</td><td>".$row['energy1']."</td><td>".$row['bronze']."</td><td>".$row['silver']."</td><td>".$row['gold']."</td><td>".$row['platinum']."</td></tr>";
 }
 echo "</table>";
 }
 if($action=='downtown'){
 $missions = mysql_query("select * from missions where location='downtown'");
 echo "<table border='1' width='750px'>";
 echo "<tr><td colspan='9' align='center'>Missions</td></tr>";
 echo "<tr><td>Mission</td><td>Level</td><td>Location</td><td>Loot</td><td>Total Ngy</td><td>bronze</td><td>bronze</td><td>bronze</td><td>bronze</td></tr>";
 while($row = mysql_fetch_array( $missions )) {
 echo "<tr><td>".$row['mission']."</td><td>".$row['level']."</td><td>".$row['location']."</td><td>".$row['loot']."</td><td>".$row['energy1']."</td><td>".$row['bronze']."</td><td>".$row['silver']."</td><td>".$row['gold']."</td><td>".$row['platinum']."</td></tr>";
 }
 echo "</table>";
 }
 if($action=='jersey'){
 $missions = mysql_query("select * from missions where location='jersey'");
 echo "<table border='1' width='750px'>";
 echo "<tr><td colspan='9' align='center'>Missions</td></tr>";
 echo "<tr><td>Mission</td><td>Level</td><td>Location</td><td>Loot</td><td>Total Ngy</td><td>bronze</td><td>bronze</td><td>bronze</td><td>bronze</td></tr>";
 while($row = mysql_fetch_array( $missions )) {
 echo "<tr><td>".$row['mission']."</td><td>".$row['level']."</td><td>".$row['location']."</td><td>".$row['loot']."</td><td>".$row['energy1']."</td><td>".$row['bronze']."</td><td>".$row['silver']."</td><td>".$row['gold']."</td><td>".$row['platinum']."</td></tr>";
 }
 echo "</table>";
 }
 if($action=='vegas'){
 $missions = mysql_query("select * from missions where location='las vegas'");
 echo "<table border='1' width='750px'>";
 echo "<tr><td colspan='9' align='center'>Missions</td></tr>";
 echo "<tr><td>Mission</td><td>Level</td><td>Location</td><td>Loot</td><td>Total Ngy</td><td>bronze</td><td>bronze</td><td>bronze</td><td>bronze</td></tr>";
 while($row = mysql_fetch_array( $missions )) {
 echo "<tr><td>".$row['mission']."</td><td>".$row['level']."</td><td>".$row['location']."</td><td>".$row['loot']."</td><td>".$row['energy1']."</td><td>".$row['bronze']."</td><td>".$row['silver']."</td><td>".$row['gold']."</td><td>".$row['platinum']."</td></tr>";
 }
 echo "</table>";
 }
 if($action=='miami'){
 $missions = mysql_query("select * from missions where location='miami'");
 echo "<table border='1' width='750px'>";
 echo "<tr><td colspan='9' align='center'>Missions</td></tr>";
 echo "<tr><td>Mission</td><td>Level</td><td>Location</td><td>Loot</td><td>Total Ngy</td><td>bronze</td><td>bronze</td><td>bronze</td><td>bronze</td></tr>";
 while($row = mysql_fetch_array( $missions )) {
 echo "<tr><td>".$row['mission']."</td><td>".$row['level']."</td><td>".$row['location']."</td><td>".$row['loot']."</td><td>".$row['energy1']."</td><td>".$row['bronze']."</td><td>".$row['silver']."</td><td>".$row['gold']."</td><td>".$row['platinum']."</td></tr>";
 }
 echo "</table>";
 }
 if($action=='chinatown'){
 $missions = mysql_query("select * from missions where location='chinatown'");
 echo "<table border='1' width='750px'>";
 echo "<tr><td colspan='9' align='center'>Missions</td></tr>";
 echo "<tr><td>Mission</td><td>Level</td><td>Location</td><td>Loot</td><td>Total Ngy</td><td>bronze</td><td>bronze</td><td>bronze</td><td>bronze</td></tr>";
 while($row = mysql_fetch_array( $missions )) {
 echo "<tr><td>".$row['mission']."</td><td>".$row['level']."</td><td>".$row['location']."</td><td>".$row['loot']."</td><td>".$row['energy1']."</td><td>".$row['bronze']."</td><td>".$row['silver']."</td><td>".$row['gold']."</td><td>".$row['platinum']."</td></tr>";
 }
 echo "</table>";
 }
 if($action=='coney island'){
 $missions = mysql_query("select * from missions where location='coney island'");
 echo "<table border='1' width='750px'>";
 echo "<tr><td colspan='9' align='center'>Missions</td></tr>";
 echo "<tr><td>Mission</td><td>Level</td><td>Location</td><td>Loot</td><td>Total Ngy</td><td>bronze</td><td>bronze</td><td>bronze</td><td>bronze</td></tr>";
 while($row = mysql_fetch_array( $missions )) {
 echo "<tr><td>".$row['mission']."</td><td>".$row['level']."</td><td>".$row['location']."</td><td>".$row['loot']."</td><td>".$row['energy1']."</td><td>".$row['bronze']."</td><td>".$row['silver']."</td><td>".$row['gold']."</td><td>".$row['platinum']."</td></tr>";
 }
 echo "</table>";
 }
 if($action=='moscow'){
 $missions = mysql_query("select * from missions where location='moscow'");
 echo "<table border='1' width='750px'>";
 echo "<tr><td colspan='9' align='center'>Missions</td></tr>";
 echo "<tr><td>Mission</td><td>Level</td><td>Location</td><td>Loot</td><td>Total Ngy</td><td>bronze</td><td>bronze</td><td>bronze</td><td>bronze</td></tr>";
 while($row = mysql_fetch_array( $missions )) {
 echo "<tr><td>".$row['mission']."</td><td>".$row['level']."</td><td>".$row['location']."</td><td>".$row['loot']."</td><td>".$row['energy1']."</td><td>".$row['bronze']."</td><td>".$row['silver']."</td><td>".$row['gold']."</td><td>".$row['platinum']."</td></tr>";
 }
 echo "</table>";
 }
?>
<?php
$action=$_GET['action'];
 if($action!=''){ echo'';
}else{
?>
<table>
<tr>
<td><a href='missions.php?action=all'>All</a></td>
</tr>
<tr>
<td><a href='missions.php?action=bronx'>Bronx</a></td>
</tr>
<tr>
<td><a href='missions.php?action=downtown'>Downtown</a></td>
</tr>
<tr>
<td><a href='missions.php?action=jersey'>Jersey</a></td>
</tr>
<tr>
<td><a href='missions.php?action=vegas'>Las Vegas</a></td>
</tr>
<tr>
<td><a href='missions.php?action=miami'>Miami</a></td>
</tr>
<tr>
<td><a href='missions.php?action=chinatown'>Chinatown</a></td>
</tr>
</table>
</div>

Link to comment
Share on other sites

  • 0

<php

$what = 'foobar';

$url = 'whatever.php';

switch($what)
{
	case 'foobar':
		$url .= '?foo=bar';
		break;

	case 'support':
		$url .= '?action=support';
		break;

	case 'lalala':
		$url .= '?index=lalala';
		break;

	default:
		break;
}

?>

?

Thank you for successfully answering my question. I will try this method immediately.

Link to comment
Share on other sites

  • 0

Ehhh...


$action = $_GET['action'];

switch($action)
{
	case 'lala':
		include('/pages/lala.php');
		break;

	case 'contact':
		include('/pages/contact.php');
		break;

	case 'about':
		include('/pages/about.php');
		break;

	default:
		include('/pages/main.php');
		break;
}

Nice, Clean, & Easy

You know Xoilgy, you made that 100 times more complicated than it had to be.

Link to comment
Share on other sites

  • 0

Ehhh...

...

Nice, Clean, & Easy

You know Xoilgy, you made that 100 times more complicated than it had to be.

You'll also want to check if there is a GET parameter, as well as trim whitespace and convert it to lower case for comparison (so somebody doesn't pass " cOnTaCt ").

And yeah Xoligy, that's a lot of code you don't need. All your changing is the query si that's the only bit of code you need to stick in the if statements (ideally switch, or possibly just passing the escaped string directly to MySQL)

Link to comment
Share on other sites

  • 0

Nice, Clean, & Easy

You know Xoilgy, you made that 100 times more complicated than it had to be.

[/font]

Im good at that, like i said im not coder :p

I scrapped all the code when my computer played up, so alls good :)

Link to comment
Share on other sites

  • 0

Code for xoligy

<?php

	$action=$_GET['action'];

	switch($action)
	{
	 case 'all':
	 	 $location = "all";
	 	 break;
	 case 'bronx':
	 	 $location = "bronx";
	 	 break;
	 case 'downtown':
	 	 $location = "downtown";
	 	 break;
	 case 'jersey':
	 	 $location = "jersey";
	 	 break;
	 case 'vegas':
	 	 $location = "las vegas";
	 	 break;
	 case 'miami':
	 	 $location = "miami";
	 	 break;
	 case 'chinatown':
	 	 $location = "chinatown";
	 	 break;
	 case 'coney island':
	 	 $location = "coney island";
	 	 break;
	 case 'moscow':
	 	 $location = "moscow"
	 	 break;
	 default:
	 	 $location = "all";
	 	 break;
	}

	if($location == 'all'){
	 $missions = mysql_query("select * from missions");
	} else {
	 $missions = mysql_query("select * from missions where location='$location'");
	}

	echo "<table border='1' width='750px'>";
	echo "<tr><td colspan='9' align='center'>Missions</td></tr>";
	echo "<tr><td>Mission</td><td>Level</td><td>Location</td><td>Loot</td>
	 <td>Total Ngy</td><td>bronze</td><td>bronze</td><td>bronze</td><td>bronze</td>
	 </tr>";

	while($row = mysql_fetch_array( $missions )) {
	 echo "<tr><td>".$row['mission']."</td><td>".$row['level']."</td><td>".$row['location']."</td><td>".$row['loot']."</td><td>".$row['energy1']."</td><td>"
	 .$row['bronze']."</td><td>".$row['silver']."</td><td>".$row['gold']."</td><td>".$row['platinum']."</td></tr>";
	}
	echo "</table>";

?> 

You could also get rid of the switch case as decryptor said, but its nice to have because its limits what can be inputted into your sql query. Lessens the chance of a sql attack.

Link to comment
Share on other sites

  • 0

wouldn't having one if statement that checks the input is one of the keywords (and returns an error if it's not) be just as safe & efficient as a switch?

Link to comment
Share on other sites

  • 0

What do I change so that the cases so that my php files are translated into ?action=support ? support.php=support

Nvm I saw nub's post.

Link to comment
Share on other sites

  • 0

just another way to add to the mix.

<?php
$action = strtolower($_GET['action']);

$allowed_actions = array('bronx', 'downtown', 'jersey', 'las vegas', 'miami', 'chinatown', 'coney island', 'moscow');

if ( in_array($action, $allowed_actions) )
{
	$missions = mysql_query('SELECT * FROM missions WHERE = "'.$action.'";');
}
else
{
	$missions = mysql_query("SELECT * FROM missions");
}

echo "<table border='1' width='750px'>";
echo "<tr><td colspan='9' align='center'>Missions</td></tr>";
echo "<tr><td>Mission</td><td>Level</td><td>Location</td><td>Loot</td><td>Total Ngy</td><td>bronze</td><td>bronze</td><td>bronze</td><td>bronze</td></tr>";

while($row = mysql_fetch_array( $missions ))
{
	echo "<tr><td>".$row['mission']."</td><td>".$row['level']."</td><td>".$row['location']."</td><td>".$row['loot']."</td><td>".$row['energy1']."</td><td>" .$row['bronze']."</td><td>".$row['silver']."</td><td>".$row['gold']."</td><td>".$row['platinum']."</td></tr>";
}

echo "</table>";
?> 

Link to comment
Share on other sites

  • 0

To be honest I used to do it this way:

<?php
if(isset($_GET['action']) && !empty($_GET['action'])) {
	if($_GET['action'] == 'lala') {
		include 'lala.php';
	}
	elseif($_GET['action'] == 'contact') {
		include 'contact.php';
	}
	else {
		include '404.php';
	}
}
else {
	include 'homepage.php';
}

I use rewrite URL's a lot, that was the only way I could think off :laugh:

Link to comment
Share on other sites

  • 0

To be honest I used to do it this way:

<?php
if(isset($_GET['action']) && !empty($_GET['action'])) {
	if($_GET['action'] == 'lala') {
		include 'lala.php';
	}
	elseif($_GET['action'] == 'contact') {
		include 'contact.php';
	}
	else {
		include '404.php';
	}
}
else {
	include 'homepage.php';
}

I use rewrite URL's a lot, that was the only way I could think off :laugh:

For your php code why does part of my page come out to be on the top of the main page instead of being on a different page? here is what I'm trying to say: http://beta.flownyx.net/?action=support

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.