SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: else...

  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2001
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    else...

    I've got this script, but i like to expand it just a liitle bit, the only problem is it won't work as I planned..

    Script:

    ---
    <?

    $fileArray = file("http://www.dft.nl");

    if ( ! $fileArray ) {
    echo '<tr><td>The news interface is down for maintenance.</td></tr>',
    '</table></body></html>';
    break;
    }

    $searchStr ='<td colspan="2" align="left" class="stijgers" nowrap="nowrap">';
    $i = 0;



    foreach($fileArray AS $line) {
    if ( strstr($line, $searchStr) ) {
    $line = str_replace("/servlet/", "http://www.dft.nl/servlet/", $line );
    echo $line, "</tr><tr>\n";
    $i++;
    }
    if ( $i == 5 ) {
    break;
    }

    }
    ?>
    ---
    What i like to do is say that when there are no results found, php prints a text like: echo 'The market is closed';
    the script takes the current stock courses from a financial site, but in the weekends, and after midnight, till 9 o'clock it gives nothing...

    I tried to add an else to the script:

    ---
    <?

    $fileArray = file("http://www.dft.nl");

    if ( ! $fileArray ) {
    echo '<tr><td>The news interface is down for maintenance.</td></tr>',
    '</table></body></html>';
    break;
    }

    $searchStr ='<td colspan="2" align="left" class="stijgers" nowrap="nowrap">';
    $i = 0;



    foreach($fileArray AS $line) {
    if ( strstr($line, $searchStr) ) {
    $line = str_replace("/servlet/", "http://www.dft.nl/servlet/", $line );
    echo $line, "</tr><tr>\n";
    $i++;
    }
    if ( $i == 5 ) {
    break;
    }
    else {
    echo 'not found';
    break;
    }
    }
    ?>
    ---
    This doesn't work, now it always gives the else value, even when it's availlible.

    Then i tried to add an if

    ( $line == "" ) {
    echo 'not found';
    break;
    }

    This didn't work, because, at least, thats what i think, the script only acts when there is a comparison in strings found. So line can never be "". Cause if it would be, the if wouldn't work, and there would be no place to place it.

    Now i believe the anwser to this problem is a string saying that if no matches are found {
    echo 'not found';
    }

    i think it would be somewhat similar to one of these lines,
    ---
    foreach($fileArray AS $line) {
    if ( strstr($line, $searchStr) ) {
    ---
    And here is when i need your help, how can i write it down?
    (If i had to guess, i'd say it's something with the strstr or AS... )

  2. #2
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Let me get this right. What you are saying is that if the stock market is closed, then there will be no matching links in that file you are reading. If there are no matching links then display a message that the stock market is closed. Is that right?

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2001
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yep that is right

  4. #4
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This should do the trick.

    PHP Code:
    ...
    foreach(
    $fileArray AS $line) { 
       if ( 
    strstr($line$searchStr) ) { 
          
    $line str_replace("/servlet/""http://www.dft.nl/servlet/"$line ); 
          echo 
    $line"</tr><tr>\n"
          
    $i++; 
       } 
       if ( 
    $i == ) { 
          break; 
       } 

    // if there were no links found
    // then the market must be closed
    if ( $i == ) {
       echo 
    'The stock market has closed due to the collapse of capitalism';


  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2001
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    almost :)

    you almost got it, but now i got it, thanks to you
    I'll share the anwser to this Q....

    By adding the:

    // if there were no links found
    // then the market must be closed
    if ( $i == 0 ) {
    echo 'The stock market has closed due to the collapse of capitalism';
    break;
    }

    on the place you told me to it repeated itself a million times, when i added a break; It only showed the collapse of the market

    So what i did was

    <?
    the original script
    ?>
    <?
    // if there were no links found
    // then the market must be closed
    if ( $i == 0 ) {
    echo 'The stock market has closed due to the collapse of capitalism';
    break;
    }
    >
    So now it will print the collapse when there are no results (i think). And else it won't!


Bookmarks

Posting Permissions

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