Jump to content

[SOLVED] mysql fetch array - image display if available - how to?


Recommended Posts

I am using a HTML search form to give me the following result.  I can get everything to work, even get it to display images that are associated with particular search term, but can't figure out how to keep it from displaying that annoying little "There's not image here image" that explorer shows, the one with box and red "x".  I know that an if statement would do the trick, but I can't do "if's" inside of the "echo" is it possible to accomplish this?  How?  Thank you so much, the expertise on this forum has been tremendously helpful!

 

 while ($row= mysql_fetch_array($result)) {
  $title = $row["ptype"]; 
  $email = $row["email"];
  $did = $row["did"];
  ///////////////////////////
	$gender = $row["gender"];
	$price = number_format($row["price"], 2,'.',',');
	$qty = $row["qty"];
	$dob_month = $row["dob_month"];
	$dob_day = $row["dob_day"];
	$p_pound = $row["p_pound"];
	$p_oz = $row["p_oz"];
	$p_date = $row["p_date"];
	$link = $row["link"];
	$p_num = $row["p_num"];
	$address = $row["address"];
	$comment = $row["comment"];
	$avail_month = $row["avail_month"];
	$avail_day = $row["avail_day"];
	//////////////////////////////

  echo "<span class=\"style52\">- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - </span><p><span class=\"styledogsforsale\">$title for Sale - ID #$did</span><br>
        <a href=\"admin/uploads/$did.jpg\" target=\"_blank\"><img src=\"admin/imagesize.php?did=$did&h=100\" border=\"0\"></a> <a href=\"admin/uploads/a/$did.jpg\" target=\"_blank\"><img src=\"admin/imagesizea.php?did=$did&h=100\" border=\"0\"></a> <a href=\"admin/uploads/b/$did.jpg\" target=\"_blank\"><img src=\"admin/imagesizeb.php?did=$did&h=100\" border=\"0\"></a> <a href=\"admin/uploads/c/$did.jpg\" target=\"_blank\"><img src=\"admin/imagesizec.php?did=$did&h=100\" border=\"0\"></a><br>D.O.B. - $dob_month $dob_day<br>$qty $gender $title<br><span class=\"style40\">Price: $$price<br>Ad Expires $avail_month $avail_day<br>$Comment</span><p>Contact Information:</strong><br> <strong>Phone Number:</strong> $p_num <br><strong>Address:</strong> $address<br> <strong>Email:</strong> <a href=mailto:$email>$email</a><br><a href=\"$link\" target=\"_blank\">$link</a>
                        <br>

   <p>";
                     
  $count++ ;
  }

$currPage = (($s/$limit) + 1);

put the if statement outisde the echo statement

 

if ( there is a picture available)
{
echo "<span class=\"style52\">- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - </span><p><span class=\"styledogsforsale\">$title for Sale - ID #$did</span><br>
       <a href=\"admin/uploads/$did.jpg\" target=\"_blank\"><img src=\"admin/imagesize.php?did=$did&h=100\" border=\"0\"></a> <a href=\"admin/uploads/a/$did.jpg\" target=\"_blank\"><img src=\"admin/imagesizea.php?did=$did&h=100\" border=\"0\"></a> <a href=\"admin/uploads/b/$did.jpg\" target=\"_blank\"><img src=\"admin/imagesizeb.php?did=$did&h=100\" border=\"0\"></a> <a href=\"admin/uploads/c/$did.jpg\" target=\"_blank\"><img src=\"admin/imagesizec.php?did=$did&h=100\" border=\"0\"></a><br>D.O.B. - $dob_month $dob_day<br>$qty $gender $title<br><span class=\"style40\">Price: $$price<br>Ad Expires $avail_month $avail_day<br>$Comment</span><p>Contact Information:</strong><br> <strong>Phone Number:</strong> $p_num <br><strong>Address:</strong> $address<br> <strong>Email:</strong> <a href=mailto:$email>$email</a><br><a href=\"$link\" target=\"_blank\">$link</a>
                       <br>

  <p>";

}
else
{
//use all the same link information but display a default.jpg
}

Tried the if statment redarrow and got the following error "Parse error: syntax error, unexpected T_IF in /home/pets/public_html/search_test.php on line 239".  I searched for a solution, have been searching, found this (I am using a cross section of the code so you can see where I inserted it)

<?"- - - - - </span><p><span class=\"styledogsforsale\">$title for Sale - ID #$did</span><br>".(admin/uploads/c/$did.jpg > "")?'<a href="admin/uploads/c/$did.jpg" target="_blank"><img src="admin/imagesizec.php?did=$did&h=100" border="0"></a>':'no picture to display'."<br>D.O.B. - $dob_month $dob_day<br>$qty $gender $title<br>"?>

But it returns an error saying I'm dividing by "0".  I appreciate all the ideas, cause I am stumped

 

Here is all of my code:

 

 <?php

  // Get the search variable from URL

  $var = @$_GET['q'] ;
  $trimmed = trim($var); //trim whitespace from the stored variable

// rows to return
$limit=10; 

// check for an empty string and display a message.
if ($trimmed == "")
  {
  echo "<p>Please enter a search...</p>";
  exit;
  }

// check for a search parameter
if (!isset($var))
  {
  echo "<p>We dont seem to have a search parameter!</p>";
  exit;
  }

