Jump to content

[SOLVED] Complete Newbie Qeustion


tlavelle

Recommended Posts

I have this page:

<html>
<head>
<basefont face="Arial">
</head>
<body>

<?php

// set server access variables
$host = "localhost";
$user = "root";
$pass = "";
$db = "wercbench";


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

// create query
$year_query = "SELECT distinct year FROM userdata";
$industry_query = "SELECT distinct comp_desc FROM comp_desc";

// execute query
$year_result = mysql_query($year_query) or die ("Error in query: $query. ".mysql_error());
$industry_result = mysql_query($industry_query) or die ("Error in query: $query. ".mysql_error());
// see if any rows were returned
if (mysql_num_rows($year_result) > 0 or mysql_num_rows($industry_result)){
     // yes
     // print them one after another

 echo"<form method=\"post\" action=\"uservals.php\" name=\"wercbench1\">";
     echo"<table style=\"width: 100%;\" border=\"1\" cellpadding=\"2\" cellspacing=\"2\">";
     echo"<tbody>";
     echo" <tr>";
     echo"   <td>Select the comparison year and group then click next";
     echo"   </td>";
     echo"   <td></td>";
     echo" </tr>";
     echo" <tr>";
     echo"  <td>";
     echo"   <select name=\"year\">";
     while($row = mysql_fetch_row($year_result)) {
     echo"   <option>".$row[0]."</option>";
 }
     echo"   </select>";
     echo"   </td>";
     echo"   <td></td>";
     echo" </tr>";


     echo" <tr>";
     echo"   <td>";
 echo"   <select name=\"industry\">";
  while($row = mysql_fetch_row($industry_result)) {
     echo"   <option>".$row[0]."</option>";
}
     echo"   </select>";
 echo"</td>";
     echo"   <td></td>";
     echo" </tr>";
     echo" <tr>";
     echo"   <td><input type=\"Submit\" value=\"Next\" name=\"wercbench1_next\"></button></td>";
     echo"   <td></td>";
     echo" </tr>";

    echo"</tbody>";
  echo"</table>";
echo"</form>";
}
else {
    // no
    // print status message
    echo "No rows found!";
}
// free result set memory
mysql_free_result($year_result);
mysql_free_result($industry_result);

// close connection
mysql_close($connection);

?>

</body>
</html>

And uservals.php shows nothing:

<html>
<head>
<basefont face="Arial">
</head>
<body>

<?php

// set server access variables
$host = "localhost";
$user = "root";
$pass = "";
$db = "wercbench";
$useryear=$_Post['year'];
$usergroup=$_Post['industry'];

echo "$useryear";
echo $_Post['year'];
?>
</body>
</html>

 

How do I pass the selected values of the dropdown to the post?

Link to comment
Share on other sites

As previosly said u need to capitalize post:

 

$useryear=$_Post['year'];
$usergroup=$_Post['industry'];

 

to

 

$useryear=$_POST['year'];
$usergroup=$_POST['industry'];

 

As long as ure using not multiple selects, ure fine with your code. Post will return the selected value.

Link to comment
Share on other sites

Conceptually, I dont really get how the array population works.

 

  while($row = mysql_fetch_row($year_result)) {

    echo"<option value=\"" .$row[0]. "\">".$row[0]."</option>";

 

does mysql_fetch_row increment the array index until it runs out of rows?  how does the array get populated with this.  Can someone narrate the logic?

Link to comment
Share on other sites

Directly from php.net:

Returns an numerical array of strings that corresponds to the fetched row, or FALSE if there are no more rows.

 

mysql_fetch_row() fetches one row of data from the result associated with the specified result identifier. The row is returned as an array. Each result column is stored in an array offset, starting at offset 0.

 

Every time the while loop loops mysql_fetch_rows gets a new row from the result returned from the sql query.

Link to comment
Share on other sites

This code:

 

if (mysql_num_rows($metric_result) > 0) {

    // yes

    // print them one after another

    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>".$row[3]."</td>";

        echo "</tr>";

    }

    echo "</table>";

}

else {

    // no

    // print status message

    echo "No rows found!";

}

 

Yields this table:

 

Metric Description Average Best Practice Enter your value here

On time receipts 0.92 0.98

On time shipments 0.98 0.99

Fill rate-line 0.96 0.99

Fill rate-order 0.96 0.99

