dkirk Posted October 16, 2007 Share Posted October 16, 2007 I am trying to create a text file based off the results of a query statement from a database. See code below: $itemq = "SELECT sdlitm, sddsc1, sduorg, sdlnid, sduncs FROM F4211 WHERE sddoco = $ordernum AND sddcto LIKE '$ordertype' ORDER BY sdlnid"; $result3 = odbc_exec($connect, $itemq); while (odbc_fetch_row($result3)){ $partnum = odbc_result($result3, 1); $desc1 = odbc_result($result3, 2); $qty = odbc_result($result3, 3)/100; $position = odbc_result($result3, 4); $uncs = odbc_result($result3, 5); $extcost = $uncs*$qty; $total = ($total + ($uncs * $qty)); $filename = "$reportnumber.txt"; $fp = fopen($filename, "w"); fwrite($fp, "Activity Number: $reportnumber\r\n"); fwrite($fp, "Warranty Type: $reason\r\n"); fwrite($fp, "Customer Name: $custname\r\n"); fwrite($fp, "$qty $partnum $desc1\r\n"); fclose($fp); Everything works fine until I get to the line: fwrite($fp, "$qty $partnum $desc1\r\n"); This line should actually print multiple lines into the text file as these are the line items from an order. There could be anywhere from 1 to 20 lines written to the text file. As it stands, the only line items written to the text file are those of the last line found on the order. The entrie script dealing with the text file are still within the confines of the while statement curly braces. How do I write these lines out in the text file?? Quote Link to comment https://forums.phpfreaks.com/topic/73524-solved-select-statement-results-into-text-file/ Share on other sites More sharing options...
BlueSkyIS Posted October 16, 2007 Share Posted October 16, 2007 only open the file and close the file once each, outside of the loop. Quote Link to comment https://forums.phpfreaks.com/topic/73524-solved-select-statement-results-into-text-file/#findComment-370969 Share on other sites More sharing options...
dkirk Posted October 16, 2007 Author Share Posted October 16, 2007 Outside of the loop only prints the last record as well. Quote Link to comment https://forums.phpfreaks.com/topic/73524-solved-select-statement-results-into-text-file/#findComment-370989 Share on other sites More sharing options...
esukf Posted October 16, 2007 Share Posted October 16, 2007 Try:- <?php $itemq = "SELECT sdlitm, sddsc1, sduorg, sdlnid, sduncs FROM F4211 WHERE sddoco = $ordernum AND sddcto LIKE '$ordertype' ORDER BY sdlnid"; $result3 = odbc_exec($connect, $itemq); $i = 0; $total = 0; $array = array(); while (odbc_fetch_row($result3)){ $array[$i]['partnum'] = odbc_result($result3, 1); $array[$i]['desc1'] = odbc_result($result3, 2); $array[$i]['qty'] = odbc_result($result3, 3)/100; $array[$i]['position'] = odbc_result($result3, 4); $array[$i]['uncs'] = odbc_result($result3, 5); $array[$i]['extcost'] = $array[$i]['uncs'] * $array[$i]['qty']; $total = ($total + ($array[$i]['uncs'] * $array[$i]['qty'])); $i++; } //write file $filename = "$reportnumber.txt"; $fp = fopen($filename, "w"); fwrite($fp, "Activity Number: $reportnumber\r\n"); fwrite($fp, "Warranty Type: $reason\r\n"); fwrite($fp, "Customer Name: $custname\r\n"); foraeach($array as $arr){ fwrite($fp, "{$arra['qty']} {$arr['partnum']} {$arr['desc1']}\r\n"); } fclose($fp); ?> Quote Link to comment https://forums.phpfreaks.com/topic/73524-solved-select-statement-results-into-text-file/#findComment-371025 Share on other sites More sharing options...
dkirk Posted October 17, 2007 Author Share Posted October 17, 2007 Thanks esukf, that did the trick. I appreciate the help, best regards.... Quote Link to comment https://forums.phpfreaks.com/topic/73524-solved-select-statement-results-into-text-file/#findComment-371482 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.