Jump to content


Photo

How to print an array from a "Post"


  • Please log in to reply
13 replies to this topic

#1 komquat

komquat
  • Members
  • PipPipPip
  • Advanced Member
  • 130 posts
  • LocationHoughton, MI

Posted 30 August 2006 - 02:31 PM

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

	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>

The last two lines will not post.  I tried:

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

And that did not work.  Any suggestions?

Thanks in advance

Andy


#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 30 August 2006 - 02:46 PM

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

Info: PHP Manual


#3 komquat

komquat
  • Members
  • PipPipPip
  • Advanced Member
  • 130 posts
  • LocationHoughton, MI

Posted 30 August 2006 - 02:49 PM

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

#4 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 30 August 2006 - 02:50 PM

Your loss.  Foreach is a godsend in your situation.

Info: PHP Manual


#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 30 August 2006 - 02:50 PM

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:
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";
}


#6 komquat

komquat
  • Members
  • PipPipPip
  • Advanced Member
  • 130 posts
  • LocationHoughton, MI

Posted 30 August 2006 - 03:11 PM

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


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

#7 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 30 August 2006 - 03:14 PM

Run this code on co2_s_sample array:
echo "<pre>" . print_r($_POST['co2_s_sample '], true) . "</pre>";


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.

#8 komquat

komquat
  • Members
  • PipPipPip
  • Advanced Member
  • 130 posts
  • LocationHoughton, MI

Posted 30 August 2006 - 03:21 PM

Run this code on co2_s_sample array:

echo "<pre>" . print_r($_POST['co2_s_sample '], true) . "</pre>";


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.


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


That is what I want to display


#9 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 30 August 2006 - 03:25 PM

So this should work fine:
$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++;
}

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

#10 komquat

komquat
  • Members
  • PipPipPip
  • Advanced Member
  • 130 posts
  • LocationHoughton, MI

Posted 30 August 2006 - 03:28 PM

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

//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>";


Thanks

#11 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 30 August 2006 - 03:33 PM

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:

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>";
}

is that the code you are having problems with.

#12 komquat

komquat
  • Members
  • PipPipPip
  • Advanced Member
  • 130 posts
  • LocationHoughton, MI

Posted 30 August 2006 - 03:38 PM

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:


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>";
}

is that the code you are having problems with.


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,

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");

Again, thanks for all the help

#13 komquat

komquat
  • Members
  • PipPipPip
  • Advanced Member
  • 130 posts
  • LocationHoughton, MI

Posted 30 August 2006 - 04:00 PM

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!!

<?
//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>";

?>

THANKS AGAIN, I love this site!!

#14 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 30 August 2006 - 04:06 PM

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.
$cu_s_number = mysql_real_escape_string($_POST['cu_s_number']);
And your for loop will work.

Here is (probably unneded) 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>";

?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users