komquat Posted August 30, 2006 Share Posted August 30, 2006 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 advanceAndy Quote Link to comment https://forums.phpfreaks.com/topic/19154-how-to-print-an-array-from-a-post/ Share on other sites More sharing options...
ober Posted August 30, 2006 Share Posted August 30, 2006 I usually echo POSTed arrays like this:<?phpforeach($_REQUEST['fields'] as $key2){ $selectstr .= $key2 . ", ";}?> Quote Link to comment https://forums.phpfreaks.com/topic/19154-how-to-print-an-array-from-a-post/#findComment-82860 Share on other sites More sharing options...
komquat Posted August 30, 2006 Author Share Posted August 30, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/19154-how-to-print-an-array-from-a-post/#findComment-82862 Share on other sites More sharing options...
ober Posted August 30, 2006 Share Posted August 30, 2006 Your loss. Foreach is a godsend in your situation. Quote Link to comment https://forums.phpfreaks.com/topic/19154-how-to-print-an-array-from-a-post/#findComment-82865 Share on other sites More sharing options...
wildteen88 Posted August 30, 2006 Share Posted August 30, 2006 if $_POST[co2_s_sample] is an array you'll want to use $_POST[co2_s_sample][$i] to access the items in that arrayOr 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] Quote Link to comment https://forums.phpfreaks.com/topic/19154-how-to-print-an-array-from-a-post/#findComment-82866 Share on other sites More sharing options...
komquat Posted August 30, 2006 Author Share Posted August 30, 2006 [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 Quote Link to comment https://forums.phpfreaks.com/topic/19154-how-to-print-an-array-from-a-post/#findComment-82892 Share on other sites More sharing options...
wildteen88 Posted August 30, 2006 Share Posted August 30, 2006 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 Link to comment https://forums.phpfreaks.com/topic/19154-how-to-print-an-array-from-a-post/#findComment-82897 Share on other sites More sharing options...
komquat Posted August 30, 2006 Author Share Posted August 30, 2006 [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 Quote Link to comment https://forums.phpfreaks.com/topic/19154-how-to-print-an-array-from-a-post/#findComment-82911 Share on other sites More sharing options...
wildteen88 Posted August 30, 2006 Share Posted August 30, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/19154-how-to-print-an-array-from-a-post/#findComment-82913 Share on other sites More sharing options...
komquat Posted August 30, 2006 Author Share Posted August 30, 2006 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 informationinclude("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>  Sample  </td> <td>    Weight    </td> <td>     Tare     </td> <td>     Post     </td> <td>     Diff.     </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 Quote Link to comment https://forums.phpfreaks.com/topic/19154-how-to-print-an-array-from-a-post/#findComment-82918 Share on other sites More sharing options...
wildteen88 Posted August 30, 2006 Share Posted August 30, 2006 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 Link to comment https://forums.phpfreaks.com/topic/19154-how-to-print-an-array-from-a-post/#findComment-82922 Share on other sites More sharing options...
komquat Posted August 30, 2006 Author Share Posted August 30, 2006 [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 Quote Link to comment https://forums.phpfreaks.com/topic/19154-how-to-print-an-array-from-a-post/#findComment-82927 Share on other sites More sharing options...
komquat Posted August 30, 2006 Author Share Posted August 30, 2006 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 statementHere is my working code!![code=php:0]<?//Include Connection informationinclude("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>  Sample  </td> <td>    Weight    </td> <td>     Tare     </td> <td>     Post     </td> <td>     Diff.     </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!! Quote Link to comment https://forums.phpfreaks.com/topic/19154-how-to-print-an-array-from-a-post/#findComment-82942 Share on other sites More sharing options...
wildteen88 Posted August 30, 2006 Share Posted August 30, 2006 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 informationinclude("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> Sample </td> <td> Weight </td> <td> Tare </td> <td> Post </td> <td> Diff. </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] Quote Link to comment https://forums.phpfreaks.com/topic/19154-how-to-print-an-array-from-a-post/#findComment-82943 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.