PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
lampster
Forum Newbie
Posts: 6 Joined: Sun Dec 17, 2006 12:34 am
Post
by lampster » Sun Dec 17, 2006 12:46 am
I'm getting a parse error on in my link code below - can anyone help me figure out where my code went wrong?
Parse error line: $return.= "<a href=\"javascript:popUp('".$photo['link'] = str_replace('index.php?', 'index2.php?',
Code: Select all
foreach ($data['results'] as $photo) {
$photo = $photo['fields'];
$return.= "<div class=\"containerBox\">";
$return.= "<a href=\"javascript:popUp('".$photo['link'] = str_replace('index.php?', 'index2.php?', $photo['link']"')\"><b>".$photo['title']."</b></a><br />";
$return.= "<a href=\"javascript:popUp('".$photo['link']."')\">"
."<img src=\"".$photo['thumb']."\" border=\"0\" alt=\"\" title=\"".$photo['title']."\" />"
."</a>\n";
$return.= "</div>";
dibyendrah
Forum Contributor
Posts: 491 Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:
Post
by dibyendrah » Sun Dec 17, 2006 1:34 am
Code is not properly written. You must escape the double quotes properly and also you have placed the functions like str_replace inside the quotes which is not correct.
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Sun Dec 17, 2006 1:39 am
dibyendrah wrote: Code is not properly written. You must escape the double quotes properly and also you have placed the functions like str_replace inside the quotes which is not correct.
This is actually legit.
lampster
Forum Newbie
Posts: 6 Joined: Sun Dec 17, 2006 12:34 am
Post
by lampster » Sun Dec 17, 2006 1:49 am
Jcart wrote: dibyendrah wrote: Code is not properly written. You must escape the double quotes properly and also you have placed the functions like str_replace inside the quotes which is not correct.
This is actually legit.
so should I call the str_replace after $photo = $photo['fields']; ?
Like so:
$photo = $photo['fields'];
$photo['link'] = str_replace('index.php?', 'index2.php?', $photo['link'];
then in my link use just
$return.= "<a href="javascript:popUp('".$photo['link']."')">"
Ollie Saunders
DevNet Master
Posts: 3179 Joined: Tue May 24, 2005 6:01 pm
Location: UK
Post
by Ollie Saunders » Sun Dec 17, 2006 9:43 am
When writing strings in PHP use single quote by default and double when you have a reason to. Generally you want to minimize the amount of escaping you have to do because it hampers readability. In this case where you need to use single and double quotes in the string you'd probably be best with heredoc (
read this ) syntax:
Code: Select all
$string = <<< EOS
single ' and double " quotes no problem. {$variables} can join in the fun too.
EOS;used Code: Select all
tags don't highlight this correctly[/size]
lampster
Forum Newbie
Posts: 6 Joined: Sun Dec 17, 2006 12:34 am
Post
by lampster » Sun Dec 17, 2006 4:25 pm
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
I've never used the heredoc syntax method - and am pretty confused now that I've read the link to the php doc.
i started with the following lineCode: Select all
foreach ($data['results'] as $photo) {
$photo = $photo['fields'];
$return.= "<div class=\"containerBox\">";
$return.= "<a href=\"javascript:popUp('".$photo['link']."')\"><b>".$photo['title']."</b></a><br />";
and attempted to revise this to replace the $photo['link'] variables with
Code: Select all
foreach ($data['results'] as $photo) {
$photo = $photo['fields'];
$return.= "<div class=\"containerBox\">";
$return.= "<a href=\"javascript:popUp('".$photo['link'] = str_replace('index.php?', 'index2.php?', $photo['link']"')\"><b>".$photo['title']."</b></a><br />";
if i apply the heredoc here - should I be breaking the line at the first instance of my variable? like so:
Code: Select all
$return.= "<a href=\"javascript:popUp('".$photo['link'] = <<<EOS
str_replace('index.php?', 'index2.php?', $photo['link']"')\"><b>".$photo['title']."</b></a><br />"EOS; [/quote]
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Ollie Saunders
DevNet Master
Posts: 3179 Joined: Tue May 24, 2005 6:01 pm
Location: UK
Post
by Ollie Saunders » Sun Dec 17, 2006 6:20 pm
I wrote: Code: Select all
$string = <<< EOS
Hello, world!
EOS;
echo $string;Code: Select all
Fatal Error Error, I'm PHP and I hate you!
EOS has whitespace before it.
Last edited by
Ollie Saunders on Wed Dec 20, 2006 8:17 pm, edited 1 time in total.
lampster
Forum Newbie
Posts: 6 Joined: Sun Dec 17, 2006 12:34 am
Post
by lampster » Wed Dec 20, 2006 8:11 pm
I'm still getting a parse error
I replaced my code
Code: Select all
foreach ($data['results'] as $photo) {
$photo = $photo['fields'];
$return.= "<div class=\"containerBox\">";
$return.= "<a href=\"javascript:popUp('".$photo['link']."')\"><b>".$photo['title']."</b></a><br />";
$return.= "<a href=\"javascript:popUp('".$photo['link']."')\">"
."<img src=\"".$photo['thumb']."\" border=\"0\" alt=\"\" title=\"".$photo['title']."\" />"
."</a>\n";
$return.= "</div>";
}
return $return;
with as you suggested
Code: Select all
foreach ($data['results'] as $photo) {
$photo = $photo['fields'];
$return.= "<div class=\"containerBox\">";
$photo['link'] = str_replace('index.php?', 'index2.php?', $photo['link']);
$return.= <<< EOS
<a href="javascript:popUp('{$photo['link']}')"><b>{$photo['title']}</b></a><br />
EOS;
$return.= <<< EOS
<a href="javascript:popUp('{$photo['link']}')"><img src="{$photo['thumb']} border="0" title="{$photo['title']}"></a>
EOS;
$return.= "</div>";
}
return $return;
did I mess something up with the second instance of the photo link?
I tried just modifying the first instance and it broke at the line where the second instance starts.
dibyendrah
Forum Contributor
Posts: 491 Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:
Post
by dibyendrah » Thu Dec 21, 2006 12:35 am
Yahoo! I found the mistake.
EOT must terminate in heredoc immidiately after finishing the text with a new line and should not contain any spaces.
Code: Select all
foreach ($data['results'] as $photo) {
$photo = $photo['fields'];
$return .= "<div class=\"containerBox\">";
$photo['link'] = str_replace('index.php?', 'index2.php?', $photo['link']);
$return .= <<<EOT
<a href="javascript:popUp('{$photo['link']}')"><b>{$photo['title']}</b></a><br />
EOT;
$return .= <<<EOT
<a href="javascript:popUp('{$photo['link']}')"><img src="{$photo['thumb']}" border="0" title="{$photo['title']}"></a>
EOT;
$return .= "</div>";
}
return $return;
lampster
Forum Newbie
Posts: 6 Joined: Sun Dec 17, 2006 12:34 am
Post
by lampster » Thu Dec 21, 2006 4:36 am
dibyendrah wrote: Yahoo! I found the mistake.
EOT must terminate in heredoc immidiately after finishing the text with a new line and should not contain any spaces.
Code: Select all
foreach ($data['results'] as $photo) {
$photo = $photo['fields'];
$return .= "<div class="containerBox">";
$photo['link'] = str_replace('index.php?', 'index2.php?', $photo['link']);
$return .= <<<EOT
<a href="javascript:popUp('{$photo['link']}')"><b>{$photo['title']}</b></a><br />
EOT;
$return .= <<<EOT
<a href="javascript:popUp('{$photo['link']}')"><img src="{$photo['thumb']}" border="0" title="{$photo['title']}"></a>
EOT;
$return .= "</div>";
}
return $return;
i was worried about that space and line issue - i'll retry
lampster
Forum Newbie
Posts: 6 Joined: Sun Dec 17, 2006 12:34 am
Post
by lampster » Thu Dec 21, 2006 7:54 pm
Thanks dibyendrah! that works.