//connect to your database ** EDIT REQUIRED HERE **
mysql_connect("localhost","pets_puppylister","fiveft12"); //(host, username, password)

//specify database ** EDIT REQUIRED HERE **
mysql_select_db("pets_puppylist") or die("Unable to select database"); //select which database we're using


// Build SQL Query  
$query = "select * from dogs where ptype like \"%$trimmed%\" AND pshow = 'y'
  order by did desc"; // EDIT HERE and specify your table and field names for the SQL query

$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);

// If we have no results, offer a google search as an alternative

if ($numrows == 0)
  {
  echo "<h4>Results</h4>";
  echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>";

  }

// next determine if s has been passed to script, if not use 0
  if (empty($s)) {
  $s=0;
  }

// get results
  $query .= " limit $s,$limit";
  $result = mysql_query($query) or die("Couldn't execute query");

// display what the person searched for
echo "<p>You searched for: "" . $var . ""</p>";

// begin to show results set
echo "";
$count = 1 + $s ;

// now you can display the results returned
  while ($row= mysql_fetch_array($result)) {
  $title = $row["ptype"]; 
  $email = $row["email"];
  $did = $row["did"];
  ///////////////////////////
	$gender = $row["gender"];
	$price = number_format($row["price"], 2,'.',',');
	$qty = $row["qty"];
	$dob_month = $row["dob_month"];
	$dob_day = $row["dob_day"];
	$p_pound = $row["p_pound"];
	$p_oz = $row["p_oz"];
	$p_date = $row["p_date"];
	$link = $row["link"];
	$p_num = $row["p_num"];
	$address = $row["address"];
	$comment = $row["comment"];
	$avail_month = $row["avail_month"];
	$avail_day = $row["avail_day"];
	//////////////////////////////

  echo "<span class=\"style52\">- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - </span><p><span class=\"styledogsforsale\">$title for Sale - ID #$did</span><br>".(admin/uploads/c/$did.jpg > "")?'<a href="admin/uploads/c/$did.jpg" target="_blank"><img src="admin/imagesizec.php?did=$did&h=100" border="0"></a>':'no picture to display'."<br>D.O.B. - $dob_month $dob_day<br>$qty $gender $title<br><span class=\"style40\">Price: $$price<br>Ad Expires $avail_month $avail_day<br>$Comment</span><p>Contact Information:</strong><br> <strong>Phone Number:</strong> $p_num <br><strong>Address:</strong> $address<br> <strong>Email:</strong> <a href=mailto:$email>$email</a><br><a href=\"$link\" target=\"_blank\">$link</a>
                        <br>

   <p>";
                     
  $count++ ;
  }

$currPage = (($s/$limit) + 1);

//break before paging
  echo "<br />";

  // next we need to do the links to other results
  if ($s>=1) { // bypass PREV link if s is 0
  $prevs=($s-$limit);
  print " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><< 
  Prev 10</a>&nbsp ";
  }

// calculate number of pages needing links
  $pages=intval($numrows/$limit);

// $pages now contains int of pages needed unless there is a remainder from division

  if ($numrows%$limit) {
  // has remainder so add one page
  $pages++;
  }

// check to see if last page
  if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

  // not last page so give NEXT link
  $news=$s+$limit;

  echo " <a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>";
  }

$a = $s + ($limit) ;
  if ($a > $numrows) { $a = $numrows ; }
  $b = $s + 1 ;
  echo "<p><span class=\"style52\">- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - </span><p>Showing results $b to $a of $numrows</p>";
  
?>

Does that snippet work?  here it is seperate from the other code:

.(admin/uploads/c/$did.jpg > "")?'<a href="admin/uploads/c/$did.jpg" target="_blank"><img src="admin/imagesizec.php?did=$did&h=100" border="0"></a>':'no picture to display'.

is there a way to make it work? 

the way you would do this depends on how your database is structured. i assume that the images you want to be displayed were at one point or another uploaded from a user. maybe this user has an account?

 

either way, upon upload, you should insert into your mysql table the filename. when you use mysql_fetch_array, check to see if there is an image in that column on the row that is being fetched, if there is anything in the field, then write the path to the images, and then use $row['filename'] to use at the end of the image location so it displays the correct image.

You are correct about the images being uploaded, I am not currently storing them in the database.  Hmmm.... I will look into that idea...

 

all you need to do is store the name of the file into a field in the database. so you have something like this:

 

<?php
        $path = "/directory/path/to/images/";

        $sql = "SELECT gender, price, qty, filename FROM your_table";
        $query = mysql_query($sql);

        while($row = mysql_fetch_array($query)){
                echo "Gender: ". $row['gender'] ."<br />\n";
                echo "Price: ". $row['price'] ."<br />\n";
                echo "Quantity: ". $row['qty'] ."<br />\n";
                echo "Image: ";

                if(!$row['filename']){
                        echo "No image available<br />\n";
                }else{
                        echo "<a href=". $path . $row['filename'] ."\">". $row['filename'] ."</a><br />\n";
                }
        }
?>

 

understand?

Thank you for all your help, I got this to work:

if(file_exists('admin/uploads/'.$did.'.jpg')){
echo "<a href=\"admin/uploads/$did.jpg\" target=\"_blank\"><img src=\"admin/imagesize.php?did=$did&h=100\" border=\"0\"></a>";
}else{
echo "";
};

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.