Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14

Thread: while help

  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts

    while help

    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:
    Code:
    $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;

  • #2
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    strpos() returns the position of the first thing found. You'd have to set strpos()'s offset after the first elemnt if you'd want to do it your way.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    I see, thanks for reply.

    That could get messy then. You know of any better ways to do what im going for?

    cheers

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 98 Times in 96 Posts
    delimiters ! , without them, e.g some way of knowing where 1 `record` starts and finishes within the text file all methods are going to be prone to error, this should work but only with correctly grouped bunches of the three markers you are looking for (e.g. a missing <version></version> and this will give erroneous data...

    PHP Code:
    <?php
    $yaks 
    file_get_contents($file);
    preg_match_all(
        
    "/<webversion>(.*)<\/webversion>.*<version>(.*)<\/version>.*<sitename>(.*)<\/sitename>/Uis",
        
    $yaks,
        
    $regs
    );

    //print_r($regs);

    //this is dodgy but anyway ~ you get the general idea
    foreach($regs[0] as $k=>$arr){
        echo 
    "Web Version {$regs[1][$k]} of Version  {$regs[2][$k]} for site  {$regs[3][$k]}.<br />";
    }
    ?>
    If you could say that each group of text containing the markers had some sort of delimiter ... say '|' or a couple of newlines .. or anything unique then we could explode() the text input by that delimiter and get safer results (and we could then lose preg_functions and use strpos etc as you did above.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #5
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    You may want to use *? instead of * in some places but I'd have to test it to be sure.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    Thanks both of you.

    Played with your function firepages but only get undefined call to function, which is odd as Zend does not see a problem. What am I doing wrong? I can't see how its undefined ... any further help would be apperciated

    Cheers,
    P

  • #7
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Which function?
    Zend debugger uses it's internal PHP version (by default) and it may be newer than the one on your test server.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #8
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    hmm yeah that is possible .. file_get_contents($file); is what is giving me the problem..

  • #9
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 98 Times in 96 Posts
    Quote Originally Posted by sir pannels
    ....file_get_contents($file); is what is giving me the problem..
    ok, must be an older version of PHP... try

    PHP Code:
    <?php
    $yaks 
    implode('',file($file));
    ?>
    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #10
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    aye must have been older version, got that working great.

    Few problems with the array vars being inside {}... for example..

    Code:
        $state='{$regs[3][$k]}';
    		if($state=='1'){ $bg='#FFFF99'; $value='ON'; }
    		$readystate='{$regs[4][$k]}';
    		if($readystate=='1'){$newstate = 'on';}
    the above, gives me a error on the = on the last line, it is expecting a } random, I have checked and all the brackets balance. I removed the brackets round state .. so
    Code:
    $state='$regs[3][$k]';
    and it did not error on the = 'on' but obviously just displays Array[x].

    What have I done wrong here? Many thanks again firepages =]

  • #11
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Remove the single-quotes. It will not evaulate in single-quotes.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #12
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 98 Times in 96 Posts
    Hi , the curly brackets are only needed for echoing within a string ...
    e.g this will work
    echo "use an array in a string = {$array['blah']['blah']} ";
    this would error (or give the wrong result)
    echo "use an array in a string = $array['blah']['blah'] ";
    to simply echo or use the variable elsewhere...
    echo $array['blah']['blah'];

    so lose those and the quotes as marek_mar mentions

    PHP Code:
    <?php
        $state
    =$regs[3][$k];
        if(
    $state=='1'){
            
    $bg='#FFFF99'
            
    $value='ON';
        }
        
    $readystate=$regs[4][$k];
        if(
    $readystate=='1'){
            
    $newstate 'on';
         }
    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #13
    Regular Coder
    Join Date
    Jun 2002
    Posts
    905
    Thanks
    23
    Thanked 5 Times in 5 Posts
    ah, great yeah they display now. Thanks.

    Something I don't understand about your code, what does the leading / in the pregmatch do or look for and same question to /Uis? Just so I have a better undestanding of whats going on.

    Got another problem that the code only prints everyother entry, it will bring back one lot of details, skip the next and then bring back the others.. like 1,3,5,7 and so forth. Maybe I will be able to fix this is I understand the /Uis and stuff

    cheers Fire and M, you are both very helpful.

  • #14
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 98 Times in 96 Posts
    the /'s are delimiters so the regular expression pattern goes in between those (they dont have to be slashes they can be pipes | etc)

    after the final slash are the modifiers
    U = ungreedy this should stop the regex from matching everything between the first <webversion> and the last <sitename> in the file , e.g. it looks for multiple instances of the same pattern
    i = case insensitive search
    s = ...errr had to look this up at http://www.php.net/manual/en/referen....modifiers.php have a looksee it explains all the modifiers

    why you are getting every other instance I really don't know without seeing the contents of $file, its a hit and miss affair relying on regex to parse this type of data , thats why some form of `record` delimiter would be useful.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •