Jump to content

Archived

This topic is now archived and is closed to further replies.

komquat

How to print an array from a "Post"

Recommended Posts

I am using forms to bring data from 1 page to the next. 

[code=php:0]
for ($i = 1; $i <= $_POST[co2_s_number]; $i++){
$display_block .= "
<tr>
<td align='center'>$_POST[co2_s_sample[$i]]</td>
<td align='center'>$_POST[test_type[$i]]</td>
[/code]

The last two lines will not post.  I tried:

[code=php:0]
<td align='center'>$_POST[co2_s_sample . [$i] . ]</td>
<td align='center'>$_POST[test_type . [$i] . ]</td>
[/code]

And that did not work.  Any suggestions?

Thanks in advance

Andy

Share this post


Link to post
Share on other sites
I usually echo POSTed arrays like this:
<?php
foreach($_REQUEST['fields'] as $key2)
{
    $selectstr .= $key2 . ", ";
}
?>

Share this post


Link to post
Share on other sites
I am not familiar enough with the foreach command.  If there is a way to do it with out, I would like to pursue that first, if I can not get around it, I will become more familiar with the Foreach command.

Any one out there have a way around with-out the foreach?

Thanks

Share this post


Link to post
Share on other sites
Your loss.  Foreach is a godsend in your situation.

Share this post


Link to post
Share on other sites
if $_POST[co2_s_sample] is an array you'll want to use $_POST[co2_s_sample][$i] to access the items in that array

Or use a foreach loop:
[code=php:0]foreach($_POST['co2_s_sample'] as $co2_example)
{
    echo '<td align="center">' . $co2_example . '</td>
<td align="center"> ' $_POST['test_type'][$co2_example] . "</td>\n";
}[/code]

Share this post


Link to post
Share on other sites
[quote author=wildteen88 link=topic=106253.msg424723#msg424723 date=1156949443]
if $_POST[co2_s_sample] is an array you'll want to use $_POST[co2_s_sample][$i] to access the items in that array

[/quote]

The co2_s_sample coming from the first page is the array.

When I first designed the page, I was able to get away with out the $_POST[var] command and only use $var[$i].  Now my web host redid my server and I no longer can do this, I have to use the $_POST command.  I am trying to rewrite all the code to work and am having a very hard time.

I will look into the foreach command.  (Ober, I did not mean to get you worked up)

Andy

Share this post


Link to post
Share on other sites
Run this code on co2_s_sample array:
[code=php:0]echo "<pre>" . print_r($_POST['co2_s_sample '], true) . "</pre>";[/code]


What do you get? That code should retrun whats in the array. Post what it retruns here.

Also its a good job your host has upgraded PHP, as it should like it was using register_globals before.

Share this post


Link to post
Share on other sites
[quote author=wildteen88 link=topic=106253.msg424757#msg424757 date=1156950851]
Run this code on co2_s_sample array:
[code=php:0]echo "<pre>" . print_r($_POST['co2_s_sample '], true) . "</pre>";[/code]


What do you get? That code should retrun whats in the array. Post what it retruns here.

Also its a good job your host has upgraded PHP, as it should like it was using register_globals before.
[/quote]

Array
(
    [1] => 1
    [2] => 54
)


That is what I want to display

Share this post


Link to post
Share on other sites
So this should work fine:
[code=php:0]$i = 1;
foreach($_POST['co2_s_sample'] as $co2_example)
{
    echo '<td align="center">' . $co2_example . '</td>
<td align="center"> ' $_POST['test_type'][$i] . "</td>\n";
$i++;
}[/code]

Did you try it? The format of the array is fine.

Share this post


Link to post
Share on other sites
I am not sure in my code where it would go, so here is my whole code, and you point me in the direction of where the above code for $cu_s_sample should go

[code=php:0]
//Include Connection information
include("include/connection.php");

//Get the Test id for new info
$sql_get_id = "SELECT test_id
FROM test_info
WHERE test_name = '$_POST[test_name]'
";

$qry_get_id = mysql_query($sql_get_id, $connection) or die ("Could not get the proper info");

while ($id= mysql_fetch_array($qry_get_id)) {
$test_id = $id[test_id];
}


for ($i = 1; $i <= $cu_s_number; $i++){
//Insert information in the correct table
$sql_insert_cu_s = "INSERT INTO cu_s
(test_id, cu_s_sample, cu_s_wt, cu_s_tare, cu_s_post)
VALUES ('$test_id', '$cu_s_sample[$i]', '$cu_s_wt[$i]', '$cu_s_tare[$i]', '$cu_s_post[$i]')
";
//Run Query
$qry_insert_cu_s = mysql_query($sql_insert_cu_s, $connection) or die ("Can not execute insert Query");

//Calculations for Cu Liquids
$cu_s_diff_value[$i] = $_POST[cu_s_post][$i] - $_POST[cu_s_tare][$i];
$diff_formatted[$i] = number_format($cu_s_diff_value[$i], 4);
if ($_POST[cu_s_wt][$i] == '0' or $_POST[cu_s_wt][$i] == null){
$cu_per_formatted[$i] = "DIV/0";
} else {
$cu_per_value[$i] = ($cu_s_diff_value[$i] / $_POST[cu_s_wt][$i]) * 100;
$cu_per_formatted[$i] = number_format($cu_per_value[$i], 1);
}
}

$display_block .= "<br>
<table border='1' cellspacing='1'>
<tr>
<th colspan='6' align='center'>Copper (Solids)</th>
</tr>
<tr>
<td>&nbsp&nbspSample&nbsp&nbsp</td>
<td>&nbsp&nbsp&nbsp&nbspWeight&nbsp&nbsp&nbsp&nbsp</td>
<td>&nbsp&nbsp&nbsp&nbsp&nbspTare&nbsp&nbsp&nbsp&nbsp&nbsp</td>
<td>&nbsp&nbsp&nbsp&nbsp&nbspPost&nbsp&nbsp&nbsp&nbsp&nbsp</td>
<td>&nbsp&nbsp&nbsp&nbsp&nbspDiff.&nbsp&nbsp&nbsp&nbsp&nbsp</td>
<td>Copper (%)</td>
</tr>";
for ($i = 1; $i <= $_POST[cu_s_number]; $i++){
$display_block .= "
<tr>
<td align='center'>$cu_s_sample[$i]</td>
<td align='center'>$cu_s_wt[$i]</td>
<td align='center'>$cu_s_tare[$i]</td>
<td align='center'>$cu_s_post[$i]</td>
<td align='center'>$diff_formatted[$i]</td>
<td align='center'>$cu_per_formatted[$i]</td>
</tr>";
}

$display_block .= "</table>";

[/code]

Thanks

Share this post


Link to post
Share on other sites
I cant see any code that is relvent to the code you posted in your first post. So I'm assuming the following is the code you are having difficulty with:
[code=php:0]

for ($i = 1; $i <= $_POST[cu_s_number]; $i++){
$display_block .= "
<tr>
<td align='center'>$cu_s_sample[$i]</td>
<td align='center'>$cu_s_wt[$i]</td>
<td align='center'>$cu_s_tare[$i]</td>
<td align='center'>$cu_s_post[$i]</td>
<td align='center'>$diff_formatted[$i]</td>
<td align='center'>$cu_per_formatted[$i]</td>
</tr>";
}[/code]

is that the code you are having problems with.

Share this post


Link to post
Share on other sites
[quote author=wildteen88 link=topic=106253.msg424783#msg424783 date=1156951999]
I cant see any code that is relvent to the code you posted in your first post. So I'm assuming the following is the code you are having difficulty with:
[code=php:0]

for ($i = 1; $i <= $_POST[cu_s_number]; $i++){
$display_block .= "
<tr>
<td align='center'>$cu_s_sample[$i]</td>
<td align='center'>$cu_s_wt[$i]</td>
<td align='center'>$cu_s_tare[$i]</td>
<td align='center'>$cu_s_post[$i]</td>
<td align='center'>$diff_formatted[$i]</td>
<td align='center'>$cu_per_formatted[$i]</td>
</tr>";
}[/code]

is that the code you are having problems with.
[/quote]

Sorry about the code, I have many files that are very similar.

Yes this is the code that I am concerned with.

I was able to get you thing to work for 1 item, can I get all items at 1 time, cu_s_sample, cu_s_wt, cu_s_tare, cu_s_post at the same time?

Also above in the code, I would like to write it to a table,

[code=php:0]
for ($i = 1; $i <= $cu_s_number; $i++){
//Insert information in the correct table
$sql_insert_cu_s = "INSERT INTO cu_s
(test_id, cu_s_sample, cu_s_wt, cu_s_tare, cu_s_post)
VALUES ('$test_id', '$cu_s_sample[$i]', '$cu_s_wt[$i]', '$cu_s_tare[$i]', '$cu_s_post[$i]')
";
//Run Query
$qry_insert_cu_s = mysql_query($sql_insert_cu_s, $connection) or die ("Can not execute insert Query");
[/code]

Again, thanks for all the help

Share this post


Link to post
Share on other sites
Thanks to wildteen88 and mostly to ober who openned my eyes to the great command of "foreach"

I was able to get it to work for all my items, with only 1 foreach statement

Here is my working code!!

[code=php:0]
<?
//Include Connection information
include("include/connection.php");

//Get the Test id for new info
$sql_get_id = "SELECT test_id
FROM test_info
WHERE test_name = '$_POST[test_name]'
";

$qry_get_id = mysql_query($sql_get_id, $connection) or die ("Could not get the proper info");

while ($id= mysql_fetch_array($qry_get_id)) {
$test_id = $id[test_id];
}

$display_block .= "<br>
<table border='1' cellspacing='1'>
<tr>
<th colspan='6' align='center'>Copper (Solids)</th>
</tr>
<tr>
<td>&nbsp&nbspSample&nbsp&nbsp</td>
<td>&nbsp&nbsp&nbsp&nbspWeight&nbsp&nbsp&nbsp&nbsp</td>
<td>&nbsp&nbsp&nbsp&nbsp&nbspTare&nbsp&nbsp&nbsp&nbsp&nbsp</td>
<td>&nbsp&nbsp&nbsp&nbsp&nbspPost&nbsp&nbsp&nbsp&nbsp&nbsp</td>
<td>&nbsp&nbsp&nbsp&nbsp&nbspDiff.&nbsp&nbsp&nbsp&nbsp&nbsp</td>
<td>Copper (%)</td>
</tr>";
$i = 1;
foreach ($_POST['cu_s_sample'] as $cu_example)
{
//Insert information in the correct table
$sql_insert_cu_s = "INSERT INTO cu_s
(test_id, cu_s_sample, cu_s_wt, cu_s_tare, cu_s_post)
VALUES ('$test_id', '" . $cu_example . "', '" . $_POST[cu_s_wt][$i] . "', '" . $_POST[cu_s_tare][$i] . "', '" . $_POST[cu_s_post][$i] . "')
";
//Run Query
$qry_insert_cu_s = mysql_query($sql_insert_cu_s, $connection) or die ("Can not execute insert Query");

//Calculations for Copper Solids
$cu_s_diff_value[$i] = $_POST[cu_s_post][$i] - $_POST[cu_s_tare][$i];
$diff_formatted[$i] = number_format($cu_s_diff_value[$i], 4);
if ($_POST[cu_s_wt][$i] == '0' or $_POST[cu_s_wt][$i] == null){
$cu_per_formatted[$i] = "DIV/0";
} else {
$cu_per_value[$i] = ($cu_s_diff_value[$i] / $_POST[cu_s_wt][$i]) * 100;
$cu_per_formatted[$i] = number_format($cu_per_value[$i], 1);
}

$display_block .= "
<tr>
<td align='center'>" . $cu_example . "</td>
<td align='center'>" . $_POST[cu_s_wt][$i] . "</td>
<td align='center'>" . $_POST[cu_s_tare][$i] . "</td>
<td align='center'>" . $_POST[cu_s_post][$i] . "</td>
<td align='center'>" . $diff_formatted[$i] . "</td>
<td align='center'>" . $cu_per_formatted[$i] . "</td>
";
$i++;
}



$display_block .= "</table>";

?>
[/code]

THANKS AGAIN, I love this site!!

Share this post


Link to post
Share on other sites
Glad you got it worked out. However you didnt actually need the foreach loop, all you needed to do is define you variables. Fo example.
[code]$cu_s_number = mysql_real_escape_string($_POST['cu_s_number']);[/code]
And your for loop will work.

Here is (probably unneded) code:
[code]<?php

//Include Connection information
include("include/connection.php");

$test_name = mysql_real_escape_string($_POST['test_name']);

//Get the Test id for new info
$sql_get_id = "SELECT test_id FROM test_info WHERE test_name = '$test_name'";

$qry_get_id = mysql_query($sql_get_id, $connection) or die ("Could not get the proper info");

$test_id = mysql_fetch_array($qry_get_id);

$cu_s_number = mysql_real_escape_string($_POST['cu_s_number']);
$cu_s_sample = mysql_real_escape_string($_POST['cu_s_sample']);
$cu_s_wt = mysql_real_escape_string($_POST['cu_s_wt']);
$cu_s_tare = mysql_real_escape_string($_POST['cu_s_tare']);
$cu_s_poste = mysql_real_escape_string($_POST['cu_s_post']);
$cu_s_diff_value = mysql_real_escape_string($_POST['cu_s_diff_value']);

for ($i = 1; $i <= $cu_s_number; $i++)
{
    //Insert information in the correct table
$sql_insert_cu_s = "INSERT INTO cu_s (test_id, cu_s_sample, cu_s_wt, cu_s_tare, cu_s_post)
                        VALUES ('$test_id', '$cu_s_sample[$i]', '$cu_s_wt[$i]', '$cu_s_tare[$i]', '$cu_s_post[$i]')";

    //Run Query
$qry_insert_cu_s = mysql_query($sql_insert_cu_s, $connection) or die ("Can not execute insert Query");

//Calculations for Cu Liquids

    $diff_formatted[$i] = number_format($cu_s_diff_value[$i], 4);

    if ($_POST['cu_s_wt'][$i] == '0' or $_POST['cu_s_wt'][$i] == null)
    {
    $cu_per_formatted[$i] = "DIV/0";
}
    else
    {
    $cu_per_value[$i] = ($cu_s_diff_value[$i] / $_POST['cu_s_wt'][$i]) * 100;
$cu_per_formatted[$i] = number_format($cu_per_value[$i], 1);
}
}

$display_block .= <<<HTML
<br>
<table border="1" cellspacing="1">
<tr>
<th colspan="6" align="center">Copper (Solids)</th>
</tr>
<tr>
<td>&nbsp;&nbsp;Sample&nbsp;&nbsp;</td>
<td>&nbsp;&nbsp;&nbsp;&nbsp;Weight&nbsp;&nbsp;&nbsp;&nbsp;</td>
<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tare&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Post&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Diff.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
<td>Copper (%)</td>
</tr>
HTML;
// DO NOT INDENT ABOVE LINE!

for ($i = 1; $i <= $cu_s_number; $i++)
{
    $display_block .= <<<HTML
<tr>
<td align="center">{$cu_s_sample[$i]}</td>
<td align="center">{$cu_s_wt[$i]}</td>
<td align="center">{$cu_s_tare[$i]}</td>
<td align="center">{$cu_s_post[$i]}</td>
<td align="center">{$diff_formatted[$i]}</td>
<td align="center">{$cu_per_formatted[$i]}</td>
</tr>
HTML;
// DO NOT INDENT ABOVE LINE!
}

$display_block .= "</table>";

?>[/code]

Share this post


Link to post
Share on other sites

×

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.