whats wrong with this foreach loop?

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
gilley55
Forum Newbie
Posts: 11
Joined: Tue Aug 19, 2008 10:13 am

whats wrong with this foreach loop?

Post by gilley55 »

I have a list of 3 barcode numbers that are being extracted from the end of each of the following lines

sm_a6003.jpg, A6003 GUYCAN CASSAREEP CASSAVA 12X425GR,069065000295
sm_a6009.jpg, A6009 CHICO CASSAREEP POMEROON 24X425GR,069065000325
sm_a6012.jpg, A6012 BROWN BETTY CASSAREEP POMEROON 24X375ML,069065001322

i am using the foreach loop to go through each number and pass it to a variable called "$value" if the number is valid as a bar code number, the draw function is called to draw a bar code representing that number. After the bar code image has been created it is to be saved under that same number. for example if the bar code image was generated from this number 069065000332 then it would be saved as 069065000332.png

MY PROBLEM:
I am trying to go through the list and generate an image for each number but for some reason only the first number in the list is being used to generate multiple images which is wrong.
Can someone explane thanks in advance

here is the code

HERE IS the CODE
foreach ($data as $line) {

$temp = explode(",", $line);
$images=trim($temp[0]);
$descriptions=trim($temp[1]);
$value=trim($temp[2]);

if(isValid($value)==true){
Image_Barcode::draw($value, 'upca', 'png');

$png=".png";
$upc_image=$value.$png;

$File = $upc_image;
$Handle = fopen($File, 'w');

$Data = $barcode=ob_get_contents();
fwrite($Handle, $Data);
}

}

fclose($Handle);
Attachments
Look at the following bar code  images. Notice all 3 bar codes are the same
Look at the following bar code images. Notice all 3 bar codes are the same
problem.png (3.77 KiB) Viewed 383 times
javelin
Forum Newbie
Posts: 1
Joined: Thu Sep 18, 2008 4:37 pm

Re: whats wrong with this foreach loop?

Post by javelin »

Hi there. I'm very new(about 2-weeks old) to PHP programming let alone, in OOP. I'm trying to learn it and in the process, I've ran into a similar foreach issue and may or may not help you but I thought I'd share it with you.

Here, I have a table that contains three groups:

dba
unix
unix_sysop

The following code should return the three groups; dba, unix, unix_sysop, but it
returns only the first entry: dba.

Code: Select all

 
[b]First code:[/b]
...
foreach($wgUser->getGroups() as $g) {
   if($debug) fwrite($out,"debug0: g:$g \n");
   ...
}
 

Code: Select all

Returns:
dba
 
What I've found is that I needed to assign the groups to an array first and then
run the foreach against it:

Code: Select all

 
[b]Second code:[/b]
$mygroups = $wgUser->getGroups();
foreach($mygroups as $g) {
   if($debug) fwrite($out,"debug0: g:$g \n");
   ...
}
 

Code: Select all

Returns:
dba
unix
unix_sysop
 
I looked at your code and it's way over my head...so I can't help you much there... :)

My question to the PHP expert reading this thread is why does the first code above don't work? I could live with the second code but I'd like to know the technical reason(if any) why the first code don't work.
gilley55
Forum Newbie
Posts: 11
Joined: Tue Aug 19, 2008 10:13 am

Re: whats wrong with this foreach loop?

Post by gilley55 »

Hi javelin,
thanks for looking at my code .
The for each loop worked in your second code because for each is specifically meant to go through items in an array if the list is not in an array form it won't work but you did the right thing when u assigned the list to array first. if my explanation is not clear enough just google php for each loop
Your code did not work because getgroups() was not an array
Post Reply