Click to See Complete Forum and Search --> : Traversing Multi-Dimensional Arrays???


atw
01-10-2007, 08:58 AM
Hi,

I'm sorry, I know this question gets asked lots but I'm struggling :(

I have setup a form (for internal use only) which generates this $_POST array:Array
(
[require] => sender_name,subject,notes
[subject] => Weekly Sales Report
[sender_name] => My Name
[period] => Period
[face-to-face] => 5
[SalesWeek] => Array
(
[ClientName1] => Client Name
[PurchaseLease1] => Purchase
[ValueProperty1] => Value
[Commission1] => Commission
[ClientName2] =>
[PurchaseLease2] =>
[ValueProperty2] =>
[Commission2] =>
[ClientName3] =>
[PurchaseLease3] =>
[ValueProperty3] =>
[Commission3] =>
[ClientName4] =>
[PurchaseLease4] =>
[ValueProperty4] =>
[Commission4] =>
[ClientName5] =>
[PurchaseLease5] =>
[ValueProperty5] =>
[Commission5] =>
[ClientName6] =>
[PurchaseLease6] =>
[ValueProperty6] =>
[Commission6] =>
[ClientName7] =>
[PurchaseLease7] =>
[ValueProperty7] =>
[Commission7] =>
[ClientName8] =>
[PurchaseLease8] =>
[ValueProperty8] =>
[Commission8] =>
[ClientName9] =>
[PurchaseLease9] =>
[ValueProperty9] =>
[Commission9] =>
[ClientName10] =>
[PurchaseLease10] =>
[ValueProperty10] =>
[Commission10] =>
)

[prospects] => Array
(
[ClientName1] =>
[PurchaseLease1] =>
[Status1] => Status
[ClientName2] =>
[PurchaseLease2] =>
[Status2] =>
[ClientName3] =>
[PurchaseLease3] =>
[Status3] =>
[ClientName4] =>
[PurchaseLease4] =>
[Status4] =>
[ClientName5] =>
[PurchaseLease5] =>
[Status5] =>
[ClientName6] =>
[PurchaseLease6] =>
[Status6] =>
[ClientName7] =>
[PurchaseLease7] =>
[Status7] =>
[ClientName8] =>
[PurchaseLease8] =>
[Status8] =>
[ClientName9] =>
[PurchaseLease9] =>
[Status9] =>
[ClientName10] =>
[PurchaseLease10] =>
[Status10] =>
)

[notes] => General Notes
[Submit] => Submit
)
I need to put the contents of the 2 sub-arrays into tables (4 <td>s for the first sub-array and 3 for the second).

Here is my code so far:<?php
foreach( $_POST as $key => $value ) {
if( is_array( $value )) {
echo '<table>';
foreach( $value as $subKey => $subValue ) {
echo "<tr>\n";
echo '<td>' . $subValue . "</tr>\n";
echo "</tr>\n";
}
echo '</table>';
}
echo $key . '==>' . $value . "<br />\n";
}
?>

This is what I get after submitting:<body>
require==>sender_name,subject,notes<br />
subject==>Weekly Sales Report<br />
sender_name==>My Name<br />
period==>Period<br />

face-to-face==>5<br />
<table><tr>
<td>Client Name</tr>
</tr>
<tr>
<td>Purchase</tr>
</tr>
<tr>
<td>Value</tr>
</tr>
<tr>
<td>Commission</tr>

</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>

<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>

<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>

</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>

<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>

<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>

</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
</table>SalesWeek==>Array<br />
<table><tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>
<td>Status</tr>

</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>

<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>

<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>

</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>

<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
<tr>
<td></tr>
</tr>
</table>prospects==>Array<br />
notes==>General Notes<br />

Submit==>Submit<br />
</body>
Again, sorry. I feel so lame but I'm losing the will to live right now :(

Sid3335
01-10-2007, 09:35 AM
foreach($array1 as $key => $value )
{
if( is_array( $value ))
{
echo '<table border="1">';
foreach( $value as $subKey => $subValue )
{
if (!empty($subValue))
{
echo "<tr>\n";
echo '<td>' . $subValue . "</td>\n";
echo "</tr>\n";
}
}
echo '</table>';
}
else
{
echo $key . '==>' . $value . "<br />\n";
}
}

atw
01-11-2007, 05:43 AM
Thanks Sid3335 but that isn't quite what I was after. I actually wanted to make the fields which were in the multi-dimensional array appear in table cells horizontally (in a table) :)

I figured it out, I just needed to add another "dimension" to the form fields. So I have go what I wanted to work working:<?php
ob_start( $test );
foreach( $_POST as $key => $value ) {
if( ($key == 'Submit') OR ($key == 'submit') ) { // _try_ to remove the Submit button from the $_POST super global!
#unset( $_POST['Submit'] );
}
if( is_array( $value )) {
echo '<table border="1" cellpadding="4" cellspacing="0" bordercolor="#000000">' . "\n";
if( strlen( trim( $value ))) {
foreach( $value as $subKey => $subValue ) {
echo "<tr>\n";
foreach( $subValue as $subSubKey => $subSubValue ) {
echo '<td>' . $subSubValue . "</td>\n";
}
echo "</tr>\n";
}
}
echo "</table>\n";
}
echo $key . ': ' . $value . "<br />\n";
}
// --- debug --- //
echo #'<PRE>'; print_r( $_POST ); echo '</PRE>';
?>

As you can see I have added an ob_start() to the start of this script, is it possible to dump the buffer into a mail() function?

i.e. mail('myemail@address.com', 'My Subject', $ob_content, $headers);

MrCoder
01-11-2007, 05:53 AM
<?php
ob_start();
?>
This will go in to the e-mail!
<?php
echo "So will this";

$ob_content = ob_get_clean(); // Populate $ob_content and clear the output buffer (Not flushed to the client)

echo "But this will not be in the e-mail, it will show on the clients machine";

mail('myemail@address.com', 'My Subject', $ob_content, $headers);
?>