 |
Linux Format forums Help, discussion, magazine feedback and more
|
| View previous topic :: View next topic |
| Author |
Message |
chriswadams
Joined: Tue Dec 01, 2009 12:54 am Posts: 26
|
Posted: Sat Jul 31, 2010 10:17 pm Post subject: PHP Coding Academy - request for Hudzilla |
|
|
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 |
|
 |
Ram LXF regular

Joined: Thu Apr 07, 2005 10:44 pm Posts: 1570 Location: Guisborough
|
Posted: Sat Jul 31, 2010 11:12 pm Post subject: |
|
|
Post your code Chris. _________________
Ubuntu LXDE 12.04 running on AMD Phenom II*4; ASUS Crosshair III Formula MB; 4 GB Ram.....
|
|
| Back to top |
|
 |
chriswadams
Joined: Tue Dec 01, 2009 12:54 am Posts: 26
|
Posted: Sun Aug 01, 2010 2:39 am Post subject: |
|
|
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 |
|
 |
nikkius
Joined: Wed Aug 04, 2010 2:09 pm Posts: 1 Location: Australia
|
Posted: Wed Aug 04, 2010 3:21 pm Post subject: |
|
|
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 |
|
 |
Ram LXF regular

Joined: Thu Apr 07, 2005 10:44 pm Posts: 1570 Location: Guisborough
|
Posted: Wed Aug 04, 2010 4:47 pm Post subject: |
|
|
Well spotted. _________________
Ubuntu LXDE 12.04 running on AMD Phenom II*4; ASUS Crosshair III Formula MB; 4 GB Ram.....
|
|
| Back to top |
|
 |
chriswadams
Joined: Tue Dec 01, 2009 12:54 am Posts: 26
|
Posted: Wed Aug 04, 2010 11:33 pm Post subject: |
|
|
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 previous topic :: View next topic |
|
|
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
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|