Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

PHP Coding Academy - request for Hudzilla

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
chriswadams



Joined: Tue Dec 01, 2009 12:54 am
Posts: 26

PostPosted: Sat Jul 31, 2010 10:17 pm    Post subject: PHP Coding Academy - request for Hudzilla Reply with quote

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.
Back to top
View user's profile Send private message
Ram
LXF regular


Joined: Thu Apr 07, 2005 10:44 pm
Posts: 1668
Location: Guisborough

PostPosted: Sat Jul 31, 2010 11:12 pm    Post subject: Reply with quote

Post your code Chris.
_________________

lubuntu LXDE 13.10 running on AMD Phenom II*4; ASUS Crosshair III Formula MB; 4 GB Ram.....
Back to top
View user's profile Send private message MSN Messenger
chriswadams



Joined: Tue Dec 01, 2009 12:54 am
Posts: 26

PostPosted: Sun Aug 01, 2010 2:39 am    Post subject: Reply with quote

Here is what I have:

Code:
<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.
Back to top
View user's profile Send private message
nikkius



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

PostPosted: Wed Aug 04, 2010 3:21 pm    Post subject: Reply with quote

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


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


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


and should be at the end of the first block of php:
Code:
      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
Back to top
View user's profile Send private message
Ram
LXF regular


Joined: Thu Apr 07, 2005 10:44 pm
Posts: 1668
Location: Guisborough

PostPosted: Wed Aug 04, 2010 4:47 pm    Post subject: Reply with quote

Well spotted.
_________________

lubuntu LXDE 13.10 running on AMD Phenom II*4; ASUS Crosshair III Formula MB; 4 GB Ram.....
Back to top
View user's profile Send private message MSN Messenger
chriswadams



Joined: Tue Dec 01, 2009 12:54 am
Posts: 26

PostPosted: Wed Aug 04, 2010 11:33 pm    Post subject: Reply with quote

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:
print ("Testing ...");


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

Code:
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.
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast