select multiple and array's = no hair left

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dooms
Forum Newbie
Posts: 3
Joined: Thu Jul 11, 2002 4:38 pm
Location: Colorado

select multiple and array's = no hair left

Post by dooms »

Found info on select multiple, but then what? I know it's an array but what step do I follow to get my output. Using IPlanet and MySQL.

here is the code.

<form method=\"post\" action=\"pdtest.php\">
<table border=0 cellpadding=5 cellspacing=5>
<tr>
<td rowspan=2><select name=\"sel_server[]\" size=30 multiple>
";
while ($row = mysql_fetch_array($sql_result)) {
$hostname = $row["hostname"];
echo "
<option value=\"$hostname\">$hostname</option>
";
}
echo "
</select></td>

code for the other page:

<?php


// file name: powerdown1.php
//create connection
$connection = mysql_connect("localhost", "root") or die ("couldn't connect to server.");
// select database
$db = mysql_select_db("toolboxdb",$connection) or die ("couldn't connect to database.");
// create sql statement
$sql = "select * from powerdown where hostname = \"$sel_server\"";
// execute SQL query and get result
$sql_result = mysql_query($sql,$connection) or die ("couldn't execute query.");

if (!$sql_result) {
echo "<P>Couldn't get list!";
} else {


echo "
<html>

looked at this for a week and not able to grasp the array part.

Thanks for the help!
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

To get your output you would need to know what the array looks like so put this code into pdtest.php:

Code: Select all

echo '<pre>';
print_r($_POST);
echo '</pre>';
You'll see that each selected value is an array element within $_POST['sel_server']. To work with this information you could use foreach():

Code: Select all

foreach ($_POST&#1111;'sel_server'] as $value) &#123;
	echo $value.' was selected<br />';
&#125;
Obviously you could do stuff other than echo within the foreach loop. It may be an idea to read up on array handling functions within PHP to find the best solution for what you want to do with the information.

Mac
dooms
Forum Newbie
Posts: 3
Joined: Thu Jul 11, 2002 4:38 pm
Location: Colorado

This helps -- But?

Post by dooms »

This helps alot.. Thanks

but how can I use this info.

I need to display a list of servers and there data. Do I do this at the $sql or after?

Thanks for your patience.[quote][/quote]
dooms
Forum Newbie
Posts: 3
Joined: Thu Jul 11, 2002 4:38 pm
Location: Colorado

code would help

Post by dooms »

See the code might help!

// file name: pdtest.php
//create connection
$connection = mysql_connect("localhost", "root") or die ("couldn't connect to server.");
// select database
$db = mysql_select_db("toolboxdb",$connection) or die ("couldn't connect to database.");
// create sql statement
$sql = "select * from powerdown";

// execute SQL query and get result
$sql_result = mysql_query($sql,$connection) or die ("couldn't execute query.");

if (!$sql_result) {
echo "<P>Couldn't get list!";
} else {


echo "
<html>
<head>
<title>Server Report</title>
</head>
<body bgcolor=\"#999999\">
<h1>Power Down Server Report</h1>
<table border=1 cellpadding=5 cellspacing=5>
<tr><th>Hostname</th><th>Point of Contact</th><th>Ext</th><th>Applications on the Server</th><th>Server Specific Notes</th></tr>
";

// fetch row and assign a name to the variable
while ($row = mysql_fetch_array($sql_result)) {
$hostname = $row["hostname"];
$point_contact = $row["point_contact"];
$contact_number = $row["contact_number"];
$server_application = $row["server_application"];
$pd_notes = $row["pd_notes"];


foreach ($_POST['sel_server'] as $value) {
echo "<tr><td><h1>$value</h1></td><td><h1>$point_contact</h1></td><td><h1>$contact_number</h1></td><td><h1>$server_application</h1></td><td><h1>$pd_notes</h1></td></tr>";
}
}
}
echo "
</table>
</body>
</html>
";
Post Reply