Page 1 of 1

php help please guys

Posted: Sun Jan 18, 2015 5:02 pm
by 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");?>

Re: php help please guys

Posted: Sun Jan 18, 2015 5:26 pm
by 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");?>

Re: php help please guys

Posted: Sun Jan 18, 2015 8:40 pm
by 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>

Re: php help please guys

Posted: Tue Jan 20, 2015 9:16 pm
by zachman61
On top of given replies, you could also check if it's empty
Code: Select all
(!empty($post_image))