% of orders shipped complete 0.96 0.99

% of overtime hours 0.1 0.04

Days of raw materials on hand 20 10

 

 

How do I turn this table into a form where the last field in each row is a text box that users can enter data into?  Is it possible to leverage the while loop in some way to dynamically generate a form?

Link to comment
Share on other sites

EDIT:

 

didn't read your post properly. Hold on...

 

change this:

echo "<td>".$row[3]."</td>";

to this:

echo '<td><input type="text" name="myvalue[]" value="' . $row[3] . '" /></td>';

That will now change the forth column into a editable column.

 

In order to submit the data you'll need to warp the table in a form and add a submit button at the end.

To do this you'll want to add the following line:

Echo '<form action="path/to/script/to/process/form/data.php" method="post">';

Before this line

echo "<table cellpadding=10 border=1>";

 

And change this line:

echo "</table>";

to:

echo '  <tr>
    <td colspan="3"> </td>
    <td align="center"><input type="submit" name="submit" value="Apply"></td>
  </tr>
</table>';

 

In the script that processes the form data you'll want to use

$_POST['myvalue']

to get the data from the form.

 

NOTE: $_POST['myvalue'] holds an array of all the form field, so $_POST['myvalue'][0] gets the first field, $_POST['myvalue'][1] gets second, $_POST['myvalue'][9] gets the 10th etc,

 

 

Link to comment
Share on other sites

I did this:

 

if (mysql_num_rows($metric_result) > 0) {

    // yes

    // print them one after another

Echo '<form action="path/to/script/to/process/form/data.php" method="post">';

    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=\"" . $row[3] . " /></td>";

        echo "</tr>";

    }

    echo "<tr><td colspan=/"3/"> </td><td align=/"center/"><input type=/"submit/" name=/"submit/" value=/"Apply/"></td></tr></table>";

}

else {

    // no

    // print status message

    echo "No rows found!";

}

 

 

and am getting this error

 

 

Parse error: syntax error, unexpected T_LNUMBER, expecting ',' or ';' in C:\xampp\htdocs\wercbench\uservals.php on line 59

 

 

Another newbie follow up question.  Do you need an echo statement per line of html or can you basically say echo "Entire HTLM Source code"??

 

 

Link to comment
Share on other sites

You used forward slashes instead of backslashes on line 22 for escaping quotes.

 

Yes using single quotes as the string delimiter allows to use double quotes (without having to escape them) within strings, however if you use a single quote within the string you'll have to escape it. Also variables wont work within single quotes. You'll have to concatenate the string to use variables within the string.

Link to comment
Share on other sites

Thanks for all your help.  I still am not there.  This code produced one row

 

if (mysql_num_rows($metric_result) > 0) {

    // yes

    // print them one after another

echo "<form action=\"path/to/script/to/process/form/data.php\" method=\"post\">";

    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=\"" . $row[3] . " /></td>";

        echo "</tr>";

    }

    echo "<tr><td colspan=\"3\"> </td><td align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Apply\"></td></tr></table>";

}

else {

    // no

    // print status message

    echo "No rows found!";

}

 

 

It produced a row with the textbox containing this:

 

/></td></tr><tr><td>On time shipments</td><td>0.98</td><td>0.99</td><td><input type=

Link to comment
Share on other sites

This seems to produce the desired result.  Is this valid?

if (mysql_num_rows($metric_result) > 0) {

    // yes

    // print them one after another

echo "<form action=\"path/to/script/to/process/form/data.php\" method=\"post\">";

    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=\"" . $row[3] . " /></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=\"submit\" value=\"Apply\"></td></tr></table>";

}

else {

    // no

    // print status message

    echo "No rows found!";

}

Link to comment
Share on other sites

No

 

Change this

//echo "<td><input type=\"text\" name=\"myvalue[]\" value=\"" . $row[3] . " /></td>";
      echo "<td><input type=\"text\" name=\"myvalue[]\" value=\"\" /></td>";

to:

echo "<td><input type=\"text\" name=\"myvalue[]\" value=\"" . $row[3] . "\" /></td>";

 

NOTE: Make sure you change value of the action attribute in the form tag on this line:

echo "<form action=\"path/to/script/to/process/form/data.php\" method=\"post\">";

To the script that processes the submited form data.

Link to comment
Share on other sites

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.