Jump to content

[SOLVED] SELECT statement results into text file


dkirk

Recommended Posts

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.