Page 1 of 1

displaying image in php mysql

Posted: Fri Jun 12, 2015 10:01 pm
by Mark
hello,

can anyone help me please i want to check to make sure that the image exists if it dosent dont display an image if it does exist then display the image this is the line of code.
Code: Select all
<img src="../news_pics/<?php echo $post_image; ?>" width="100" height="100">
Full Page Code:-
Code: Select all
<?php include('styles/top.php'); ?>

    <div id="left">

       <?php 

include("includes/connect.php");

$select_posts = "SELECT * FROM news ORDER BY post_id DESC LIMIT 1";

$run_posts = mysql_query($select_posts);

while($row=mysql_fetch_array($run_posts)){

	$post_id = $row['post_id']; 
	$post_title = $row['post_title'];
	$post_date = $row['post_date'];
	$post_author = $row['post_author'];
	$post_image = $row['post_image'];
	$post_content = substr($row['post_content'],0,200);



?>

<h2>
<a href="pages.php?id=<?php echo $post_id; ?>">

<?php echo $post_title; ?>

</a>

</h2>

<p align="left">Published on:&nbsp;&nbsp;<b><?php echo $post_date; ?></b></p>

<p align="right">Posted by:&nbsp;&nbsp;<b><?php echo $post_author; ?></b></p>

<center><img src="news_pics/<?php echo $post_image; ?>" width="500" height="300" /></center>

<p align="justify"><?php echo $post_content; ?></p>

<p align="right"><a href="pages.php?id=<?php echo $post_id; ?>">Read More</a></p>
<hr>

<?php } ?>

    
    </div>
    
    
<?php include('styles/bottom.php'); ?>

Re: displaying image in php mysql

Posted: Sat Jun 13, 2015 7:42 am
by Filip
Code: Select all
<? 
$path = "../path/to/image";
echo "<img src=\"".(file_exists($path)) ? $path : "path/to/default" . "\" />";
?>

Re: displaying image in php mysql

Posted: Sat Jun 13, 2015 10:40 am
by Mark
hello,

i tried this but it only shows the name of the image that should be there and not the image.

Code: Select all
    <?
    
    $path = "../news_pics/$post_image";
    echo "<img src=\"".(file_exists($path)) ? $path : "" . "\" />";
    
    ?>  

Re: displaying image in php mysql

Posted: Sat Jun 13, 2015 3:00 pm
by Filip
Mark wrote:
hello,

i tried this but it only shows the name of the image that should be there and not the image.

Code: Select all
    <?
    
    $path = "../news_pics/$post_image";
    echo "<img src=\"".(file_exists($path)) ? $path : "" . "\" />";
    
    ?>  
Sorry, I've been typing this code on a mobile phone so I left out parenthesis..
The code should be:
Code: Select all
    <?
    
    $path = "../news_pics/$post_image";
    echo "<img src=\"".((file_exists($path)) ? $path : "") . "\" />";
    
    ?>  
KR,
-Filip

Re: displaying image in php mysql

Posted: Sat Jun 13, 2015 4:02 pm
by Mark
how would i set the size of the image?.

Re: displaying image in php mysql

Posted: Sat Jun 13, 2015 9:13 pm
by Filip
Add style="width: 123px" attribute into the img tag

Re: displaying image in php mysql

Posted: Sat Jun 13, 2015 10:30 pm
by Mark
where does it go in the code.

<?

$path = "news_pics/$post_image";
echo "<img src=\"".((file_exists($path)) ? $path : "") . "\" />";

?>

Re: displaying image in php mysql

Posted: Tue Jun 16, 2015 6:12 am
by Filip
Code: Select all
<?

$path = "news_pics/$post_image";
echo "<img style=\"width: 123px;\" src=\"".((file_exists($path)) ? $path : "") . "\" />";

?>