lastlifelost Posted September 30, 2009 Share Posted September 30, 2009 I've written this php loop: <?php $x=array("jpg1","jpg2","jpg3");foreach ($x as $value){echo "<div class='imgcontainer'>";echo " <a href='java script:void(0);' onclick='showWindow('" . $value . "')'>";echo " <img alt='" . $value . "' src='images/products/thumbs/" . $value . ".jpg' height='120' width='160' />";echo " </a>";echo " </div> <!-- END .imgcontainer -->";echo " <!-- BEGIN #" . $value . "-->";echo " <div class='popupdiv' id='" . $value . ">";echo " <div id='subdiv'>";echo " <div class='close'>";echo " <a href='java script:void(0);' onclick='CloseWindow('" . $value . "')'>Close Gallery</a>";echo " </div>";echo " <a href='http://woodturningvt.com/images/products/" . $value . ".JPG' target='blank'> <!-- full size view -->";echo " <img src='http://woodturningvt.com/images/products/" . $value . ".JPG' alt='$value' title='click for full size view'/>";echo " </a>";echo " <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam porttitor rutrum massa ac fringilla. Proin fringilla tincidunt dui, et condimentum nulla viverra vitae. Proin ipsum mauris, suscipit ac adipiscing ut, porttitor eu velit. Vivamus blandit orci vel justo sagittis viverra. In hac habitasse platea dictumst. Ut id eros sodales arcu facilisis tristique a cursus metus. Praesent iaculis egestas turpis sed hendrerit. Etiam eros eros, iaculis a ultrices in, sagittis in erat. Curabitur tristique purus eu nisi euismod ut rutrum mauris tincidunt. Quisque sit amet sem nunc. Sed sed eleifend lacus. </p>";echo " </div> <!-- END .subdiv -->";echo " </div> <!-- END .popupdiv -->";echo " <!-- END #" . $value . " -->";}?> But the output html source doesn't retain any formatting. It just drops into a mess of characters on the page, really. It works and all, but the source is a mess! Is there a way to make each echo render on a new line in the resulting html? Link to comment Share on other sites More sharing options...
jeffman Posted September 30, 2009 Share Posted September 30, 2009 Add the newline character to the end of each line "\n" .You might also read about heredoc formatting. It'll save you some hassle, including the need for the newline chars. Make sure you read about PHP's version of heredocs. Link to comment Share on other sites More sharing options...
justsomeguy Posted September 30, 2009 Share Posted September 30, 2009 Here's that part from the manual:http://www.php.net/manual/en/language.type....syntax.heredoc Link to comment Share on other sites More sharing options...
lastlifelost Posted September 30, 2009 Author Share Posted September 30, 2009 /n! That was the key I was looking for! I new it was "n" something... Thanks for the suggestion of heredoc. I'm going to use that instead. Seems pretty straight forward, I just drop my code in between $mystring <<<whatevermystring; right? Link to comment Share on other sites More sharing options...
justsomeguy Posted September 30, 2009 Share Posted September 30, 2009 The end identifier needs to be the same as the first one, e.g.:$mystring <<<whateverwhatever;Just make sure that ending identifier is alone on its own line, it can't even have spaces before it. This is what the first few lines would look like:echo <<<end_of_text<div class='imgcontainer'><a href='java script:void(0);' onclick="showWindow('$value')"><img alt='$value' src='images/products/thumbs/{$value}.jpg' height='120' width='160' /></a></div> <!-- END .imgcontainer --><!-- BEGIN #{$value} -->end_of_text; Link to comment Share on other sites More sharing options...
lastlifelost Posted September 30, 2009 Author Share Posted September 30, 2009 The end identifier needs to be the same as the first one, e.g.:$mystring <<<whateverwhatever;...Thanks for catching that! Link to comment Share on other sites More sharing options...
jeffman Posted September 30, 2009 Share Posted September 30, 2009 Just make sure that ending identifier is alone on its own line, it can't even have spaces before it.I really hate that part. I'm scrupulous about logical indentation, and this rule makes my code look like ####. Link to comment Share on other sites More sharing options...
lastlifelost Posted September 30, 2009 Author Share Posted September 30, 2009 OK, I'm trying to use the heredoc solution posted above, but I keep getting this error:Parse error: syntax error, unexpected T_SL in /home/content/l/a/s/lastlifelost/html/jpgphp2.php on line 13Here's the code: <?php $images=array("jpg1","jpg2","jpg3");foreach ($images as $value){echo <<<gallery <div class="imgcontainer"> <a href="java script:void(0);" onclick="showWindow('$value')"> <img alt="$value" src="images/products/thumbs/$value.jpg" height="120" width="160" /> </a> </div> <!-- END .imgcontainer --> <!-- BEGIN #$value --> <div class="popupdiv" id="$value"> <div id="subdiv"> <div class="close"> <a href="java script:void(0);" onclick="CloseWindow('$value')">Close Gallery</a> </div> <a href="http://woodturningvt.com/images/products/$value.JPG" target="blank"> <!-- full size view --> <img src="http://woodturningvt.com/images/products/$value.JPG" alt="$value" title="click for full size view"/> </a> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam porttitor rutrum massa ac fringilla. Proin fringilla tincidunt dui, et condimentum nulla viverra vitae. Proin ipsum mauris, suscipit ac adipiscing ut, porttitor eu velit. Vivamus blandit orci vel justo sagittis viverra. In hac habitasse platea dictumst. Ut id eros sodales arcu facilisis tristique a cursus metus. Praesent iaculis egestas turpis sed hendrerit. Etiam eros eros, iaculis a ultrices in, sagittis in erat. Curabitur tristique purus eu nisi euismod ut rutrum mauris tincidunt. Quisque sit amet sem nunc. Sed sed eleifend lacus. </p> </div> <!-- END .subdiv --> </div> <!-- END .popupdiv --> <!-- END #$value --> gallery;?> What am I doing wrong? Line 13 is echo <<<gallery. Did I mess up the syntax somewhere? Is it possibly because my host server doesn't have the right version of PHP. I'm hosted by GoDaddy. Would it be safe to assume that they have the current versions uploaded? What's wrong with this code? Link to comment Share on other sites More sharing options...
justsomeguy Posted September 30, 2009 Share Posted September 30, 2009 That's interesting. According to this page:http://www.php.net/manual/en/tokens.phpThe T_SL parser token is the << bitwise shift operator. For some reason it's stopping at << and not using <<<. I'll look into it. Link to comment Share on other sites More sharing options...
justsomeguy Posted September 30, 2009 Share Posted September 30, 2009 This version is working for me.. I'm not sure what I did to make it work. I tried a couple things with the same results then went back and changed something back and it worked. I can't even get that error to show now. This is your same code with an extra } at the end to close the foreach loop: <?php$images=array("jpg1","jpg2","jpg3");foreach ($images as $value){echo <<<gallery <div class="imgcontainer"> <a href="java script:void(0);" onclick="showWindow('$value')"> <img alt="$value" src="images/products/thumbs/$value.jpg" height="120" width="160" /> </a> </div> <!-- END .imgcontainer --> <!-- BEGIN #$value --> <div class="popupdiv" id="$value"> <div id="subdiv"> <div class="close"> <a href="java script:void(0);" onclick="CloseWindow('$value')">Close Gallery</a> </div> <a href="http://woodturningvt.com/images/products/$value.JPG" target="blank"> <!-- full size view --> <img src="http://woodturningvt.com/images/products/$value.JPG" alt="$value" title="click for full size view"/> </a> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam porttitor rutrum massa ac fringilla. Proin fringilla tincidunt dui, et condimentum nulla viverra vitae. Proin ipsum mauris, suscipit ac adipiscing ut, porttitor eu velit. Vivamus blandit orci vel justo sagittis viverra. In hac habitasse platea dictumst. Ut id eros sodales arcu facilisis tristique a cursus metus. Praesent iaculis egestas turpis sed hendrerit. Etiam eros eros, iaculis a ultrices in, sagittis in erat. Curabitur tristique purus eu nisi euismod ut rutrum mauris tincidunt. Quisque sit amet sem nunc. Sed sed eleifend lacus. </p> </div> <!-- END .subdiv --> </div> <!-- END .popupdiv --> <!-- END #$value -->gallery;}?> That's running for me, the } is the only difference. Removing the bracket does not show the original parse error, it shows a different one. I'm not sure what's up there. Link to comment Share on other sites More sharing options...
lastlifelost Posted September 30, 2009 Author Share Posted September 30, 2009 I'm absolutely flabbergasted at this point. I went into my php file and added the } manually and got the same parsing error. On a whim, I then copy and pasted the code fro your last post into the file, saved and re-uploaded. Error solved. What a strange, strange thing.Just for giggles, I went back in and undid the copy/paste and reuploaded again. Error is still gone. Well, at least it works now, right? Link to comment Share on other sites More sharing options...
Synook Posted October 1, 2009 Share Posted October 1, 2009 Maybe you had a zero-length non-breaking character in between the second and third GT signs? Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.