Content showing PHP - php

I have a problem in a file : movies.php
I want to show all movies on the files when there is no id, and if the id exists, i want to show the movie with that id , i used :
echo "<div id='head'>$title</div>";
echo "<div id='bodyar'>$content</div> <br />
<hr>Category : <span class='date'>$moviecategory</span></hr>
<hr>Views : <span class='date'>$views_numimg</span></hr>
<hr></hr> <br />"; exit;}
$orderposts = mysql_query("select * from movie ");
echo "<div class='bodypanelposts'>";
while ($rowar = mysql_fetch_assoc($orderposts)) {
$id_po = $rowar['id'];
$picture = $rowar['picture'];
$title = $rowar['title'];
echo "<div id='movieall'><table id='classing' border='0'
cellspacing='2'><tr> <td>";
echo "<a href='movies.php?id=$id_po'><img src='$picture' alt='$image_caption' width='180' height='250'><br /></div><div class='movies'>$title</div></a><br />LIKE BOX GOES HERE</tr></td></table></div>";
}
The problem is , after using that , the footer is not appearing anymore ..
I want it to appear.

To let PHP know it has to start interpret the code, you need start tags:
<?php
// PHP code here
?>
You should also concat variables by a dot instead of putting into the quotes:
echo "<div id='head'>" . $title . "</div>";
(Some might say this is not important but it is IMO, PHP can't handle it properly in every case.)
When using exit;, you tell PHP to quit and flush the result to the browser.
There is also a closing } bracket after the exit, but I don't see any opening { bracket.

