PHP Coding Academy - request for Hudzilla

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

PHP Coding Academy - request for Hudzilla

Postby chriswadams » Sat Jul 31, 2010 9:17 pm

Hey Paul --

I'm working on Project 2, and I really messed up on step 2. Would you mind posting the full, final source code, so I could have a look at it?

Thanks,
chriswadams.
User avatar
chriswadams
 
Posts: 27
Joined: Tue Dec 01, 2009 12:54 am

Postby Ram » Sat Jul 31, 2010 10:12 pm

Post your code Chris.

lubuntu LXDE 13.10 running on AMD Phenom II*4; ASUS Crosshair III Formula MB; 4 GB Ram.....
User avatar
Ram
LXF regular
 
Posts: 1683
Joined: Thu Apr 07, 2005 9:44 pm
Location: Guisborough

Postby chriswadams » Sun Aug 01, 2010 1:39 am

Here is what I have:

Code: Select all
<html>
<body>
<?php
   if(isset($_POST["user_input"])){
      $input=$_POST["user_input"];
      
      //don't try to load the saved_feeds file if it doesn't exist!
      
      if (file_exists("saved_feeds")){
         $feeds=file_get_contents("saved_feeds");
         $feeds=explode("\n", $feeds);
      
         if(!in_array($input, $feeds)){
            //it's a new feed!
            file_put_contents("saved_feeds", "$input\n", FILE_APPEND);
         }
      }else{

      file_put_contents("saved_feeds", "$input\n");
      }
   }

      $feed = simplexml_load_file($input);
      $titles = $feed->xpath("//item/title");
      foreach ($titles as $title){
         print "<p>$title</p>";
      }

?>
<form method="post" action="project2.php">
<input type="text" name="user_input" />
<input type="submit" />
</form>
<?php      
   if (file_exists("saved_feeds")){
      $feeds=file_get_contents("saved_feeds");
      $feeds=explode("\n", $feeds);
      
      foreach ($feeds as $feed){
         echo "<p><a href=\"$feed\">$feed</a></p>";
      }
   }
?>
</body>
</html>


Mainly, I'm confused about the ' if (file_exists("saved_feeds")){ ' block of code. On p. 39 of the 'Coding Academy', Hudzilla indicates that it goes toward the end of the script, but on p. 42 he indicates that it goes at the top ... or perhaps both. I don't know whether the redundancy is right or wrong. I have tried all three possibilities with no results.

Note that the script worked prior to adding in that block on p. 39, but as it is now, it doesn't do anything. This makes me think I have also somehow messed up the syntax, maybe leaving out a semicolon or parenthsis somewhere, but I can't find it. If you can spot it, I would be grateful.

Thanks,
chriswadams.
User avatar
chriswadams
 
Posts: 27
Joined: Tue Dec 01, 2009 12:54 am

Postby nikkius » Wed Aug 04, 2010 2:21 pm

I think the problem with your code is a curly bracket (or brace) in the wrong place.
you have:
Code: Select all
   }else{
      file_put_contents("saved_feeds", "$input\n");
   }
}


and you have at the end of the first block of php:
Code: Select all
   print "<p>$title</p>";
}
?>


Should be:
Code: Select all
   }else{
      file_put_contents("saved_feeds", "$input\n");
   }


and should be at the end of the first block of php:
Code: Select all
      print "<p>$title</p>";
   }
}
?>


As to the ' if (file_exists("saved_feeds")){ ' block of code,
page 39 is to display the list of previously entered feeds in the browser if the saved list exists, page 42 is about checking the saved file exists, if not create it and don't add duplicate entries to the saved file, and get rss feed list from the site the user entered and display the titles.

The 'display titles' bit only works for me when entering http://www.tuxradar.com/rss but that may just be because I don't know any other rss feed url's that conform to the code.

hth
nikkius
 
Posts: 1
Joined: Wed Aug 04, 2010 1:09 pm
Location: Australia

Postby Ram » Wed Aug 04, 2010 3:47 pm

Well spotted.

lubuntu LXDE 13.10 running on AMD Phenom II*4; ASUS Crosshair III Formula MB; 4 GB Ram.....
User avatar
Ram
LXF regular
 
Posts: 1683
Joined: Thu Apr 07, 2005 9:44 pm
Location: Guisborough

Postby chriswadams » Wed Aug 04, 2010 10:33 pm

Hi, Ram & nikkius:

That was well spotted nikkius. And you were right, moving the brace fixed that part of the problem.

What remained was that the resulting headlines weren't printed as hyper-text. By placing this line --

Code: Select all
print ("Testing ...");


-- in the second code block, and adding this --

Code: Select all
echo "<p><a href=\"$title\">$title</a></p>";


-- to the first code block, I discovered that the script was completely ignoring the second block. Is it supposed to do that?

The second change above, produced clickable links, although strangely, none lead to any feeds.

Anyway, thanks for the help, guys!
chriswadams.
User avatar
chriswadams
 
Posts: 27
Joined: Tue Dec 01, 2009 12:54 am


Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests