SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default image if none appears in DB

    Hi I am displaying records on my page with an image from the database, as it is not a requirement for a user to upload an image I want to use a default image when there is no image stored in the database I have been trying this but cannot get it working?

    Code PHP:
    <img src="/uploads/
    	<?php 
    	$image = $row2['name'];
    	$default = "default.jpg";
    if ( $image == "" ) echo $default;
    	else
      echo $image;
    	?>
        " alt="a" width="90" height="90" />
    Appreciate any help I get

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    What happens? What is the result of your script? (look at the html code of the page in your browser).
    Maybe the field isn't really empty? Maybe it has the value NULL, or some space in it? Try
    PHP Code:
    $image trim($row2['name']); 

  3. #3
    messing with my mind fristi's Avatar
    Join Date
    Feb 2009
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe this can help?

    PHP Code:
    <?php

      $image 
    $row2['name'];
      if (empty(
    $image)) $image 'default.jpg';
      echo 
    '<img src="/uploads/'$image'" alt="a" width="90" height="90" />';

    ?>
    To PHP or to Perl, that is the question!
    (Bucket - simpletest) User

  4. #4
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi guys thanks for the quick replies i tried those two methods and am still getting the same problem with only records with an image appearing.

    I am thinking now that this could be down to the array I am using to take the information from the db. Is there any chance that the array is ceasing to function and display the records if there is nothing in the "name" field. Is there a way I can fix this, I am posting some more php code below to better explain what I am trying to say.

    Code PHP:
    		//Get all the rows
    		$query="SELECT * FROM $table  ORDER BY 'id' LIMIT $offset, $RPP";
    		$result=mysql_query($query) or die('Failed selecting applications: ' . mysql_error());
    		$num=mysql_numrows($result);
     
    		//Echo each row
    		while($row = mysql_fetch_array($result)) {
     
     
    			$query2="SELECT * FROM images WHERE propertyid = ".$row['id']." LIMIT 1";
    		$result2=mysql_query($query2) or die('Failed selecting applications: ' . mysql_error());
    		$num2=mysql_numrows($result2);
     
    		//Is it possible that this array will only show record that do have an image?
    		while($row2 = mysql_fetch_array($result2)) {?>
    			<div id="searchresults">
     
    <div id="searchresults1"> <img src="/uploads/
    	<?php 
    	$image = trim($row2['name']);
    	$default = "default.jpg";
    if(empty($image)){ echo $default;}
    	else
      echo $image;
    	?>

  5. #5
    messing with my mind fristi's Avatar
    Join Date
    Feb 2009
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If nothing is shown at all, that means that there is no array to fetch for $result2.

    Try echoing $num2 to the screen before you enter the second while loop. That way you will see if there are any results or 0.



    Code PHP:
    <?php
      //Get all the rows
      $query = "SELECT * FROM $table  ORDER BY 'id' LIMIT $offset, $RPP";
      $result = mysql_query($query) or die('Failed selecting applications: ' . mysql_error());
      $num = mysql_numrows($result);
     
      //Echo each row
      while($row = mysql_fetch_array($result)) {
     
        $query2 = "SELECT * FROM images WHERE propertyid = ".$row['id']." LIMIT 1";
        $result2 = mysql_query($query2) or die('Failed selecting applications: ' . mysql_error());
        $num2 = mysql_numrows($result2);
        echo $num2;
     
          //If you limit the result to 1, there is no need for a loop.
        $row2 = mysql_fetch_array($result2));
        $image = $row2['name'];
        if (empty($image)) $image = 'default.jpg';
     
        echo '<div id="searchresults"><div id="searchresults1">',
             '<img src="/uploads/', $image, '" alt="a" width="90" height="90" />';
     
     ?>
    To PHP or to Perl, that is the question!
    (Bucket - simpletest) User

  6. #6
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi fristi,

    When I echo $num2 I get the number one appearing alongside each record that has an image and then just 0's for all the records that have no image and also I get no other fields for these records even though they are populated in the first table.

    I have just been looking at SQL JOINS could one be used here in this case instead?

    Thanks so much for the help so far.

  7. #7
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Tried a SQL join and that worked so pretty hapy with that for now. I am so grateful for the help though.
    Thank you so much fristi and guido2004.

  8. #8
    messing with my mind fristi's Avatar
    Join Date
    Feb 2009
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lostprophetsie View Post
    Tried a SQL join and that worked so pretty hapy with that for now. I am so grateful for the help though.
    Thank you so much fristi and guido2004.
    I'm happy that you solved it yourself It gives this feeling of self fulfillment doesn't it Happy to try and help you
    To PHP or to Perl, that is the question!
    (Bucket - simpletest) User


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •