• Top Members
    Reps
    Posts
  • 834 Replies
    2585 Replies
  • 716 Replies
    2026 Replies
  • 331 Replies
    1928 Replies

displaying image in php mysql

User avatar
Mark
VIP - Donator
Posts: 370

displaying image in php mysql

Fri Jun 12, 2015 11:01 pm

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

User avatar
Filip
Coding Guru
Posts: 833

Re: displaying image in php mysql

Sat Jun 13, 2015 8:42 am

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
Posts: 370

Re: displaying image in php mysql

Sat Jun 13, 2015 11:40 am

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 : "" . "\" />";
    
    ?>  

User avatar
Filip
Coding Guru
Posts: 833

Re: displaying image in php mysql

Sat Jun 13, 2015 4:00 pm

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
Posts: 370

Re: displaying image in php mysql

Sat Jun 13, 2015 5:02 pm

how would i set the size of the image?.

User avatar
Filip
Coding Guru
Posts: 833

Re: displaying image in php mysql

Sat Jun 13, 2015 10:13 pm

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
Posts: 370

Re: displaying image in php mysql

Sat Jun 13, 2015 11:30 pm

where does it go in the code.

<?

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

?>

User avatar
Filip
Coding Guru
Posts: 833

Re: displaying image in php mysql

Tue Jun 16, 2015 7:12 am

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

Post Reply

Return to “Help & Support”