Jump to content
xisto Community
Sign in to follow this  
jlhaslip

Complex Php Echo Statement Assistance Cant quite get this one, help appreciated

Recommended Posts

Can someone help me out with this one, please. I have a PHP script in which I am trying to write a fairly complex piece of html and I can't seem to get the coding of the PHP echo statement. There are single and double quotes scattered throughout this line of html and it is driving me nuts attempting to manage the output. So far, the only way I can get it to work is to break the echo'ing of the line of html code into the following lines of PHP:

echo "\t" ;echo '<li><a href="';echo $full_dir . $file ;echo '" alt="full-size"><img src="' ;echo $dir . $file ;echo '" alt="' ;echo $dir . $file ;echo '" ><img src="' ;echo $mid_dir . $file ;echo '" alt="mid-size" class="preview" /></a></li> ';echo "\n\r\t";

The script is managing to correctly output a string of html as follows:

<li><a href="full_images/pict0023.jpg" alt="full-size"><img src="images/pict0023.jpg" alt="images/pict0023.jpg" ><img src="mid_images/pict0023.jpg" alt="mid-size" class="preview" /></a></li>

I have broken it down into segments with carriage returns in this example, but it could be a single, continuous piece of code, too.This sample of PHP code works correctly in a Browser, so it isn't an issue with the 'correctness' of the html, it has to do with the complexity of the single and double quotes and the PHP required to output this line as one string instead of the 11 echo statements.I will continue to make some attempts, but so far I just am not managing to sort out the method of escaping the right pieces of code and arranging all the bits and pieces. Any suggestions for cleaning up and leaning out this code would be appreciated.

Share this post


Link to post
Share on other sites

Try this echo instead..

echo "\t";echo "<li><a href='";echo $full_dir . $file;echo "' alt='full-size'><img src='";echo $dir . $file;echo "' alt='";echo $dir . $file;echo "' ><img src='";echo $mid_dir . $file;echo "' alt='mid-size" class='preview' /></a></li> ";echo "\n\r\t";

Basically I've swapped " to become the first and last instead of using '.
I believe " has higher priority than ', hence " should comes first.

Share this post


Link to post
Share on other sites

It's quite late here (I just got back from working on a project all night), so I might not be thinking strait, but I'm fairly sure that you need to escape quotes if you use them more than once. Also, I don't see why you can't use more concatenations instead of echo commands to make it cleaner.Try:

echo "\t" ;echo "<li><a href=\"" . $full_dir . $file . "\" alt=\"full-size\">";echo "<img src=\"" . $dir . $file . " alt=\"" . $dir . $file . "\" / >";echo "<img src=\"" . $mid_dir . $file . " alt=\"mid-size\" class=\"preview\" />";echo "</a></li>";echo "\n\r\t";

I have no idea whether it'll work as it's completely off the top of my head, but it seems reasonable, and it's quite a bit cleaner code.

Share this post


Link to post
Share on other sites

Even though this point is not relavant to the topic, I still feel like adding it.
When using complex statements of printing, use the code "print".

There is a difference between echo and print, but speed-wise it should be irrelevant which one you use. print() behaves like a function in that you can do: $ret = print "Hello World";And $ret will be 1.That means, that print can be used as part of a more complex expression where echo cannot.


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.