Jump to content

Pulling Image URL From DB


MartinH92

Recommended Posts

Hello!
 
I am currently trying to pull the URL of a image from my database to display when a search is performed.
 
I have serialized data in the 'property_gallery' column currently. I have used the below to display the search results:
 
       <?php
    
        $query = $_GET['query']; 
         
        $min_length = 3;
         
        if(strlen($query) >= $min_length){ 
             
            $query = htmlspecialchars($query); 
             
            $query = mysql_real_escape_string($query);
             
            $raw_results = mysql_query("SELECT * FROM properties
                WHERE (`property_city` LIKE '%".$query."%') OR (`property_title` LIKE '%".$query."%')") or die(mysql_error());
             
            if(mysql_num_rows($raw_results) > 0){ 
                 
                while($results = mysql_fetch_array($raw_results)){
                 
                    echo "<p><h3>".$results['property_title']."</h3>".$results['property_city']."<br>".$results['property_address2']."</br>".$results['property_postcode1']."".$results['property_postcode2']."</br>£".$results['property_price']."</br></br>
                        
                   
                    </p>";
                        
                         $propertyimage = $results['property_gallery'];
                         $propertyimage = unserialize($propertyimage);
                      echo "<p>".$propertyimage['Photo 1'][0]."</p>";
    
    
                }
                
            }
            else{ 
                echo "No results";
            }
             
        }
        else{ 
            echo "Minimum length is ".$min_length;
        }
    ?>

Currently all data is displaying apart from the image url.

 

Thanks,

Martin.

 
Link to comment
Share on other sites

Not sure what you are expecting.

Here is a cleaned up version of your code:

  echo "<p>
	<h3>". $results['property_title']. "</h3>" . 
	$results['property_city'] . "<br>" . 
	$results['property_address2'] . "</br>" . 
	$results['property_postcode1'] . "" . 
	$results['property_postcode2'] . "</br>
	£" . $results['property_price'] . "</br></br>
	</p>";
    $propertyimage = $results['property_gallery'];
    $propertyimage = unserialize($propertyimage);
echo "<p>" . $propertyimage['Photo 1'][0] . "</p>";
This is showing me a block of code in one paragraph tag followed by another paragraph tag with (apparently) some image/url value all by itself.

What do you mean by "apart from image url"? It's not like you are doing anything that would attempt to organize these two distinct parts.

 

PS - why do you have to do the serialize on this 'url'?

Link to comment
Share on other sites

Hey,

Thanks for your reply.

 

The separation into 2 paragraphs was just to show it more clearly. I am trying to get the image URL from the column named 'property_gallery' in my database. The entry is currently serialized, i am attempting to unserialize and display the image link, it is currently in the database like this:

 

a:15:{s:34:"eefc9e10ebdc4a2333b42b2dbb8f27b6_0";a:2:{s:4:"name";s:38:"eefc9e10ebdc4a2333b42b2dbb8f27b6_0.jpg";s:7:"caption";s:8:"Photo 21";}s:34:"eefc9e10ebdc4a2333b42b2dbb8f27b6_1";a:2:{s:4:"name";s:38:"eefc9e10ebdc4a2333b42b2dbb8f27b6_1.jpg";s:7:"caption";s:8:"Photo 14";}s:34:"eefc9e10ebdc4a2333b42b2dbb8f27b6_2";a:2:{s:4:"name";s:38:"eefc9e10ebdc4a2333b42b2dbb8f27b6_2.jpg";s:7:"caption";s:8:"Photo 17";}s:34:"eefc9e10ebdc4a2333b42b2dbb8f27b6_3";a:2:{s:4:"name";s:38:"eefc9e10ebdc4a2333b42b2dbb8f27b6_3.jpg";s:7:"caption";s:8:"Photo 18";}s:34:"eefc9e10ebdc4a2333b42b2dbb8f27b6_4";a:2:{s:4:"name";s:38:"eefc9e10ebdc4a2333b42b2dbb8f27b6_4.jpg";s:7:"caption";s:8:"Photo 20";}s:34:"eefc9e10ebdc4a2333b42b2dbb8f27b6_5";a:2:{s:4:"name";s:38:"eefc9e10ebdc4a2333b42b2dbb8f27b6_5.jpg";s:7:"caption";s:8:"Photo 16";}s:34:"eefc9e10ebdc4a2333b42b2dbb8f27b6_6";a:2:{s:4:"name";s:38:"eefc9e10ebdc4a2333b42b2dbb8f27b6_6.jpg";s:7:"caption";s:7:"Photo 8";}s:34:"eefc9e10ebdc4a2333b42b2dbb8f27b6_7";a:2:{s:4:"name";s:38:"eefc9e10ebdc4a2333b42b2dbb8f27b6_7.jpg";s:7:"caption";s:7:"Photo 7";}s:34:"eefc9e10ebdc4a2333b42b2dbb8f27b6_8";a:2:{s:4:"name";s:38:"eefc9e10ebdc4a2333b42b2dbb8f27b6_8.jpg";s:7:"caption";s:7:"Photo 9";}s:34:"eefc9e10ebdc4a2333b42b2dbb8f27b6_9";a:2:{s:4:"name";s:38:"eefc9e10ebdc4a2333b42b2dbb8f27b6_9.jpg";s:7:"caption";s:8:"Photo 10";}s:35:"eefc9e10ebdc4a2333b42b2dbb8f27b6_10";a:2:{s:4:"name";s:39:"eefc9e10ebdc4a2333b42b2dbb8f27b6_10.jpg";s:7:"caption";s:7:"Photo 3";}s:35:"eefc9e10ebdc4a2333b42b2dbb8f27b6_11";a:2:{s:4:"name";s:39:"eefc9e10ebdc4a2333b42b2dbb8f27b6_11.jpg";s:7:"caption";s:8:"Photo 11";}s:35:"eefc9e10ebdc4a2333b42b2dbb8f27b6_12";a:2:{s:4:"name";s:39:"eefc9e10ebdc4a2333b42b2dbb8f27b6_12.jpg";s:7:"caption";s:7:"Photo 1";}s:35:"eefc9e10ebdc4a2333b42b2dbb8f27b6_13";a:2:{s:4:"name";s:39:"eefc9e10ebdc4a2333b42b2dbb8f27b6_13.jpg";s:7:"caption";s:7:"Photo 2";}s:35:"eefc9e10ebdc4a2333b42b2dbb8f27b6_14";a:2:{s:4:"name";s:39:"eefc9e10ebdc4a2333b42b2dbb8f27b6_14.jpg";s:7:"caption";s:7:"Photo 4";}}

Link to comment
Share on other sites

You have designed (if you can call it that) a poor relational db. You have used 'serialize' to combine a set of values into one column which is not proper relational database methodology. Rather than combine ANY set of values as one field for storing into a table, one should create a table (with a foreign key) that holds multiple records for the primary key that you then write a query that will collect all of the material needed for your reporting.

 

Table 1: key, attributes

Table 2: key (matching the key of tbl 1 or a FK in table 1), url, (optional:url sequence)

 

Any query result will give you all of the data you want from table 1 with a url. There will be multiple records each with a different url. YOu simply loop thru them checking if the PK has changed or not, and outputting the data as you want it to look.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.