sir pannels
01-09-2006, 09:38 AM
Hi all,
Having some problems with a while loop, not to sure how to go about what I want.
Checked out php.net and some online tutorials but still struggiling slightly.
What I have is a few lines of code that takes some information for a file and puts it into variables then echo's the values at the end of the script.
The script takes the data out of the file by reading the values between certain markers in the file such as <webversion>VALUE</webversion>.
That works great however <webversion>VALUE</webversion> will appear in the file either 16 or 24 times with a different value.
I'm trying to wrirte a loop to cycle through each ocurence of <webversion>VALUE</webversion> and print it each time it is there.
The while I have at the moment does it 24 times however only prints the first ocurence of webversion value, 24 times.
Can anyone offer advice on where I'm going wrong? Loops have never been my strong point.
Many thanks,
P
Here's the code I have that prints the same value 24 times:
$fh = fopen($file,"r");
$d="";
while($data = fgets($fh)) {
$d.=$data;
}
fclose($fh);
$i = 0;
while ($i <= 24):
#### General Information Gathering & Display - This below actualy works.
$posOpen = strpos($d, "<webversion>") + 12;
$posClose = strpos($d, "</webversion>");
$webversion = substr($d, $posOpen, $posClose - $posOpen);
$posOpen = strpos($d, "<version>") + 9;
$posClose = strpos($d, "</version>");
$version = substr($d, $posOpen, $posClose - $posOpen);
$posOpen = strpos($d, "<sitename>") + 10;
$posClose = strpos($d, "</sitename>");
$sitename = substr($d, $posOpen, $posClose - $posOpen);
#### End of gathering
echo ("Web Version $webversion of Version $version for site $sitename.<br>");
$i++;
endwhile;
Having some problems with a while loop, not to sure how to go about what I want.
Checked out php.net and some online tutorials but still struggiling slightly.
What I have is a few lines of code that takes some information for a file and puts it into variables then echo's the values at the end of the script.
The script takes the data out of the file by reading the values between certain markers in the file such as <webversion>VALUE</webversion>.
That works great however <webversion>VALUE</webversion> will appear in the file either 16 or 24 times with a different value.
I'm trying to wrirte a loop to cycle through each ocurence of <webversion>VALUE</webversion> and print it each time it is there.
The while I have at the moment does it 24 times however only prints the first ocurence of webversion value, 24 times.
Can anyone offer advice on where I'm going wrong? Loops have never been my strong point.
Many thanks,
P
Here's the code I have that prints the same value 24 times:
$fh = fopen($file,"r");
$d="";
while($data = fgets($fh)) {
$d.=$data;
}
fclose($fh);
$i = 0;
while ($i <= 24):
#### General Information Gathering & Display - This below actualy works.
$posOpen = strpos($d, "<webversion>") + 12;
$posClose = strpos($d, "</webversion>");
$webversion = substr($d, $posOpen, $posClose - $posOpen);
$posOpen = strpos($d, "<version>") + 9;
$posClose = strpos($d, "</version>");
$version = substr($d, $posOpen, $posClose - $posOpen);
$posOpen = strpos($d, "<sitename>") + 10;
$posClose = strpos($d, "</sitename>");
$sitename = substr($d, $posOpen, $posClose - $posOpen);
#### End of gathering
echo ("Web Version $webversion of Version $version for site $sitename.<br>");
$i++;
endwhile;