php help please guys

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

php help please guys
Mark
hello guys,

im displaying my images from my MySQL database how would i get it so if they don't have an image it dosent display the image as with the code i use below it displays a box even though there isn't an image in the database.
Code: Select all
<?php require("./styles/top.php");?>
   
<p>Latest News</p>

<?php
include("includes/connect.php");

$select_posts = "select * from posts";

$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_keywords = $row['post_keywords'];
    $post_content = substr($row['post_content'],0,200);
    
    
}

?>

<?php echo $post_title; ?><br />
Published on:<?php echo $post_date ?> By <?php echo $post_author ?><br /><br />
<center><img src="/cms/pics/<?php echo $post_image; ?>" width="500" height="300" /></center>
<?php echo $post_content; ?><br />
<p align="left"><a href="pages.php?id=<?php echo $post_id; ?>">Read More</a></p>
     
<?php require("./styles/bottom.php");?>
http://www.mbappz.com
User avatar
comathi
Coding God
Coding God
Posts: 1242
Joined: Fri Mar 26, 2010 1:59 pm

Re: php help please guys
comathi
Just adding a conditional statement before echoing the image should work:
Code: Select all
<?php require("./styles/top.php");?>
   
<p>Latest News</p>

<?php
include("includes/connect.php");

$select_posts = "select * from posts";

$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_keywords = $row['post_keywords'];
    $post_content = substr($row['post_content'],0,200);
    
    
}

?>

<?php echo $post_title; ?><br />
Published on:<?php echo $post_date ?> By <?php echo $post_author ?><br /><br />
<?php if($post_image != ""){ ?><center><img src="/cms/pics/<?php echo $post_image; ?>" width="500" height="300" /></center> <?php } ?>
<?php echo $post_content; ?><br />
<p align="left"><a href="pages.php?id=<?php echo $post_id; ?>">Read More</a></p>
     
<?php require("./styles/bottom.php");?>
User avatar
visualtech
VIP - Donator
VIP - Donator
Posts: 265
Joined: Sat Nov 19, 2011 2:19 pm

Re: php help please guys
visualtech
Try this one!
Code: Select all
<center>
	
	<?php 

	$url = ((isset($post_image) || $post_image == "" || $post_image == null || $post_image === "" || $post_image === null) ? "<img src=\"/cms/pics/$post_image\" width=\"500\" height=\"300\" />" : "<img src=\"http://placehold.it/500x300\"/>" );
	echo $url;

	?>

</center>
Image
User avatar
zachman61
VIP - Donator
VIP - Donator
Posts: 1891
Joined: Wed Dec 16, 2009 9:56 pm

Re: php help please guys
zachman61
On top of given replies, you could also check if it's empty
Code: Select all
(!empty($post_image))
Nailing my feet to the floor is easier than using my own muscles to balance, but you don't see me doing that :)
4 posts Page 1 of 1
Return to “Help & Support”