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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • 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.