tlavelle Posted August 17, 2007 Share Posted August 17, 2007 Another noobie question. So I have this form if(!isset($_POST['wercbench2']) and (mysql_num_rows($metric_result)) > 0){ // yes // print them one after another //echo "<form action=\"results.php\" method=\"post\" name\"wercbench2\">"; echo "<form action=\"results.php\" method=\"post\" name\"wercbench2\">"; echo "<table cellpadding=10 border=1>"; echo "<tr>"; echo "<td>Metric Description</td>"; echo "<td>Average</td>"; echo "<td>Best Practice</td>"; echo "<td>Enter your value here</td>"; echo "</tr>"; while($row = mysql_fetch_row($metric_result)) { echo "<tr>"; echo "<td>".$row[0]."</td>"; echo "<td>" . $row[1]."</td>"; echo "<td>".$row[2]."</td>"; echo "<td><input type=\"text\" name=\"myvalue[]\" value=\"\" /></td>"; echo "</tr>"; } echo "<tr><td colspan=\"3\"> </td><td align=\"center\"><input type=\"submit\" name=\"wercbench_next2\" value=\"Compare\"></td></tr></table>"; } And this code that processes the form $len = mysql_num_rows($metric_result); for( $i = 0; $i < $len; ++$i ) { $row = mysql_fetch_row($metric_result); echo "<tr>"; echo "<td>" . $row[0]. "</td>"; echo "<td>" . $row[1]. "</td>"; echo "<td>" . $row[2]. "</td>"; echo "<td>" . $myvalue[$i] . "</td>"; echo "</tr>"; } } $_SESSION['Posted_values'] = array(); foreach($_POST as $fld => $val) $_SESSION['Posted_values'][$fld] = $val; echo '<pre>' . print_r($_SESSION,true) . '</pre>'; My questions are: How does the $myvalue array get populated? Where do the indexes come from? This code snippet seems to understand the $myvalue array $_SESSION['Posted_values'] = array(); foreach($_POST as $fld => $val) $_SESSION['Posted_values'][$fld] = $val; echo '<pre>' . print_r($_SESSION,true) . '</pre>'; while this snippet does not $len = mysql_num_rows($metric_result); for( $i = 0; $i < $len; ++$i ) { $row = mysql_fetch_row($metric_result); echo "<tr>"; echo "<td>" . $row[0]. "</td>"; echo "<td>" . $row[1]. "</td>"; echo "<td>" . $row[2]. "</td>"; echo "<td>" . $myvalue[$i] . "</td>"; echo "</tr>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/65452-how-does-the-myvalue-get-populated/ Share on other sites More sharing options...
MadTechie Posted August 17, 2007 Share Posted August 17, 2007 probably from an included file.. not in that code supplied unless global register is on Quote Link to comment https://forums.phpfreaks.com/topic/65452-how-does-the-myvalue-get-populated/#findComment-326841 Share on other sites More sharing options...
wildteen88 Posted August 17, 2007 Share Posted August 17, 2007 In the following code: <?php $_SESSION['Posted_values'] = array(); foreach($_POST as $fld => $val) $_SESSION['Posted_values'][$fld] = $val; echo '<pre>' . print_r($_SESSION,true) . '</pre>'; ?> PHP is looping through the $_POST array variable which it then stores all _POST data into the Posted_values session variable. In your second block of code where does $myvalue come from? Looks like you are pulling data out of the database then you seem to pop $myvalue into that block of code from no where. If you want to use the myvalue from the Posted_values session you should use $_SESSION['Posted_values']['myvalue'] instead. Quote Link to comment https://forums.phpfreaks.com/topic/65452-how-does-the-myvalue-get-populated/#findComment-326887 Share on other sites More sharing options...
tlavelle Posted August 17, 2007 Author Share Posted August 17, 2007 $myvalue comes from user entered data from the first form in the first file. Quote Link to comment https://forums.phpfreaks.com/topic/65452-how-does-the-myvalue-get-populated/#findComment-326937 Share on other sites More sharing options...
dbo Posted August 17, 2007 Share Posted August 17, 2007 Can you paste the output for print_r($_SESSION, true)? Quote Link to comment https://forums.phpfreaks.com/topic/65452-how-does-the-myvalue-get-populated/#findComment-326946 Share on other sites More sharing options...
tlavelle Posted August 17, 2007 Author Share Posted August 17, 2007 I'm sorry. I am just not wrapping my head around this. This code <?php session_start(); ?> <html> <head> <basefont face="Arial"> </head> <body> <?php // set server access variables $host = "localhost"; $user = "root"; $pass = ""; $db = "wercbench"; $useryear=$_SESSION['sess_year']; // open connection $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); // select database mysql_select_db($db) or die ("Unable to select database!"); //define Query $metric_query = "SELECT metric_desc.met_desc, userdata.median, userdata.best_pract FROM metric_desc, userdata where metric_desc.met_key=userdata.met_key and $useryear=userdata.year"; //execute query $metric_result = mysql_query($metric_query) or die ("Error in query: $query. ".mysql_error()); if(!isset($_POST['wercbench2']) and (mysql_num_rows($metric_result)) > 0){ echo "<form action=\"results.php\" method=\"post\" name\"wercbench2\">"; echo "<table cellpadding=10 border=1>"; echo "<tr>"; echo "<td>Metric Description</td>"; echo "<td>Average</td>"; echo "<td>Best Practice</td>"; echo "<td>Your Data</td>"; echo "<td>Your Data compared to Best Practice</td>"; echo "</tr>"; //create index for $myvalue array based on number of results returned from $metric_result query $len = mysql_num_rows($metric_result); for( $i = 0; $i < $len; ++$i ) { $row = mysql_fetch_row($metric_result); echo "<tr>"; echo "<td>" . $row[0]. "</td>"; echo "<td>" . $row[1]. "</td>"; echo "<td>" . $row[2]. "</td>"; echo "<td>" . $_SESSION['Posted_values']['myvalue']. "</td>"; echo "</tr>"; } } $_SESSION['Posted_values'] = array(); foreach($_POST as $fld => $val) $_SESSION['Posted_values'][$fld] = $val; echo '<pre>' . print_r($_SESSION,true) . '</pre>'; ?> </body> </html> is now yielding these results Array ( [Posted_values] => Array ( [myvalue] => Array ( [0] => 6 [1] => 6 [2] => 6 [3] => 6 [4] => 6 [5] => 6 [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => [17] => [18] => [19] => [20] => [21] => [22] => [23] => [24] => [25] => [26] => [27] => [28] => [29] => [30] => [31] => [32] => [33] => [34] => [35] => [36] => [37] => [38] => [39] => [40] => [41] => [42] => [43] => [44] => [45] => [46] => [47] => [48] => [49] => [50] => [51] => [52] => [53] => [54] => ) [wercbench_next2] => Compare ) [sess_year] => 2005 [sess_ind] => All ) Metric Description Average Best Practice Your Data Your Data compared to Best Practice On time receipts 0.92 0.98 Array On time shipments 0.98 0.99 Array Fill rate-line 0.96 0.99 Array Fill rate-order 0.96 0.99 Array % of orders shipped complete 0.96 0.99 Array % of overtime hours 0.1 0.04 Array Days of raw materials on hand 20 10 Array . . . . . . Quote Link to comment https://forums.phpfreaks.com/topic/65452-how-does-the-myvalue-get-populated/#findComment-326949 Share on other sites More sharing options...
wildteen88 Posted August 17, 2007 Share Posted August 17, 2007 Try using: echo "<td>" . $_SESSION['Posted_values']['myvalue'][$i] . "</td>"; instead of echo "<td>" . $_SESSION['Posted_values']['myvalue'] . "</td>"; $_SESSION['Posted_values']['myvalue'] contains an array. Quote Link to comment https://forums.phpfreaks.com/topic/65452-how-does-the-myvalue-get-populated/#findComment-326953 Share on other sites More sharing options...
dbo Posted August 17, 2007 Share Posted August 17, 2007 Hell I've learned something. I didn't realize you could name your fields (in HTML) as myvalue[] for example and it will force the value into an array. I guess I've never really had a need to but it could be useful. Quote Link to comment https://forums.phpfreaks.com/topic/65452-how-does-the-myvalue-get-populated/#findComment-326958 Share on other sites More sharing options...
MadTechie Posted August 17, 2007 Share Posted August 17, 2007 works a treat on forms Quote Link to comment https://forums.phpfreaks.com/topic/65452-how-does-the-myvalue-get-populated/#findComment-326961 Share on other sites More sharing options...
tlavelle Posted August 17, 2007 Author Share Posted August 17, 2007 Thanks...A few questions. I just cannot wrap my mind around this Each textbox in the form has a name of $myvalue[] so the name of each textbox is an array variable? Does the posting of that form create the index values for each array element? How do the name value pairs get generated? also, what is this? . $_SESSION['Posted_values']['myvalue'][$i] . I understand $_SESSION['Posted_values'] is one array I am not really sure what ['myvalue'] is. IS this an additional index for a 2 dimensional array? And I get what $i is What statement assigns the values of the textboxes to the $myvalue array? Quote Link to comment https://forums.phpfreaks.com/topic/65452-how-does-the-myvalue-get-populated/#findComment-326962 Share on other sites More sharing options...
tlavelle Posted August 17, 2007 Author Share Posted August 17, 2007 Hell I've learned something. I didn't realize you could name your fields (in HTML) as myvalue[] for example and it will force the value into an array. I guess I've never really had a need to but it could be useful. Yes...please expalin this moire. I don't understand hwo this works Quote Link to comment https://forums.phpfreaks.com/topic/65452-how-does-the-myvalue-get-populated/#findComment-326963 Share on other sites More sharing options...
dbo Posted August 17, 2007 Share Posted August 17, 2007 Hrmmm. Can you do it as an associative array of sorts? I'm not sure how a list of unamed variables buys me much if I have no way to identify them. Quote Link to comment https://forums.phpfreaks.com/topic/65452-how-does-the-myvalue-get-populated/#findComment-326965 Share on other sites More sharing options...
dbo Posted August 17, 2007 Share Posted August 17, 2007 Which I think is why tlavelle is having a problem... Quote Link to comment https://forums.phpfreaks.com/topic/65452-how-does-the-myvalue-get-populated/#findComment-326966 Share on other sites More sharing options...
tlavelle Posted August 17, 2007 Author Share Posted August 17, 2007 I am sorry. I am not following those last 2 comments. What do you want as an associative array? and what list of unamed variables are you referring to? Quote Link to comment https://forums.phpfreaks.com/topic/65452-how-does-the-myvalue-get-populated/#findComment-326969 Share on other sites More sharing options...
dbo Posted August 17, 2007 Share Posted August 17, 2007 They were directed at madtechie. But the "unnamed" array indexes I was talking about are in your myvalue[] array. It's just a normal array indexed with numbers. If you could have myvalue['textbox1'] = "Jim"; etc it might be more useful.... this is the associative array I was talking about... or lack there of. Quote Link to comment https://forums.phpfreaks.com/topic/65452-how-does-the-myvalue-get-populated/#findComment-326971 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.