1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

PHP Include by using start of line?

Discussion in 'Web Design & Development' started by Damonc, Jan 10, 2013.

Thread Status:
Not open for further replies.
Advertisement
  1. Damonc

    Damonc Thread Starter

    Joined:
    May 9, 2001
    Messages:
    657
    First Name:
    Damon
    Hi,
    Is there a way to use the PHP Include function to include a line from a file depending on what it starts with?

    I currently have the following script to extract a line from an rss feed to display the current temp, however, when theres a storm warning current, it appears on that line which obviously doesn't work properly where im using it.

    Code:
              <?php 
    	$file = file('http://rss.weatherzone.com.au/?u=12994-1285&lt=aploc&lc=2007&obs=1&fc=1&warn=1');
    echo $file[41];
    ?>
    if i could get a script to search just for a line starting with "<b>Temperature:</b>" I'd be fine as its where the information is that I want, its the only line that starts with that info, and then if its line number changes it wont be a problem..
     
  2. JiminSA

    JiminSA

    Joined:
    Dec 15, 2011
    Messages:
    3,393
    First Name:
    Jim
    Hi Damonc, maybe you should examine each line of your feed, by converting it to simple xml, checking the first 19 characters for "<b>Temperature:</b>" and only echo the matches? Something along the lines of...
    e.g.
    PHP:
        $content file_get_contents($feed_url);
        
    $x = new SimpleXmlElement($content);
        echo 
    "<ul>";
        foreach(
    $x->channel->item as $entry) {
                    if (
    substr($entry,0,19) == "<b>Temperature:</b>") echo "<li><a href='$entry->link' title='$entry->title'>" $entry->title "</a></li>";
        }
        echo 
    "</ul>";
    I haven't tested this code so don't trust it - it's just to give you a guideline...
     
  3. Damonc

    Damonc Thread Starter

    Joined:
    May 9, 2001
    Messages:
    657
    First Name:
    Damon
    Can you let me know how you go? I kinda took one look at the above code and got confused as my self taught php-knowledge isn't that great.
     
  4. JiminSA

    JiminSA

    Joined:
    Dec 15, 2011
    Messages:
    3,393
    First Name:
    Jim
    The include function simply slips php code into your page where it's declared. With such a short piece of code, I wouldn't use include I would simply insert the code where applicable.
     
  5. Damonc

    Damonc Thread Starter

    Joined:
    May 9, 2001
    Messages:
    657
    First Name:
    Damon
    Could I trouble you to give me a bit of a hand with this when you have a bit of time? I don't know enough of what i'm doing to get it to work..
     
  6. JiminSA

    JiminSA

    Joined:
    Dec 15, 2011
    Messages:
    3,393
    First Name:
    Jim
    Sure, no problem - could you post the code for the entire page where your feed is displayed, please?
     
  7. Damonc

    Damonc Thread Starter

    Joined:
    May 9, 2001
    Messages:
    657
    First Name:
    Damon
    OK, So here is the code, the PHP include reference is pretty much at the end, this is a menu system that the temp appears on the right hand side of the top bar.. this is then inserted into every page throughout the website. I realise the code may not be the best, but its all self taught.. and for the most part, works...

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    	
    <link rel="stylesheet" href="css/jd.gallery.css" type="text/css" media="screen" />
    <head>
    
    <script src="scripts/mootools-1.2.1-core-yc.js" type="text/javascript"></script>
    		<script src="scripts/mootools-1.2-more.js" type="text/javascript"></script>
    		<script src="scripts/jd.gallery.js" type="text/javascript"></script>
    		<script src="scripts/jd.gallery.transitions.js" type="text/javascript"></script>
    <script type="text/javascript" language="javascript" src="lytebox.js"></script>
    <link rel="stylesheet" href="css/lytebox.css" type="text/css" media="screen" />
    <link rel="stylesheet" type="text/css" href="pro_drop_1/pro_drop_1.css" />
    
    <script src="pro_drop_1/stuHover.js" type="text/javascript"></script>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    
    
    
    <script language="JavaScript" type="text/javascript">
    
    function sivamtime() {
      now=new Date();
      hour=now.getHours();
      min=now.getMinutes();
      sec=now.getSeconds();
    
    if (min<=9) { min="0"+min; }
    if (sec<=9) { sec="0"+sec; }
    if (hour>12) { hour=hour-12; add="PM"; }
    else { hour=hour; add="AM"; }
    if (hour==12) { add="PM"; }
    
    time = ((hour<=9) ? "0"+hour : hour) + ":" + min + ":"+sec+  add;
    
    if (document.getElementById) { document.getElementById('theTime').innerHTML = time; }
    else if (document.layers) {
     document.layers.theTime.document.write(time);
     document.layers.theTime.document.close(); }
    
    setTimeout("sivamtime()", 1000);
    }
    window.onload = sivamtime;
    
    // -->
    
    </script>
    
    
    
    
    </head>
    
    <body>
    <!-- Start Menu -->
    <span class="preload1"></span>
      <span class="preload2"></span>
    <!-- Connection for announcer menu -->
    <?php
    $host="localhost"; // Host name 
    $username="USER"; // Mysql username 
    $db_name="fmcomau_data"; // Database name
    $db_pass="PASSWORD"; 
    $tbl_name="announpages"; // Table name
    
    // Connect to server and select database.
    mysql_connect("$host", "$username", "$db_pass")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");
    
    $sql="SELECT * FROM $tbl_name WHERE active = 'Yes'";
    $result=mysql_query($sql);
    ?>
    
      <ul id="nav">
        <li class="top"><a href="../" class="top_link"><span>Home</span></a></li>
        <li class="top"><a href="stream.php" class="top_link"><span>Listen Online!</span></a></li>
        <li class="top"><a href="#menu" id="fmcomau_data" class="top_link"><span class="down">Whats on FM 100.3</span></a>
          <ul class="sub">
            <li class="mid"><a href="#menu" class="fly">Announcers</a>
              <ul>
              <?php
    while($rows=mysql_fetch_array($result)){
    ?>
                <?php echo $rows['code1']; echo $rows['id']; echo $rows['code2']; echo $rows['announcer']; echo $rows['code3'];?>
    <?php
    }
    ?>
    <?php
    mysql_close();
    ?>
    </ul>
                
    <!-- Connection for feature menu -->
    <?php
    $host="localhost"; // Host name 
    $username="USER"; // Mysql username 
    $db_name="fmcomau_data"; // Database name
    $db_pass="PASSWORD"; 
    $tbl_name="featurepages"; // Table name
    
    // Connect to server and select database.
    mysql_connect("$host", "$username", "$db_pass")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB");
    
    $sql="SELECT * FROM $tbl_name WHERE active = 'Yes'";
    $result=mysql_query($sql);
    ?>
    
                
    <?php
    while($rows=mysql_fetch_array($result)){
    ?>
                <?php echo $rows['code1']; echo $rows['id']; echo $rows['code2']; echo $rows['title']; echo $rows['code3'];?>
    <?php
    }
    ?>
    <?php
    mysql_close();
    ?>
    
    <!-- Static Feature menu items -->
            <li><a href="request.php">Request a song</a></li>
            <li><a href="lostfound.php">Lost & Found</a></li>
            <li><a href="comps.php">Comps & Promos</a></li>
            <li><a href="sport.php">Sports Star of the week</a></li>
            <li><a href="courtesyawards.php">Courtesy Awards</a></li>
            <li><a href="https://www.facebook.com/pages/fmcomau_data/196798303677812">FM 100.3 on Facebook</a></li>
            </ul>
          </li>
        <li class="top"><a href="#menu" id="fmcomau_data" class="top_link"><span class="down"> Local Events & Info  </span></a>
          <ul class="sub">
            <li class="mid"><a href="http://www.armidaletourism.com.au/events_monthly.asp" target="_blank">Events calendar (Ext. Link)</a></li>
            <li><a href="see.php">Things to see</a></li>
            <li><a href="eat.php">Places to eat</a></li>
            <li><a href="traffic.php">Nrthn NSW traffic conditions</a></li>
    
            </ul>
          </li>
        <li class="top"><a href="contact.php" class="top_link"><span>Contact Us</span></a></li>
        <li class="top"><a href="#nogo1" class="top_link"><span id="theTime" font-family: arial; font-size: 9pt"></span></a></li><li class="top"><a href="#" class="top_link"><span>
        <div class="wide">
        <?php 
    	$file = file('http://rss.weatherzone.com.au/?u=12994-1285&lt=aploc&lc=2007&obs=1&fc=1&warn=1');
    echo $file[41];
    ?></div>
        </span></a></li>
    
      </ul>   
      
    
    
    
    </body>
    </html>
     
  8. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1084530

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice