This is the code that's creates the error.
<div>
<?php while($row2 = mysqli_fetch_assoc($get_comment)){?>
<div class="comment">
<p><?php echo $row2['comment']?></p>
</div>
<?php}?>
</div>
Problem is here <?php}?>. You should use an space between php tags like <?php } ?>
For example if you write like <?phpecho "Hello";?> This is invalid. Because compiler will not found any starting tag like <?php Because it is <?phpecho. In your case it is <?php}
I've been editing the following block of code and now when I use it then the page will not load but I also don't get anything in the error log either. I am guessing this has happened due to a missing or misplaced character somewhere but have looked and looked over it and cannot spot what it is! does it stand out to anyone?
<?php if ($logourldecoded != "") { ?>
<?php if ($companyname != "") { ?>
<img src="images/site-logo-high-res.png" alt="logo" style="max-height:91px; max-width:372px; min-height:91px;" />
<?php } else { ?>
<div class="companynameclass" style="font-size: <?php echo $data[font_size]; ?>;">
<?php echo $companyname; ?>
</div>
<?php } else { ?>
<img src="<?php echo $logourldecoded; ?>" alt="logo" style="max-height:91px; max-width:372px; min-height:91px;" />
<?php } ?>
I also know it's not that any of the data I'm calling with $NAME_HERE being incorrect or missing as if i just echo them all indiviually they are exactly as expected and before my edits where showing.
Missing } to close the inner IF
<?php
if ($logourldecoded != "") {
?>
<?php
if ($companyname != "") {
?>
<img src="images/site-logo-high-res.png" alt="logo" style="max-height:91px; max-width:372px; min-height:91px;" />
<?php
} else {
?>
<div class="companynameclass" style="font-size: <?php echo $data[font_size]; ?>;">
<?php echo $companyname; ?>
</div>
<?php
// the missing line
}
?>
<?php
} else {
?>
<img src="<?php echo $logourldecoded; ?>" alt="logo" style="max-height:91px; max-width:372px; min-height:91px;" />
<?php
}
?>
You might consider not placing <?php ..?> tags on each line, it makes the code almost completely unreadable, and whats unreadable is by definition unmaintainable
This question already has answers here:
HTML is still reading php code with <!---->
(7 answers)
Closed 8 years ago.
I currently have the following code on my page.
<?php
if($result["r_approved"] == "APPROVED"){
echo "<!--";
}
?>
<div class="main">
<div class="main-sub">
<?php include('http://www.contractorsintelligence.com/contractors-license/includes-page-elements/navigation1.php'); ?>
<div id="mid-top"><img src="https://www.contractorsintelligence.com/images/shadowbg-top.png" width="990" height="20" alt="Top Spacer"/></div>
<div id="mid_shdw">
<?php
if($result["r_approved"] == "APPROVED"){
echo "-->";
}
?>
With this code, I'm trying to block/ignore a block of code with <!-- and -->, but it does not want to ignore php code. How would I use PHP to block out an entire section of the code? I would really appreciate if you would use my current "if" statement variables.
Why not this:
<?php
if($result["r_approved"] != "APPROVED"){
?>
<div class="main">
<div class="main-sub">
<?php include('http://www.contractorsintelligence.com/contractors-license/includes-page-elements/navigation1.php'); ?>
<div id="mid-top"><img src="https://www.contractorsintelligence.com/images/shadowbg-top.png" width="990" height="20" alt="Top Spacer"/></div>
<div id="mid_shdw">
<?php
}
?>
Put the code inside the if. Please note that I also changed the if to a "does not equal"
<?php if($result["r_approved"] !== "APPROVED"): ?>
<div class="main">
...
</div>
<?php endif; ?>
I used the alternative syntax for readability reasons as I think it looks much cleaner than with the brackets if you mix PHP with HTML.
I'd say a much cleaner way to do this would be to use the alternative syntax for the if statement. This removes the obfuscation caused by printing comments from php. In your case, that would be:
<?php if(! $result["r_approved"] == "APPROVED"):?>
<div class="main">
<div class="main-sub">
<?php include('http://www.contractorsintelligence.com/contractors-license/includes-page-elements/navigation1.php'); ?>
<div id="mid-top"><img src="https://www.contractorsintelligence.com/images/shadowbg-top.png" width="990" height="20" alt="Top Spacer"/></div>
<div id="mid_shdw">
<?php endif; ?>
You can read more on the alternative syntax here.
Following code will produce unwanted whitespace between icons.
<div>
<img src="icon1.png" />
<img src="icon2.png" />
</div>
I need to keep image tags on single lines because I have some conditions in my .phtml file, it looks something like this:
<div>
<?php if ($condition1) : ?>
<img src="icon1.png" />
<?php endif ?>
<?php if ($condition2) : ?>
<img src="icon2.png" />
<?php endif ?>
</div>
I don't want to have all code messed up on a single line. Is there any solution for situations like this?
Apply font-size:0px; style to your div.
You may use echo to output parts of html code. You'll get something like this
<div>
<?php if (true) :
echo '<img src="icon2.png" />';
endif;
if (true) :
echo '<img src="icon2.png" />';
endif;
?>
</div>
Let's say I have the following code:
<?php
echo "<div id=\"root\">";
echo "<div id=\"child_of_root\">";
echo "<img src=\"picture1.png\">";
echo "<img src=\"picture2.png\">";
echo "<img src=\"picture3.png\">";
echo "<img src=\"picture4.png\">";
echo "<img src=\"picture5.png\">";
echo "</div>";
echo "</div>";
?>
If I ran this the following HTML would be rendered all inline without any line breaks:
<div id="root"><div id="child_of_root"><img src="picture1.png"><img src="picture2.png"><img src="picture3.png"><img src="picture4.png"><img src="picture5.png"></div></div>
If I ran the following code:
<?php
echo "<div id=\"root\">\n";
echo "\t"."<div id=\"child_of_root\">\n";
echo "\t\t"."<img src=\"picture1.png\">"."\n";
echo "\t\t"."<img src=\"picture2.png\">"."\n";
echo "\t\t"."<img src=\"picture3.png\">"."\n";
echo "\t\t"."<img src=\"picture4.png\">"."\n";
echo "\t\t"."<img src=\"picture5.png\">"."\n";
echo "\t"."</div>"."\n";
echo "</div>";
?>
It wound render the following beautiful HTML:
<div id="root">
<div id="child_of_root">
<img src="picture1.png">
<img src="picture2.png">
<img src="picture3.png">
<img src="picture4.png">
<img src="picture5.png">
</div>
</div>
Is there a way I could achieve these beautiful indents without having to put \t before every line I want to indent. I mean so that I can indent a block instead of one line.
For one thing, it's HTML markup, so it doesn't matter how it's formatted, the browser renders it all the same. Using a tool like Firebug can give you a much better way of navigating HTML in your web-pages.
On another note, you don't have to continually use echo commands to output HTML. PHP is more-or-less a templating language in itself, so you could just exit PHP, output your HTML in your own format, and then re-enter PHP.
For example:
<?php // ... your code before this ... ?>
<div id="root">
<div id="child_of_root">
<img src="picture1.png">
<img src="picture2.png">
<img src="picture3.png">
<img src="picture4.png">
<img src="picture5.png">
</div>
</div>
<?php // ... your code after this ... ?>
If your output needs some level of dynamism to it, you can always use PHP to add in loops and whatnot.
<?php // ... your code before this ... ?>
<div id="root">
<div id="child_of_root">
<?php for ($x = 1; $x <= 5; $x++): ?>
<img src="picture<?php echo $x; ?>.png">
<?php endfor; ?>
</div>
</div>
<?php // ... your code after this ... ?>
If you still need formatted HTML, maybe for displaying code samples or something, you'll either have to continue manually using \n and \t, or you could check out the PHP Tidy extension, which is built for formatting HTML.
First of all use:
echo '<img src="picture1.png">';
instead of
echo "<img src=\"picture1.png\">";
Code is much more clear.
If you want to return HTML code in PHP, there is no other way to do indents.
However why you want to print HTML code in PHP ? Can't you just exit PHP block here ?
<?php
// do something and exit PHP block
?>
<div id="root">
<div id="child_of_root">
<img src="picture1.png">
<img src="picture2.png">
<img src="picture3.png">
<img src="picture4.png">
<img src="picture5.png">
</div>
</div>
<?php
// do again something in PHP
?>
I would suggest using HEREDOC for this. It's best for holding large blocks of HTML and Text that you wish to retain formatting:
//Note that I used spaces and not tabs, but that's only due to the post editor.
echo <<<HTML
<div id="root">
<div id="child_of_root">
<img src="picture1.png">
<img src="picture2.png">
<img src="picture3.png">
<img src="picture4.png">
<img src="picture5.png">
</div>
</div>
HTML;
You can also do
$variable = <<<OPENINGTAG
text
text
text
OPENINGTAG;
echo $variable;
Variables will also be parsed inside HEREDOC strings. Just be careful of the ending tag, it's very temperamental. No spaces before or after on it's line. I don't even know if comments are allowed.
Here it is using Dominic Barnes example with a loop:
<?php
echo <<<HTML
<div id="root">
<div id="child_of_root">
HTML;
for ($x = 1; $x <= 5; $x++)
{
echo <<<HTML
<img src="picture$x.png">
HTML;
}
echo <<<HTML
</div>
</div>
HTML;
?>
could also do:
<?php
$output = <<<HTML
<div id="root">
<div id="child_of_root">
HTML;
for ($x = 1; $x <= 5; $x++)
{
$output .= <<<HTML
<img src="picture$x.png">
HTML;
}
$output .= <<<HTML
</div>
</div>
HTML;
echo $output;
?>
NOWDOC is also available in 5.3+ which act as single quoted strings with no variable parsing.
HEREDOC and NOWDOC strings can be concatenated on to in the same way as normal strings.
If you put your picture ids in an array, you can do a foreach() loop that echoes each of the images with the new line and tab characters in front without having to code it by hand. E.g.
echo '<div id="root">\n';
echo '\t<div id="child_of_root">\n';
$images = array('picture1', 'picture2', 'picture3', 'picture4', 'picture5');
foreach ($images as $image) {
echo '\t\t<img src="'.$image.'.png">\n';
}
echo '\t</div>\n';
echo '</div>';
I tried Tidy, but for some reason no matter how I configure it, it won't seem to indent. However, somebody else has written some code that will:
PHP function for cleaning up HTML and JavaSctipt code
There are also some intelligent guidelines for avoiding the need for such code:
Get beautifully indented HTML with your PHP templates: two rules to follow
Also: fredsco.com/programming/indenting-html-output-in-php
Here is an example that worked for me:
<?php
echo '
<div style="margin-left: 100px">
<div id="root">
<div id="child_of_root">
<img src="picture1.png" /><br />
<img src="picture2.png" /><br />
<img src="picture3.png" /><br />
<img src="picture4.png" /><br />
</div>
</div>
</div>
';
?>