Jump to content

Recommended Posts

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


}

Link to comment
https://forums.phpfreaks.com/topic/65452-how-does-the-myvalue-get-populated/
Share on other sites

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.

 

 

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

.

.

.

.

.

.

 

 

 

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?

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.