A better way to handle your HTML is to do it like this:
<div id='head'><?=$title?></div>
<div id='bodyar'><?=$content?></div>
<br />
<table>
<tr><td>Category</td><td><span class='date'><?=$moviecategory?></span></td></tr>
<tr><td>Views</td><td><span class='date'><?=$views_numimg?></span></td></tr>
</table>
<div class='bodypanelposts'>
<?php
while ($rowar = mysql_fetch_assoc($orderposts)) {
$id_po = $rowar['id'];
$picture = $rowar['picture'];
$title = $rowar['title'];
echo <<<HTML
<div id='movieall'>
<table id='classing' border='0' cellspacing='2'>
<tr><td><a href='movies.php?id=$id_po'><img src='$picture' alt='$image_caption' width='180' height='250'><div class='movies'>$title</div></a>
<br />LIKE BOX GOES HERE
</td></tr>
</table>
</div>
HTML;
?>
</div>
Notice the <?= tags to do inline PHP echo statements, allowing you to write HTML without having to wrap them in echo statements.
You can also use HEREDOC syntax to echo out a large chunk of HTML with variables inline.
These two methods make it much easier to reason about what your code is outputting.

Related

Retrieve blob image into html php

Display image from mysql.
<?php
if(isset($id1)){
$query="SELECT * FROM `newss` WHERE `id`=".$id1;
$result=mysql_query($query);
if(!$result){
echo "<h3>News Not Availabel</h3>";
}else{
while($row=mysql_fetch_array($result)){
echo "
<img src=data:image/png;<?php echo ".$row['imges']."; ?> />
<figure class=date>
<h2>".$row['description']."</h2>
<div class=blog-detail-meta>
<span class=date><span class=fa fa-file-o></span>".$row['dates']."</span>
</div>
</header>
<hr>
<p>".$row['para1']." </p>
<p>
".$row['para2']."
</p>
<p>
".$row['para3']."
</p>
";
}
}
}else{
header("location:index.php");
}
?>
Image not properly displayed.
I stored raw data blob (png format). I want to display image, but only symbols are displayed.
Try
echo "<img src=\"data:image/png;base64,".$row['imges']."\" />" ;
Instead of
echo "<img src=data:image/png;<?php echo ".$row['imges']."; ?> />" ;
Is the table name really "newss"?
Don't do things like:
echo "
HTML CODE
";
Do this:
?>
HTML CODE <?php echo $php_code;?>
<?php
You currently trying to use PHP opening Tag <?php within PHP self.
<?php
echo "<?php echo 'test';?>";
?>
PHP won't parse this, but will just echo it as pure text.
Change following line:
<img src=data:image/png;<?php echo ".$row['imges']."; ?> />
to:
<img src="data:image/png;".$row['imges'].";"/>
Also use double quotes for HTML-Parameters like src="path/to/file.format".
Not being lazyis very important in coding!
If everything does not work, what is the output of $row['imges'] ?

Advice on cleaner way to write this While statement

So the following code works, it is doing everything i want it to do. However, as i step back it seems like an overly convoluted approach to what is arguably one of the most common tasks in php.
I know enough about php to figure out what most things are doing when i see them, and to create some rather ugly code like you will see below; however, the finer points evade me.
I was hoping that if someone had some free time, he/she could look this over and show me a more concise way to approach this.
<?php
$result = mysql_query('SELECT * FROM events');
$i = 1;
while ($row = mysql_fetch_assoc($result)) {
echo '<div id="item_gallery_s'.$i .'"'. 'class="fluid profileImgWrap goldDiagGrad">' .
'<div class="profile_name">' . $row['name'] . '<br /><span class="profile_date">' .
'<a href="http:#"
target="_blank"
title="some title">' . $row['place'] .
'</a></span></div><!-- DCD Diva Name -->' .
'<a rel="events[events]"
href="#">' .
'<div class="profile_banner">Custom Banner</div><!-- Banner -->' .
'<img src='.'"img/upload/'.$row['icon'].
'"' .
'alt="image description |'.$row['name'].
'"/>' .
'<!-- Photo --></a></div><!-- END #item_gallery_s'.$i .'-->';
$i++;
}?>
The loop itself is fine but you'll find varying opinions on the HTML-in-strings. For the past seven years I've encouraged my team to either use HTML with php tags or we rely on a full templating system:
<?php while ($row = mysql_fetch_assoc($result)): ?>
<div><?= $row['something'] ?></div>
<?php endwhile ?>
Though we have short tags enabled for even cleaner code. The benefit of this is that it's cleaner - less quotes, escaping problems, and IDEs will be able to syntax highlight the html. Most treat the html as string when it's inside quotes.
That's as "concise" as it gets.
You could not use an echo inside the while. And use php short tags.
while ($row = mysql_fetch_assoc($result)) {
?>
<?=$row['place'];?>
<?php
}
?>
Another way to "clean up", would be to use a template engine, but once again that would be just for the HTML part.
{place}
Good coding!
You can clean this up a bit by interspersing actual HTML, rather than simply echoing it:
<?php
$result = mysql_query('SELECT * FROM events');
$i = 1;
while ($row = mysql_fetch_assoc($result)) {
?>
<div id="item_gallery_s<?php echo $i; ?>" class="fluid profileImgWrap goldDiagGrad">
<div class="profile_name">
<?php echo $row['name']; ?>
<br />
<span class="profile_date"><?php echo $row['place']; ?></span>
</div><!-- DCD Diva Name -->
<a rel="events[events]" href="#"><div class="profile_banner">Custom Banner</div><!-- Banner -->
<img src="img/upload/<?php echo $row['icon']; ?>" alt="image description |<?php echo $row['name']; ?>"/><!-- Photo --></a>
</div><!-- END #item_gallery_s<?php echo $i; ?> -->
<?php
$i++;
}?>
Another option, depending on how much work like this you have to do, would be a full-blown template engine such as Smarty.
Here's how I would probably format this code (as a matter of personal style):
<?php
$result = mysql_query('SELECT * FROM events');
$i = 1;
while ($row = mysql_fetch_assoc($result)) {
echo
'<div id="item_gallery_s'.$i.'" class="fluid profileImgWrap goldDiagGrad">
<div class="profile_name">' . $row['name'] . '<br /><span class="profile_date">
<a href="http:#" target="_blank" title="some title">' . $row['place'] .
'</a></span>
</div><!-- DCD Diva Name -->
<a rel="events[events]" href="#">
<div class="profile_banner">Custom Banner</div><!-- Banner -->
<img src="img/upload/' . $row['icon']. '"
alt="image description |' . $row['name']. '"/>
<!-- Photo -->
</a>
</div><!-- END #item_gallery_s'.$i .'-->';
$i++;
}
?>
Try also to use consistent indentation to make it easy to tell what matches up with what. By the way, a <div> (block element) inside an <a> (inline element) is bad form. Did you mean to use a <span>? Learn to use the W3C validator to pick up this stuff.

PHP echo inside echo

I'm trying to call an HTML/PHP content that it's inside my database using:
<?php echo $row_content['conteudo']; ?>
When the row is called the HTML appears correctly but the PHP doesn't.
I belieave it's cause of the echo inside the main echo.
<?php echo "
<h3>Hello</h3>
<?php do { ?>
<div class=\"indios\">
<a href=\"indio.php?id=<?php echo $row_indiosct['id']; ?>\">
<img src=\"galeria/indios/<?php echo $row_indiosct['foto']; ?>\" alt=\"<?php echo $row_indiosct['nome']; ?>\" />
<br /><?php echo $row_indiosct['nome']; ?></a></div>
<?php } while ($row_indiosct = mysql_fetch_assoc($indiosct)); ?> "
?>
The line one of this code is the same echo as the first code field, it's not repeating, it's there just for help and to understand where is the problem.
I already fixed some quotation marks but it gives an error in the line of the 1st echo.
That is some of the ugliest code I have ever seen...
<?php
echo '
<h3>Hello</h3>';
while ($row_indiosct = mysql_fetch_assoc($indiosct))
{
echo '
<div class="indios">
<a href="indio.php?id='.$row_indiosct['id'].'">
<img src="galeria/indios/'. $row_indiosct['foto'].'" alt="'.$row_indiosct['nome'].'" />
<br />'.$row_indiosct['nome'].'</a>
</div>';
}
?>
You could also use the HEREDOC syntax.
Don't do this. Multi-line echoes, especially when you've got embedded quotes, quickly become a pain. Use a HEREDOC instead.
<?php
echo <<<EOL
<h3>Hello</h3>
...
<div class"indios">
...
EOL;
and yes, the PHP inside your echo will NOT execute. PHP is not a "recursively executable" language. If you're outputting a string, any php code embedded in that string is not executed - it'll be treated as part of the output, e.g.
echo "<?php echo 'foo' ?>"
is NOT going to output just foo. You'll actually get as output
<?php echo 'foo' ?>
You have misunderstood how PHP works. PHP is processed by the server. When it encounters your script, it sees the following:
<?php echo "some long piece of text that you have told PHP not to look at" ?>
What is the reasoning behind trying to nest PHP calls inside strings?
evaluate code php in string using the function eval(): this post Execute PHP code in a string
<?php
$motto = 'Hello';
$str = '<h1>Welcome</h1><?php echo $motto?><br/>';
eval("?> $str <?php ");
http://codepad.org/ao2PPHN7
also if your need the code buffer output in a string also you can using the ob_start() method:
<?php ob_start(); ?>
<h3>Hello</h3>;
<?php
while ($row_indiosct = mysql_fetch_assoc($indiosct)){ ?>
<div class="indios">
<a href="indio.php?id='<?php echo $row_indiosct['id']'">
<img src="galeria/indios/'<?php echo $row_indiosct['foto'].'" alt="'.$row_indiosct['nome'].'" />
<br />'.$row_indiosct['nome'].'</a>
</div>';
<?php } ?>

How to set this if isset function

I have the following code that sets a background if user has uploaded to database. If user has NOT uploaded an image then the result is a blank img src=''
I need to set this as an if isset function so I can plug in an alternate image if user has not uploaded anything.
Here is the current code:
<div id="background"><?php echo isset($background_image) && file_exists(ROOT.$background_image)?"<img src='$background_image' alt='' />":'';?></div>
Your code's a little dirty, opening php and closing it mid-html tag is only going to make it confusing for you in the future.
You're echoing back an isset which is just echo'ing back a boolean.
Try this;
$background_image = ""; // Not sure what you're using here - their username? Dump it in here anyway.
if (file_exists($background_image))
{
echo " <div id=\"background\">
<img src=\"{$background_image}\" alt=\"\" title=\"\" />
</div>";
}
Hope this helps.
Eoghan
I'm not sure what you mean, but a another aproach to what you are trying to do would be:
<div id="background">
<?php
$optionalImage = 'background.png';
$userImage = getUserImage();
if(empty($userImage)) {
$userImage = $optionalImage;
}
?>
<img src="<?php echo $userImage; ?>" />
</div>
Is it really neccessary to set the blank source of the image?
But a understandable and corrected code of what you are attempting is this
<div id="background">
<?php
echo "<img src='";
echo isset($background_image) && file_exists(ROOT.$background_image) ? $background_image : '';
echo "' alt='' />";
?>
</div>
The problem was that, either you were echo entire <img> tag, or just display ' '(Blank) with attached endings.
The short form:
echo "<img src='".(isset($background_image) && file_exists(ROOT.$background_image) ? $background_image : '')."' alt='' />";

PHP - indent block of html

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>
';
?>

Categories