...

View Full Version : Select option



Radeom
07-13-2012, 05:55 PM
I cant explain better, what i want(if server is 1 then show option "server name 1" or $server is ALL, then show all of server names):



$server == "from database"

<select type="text" name="server">

if ($server == "1")
(
<option value="1">Server name 1</option>
)
elseif ($server == "2")
(
<option value="2">Server name 2</option>
)
elseif ($server == "ALL")
(
<option value="ALL">Server name 1</option>
<option value="ALL">Server name 2</option>
)
</select>

Fou-Lu
07-13-2012, 06:19 PM
Which is right (branch wise), except you cannot apply a value of "ALL" to your option values. If they are not unique there is no way to determine what value was selected.

I'm not sure what it is you are trying to do here. Select menus are pointless with only one option within them, and completely useless if multiple options have the same value to them. Given your $server variable as well, you'll end up with a select menu that has no options within it.
So, uhh, what is it you are actually trying to do?

DrDOS
07-13-2012, 07:59 PM
This code definitely needs to be smarter. First, put all your server names in an indexed array, that is by the number. I just write the code, OK.


in the post part of your php put $svr = $_POST['server_name'];
make all the values in the options just numbers, no need for the quotes, and add one more number than the number of servers for the 'all' option, in this case 2.
remember that php numbers from 0
$server_names=array("name1","name2");
if ( $svr != 2 ) $name = $server_names[$svr];
else
{
// In here you put a for loop that prints out all the server names from $server_names
}

jdswebservice
07-14-2012, 01:41 AM
First off, your coding is HORRIBLE, unless this is pseudo code... :/ anyways heres a switch statement for ya.


<?php
//Define the variables here
$server = "some variable";
$output = "";
//do your switch function!
switch ($server){
case "1":
$output = "<option value=\"1\">Server 1</option>";
break;
case "2":
$output = "<option value=\"2\">Server 2</option>";
break;
case "ALL":
$output = "<option value=\"1\">Server 1</option>";
$output .= "<option value=\"2\">Server 2</option";
break;
default:
$output = "<option value=\"1\">Server 1</option>";
$output .= "<option value=\"2\">Server 2</option";
break;
}
?>
<html>
<head>
<title> Coding Forums Post 267803</title>
</head>
<body>
<form method="post" action="some_script.php">
<select name="server_select">
<?php echo($output); ?>
</select>
<br>
<input type="submit" value="Submit this form">
</form>
</body>
</html>

Radeom
07-14-2012, 10:42 AM
Thanks!


<?php

$db_value = "ALL";

switch($db_value)
{
case '1':
$output = "<option value=\"1\">Server 1</option>";
break;

case '2':
$output = "<option value=\"2\">Server 2</option>";
break;

case 'ALL':
$output = "<option value=\"1\">Server 1</option>";
$output .= "<option value=\"2\">Server 2</option";
break;
}
?>

<select name="server">
<?php echo($output); ?>
</select>

jdswebservice
07-14-2012, 08:50 PM
Hopefully that helps and does what you wanted! :)

#resolved!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum