displaying image in php mysql

8 posts Page 1 of 1
Contributors
User avatar
Mark
VIP - Donator
VIP - Donator
Posts: 372
Joined: Mon Aug 17, 2009 10:35 pm

displaying image in php mysql
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'); ?>
http://www.mbappz.com
Filip
Coding Guru
Coding Guru
Posts: 833
Joined: Wed Jan 05, 2011 3:59 pm

Re: displaying image in php mysql
Filip
Code: Select all
<? 
$path = "../path/to/image";
echo "<img src=\"".(file_exists($path)) ? $path : "path/to/default" . "\" />";
?>
CodenStuff wrote:
Nope, it's just your sick and dirty mind. You sick twisted warped little pervo :D
User avatar
Mark
VIP - Donator
VIP - Donator
Posts: 372
Joined: Mon Aug 17, 2009 10:35 pm

Re: displaying image in php mysql
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 : "" . "\" />";
    
    ?>  
http://www.mbappz.com
Filip
Coding Guru
Coding Guru
Posts: 833
Joined: Wed Jan 05, 2011 3:59 pm

Re: displaying image in php mysql
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
CodenStuff wrote:
Nope, it's just your sick and dirty mind. You sick twisted warped little pervo :D
User avatar
Mark
VIP - Donator
VIP - Donator
Posts: 372
Joined: Mon Aug 17, 2009 10:35 pm

Re: displaying image in php mysql
Mark
how would i set the size of the image?.
http://www.mbappz.com
Filip
Coding Guru
Coding Guru
Posts: 833
Joined: Wed Jan 05, 2011 3:59 pm

Re: displaying image in php mysql
Filip
Add style="width: 123px" attribute into the img tag
CodenStuff wrote:
Nope, it's just your sick and dirty mind. You sick twisted warped little pervo :D
User avatar
Mark
VIP - Donator
VIP - Donator
Posts: 372
Joined: Mon Aug 17, 2009 10:35 pm

Re: displaying image in php mysql
Mark
where does it go in the code.

<?

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

?>
http://www.mbappz.com
Filip
Coding Guru
Coding Guru
Posts: 833
Joined: Wed Jan 05, 2011 3:59 pm

Re: displaying image in php mysql
Filip
Code: Select all
<?

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

?>
CodenStuff wrote:
Nope, it's just your sick and dirty mind. You sick twisted warped little pervo :D
8 posts Page 1 of 1
Return to “Help & Support”