Buy Anything On eBay - Credit Card Consolidation - Loans - Loans - Loan

PDA

View Full Version : Arrays


karl_murphy
Feb 6th 2008, 7:41 am
I am trying to populate an array with values selected from a multi list box and then retrieve these values and allocate them to variables. I have created a from that allows me to select values from a multi list box and print these to the screen. However, I am having trouble sending these to another script and allocating them to variables. Any ideas?

Regards

nico_swd
Feb 6th 2008, 7:46 am
Please post your current code and explain where the problem is.

Does the name of the multi list box have square brackets in it? Like this?

name="some_name[]"


You need this in order to receive all selected values as an array.

karl_murphy
Feb 6th 2008, 8:02 am
The following script sets up the form where the user selects recipients, types a subject and also the main body of the message. When the user clicks the "Select recipients" button the correct recipients are printed to the screen.

$sql2 = "SELECT DISTINCT * FROM `Teacher` ORDER BY ST_Surname ASC";
include("../includes/php/connect2.php");

echo "<form method = 'post' action = $PHP_SELF>";
echo "<table>";
echo "<tr>";
echo "<td>";
echo "<strong>Please select recipients</strong>";
echo "</td>";
echo "<td>";
echo "<select name = 'multiple_recipients[]' multiple = 'true' size = 6>";
while ($row2 = mysql_fetch_assoc($rst2))
{
$individual = $row2['ST_Code'];
echo "<option value = $individual>$individual</option>";
}
echo "</select>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>";
echo "<input type = 'submit' name = 'submit' value = 'Select recipent(s)' />";
echo "</td>";
echo "</tr>";
echo "</table>";
echo "<br />";
if (!$_POST['submit'])
{}

else
{
$recipients = ($_POST['multiple_recipients']);
echo "<table border = '1'>";
echo "<tr>";
echo "<td>";
echo "<label><strong>Recipent(s)</strong></label>";
echo "</td>";

if (is_null($recipients))
{
echo "<td>You have not selected any recipients!</td>";
}

else
{
foreach ($recipients as $a_recipient)
{
echo "<td>".$a_recipient."</td>";
}

}
echo "</tr>";
echo "</table>";

echo "<table>";
echo "</form>";
echo "<br />";
echo "<form method = 'post' action = 'send_individual_msg.php?recipients=".serialize($recipients)."'>";
echo "<table>";
echo "<tr>";
echo "<td>";
echo "<strong>Subject</strong>";
echo "</td>";
echo "<td>";
echo "<input type = 'text' name = 'subject' size = '56' maxlength = '56' />";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>";
echo "<strong>Message body</strong>";
echo "</td>";
echo "<td>";
echo "<textarea name = 'message' cols = '43' rows = '6'></textarea>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>";
echo "<input type = 'submit' name = 'submit' value = 'Send message' />";
echo "</td>";
echo "</tr>";
echo "</table>";
echo "</form>";
}

When the "Send message" button is clicked I am having trouble retrieving each individual element from the array. The code is as follows:

$recipients = unserialize($_GET['recipients']);
$subject = $_POST['subject'];
$message = $_POST['message'];

/*$recipient_a = $recipients[1];
$recipient_b = $recipients[2];
if (!$recipient_b)
{ $recipient_b = "";}

echo $recipient_a;
echo $recipient_b; */
$r_a = $recipients[0];
echo $r_a,'<br />';
$r_b = $recipients[1];
echo $r_b,'<br />';
//echo $recipients;
echo $subject,'<br />';
echo $message;

The $subject and $message print fine, but I've had no luck with the array of recipients.

Regards

greatlogix
Feb 6th 2008, 10:18 am
First you are using form method is post you can't get recipients array using $_GET ( $_GET['recipients']). Try this $_POST['recipients']

Second, I did not find code which is serializing your array

karl_murphy
Feb 7th 2008, 3:20 am
Someone else advised the serializing code. Never came across it myself. Any ideas how I would go about using this method?

bpasc95
Feb 8th 2008, 3:41 pm
Finding out what is in an array on a form post can be a bit tricky. This code I found somewhere really helped shed a lot of light on arrays:


//PRINT ARRAY VALUES INTO A TABLE
function print_array_data( $TheArray ) {
// Note: the function is recursive
echo "<table border=1>\n";
$Keys = array_keys( $TheArray );
foreach( $Keys as $OneKey ) {
echo "<tr>\n";
echo "<td bgcolor='#727450'>";
echo "<B>" . $OneKey . "</B>";
echo "</td>\n";
echo "<td bgcolor='#C4C2A6'>";
if ( is_array($TheArray[$OneKey]) )
print_array_data($TheArray[$OneKey]);
else
echo $TheArray[$OneKey];
echo "</td>\n";
echo "</tr>\n";
}
echo "</table>\n";
}


Where you post your form, add this:

print_array_data($_POST);

The above will give you a good visualization of the array. In this case, everything in the $_POST

This helped me quite a bit in past. Use it quite a bit today when dealing with sessions.

-Bing

gwkg
Feb 8th 2008, 8:48 pm
You don't have to echo every line of code. Simply close php with ?> type html and reopen <?php


<?php
$sql2 = "SELECT DISTINCT * FROM `Teacher` ORDER BY ST_Surname ASC";
include("../includes/php/connect2.php");

echo "<form method = 'post' action = ".$PHP_SELF.">";
?>
<table>
<tr>
<td>
<strong>Please select recipients</strong>
</td>
<td>
<select name = 'multiple_recipients[]' multiple = 'true' size = 6>
<?php
while ($row2 = mysql_fetch_assoc($rst2))
{
$individual = $row2['ST_Code'];
echo "<option value = ".$individual.">".$individual."</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td>
<input type = 'submit' name = 'submit' value = 'Select recipent(s)' />
</td>
</tr>
</table>
<br />
<?php
if (!$_POST['submit'])
{}

etc...
?>