Jump to content

[SOLVED] SWITCH + WHILE


DEVILofDARKNESS

Recommended Posts

There is this page: weapons.php

The main part is coded:

 

echo "Welcome in the weapon-section.<br><br>
			Our Weapons:<br><br>";
			$query = "SELECT * FROM weapons WHERE shop='WOSH'";
			$result = mysql_query($query);
			while($WOSHWeapons = mysql_fetch_array($result)){
				echo "<a href='./info.php?type=weapon&name=" . $WOSHWeapons['weapon_short_name'] . "'>" . $WOSHWeapons['weapon_name'] . "</a><br>";
			}

 

 

So If you click on a link you go to ./info.php?type=weapon&name=//shortname

 

On info.php I can handle this information easy by using switch(){case ... }

But If I have a lot of weapons, I will have to write a lot of case ...: break;

 

So I tried:

<?php
		$type = $_GET['type'];
		switch($type){
		case weapon:
			$name = $_GET['name'];
			$query = "SELECT * FROM weapons";
			$result = mysql_query($query);
			switch($name){
				while($InfWeap = mysql_fetch_array($result)){
					case $InfWeap['weapon_short_name']: echo "Full Name: " . $InfWeap['weapon_name'] . "<br>Short Name: " . $InfWeap['weapon_short_name'];
					break;
				}
				default:
					echo "Default";
			}
		break;

 

It doesn't work because I use a while loop inside a switch loop

 

Is there a certain way to do this,

if there isn't please tell me an easier method to show the info about the weapons.

 

BTW: It's about a futuristic game, not a weapon shop :P

Link to comment
https://forums.phpfreaks.com/topic/169783-solved-switch-while/
Share on other sites

Switch is not a loop, it is case driven expression.

 

<?php
		$type = $_GET['type'];
		switch($type){
		case weapon:
			$name = $_GET['name'];
			$query = "SELECT * FROM weapons";
			$result = mysql_query($query);

		             while($InfWeap = mysql_fetch_array($result)){
                                                             switch($name){
				case $InfWeap['weapon_short_name']: 
                                                                     echo "Full Name: " . $InfWeap['weapon_name'] . "<br>Short Name: " . $InfWeap['weapon_short_name'];
				break;
				default:
				     echo "Default";
                                                              }
            				}

 

Should get it to work, pending I put the braces back in right etc.

Link to comment
https://forums.phpfreaks.com/topic/169783-solved-switch-while/#findComment-895713
Share on other sites

Okay that works, thanks

 

If I exactly copy like you 'wrote' it, I get a lot of: defaults between the normal text,

if I use the same code except I don't use default: echo "Default",

then I get the right text,

 

Is there a way that I can implement the default:

?

Link to comment
https://forums.phpfreaks.com/topic/169783-solved-switch-while/#findComment-895721
Share on other sites

Well it's quit difficult to do that, because the php will think I'm closing the switch statement:

 

switch($type){
		case weapon:
			$name = $_GET['name'];
			$query = "SELECT * FROM weapons";
			$result = mysql_query($query);

		             while($InfWeap = mysql_fetch_array($result)){
                                                             switch($name){
				case $InfWeap['weapon_short_name']: 
                                                                     echo "Full Name: " . $InfWeap['weapon_name'] . "<br>Short Name: " . $InfWeap['weapon_short_name'];
				break;
                                                              }
				default:
					echo "Default";
                                                               }
		break;

Link to comment
https://forums.phpfreaks.com/topic/169783-solved-switch-while/#findComment-895739
Share on other sites

As I see it you can do away with the switch/case completely. You can just do this instead:

 

switch($type)
{
   case 'weapons':

        $name = mysql_real_escape_string($_GET['name']);
        $sql = "SELECT * FROM weapons WHERE weapon_short_name = '$name'";
        $result = mysql_query($sql);
        if(mysql_num_rows() == 1)
        {
            $InfWeap = mysql_fetch_assoc($result);
            echo "Full Name: " . $InfWeap['weapon_name'] . "<br>Short Name: " . $InfWeap['weapon_short_name'];
        }
        else
        {
             echo 'Weapon not found!';
        }
   break;
}

 

Link to comment
https://forums.phpfreaks.com/topic/169783-solved-switch-while/#findComment-895754
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.