473,320 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

fwrite loop and last record

anfetienne
424 256MB
hi im back again.......i have a code to create strings and save it within a text file to pass variables to flash. im using the string format below.

Expand|Select|Wrap|Line Numbers
  1. mylist=apple,cherry,lemon,banana&mylist2=apple,cherry,lemon,banana
  2.  
this code below is what im using to write my string to the text file, basically the info is collected from a db and i use a loop to cycle through and write each value in. I have to seperate these values using a comma ',' like in my above example. i can do this but then i am left with a comma at the end of my string before the next variable starts like this mylist=apple,cherry,lemon,banana,&

is there a way to add commas and once it gets to the last value to write to get php to check that its the last value and not add a comma in?

Expand|Select|Wrap|Line Numbers
  1. $file = "upload/$random_digit/gallery.txt";
  2. $imgHandle = fopen($file, 'w') or die("can't open file");
  3.  
  4.     $iv= 1;
  5.     $lQ = ',';
  6.  
  7.     $stringData = "&imgLoc=";
  8.     fwrite($imgHandle, $stringData);
  9.  
  10.             for($i=0;$i<$totalRecords;$i++)
  11.             {
  12.                 $stringData = $returnedData[$i]["imageLoc"] .$lQ ;
  13.                 fwrite($imgHandle, $stringData);
  14.             } 
  15.  
  16. fclose($imgHandle);
  17.  
Jul 30 '09 #1
4 4309
Niheel
2,460 Expert Mod 2GB
You might be able to use rtrim function in php and ask it delete the comma from the end of the string.

http://us2.php.net/manual/en/function.rtrim.php

Expand|Select|Wrap|Line Numbers
  1. $trimmed = rtrim($string, ",");
  2.  
Jul 31 '09 #2
anfetienne
424 256MB
yeah i had a look at that....it's basically the same as str_replace....i've used str_replace and it works great.....thanks for the advice it is something i'm looking into in more detail
Jul 31 '09 #3
code green
1,726 Expert 1GB
I would improve the efficiency drastically by concatenating then writing one big string.
Then you can easily trim the last comma
Expand|Select|Wrap|Line Numbers
  1. $file = "upload/$random_digit/gallery.txt"; 
  2. $imgHandle = fopen($file, 'w') or die("can't open file");   
  3.     $iv= 1; 
  4.     $lQ = ',';   
  5.     $stringData = "&imgLoc="; 
  6.     for($i=0;$i<$totalRecords;$i++) 
  7.     { 
  8.         $stringData .= $returnedData[$i]["imageLoc"] .$lQ ; 
  9.     }
  10.    $stringData = rtrim($stringData,',');  
  11.    fwrite($imgHandle, $stringData); 
  12. fclose($imgHandle); 
  13.  
Even if this causes memory problems due to a huge string I would still do the same but split into chunks
Jul 31 '09 #4
anfetienne
424 256MB
that's exactly what i have done.....the speed it does it in is good and it doesn't take much memory.....i basically build the string and at the end i str_replace the unwanted commas then save it back into the file
Jul 31 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: !TG | last post by:
I currently use Do while loop, but I'd rather use a For Loop though I have never gotten the hang of them. Would some one please be so kind as to show me how to loop through a recordset.
10
by: cs | last post by:
if i have typedef struct{ unsigned a; unsigned *b; }tp; i would like to ask if it is ok for the "C standard 89" doing something like this {
3
by: Ivan V via DotNetMonster.com | last post by:
Hi All: I would like to know if I can loop my selection in a combobox. For instance, I got 8 items in a combobox, and when I selected it using the up and down arrow, it will stop in the first...
6
by: sumit1680 | last post by:
Hi everyone, I am using the below listed code #include<stdio.h> #include<stdlib.h> #include<string.h> int main()
23
by: tshad | last post by:
Is there a way to know if you are looking at the last record record of foreach loop other then setting up a loop counter that you manually increment? foreach (Racecar racecar in...
3
by: Xean45 | last post by:
Hey all, Sorry to be the noob with two posts asking a question, but I've been fighting with this for a couple days now, and as far as I can tell, this should be working, but PHP isn't my main...
2
kcdoell
by: kcdoell | last post by:
Hello I have a code where I want to delete the records that are found in my DAO recordset. I took a stab at this for the first time and got it to work but it is only delete one record at a time. If...
2
by: shivapadma | last post by:
1.i am just trying to insert a "structure record" into a file using fwrite() function by the following code 2. #include<stdio.h> #include<conio.h> struct student { char name; int rollno;...
25
by: Abubakar | last post by:
Hi, recently some C programmer told me that using fwrite/fopen functions are not efficient because the output that they do to the file is actually buffered and gets late in writing. Is that...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.