Forum Moderators: coopster

Message Too Old, No Replies

2 loops squence

duplicate result

         

nanat

10:27 am on Jul 21, 2009 (gmt 0)

10+ Year Member



suppose to be my result of my loops only 2 loops but the result is 4 loops

<?php

include ("connect.php");

$pctr=count($_POST['txtactive']);

$y=0;
$pctr=$pctr/2;
while ($y < $pctr)
{
$cost=$_POST['cost'][$y];
$ctr=count($_POST['txtlv1id']);
$x=0;

while ($x < $ctr)
{
$running=$_POST['txtlv1id'][$x];
$value=$_POST['txtlval'][$x];
$tp=$_POST['txtpd'][$x];

echo $tp,"--",$value, $cost, "<br>";

++$x;
}
++$y;
}
?>

i divide it into two so that my loops wud be not = 4

$pctr=$pctr/2;

but the result is
1--333-11
2--333-11
1--444-11
2--444-11

it so suppose to be:
1--333-11
2--333-11
1--444-22
2--444-22

what wrong with my code..

tnx guys :D

andrewsmd

1:44 pm on Jul 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to give some examples of what your post variables could be. Possibly post the html with this?

nanat

12:26 am on Jul 23, 2009 (gmt 0)

10+ Year Member



here my .php


<?php

$result = mssql_query("select * from LevelOne where f0_SubGroupId='37' and IsActive='Y'");
$exist = mssql_num_rows($result);

if($exist > 0)
{
$number = 1;
for ($i = 0; $i < $exist; $i++)
{
$GID = mssql_result($result, $i, "m0_LeveloneId");
$FOID = mssql_result($result, $i, "f0_SubGroupId");
$SubGDes = mssql_result($result, $i, "LeveloneDescription");
$IsActive = mssql_result($result, $i, "IsActive");
$unit = mssql_result($result, $i , "Unit");

?>
<tr>

<td nowrap="nowrap" style="background-color:#D3D4D6; border: solid 1px #000; border-color:#FFFFFF; color:Black; font-family:Arial, Helvetica, sans-serif; font-size:11px" >
<?=$number?> <?=$SubGDes?> <?=$GID?> </td>
<td><input type="text" size="6" name="cost[]" onKeyPress="return keyCheck(event, this)"></td>
<input type="hidden" name="txtactive[]" value="<?=$number?>">
<td><?=$unit?></td>
<td>Qty/Cost</td>
<td>&nbsp;</td>

<?

$pd=13;
$ctr=1;

for($t = 0; $t < $pd; $t++)
{

?>
<td><input type="text" size="6" name="txtlval[]" onKeyPress="return keyCheck(event, this)"/>
<input type="hidden" name="txtlvlno[]" value="<?=$GID?>"/>
<input type="hidden" name="txtlv1id[]" value="<?=$FOID?>"/>
<input type="hidden" name="txtpd[]" value="<?=$ctr?>"/>
<input type="hidden" name="txtsubId[]" value="<?=$SubGDes?>" />

</td>
<?

$ctr++;
}

$number++;
}


?> <tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>

<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="button" id="button" value="Submit" /></td>
<td><input type="reset" name="button2" id="button2" value="Reset" /></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<?
}
else
{
echo '<font color="red">Access Denied Or no Entry</font>';

}

?>

andrewsmd

1:30 pm on Jul 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So what is the error you are getting? I don't see how this is realted to your first post. Also, I cleaned up your code to make it easier to debug
<?php

$result = mssql_query("select * from LevelOne where f0_SubGroupId='37' and IsActive='Y'");
$exist = mssql_num_rows($result);

if($exist > 0)
{
$number = 1;
for ($i = 0; $i < $exist; $i++)
{
$GID = mssql_result($result, $i, "m0_LeveloneId");
$FOID = mssql_result($result, $i, "f0_SubGroupId");
$SubGDes = mssql_result($result, $i, "LeveloneDescription");
$IsActive = mssql_result($result, $i, "IsActive");
$unit = mssql_result($result, $i , "Unit");

echo("<tr>

<td nowrap=\"nowrap\"
style=\"background-color: #D3D4D6; border: solid 1px #000; border-color: #FFFFFF; color: Black; font-family: Arial, Helvetica, sans-serif; font-size: 11px\">
$number $SubGDes $GID</td>
<td><input type=\"text\" size=\"6\" name=\"cost[]\"
onKeyPress=\"return keyCheck(event, this)\"></td>
<input type=\"hidden\" name=\"txtactive[]\" value=\"$number\">
<td>$unit</td>
<td>Qty/Cost</td>
<td>&nbsp;</td>");

$pd=13;
$ctr=1;

for($t = 0; $t < $pd; $t++)
{

echo("<td><input type=\"text\" size=\"6\" name=\"txtlval[]\"
onKeyPress=\"return keyCheck(event, this)\" /> <input type=\"hidden\"
name=\"txtlvlno[]\" value=\"$GID\" /> <input type=\"hidden\"
name=\"txtlv1id[]\" value=\"$FOID\" /> <input type=\"hidden\"
name=\"txtpd[]\" value=\"$ctr\" /> <input type=\"hidden\"
name=\"txtsubId[]\" value=$SubGDes\" /></td>");

$ctr++;
}

$number++;
}

echo("<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>

<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type=\"submit\" name=\"button\" id=\"button\" value=\"Submit\" /></td>
<td><input type=\"reset\" name=\"button2\" id=\"button2\" value=\"Reset\" /></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>");

}
else
{
echo '<font color="red">Access Denied Or no Entry</font>';

}

?>

andrewsmd

1:34 pm on Jul 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One other question, you have some ids and names like
<input type="text" size="6" name="cost[]"
Is that name literally supposed to be cost[] or is it supposed to be some kind of php array with a sub i.e. $cost[1]; ?