homepage Welcome to WebmasterWorld Guest from 54.196.63.93
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
for loops with select option
nanat

5+ Year Member



 
Msg#: 4001916 posted 5:23 am on Oct 6, 2009 (gmt 0)

hi guys i try to figure out this for 3 days and i don't get it so give up.. if syntoms persist post a thread or find and expert.. :D

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>
?>


MY question is how can i get the <select name="packTy"> base in the 3 rows?

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]

 

tomda

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4001916 posted 5:58 am on Oct 6, 2009 (gmt 0)

Your php is correct.

What is exactly your problem. Do you get POST data ?

I think that your submit button should be enclosed within the form tag. Just put the </form> after the submit button.

nanat

5+ Year Member



 
Msg#: 4001916 posted 6:35 am on Oct 6, 2009 (gmt 0)

my problem is the on submission on the select. supposed to be i have 3 loops $row and the content data is A,B,C,D,E,F

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..

jd01

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4001916 posted 6:55 am on Oct 6, 2009 (gmt 0)

This section of code:

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.

tomda

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4001916 posted 6:58 am on Oct 6, 2009 (gmt 0)

My bad... JD01 seems to be right.

[edited by: tomda at 7:00 am (utc) on Oct. 6, 2009]

nanat

5+ Year Member



 
Msg#: 4001916 posted 6:58 am on Oct 6, 2009 (gmt 0)

thx guys for the quick replies
here my function selectProduction()

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

}
?>


tomda

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4001916 posted 7:07 am on Oct 6, 2009 (gmt 0)

I may be wrong but I think that packTy should be in array form

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.';
}

}

nanat

5+ Year Member



 
Msg#: 4001916 posted 7:19 am on Oct 6, 2009 (gmt 0)

thx tomda.. i already test it that function before it seems the return is A B

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

jd01

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4001916 posted 7:32 am on Oct 6, 2009 (gmt 0)

If this is accurate for what you want:

<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...

nanat

5+ Year Member



 
Msg#: 4001916 posted 7:57 am on Oct 6, 2009 (gmt 0)

tnx.. jd01.. why is it the return steps is array,array,array?

jd01

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4001916 posted 8:03 am on Oct 6, 2009 (gmt 0)

If you mean the echo, it's because I forgot to change it to this when I first posted:

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.

nanat

5+ Year Member



 
Msg#: 4001916 posted 8:35 am on Oct 6, 2009 (gmt 0)

sory jd01 but its doesn't make sense for my looping the result is only in the 1st loop 1 A..

$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

jd01

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4001916 posted 8:59 am on Oct 6, 2009 (gmt 0)

<?php

/* 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>
?>

jd01

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4001916 posted 9:10 am on Oct 6, 2009 (gmt 0)

/* 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?

I Meant: Possibly col[$c]? Above. Sorry, getting late and didn't get it edited in time. */

jd01

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4001916 posted 9:32 am on Oct 6, 2009 (gmt 0)

Honestly, if it was me, the first thing I would do is print_r(POST) after each submit and make sure the information was stored as I expected and then begin trouble shooting from there... I've spent probably 1/2 hour total on this and have just been trying to figure out what you want and trying to see errors from a very limited perspective, because I have limited information and understanding of what your goal is and how you see the entire script working together.

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.

nanat

5+ Year Member



 
Msg#: 4001916 posted 12:34 am on Oct 7, 2009 (gmt 0)

sory for late reply.. very well said jd01.. now i know how to debug for loops and array methods with in an hours usually i debug my codes with a days.. for now my script its working smoothly.. thanks :bow: :bow:

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved