appending a text file using large amounts of code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tjosephads
Forum Newbie
Posts: 5
Joined: Tue Apr 07, 2009 4:08 pm

appending a text file using large amounts of code

Post by tjosephads »

Hi,

I am trying to get a recordset to print to a text file, but am unsure of the correct syntax. The section I'm wanting to print is as follows:

Code: Select all

<?php do { ?>
    <div>
      <?php print "/"."PO1"."*"; ?>
      <?php 
      $zerofill = 4;
      $linereturn = ++$linebasenumber;
      echo str_pad($linereturn, $zerofill, "0", STR_PAD_LEFT);
      ?>
      <?php echo "line #0001"."*"; ?>
      <?php echo $row_rs_edibase['deliveryqty']."*"; ?>
      <?php echo $row_rs_edibase['sellum']."*"."0.00"."*"."*"."VN"; ?>
      <?php echo $row_rs_edibase['productid']."*"."UP"."*"; ?>
      <?php echo $row_rs_edibase['upcea']; ?>
  </div>
    <?php } while ($row_rs_edibase = mysql_fetch_assoc($rs_edibase)); ?>
This would allow a variable amount of records to be submitted to the text file in the correct format. Anyone know how to wrap a print to text file function around this code block?

Thanks.
Last edited by Benjamin on Fri Apr 24, 2009 8:45 pm, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: appending a text file using large amounts of code

Post by McInfo »

$row_rs_edibase contains nothing until it is loaded by mysql_fetch_assoc(), so use a while loop instead of a do-while loop.

If you load your output string into a variable, you can echo it as well as save it to a file.

I'm not sure what you want your output to look like, but here is the basic idea.

Code: Select all

<?php
$zerofill = 4;
$out = '';
while ($row_rs_edibase = mysql_fetch_assoc($rs_edibase))
{
    $linereturn = ++$linebasenumber;
    $out .= '<div>'
          . '/PO1*'
          . str_pad($linereturn, $zerofill, '0', STR_PAD_LEFT)
          . 'line #0001*'
          . $row_rs_edibase['deliveryqty'].'*'
          . $row_rs_edibase['sellum'].'*0.00**VN'
          . $row_rs_edibase['productid'].'*UP*'
          . $row_rs_edibase['upcea']
          . '</div>';
}
echo $out;
file_put_contents('outfile.txt', $out);
?>
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 11:27 am, edited 1 time in total.
tjosephads
Forum Newbie
Posts: 5
Joined: Tue Apr 07, 2009 4:08 pm

Re: appending a text file using large amounts of code

Post by tjosephads »

For some reason this code doesn't return all records, it excludes the last record in the set of data I pull up. Any thoughts as to why that may be happening?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: appending a text file using large amounts of code

Post by McInfo »

What is the query you are using?

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 11:27 am, edited 1 time in total.
tjosephads
Forum Newbie
Posts: 5
Joined: Tue Apr 07, 2009 4:08 pm

Re: appending a text file using large amounts of code

Post by tjosephads »

The query is below. Thanks.

Code: Select all

<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
 
  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
 
  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
 
$maxRows_rs_edibase = 10;
$pageNum_rs_edibase = 0;
if (isset($_GET['pageNum_rs_edibase'])) {
  $pageNum_rs_edibase = $_GET['pageNum_rs_edibase'];
}
$startRow_rs_edibase = $pageNum_rs_edibase * $maxRows_rs_edibase;
 
$colname_rs_edibase = "-1";
if (isset($_SESSION['ordersession'])) {
  $colname_rs_edibase = $_SESSION['ordersession'];
}
mysql_select_db($database_prod, $prod);
$query_rs_edibase = sprintf("SELECT * FROM EDI_base WHERE onlineorderid = %s", GetSQLValueString($colname_rs_edibase, "text"));
$query_limit_rs_edibase = sprintf("%s LIMIT %d, %d", $query_rs_edibase, $startRow_rs_edibase, $maxRows_rs_edibase);
$rs_edibase = mysql_query($query_limit_rs_edibase, $prod) or die(mysql_error());
$row_rs_edibase = mysql_fetch_assoc($rs_edibase);
 
if (isset($_GET['totalRows_rs_edibase'])) {
  $totalRows_rs_edibase = $_GET['totalRows_rs_edibase'];
} else {
  $all_rs_edibase = mysql_query($query_rs_edibase);
  $totalRows_rs_edibase = mysql_num_rows($all_rs_edibase);
}
$totalPages_rs_edibase = ceil($totalRows_rs_edibase/$maxRows_rs_edibase)-1;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test Order Capture</title>
</head>
 
<body>
<?php
$orderheader = "orderheaderplaceholder";
$baseinfo = "baseinfoplaceholder";
?>
<?php
$zerofill = 4;
$out = '';
while ($row_rs_edibase = mysql_fetch_assoc($rs_edibase))
{
    $linereturn = ++$linebasenumber;
    $out .= '/PO1*'
          . str_pad($linereturn, $zerofill, '0', STR_PAD_LEFT)
          . '*'
          . $row_rs_edibase['deliveryqty'].'*'
          . $row_rs_edibase['sellum'].'*0.00**VN*'
          . $row_rs_edibase['productid'].'*UP*'
          . $row_rs_edibase['upcea'];
}
echo $out;
file_put_contents('outfile.txt', $orderheader.$out.$baseinfo, FILE_APPEND);
?>
</body>
</html>
<?php
mysql_free_result($rs_edibase);
?>
Last edited by Benjamin on Tue Apr 28, 2009 4:33 pm, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: appending a text file using large amounts of code

Post by McInfo »

Remove line 46 and see what happens.

Line 46:

Code: Select all

$row_rs_edibase = mysql_fetch_assoc($rs_edibase);
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 11:28 am, edited 1 time in total.
tjosephads
Forum Newbie
Posts: 5
Joined: Tue Apr 07, 2009 4:08 pm

Re: appending a text file using large amounts of code

Post by tjosephads »

actually, I changed the while line to:

Code: Select all

while ($row_rs_edibase = mysql_fetch_assoc($all_rs_edibase))
it seems to be working now.

Thanks!
Post Reply