eildydar Posted February 10, 2011 Share Posted February 10, 2011 Hey guess I have a question about For Loops. I'm very new to php so please forgive anything stupid I am doing The follow code shows what I am trying to do with my for loop. It works great except that the $sizu value is spread out over all the fields. So instead of saying 150 in the first record set it prints 1 and then in the second record it prints 2 etc. Any idea what is causing this? If you need to see the rest of the code let me know I'm just trying to save bandwidth at first. for ($i=0; $i < count($var); $i++) { $quer = ("SELECT sbbx0, sbsizu FROM dwoshp WHERE sbfile = '$_POST[order]' and sbbrnu = '$_POST[location]' AND sbvaru = '$varu[$i]'"); $res = mysql_query($quer); while($r = mysql_fetch_array($res)) { $quant = $r['sbbx0']; $sizu = $r['sbsizu']; } $query_AddDots = "INSERT INTO tempresult(variety,grade,cont, quantity, size) VALUES ('$var[$i]', '$invgrade[$i]','TP','$quant', $sizu[$i])"; $AddDots = mysql_query($query_AddDots) or die(mysql_error()); } Quote Link to comment https://forums.phpfreaks.com/topic/227268-for-loop-question/ Share on other sites More sharing options...
Muddy_Funster Posted February 10, 2011 Share Posted February 10, 2011 Not sure what it is that you expect to happen here. Could you give a bit more information regarding the context of the queries that you are running, and a breakdown of the data fields in your table? Quote Link to comment https://forums.phpfreaks.com/topic/227268-for-loop-question/#findComment-1172343 Share on other sites More sharing options...
eildydar Posted February 10, 2011 Author Share Posted February 10, 2011 sorry at the end is a select * from tempresults; and instead of printing Variety Grade Cont 12346 150 Variety Grade Cont 126 120 instead it prints Variety Grade Cont 12346 1 Variety Grade Cont 126 5 Quote Link to comment https://forums.phpfreaks.com/topic/227268-for-loop-question/#findComment-1172345 Share on other sites More sharing options...
eildydar Posted February 10, 2011 Author Share Posted February 10, 2011 Okay I fixed one problem and came up with another. I took out the [$i] from the end of the $sizu variable. That made the first line output correctly with 150 but the second line is also 150. It looks like the following RED DELICIOUS WX TP 49 150 RED DELICIOUS WX TP 49 150 I think it has to do with the $var being the same because if I query for a different order number it outputs correctly as shown below DANJOU PEARS US1 TP 180 54 BOSC US1 TP 120 48 ORGANIC DANJ US1 TP 180 212 ORGANIC RED USX TP 180 212 Quote Link to comment https://forums.phpfreaks.com/topic/227268-for-loop-question/#findComment-1172417 Share on other sites More sharing options...
cgeisler515 Posted February 10, 2011 Share Posted February 10, 2011 We need more information then what you have provided we have no clue what $var has been assigned to. Could you paste the code before the for loop starts. Quote Link to comment https://forums.phpfreaks.com/topic/227268-for-loop-question/#findComment-1172424 Share on other sites More sharing options...
eildydar Posted February 10, 2011 Author Share Posted February 10, 2011 $query = ('CREATE TEMPORARY TABLE tempresult(variety varchar(50), grade varchar(50), cont varchar(50),quantity varchar(50), order_num varchar(50), location varchar(50), size varchar(50))'); $result = mysql_query($query) or die(mysql_error()); $query1= ('CREATE TEMPORARY TABLE invoicegrade select dmcongr.cggrdu, dmcongr.cgrseq, dmcongr.cggrdi, invgradejoin.cggrdu AS invgrade FROM dmcongr INNER JOIN dmcongr AS invgradejoin ON dmcongr.cggrdi = invgradejoin.cgrseq'); $result1 = mysql_query($query1) or die(mysql_error()); $query2= ('CREATE TEMPORARY TABLE tempGrade(SELECT t1.invgrade, dwoshp.sbfile as ordering, dwoshp.sbbrnu as brand FROM invoicegrade t1 left join dwoshp on t1.cggrdu = dwoshp.sbgrdu where dwoshp.sbfile = "'.$_POST[order].'" AND dwoshp.sbbrnu = "'.$_POST[location].'")'); $result2= mysql_query($query2) or die(mysql_error()); $query3=('CREATE TEMPORARY TABLE tempVar(SELECT dmconvr.cxvrnm, dwoshp.sbvaru, dwoshp.sbsizu, dwoshp.sbfile as ordering, dwoshp.sbbrnu as brand FROM dmconvr left join dwoshp on dmconvr.cxrseq = dwoshp.sbvar WHERE dwoshp.sbfile = "'.$_POST[order].'" AND dwoshp.sbbrnu = "'.$_POST[location].'")'); $result3=mysql_query($query3) or die(mysql_error()); $query6=('SELECT invgrade FROM tempGrade'); $result6 = mysql_query($query6); $query7 = ('SELECT cxvrnm, sbvaru FROM tempVar'); $result7 = mysql_query($query7); while($row=mysql_fetch_array($result6)) { $invgrade[] = $row['invgrade']; } while($rows=mysql_fetch_array($result7)) { $var[] = $rows['cxvrnm']; $varu[] = $rows['sbvaru']; } for ($i=0; $i < count($var); $i++) { $quer = ("SELECT sbbx0, sbsizu FROM dwoshp WHERE sbfile = '$_POST[order]' and sbbrnu = '$_POST[location]' AND sbvaru = '$varu[$i]'"); $res = mysql_query($quer); while($r = mysql_fetch_array($res)) { $quant = $r['sbbx0']; $sizu = $r['sbsizu']; } $query_AddDots = "INSERT INTO tempresult(variety,grade,cont, quantity, size) VALUES ('$var[$i]', '$invgrade[$i]','TP','$quant', $sizu)"; $AddDots = mysql_query($query_AddDots) or die(mysql_error()); } Quote Link to comment https://forums.phpfreaks.com/topic/227268-for-loop-question/#findComment-1172444 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.