Forum Moderators: coopster
assuming i have select statement inside the function name selectProduction():
<select name="packTy">
this my for loops
<?php
<form method="POST" action="SaveProduction.php">
$row = 3;
$col = 6;
for ($r=1; $r<=$row; $r++)
{
echo '<table>';
echo '<tr>';
?>
<td width="285" id="info" NOWRAP="NOWRAP">
<table>
<tr></select></td>
<td width="44" height="30" valign="top">
<?php echo selectProduction();?>
<input type="hidden" size="6" name="value[]" value="<?=$r?>">
</td></tr>
</table>
</td>
<?for ($c=1; $c <=$col; $c++)
{
echo'<td><input type="text" size="6" name="col[]"></td>';
}
echo '</tr>';
echo '</table>';
}
</form>
<tr>
<td><input type="submit" name="SQuery" value="Submit" /></td>
</tr>
?>
and post it TO my database..
if(isset($_POST["SQuery"]))
{
$value = count($_POST["value"]);for($i=0; $i<$value; $i++)
{
$PackTye = $_POST["packTy"][$i];
echo ''.$PackTye.';
}
}
[edited by: dreamcatcher at 6:13 am (utc) on Oct. 6, 2009]
[edit reason] Fixed page view. [/edit]
looping..
<select name="packTy" value='A'>
<select name="packTy" value='A'>
<select name="packTy" value='B'>
when i submit it the well return F, F, F, it will only get the last value on my array supposed to A,A,B..
my question is how can i get the [b]packTy[\b] with the same value i selected..
for($i=0; $i<$value; $i++)
{
$PackTye = $_POST["packTy"][$i];
echo '.$PackTye.';
}
Only stores the value of $PactTye as of the last iteration of the loop, not all iterations, so if it has something to do with the function: selectProduction you can expect to only get the last value passed... (IOW Your current code has the value of $PackTye set to change every time through the for loop and finally ends up at the last value passed by the POST, which appears to be F.)
for($i=0; $i<$value; $i++)
{
$PackTye[$i] = $_POST["packTy"][$i];
echo '.$PackTye.';
}
Would probably work better for you.
Then you can reference $PackTye[NUMBER] for your outputs.
function selectProduction()
{
$resultz = mssql_query("select PackTypeID,PackType,Market from PackType where Market ='China'");
$limitz = mssql_num_rows($resultz);if($limitz > 0)
{echo '<select name="packTy">';
for ($v = 0; $v < $limitz; $v++)
{
$PackType = mssql_result($resultz, $v, "PackType");
$PackTypeID = mssql_result($resultz, $v, "PackTypeID");echo '
<option value='.$PackTypeID.'>'.$PackType.'</option>
';}
echo '</select>';}
else
{echo 'Pag Sure oi';
}}
?>
Change
echo '<select name="packTy">';
to
echo '<select name="packTy[]">';
Anyway, debug your PHP using this
if(isset($_POST["SQuery"]))
{
print_r($_POST["SQuery"]);
$value = count($_POST["value"]);for($i=0; $i<$value; $i++) {
$PackTye = $_POST["packTy"][$i];
echo ''.$PackTye.';
}
on submission form return
1st loop
1 A
2 B
3
2nd loop
1
2
3
---------------------------------
here is the default one
how cant i get the value of <select name="packTy">
and put in my second loop $row and return it
----------------------------------
$col = 6
1st loop
1 A
2 A
3 A
4 A
5 A
6 A
second
1 B
2 B
3 B
4 B
5 B
6 B
<select name="packTy" value='A'>
<select name="packTy" value='A'>
<select name="packTy" value='B'>
You need to pass the information into the function and add it to the select:
function selectProduction()
{
global $PackTye, $r;
$resultz = mssql_query("select PackTypeID,PackType,Market from PackType where Market ='China'");
$limitz = mssql_num_rows($resultz);
if($limitz > 0)
{
echo '<select name="packTy" value="'.$PackTye[$r].'">';
for ($v = 0; $v < $limitz; $v++)
{
$PackType = mssql_result($resultz, $v, "PackType");
$PackTypeID = mssql_result($resultz, $v, "PackTypeID");
echo '
<option value='.$PackTypeID.'>'.$PackType.'</option>
';
}
echo '</select>';
}
else
{
echo 'Pag Sure oi';
}
}
?>
ADDED:
You will also need to use this (suggested previously) to set the array $PackTye:
for($i=0; $i<$value; $i++)
{
$PackTye[$i] = $_POST["packTy"][$i];
echo '.$PackTye[$i].';
}
Added [$i] to $PackTye for accuracy of the echo, although it should not matter for the actual function since it is stored and accessed as an array...
for($i=0; $i<$value; $i++)
{
$PackTye[$i] = $_POST["packTy"][$i];
echo '.$PackTye[$i].';
}
I forgot the key [$i] to echo the correct array piece, so it tried to echo $PackTye as a variable, not an array, which gives you 'array' in the echo statement rather than the value associated with the key... Sry.
Other than that, I'm not sure why it would, so you'll have to be more specific.
/* This is what your code looks like 'straight through'. I removed the function and made the changes I suggested, then pasted the code straight through, because sometimes it's easier to trouble shoot. I cannot find an issue based on what you requested compared to how the code is written, until possibly in the bottom section where I made a note, so either: A.) I'm just plain missing something. B.) I do not understand your project well enough. C.) There is an error in some other portion of the code causing it to behave unexpectedly. NOTE: You will have to POST data to the page for the code (all the way down to the for ($c=1; $c <=$col; $c++) line) to run correctly, becuse there is nothing to stop it from running in the absence of POST data. It will try to run and connect to your database and complete the loops every time the page is loaded. I don't know enough about how you are doing things to make much of a suggestion beyond: Make sure there is data posted before attempting to run the top section of code... KEEP IN MIND: This code will not work as posted. You will ned to make adjustments to it, but the adjustments should be simple and minor and have to be specific to the rest of your code, so I can't really help you there. */
if(isset($_POST["SQuery"]))
{
$value = count($_POST["value"]);
for($i=0; $i<$value; $i++)
{
$PackTye[$i] = $_POST["packTy"][$i];
echo '.$PackTye[$i].';
}
}
<form method="POST" action="SaveProduction.php">
$row = 3;
$col = 6;
for ($r=1; $r<=$row; $r++)
{
echo '<table>';
echo '<tr>';
?>
<td width="285" id="info" NOWRAP="NOWRAP">
<table>
<tr>
</select></td>
<td width="44" height="30" valign="top">
<?php
$resultz = mssql_query("select PackTypeID,PackType,Market from PackType where Market ='China'");
$limitz = mssql_num_rows($resultz);
if($limitz > 0)
{
echo '<select name="packTy" value="'.$PackTye[$r].'">';
for ($v = 0; $v < $limitz; $v++)
{
$PackType = mssql_result($resultz, $v, "PackType");
$PackTypeID = mssql_result($resultz, $v, "PackTypeID");
echo '
<option value='.$PackTypeID.'>'.$PackType.'</option>
';
}
echo '</select>';
}
else
{
echo 'Pag Sure oi';
}
}
?>
<input type="hidden" size="6" name="value[]" value="<?=$r?>">
</td>
</tr>
</table>
</td>
<?php
/* EDITED: I found where $col comes from, but it looks like you mean something like $col rather than what you have... col[] will set the name of the text input to col[] and try to pass that in the POST as POST[col[]]... Other than that I have no idea what this section of code does, so there's no way for me to know if this is where the issue is or not. ADDED: I think part of the issue is your Form Field Names... value[] and col[]. I don't understand what they are supposed to do? It's not something I've seen / used, so maybe they should be something different? Possibly col[$col]? */
for ($c=1; $c <=$col; $c++)
{
echo'<td><input type="text" size="6" name="col[]"></td>';
}
echo '</tr>';
echo '</table>';
}
</form>
<tr>
<td><input type="submit" name="SQuery" value="Submit" /></td>
</tr>
?>
I Meant: Possibly col[$c]? Above. Sorry, getting late and didn't get it edited in time. */
As I said above, I have not seen col[] or value[] as form field names. It's possible it's entirely valid and I don't know about it, so dumping the POST variable with print_r() should tell you more than I can. Personally, I always work with meaningful names in my form fields, so there is no confusion...
echo'<td><input type="text" size="6" name="col'.$c.'"></td>';
Is what I would probably use, then it is just as easy to access with a loop.
If there are 6 cols:
for($i=0;$i<6;$i++) {
$col[$i]=$=POST['col'.$i];
}
Or something similar will give me an array of the cols on the page receiving the POSTed data.