Jump to content

Dumb Formatting Question


lastlifelost

Recommended Posts

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

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

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

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

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

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

Maybe you had a zero-length non-breaking character in between the second and third GT signs? :)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...