473,569 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

fwrite loop and last record

anfetienne
424 Contributor
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,ch erry,lemon,bana na,&

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 4324
Niheel
2,456 Recognized Expert Moderator Top Contributor
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 Contributor
yeah i had a look at that....it's basically the same as str_replace.... i've used str_replace and it works great.....thank s for the advice it is something i'm looking into in more detail
Jul 31 '09 #3
code green
1,726 Recognized Expert Top Contributor
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 Contributor
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
25072
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
2274
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
1673
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 record even I keep on press the up arrow, on the other hand, if it's the last selection, it will stop in the last record even I keep on press the down...
6
524
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
29981
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 RaceCarCollection) { ... if last row do something? }
3
2288
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 language, so I'm sure there's something being missed. I've made a small program that allows the user to first select a country, then a state/province,...
2
15112
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 I execute the code again my record count will be minus one and then it will delete another single record etc etc until there are no records to...
2
11443
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
15531
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 true? regards, ...ab
0
7703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7618
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7926
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6287
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3657
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2117
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1228
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.