0

I have some images in a folder. I've stored names of the pictures in a table in a database. Now I want to display the pictures by making a select from the table like this:

$a=$_GET['nume'];


$c= "select * from angajati where afisare_angajat='".$a."'" ;
    $b = mysql_query($c);

    while($d = mysql_fetch_array($b)){
        echo "<img src='/wp-content/themes/veles/images/angajati/'.$d['afisare_angajat'].'' />";
    }

But there seems to be a problem with this: '.$d['afisare_angajat'].' ..if I put the name of the picture it shows but if I left it like this nothing shows up..

hakre
  • 193,403
  • 52
  • 435
  • 836
Bibu
  • 201
  • 3
  • 10
  • 25
  • Where exactly is the problem? You seem to know exactly what to do... where are you having problems with this? – Lix Sep 25 '12 at 08:28
  • you have to fetch_assoc_array, that way you will get the result $d to be indexed by column names, and $d['column_name'] should return the value associated with that key/index – Loopo Sep 25 '12 at 10:28

3 Answers3

0

You should do something like this:

<?php
// I do not know what database system and API you are using
$pic_name = QueryPictureNameFromDB();

// Output html code to properly display the image
?>
<img src="<?php echo $pic_name; ?>" />

The resulting output (in html) would look like:

<img src="myPic1234.png" />

You should understand that PHP may output any form of text and that text may be interpreted differently by the client. In your case, you are creating html using php scripts. That html "text" is what the client interprets and sees. And so, in your php script, you must output html to define what the user will see (images for this case).

Your way of querying the image name is also not appropriate. In your query, you already know what your image name is. You must somewhat have something like this:

SELECT * FROM table WHERE id=required_img_id
Mark Garcia
  • 17,424
  • 4
  • 58
  • 94
0

Here is an example of how you could accomplish this. Get all the results from the database, turn them into an associative array, then loop through and out put the names.

<?php
    $getImagesSql = 'select * from table where name_image_from_table = name_image_from_folder';
    $getImagesQuery = mysql_query($getImagesSql);

    while($getImagesAssoc = mysql_fetch_assoc($getImagesQuery)){
        echo '<img src="/directory/goes/here/'.$getImagesAssoc['imageNameCol'].'" />';
    }
CharliePrynn
  • 3,034
  • 5
  • 40
  • 68
  • it doesn't bring me the associated images...if i put instead of .$getImagesAssoc['imageNameCol']. the name of the picture then it brings up but if I put this it doesn't – Bibu Sep 25 '12 at 09:07
  • Change imageNameCol to the column that your image names are stored under. imageNameCol is an example, if could be anything. – CharliePrynn Sep 25 '12 at 09:20
  • Does any html come out on the frontend? Have you tried inspecting the element to see what the name of the file is? Also, have you run the query in phpmyadmin to see if you get any results returned? – CharliePrynn Sep 25 '12 at 09:42
0

Use a function like scandir to get the filenames out of the folder, you may want to filter them to avoid looking for other filetypes in your database. Something like the following.

/* open database connection */
$link = mysqli_connect("localhost", "my_user", "my_password", "images");

/* check connection */
if (mysqli_connect_errno()) {
   printf("Connect failed: %s\n", mysqli_connect_error());
   exit();
}

/* set the directory for your image files */
$dir    = '/images';
/* get a list of all files in the directory */
$filenames = scandir($dir);

/* go through all filenames in the list */
foreach ($filename in $filenames){
   get_image_name($filename)
};   

/* function that prints the associated data for the given filename */

function get_image_name($filename){
   /* construct query for filename */
   $sql = "SELECT * from table WHERE name_image_from_table = '".$filename."'";
   /* run the query */
   if ($result = mysqli_query($link,$sql)){
       /* if the query went OK check if there was one result */
       if (mysqli_num_rows($result) == 1){
           /* put the result in an associative array with the column names as keys */
           $row = mysqli_fetch_assoc($result)
           /* output the html for displaying the image and its name */
           echo "<img src='".$dir."/".$filename."'/>".$row['ImageNameField']."<br/>";
       } 
   }
}
Loopo
  • 2,204
  • 2
  • 28
  • 45