Jump to content

Getting value from database and pass it to php page


php_beginner_83

Recommended Posts

Hi Everyone

 

I'm trying to loop through an array of images that I create from getting the image paths from a database and am having problems getting it to work...any ideas?

 

Thanks

 

$result = mysql_query("SELECT * FROM pictures
INNER JOIN pics_in_albums
ON pictures.ID = pics_in_albums.PicID
INNER JOIN albums
ON pics_in_albums.AlbumID = albums.ID
WHERE albums.ID = 1") or die(mysql_error());
  
$paths = array();
  
$counter = 0;
//fetch tha data from the database
while ($row = mysql_fetch_assoc($result))
{
    
   $currentImage = "images//" . substr(strrchr($row['Path'],92),1);
  
   $counter++;
    
   if ($counter % 3 == 0)
    {
         echo '<a href=#><img src="thumb.php?image=$currentImage alt="" /></a><br/>';
   }
   else
   {
        echo '<a href=#><img src="thumb.php?image=$currentImage" alt="" /></a>';
    
   }
    
}
?>

 

However, when I explicitly type in the name of the image it works fine and creates the thumbnail..

<img src="thumb.php?image=beach.jpg alt="" />

 

Thanks.

Try This.

 

<?php

$result = mysql_query("SELECT * FROM pictures
INNER JOIN pics_in_albums
ON pictures.ID = pics_in_albums.PicID
INNER JOIN albums
ON pics_in_albums.AlbumID = albums.ID
WHERE albums.ID = 1") or die(mysql_error());

$paths = array();

$counter = 0;
//fetch tha data from the database
while ($row = mysql_fetch_assoc($result))
{
   
   $currentImage = "images//" . substr(strrchr($row['Path'],92),1);

   $counter++;
   
   if ($counter % 3 == 0)
    {
         echo "<a href=#><img src='thumb.php?image=$currentImage' alt='' /></a><br/>";
   }
   else
   {
        echo "<a href=#><img src='thumb.php?image=$currentImage' alt='' /></a>";
   
   }
   
}
?>

Just incase anybody else is wondering what herghost was suggesting (took me awhile to spot the difference, maybe I'm just slow), they have basically switched the single and double quotes over on the rows of code that echo out anchor links.

 

<?php
$var = "Test";

echo '$var';
// will give an output of $var

echo "$var";
// will give an output of Test
?>

 

Also in the OP's code, the src attribute of the first anchor tag didn't have a closing quotation anyway.

 

I should also point out that the href attribute doesn't have quote's around it in either post, which I assume it should have.

 

<?php
if ($counter % 3 == 0) {
  echo '<a href="#"><img src="thumb.php?image="'. $currentImage .'" alt='' /></a><br/>';
} else {
  echo '<a href="#"><img src="thumb.php?image="'. $currentImage .'" alt='' /></a>';
}
?>

 

Note: I changed the order back, for no other purpose than thats my prefered method, the way herghost suggested should have been fine.

 

If this doesn't work, I suggest you look at the outputted HTML and check if the value you have in the src attribute looks correct and is in the form of (what I'm presuming was the correct format) of "thumb.php?image=beach.jpg".

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.