Jump to content

[SOLVED] fputs() problem


Gnub

Recommended Posts

Im trying to put several table results into a simple .txt file.  However im getting an error which will be explained below.  There are approximately...20-30 of fields to be put in.  Im attempting to have the .txt file look similar to a CVS file, so comma's and the like.  so at the end of the first record, i want a new line, and starts a new record...rince and repeat.   

 

I was getting the results, however, after each record, a new line is not started, and continues off for a long while, before starting a new line, without me asking. (on notepad) 

 

Currently im getting an error after i've tried a different approach, but that didn't work.

 

Warning: Wrong parameter count for fputs() in ..URL../CVSGen.php on line 41.

 

line 41, is the last part of the fputs().

");

 


fputs($file, "\n['], .$row[Row_No]." , ".$row[Offer_ID]." , ".$row[Total]." , ".$row[Rating]");

 

The fopen, and all the others are present.  Is there an easier way? or can i get a fix for this?

Link to comment
Share on other sites

Ok, cheers for that B!  I managed to solve that small error. Splitting the Fputs() into smaller pieces, there isn't as much strain on the function.

 

now it gives me some other error....

 

Parse error: syntax error, unexpected ']' in URL Line #

 

fputs($file, ".$row[Operator]." , ".$row[PublicNote]." , ".$row." , ".$row." , ".$row[include]."

 

 

However, this part is identical to the others, appart from the obvious column name, i've singled the part out in red as the problem...can you see anything i cant? :)

Link to comment
Share on other sites

Why are you starting with ".$row[Operator]." ?

 

Syntax highlight it again:

fputs($file, ".$row[Operator]." , ".$row[PublicNote]." , ".$row[Email]." , ".$row[url]." , ".$row[include]."

 

You're still pushing way too many vars into fputs. You need to fix up your string first. Post up all of the fputs code for this particular area.

Link to comment
Share on other sites

fputs() accepts 3 arguments. The file handle must be the first, the string must be the second, and you can optionally include a length.

 

<?php
fputs($file, ".$row[Operator]." , ".$row[PublicNote]." , ".$row[Email]." , ".$row[url]." , ".$row[include]." , ".$row[CCC]." , ".$row[Tod]." , ".$row[bCC]." , ".$row[Amex]." , ".$row[CDW]." , ".$row[bookingFee]." , ".$row[ABTA]." , ".$row[ATOL]." , ".$row[OfferID]." , ".$row[OtherBond]." , ".$row[OfferLink]." , ".$row[AccRef]." , ".$row[NAME]." , ".$row[Hotel]." , ".$row[supplier]." , ".$row[Flights]." , ".$row[ACCOM]." , ".$row[Total]");
?>

 

Your problem is how you start it. Even though strings start with quotes, starting with a variable means that you skip the quotes. As well, you don't need them at the end either.

 

<?php
fputs($file, $row[Operator]." , ".$row[PublicNote]." , ".$row[Email]." , ".$row[url]." , ".$row[include]." , ".$row[CCC]." , ".$row[Tod]." , ".$row[bCC]." , ".$row[Amex]." , ".$row[CDW]." , ".$row[bookingFee]." , ".$row[ABTA]." , ".$row[ATOL]." , ".$row[OfferID]." , ".$row[OtherBond]." , ".$row[OfferLink]." , ".$row[AccRef]." , ".$row[NAME]." , ".$row[Hotel]." , ".$row[supplier]." , ".$row[Flights]." , ".$row[ACCOM]." , ".$row[Total]);
?>

 

That should fix the string problem.

 

Now for your other problem, include is a reserved keyword. You cannot use it, as PHP will always give you errors. You're going to have to rename your include field to something else.

 

As you can see in the syntax highlight above, Include shows up as green, indicating that it's a reserved keyword.

Link to comment
Share on other sites

I take back what I said about include. The way you're using it, yes it will be taken as their reserved keyword, however the way you're doing it is bad practice.

 

With the way you're doing it now, it first checks to see what's in the [] to see if it's a defined symbol (any function names, keywords, etc.), and if it's not it takes it as a string literal. Relying on this is defined as a bad practice in the PHP manual itself.

 

What you want to do is access the fields using a single quoted string, like so:

 

<?php
fputs($file, $row['Operator']." , ".$row['PublicNote']." , ".$row['Email']." , ".$row['URL']." , ".$row['Include']." , ".$row['CCC']." , ".$row['Tod']." , ".$row['BCC']." , ".$row['Amex']." , ".$row['CDW']." , ".$row['BookingFee']." , ".$row['ABTA']." , ".$row['ATOL']." , ".$row['OfferID']." , ".$row['OtherBond']." , ".$row['OfferLink']." , ".$row['AccRef']." , ".$row['NAME']." , ".$row['Hotel']." , ".$row['Supplier']." , ".$row['Flights']." , ".$row['ACCOM']." , ".$row['Total']);
?>

Link to comment
Share on other sites

A better way of doing this (IMHO) would be to put all the values you want in the comma separated string into a temporary array and then use implode to put the commas in when you write the record.. Also, I always put the "\n" at the end of the record not at the beginning.

 

<?php
$fields = array('Operator','PublicNote','Email','URL','Include','CCC','Tod','BCC','Amex','CDW','BookingFee','ABTA','ATOL','OfferID',
                    'OtherBond','OfferLink','AccRef','NAME','Hotel','Supplier','Flights','ACCOM','Total');
$tmp = array();
foreach($fields as $fld)
     $tmp[] = $row[$fld];
fputs($file,implode(',',$tmp) . "\n");
?>

 

By putting the fields you want to write out in an array, it's very easy to add another value.

 

Ken

Link to comment
Share on other sites

Kudo's to you both.  Both seem to work, but i must have screwed up somewhere, none of the DB's fields are appearing on the .txt file. 

 

We'll use B's option as an example.  but both seem to deliver when working to the fputs().

 

<?php

$sql = "SELECT * from <Table> Limit 0,100;";
$db = mysql_connect("N/A", "User", "Pass");
mysql_select_db("User",$db);
$result = mysql_query($sql,$db) or die(mysql_error());

while($row = mysql_fetch_object($result))
{ 

$filename = "todo.txt";
touch("$filename");
$file = fopen("$filename", "a");

fputs($file, $row['Operator']." , ".$row['PublicNote']." , ".$row['Email']." , ".$row['URL']." , ".$row['Include']." , ".$row['CCC']." , ".$row['Tod']." , ".$row['BCC']." , ".$row['Amex']." , ".$row['CDW']." , ".$row['BookingFee']." , ".$row['ABTA']." , ".$row['ATOL']." , ".$row['OfferID']." , ".$row['OtherBond']." , ".$row['OfferLink']." , ".$row['AccRef']." , ".$row['NAME']." , ".$row['Hotel']." , ".$row['Supplier']." , ".$row['Flights']." , ".$row['ACCOM']." , ".$row['Total']);

fclose($file);

?>

 

What i get as a return is:

, , , ,

, , , ,

, , , ,

, , ,/n

 

Connection variables, and all are correct, as they are in use in other scripts.

 

thanks for your patience guys.

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.