Jump to content

Displaying An Image Instead Of Value For The Results Of A Mysql_Fetch_Array Query


ChayaCooper

Recommended Posts

I would like to display some of the results of a mysql_fetch_array query as images instead of text, but I’m having trouble getting this to work when there is more than one value in the column (even though it is array).

 

This displays the value as text (regardless of the number of values in the column)

<?php echo $row['silhouettes_like']; ?>

 

This replaces the text with an image, but it stops displaying images if there is more than one value in the column.

<?php echo "<img src='files/" . $row['silhouettes_like']. ".png'>"; ?>

 

The images names match the values (i.e. turtleneck)

 

The query I’m running is:

<?php
$result = mysql_query("SELECT * FROM style WHERE style.user_id=$user_id ") or die(mysql_error());
$row = mysql_fetch_array($result );	
?>

Edited by ChayaCooper
Link to comment
Share on other sites

Because the column is filled with values from checkboxes or multi-selects, and the user typically selects more than one answer.

 

I don't have any trouble printing several values using mysql_fetch_array as long as I do so as text, but as soon as I try replacing the text with images I run into a problem when there more than one value.

 

I'm not sure if this is helps, but the code that I use to update this array is:

$setlist='';
foreach ($_POST as $key=>$value){
if ($key=='silhouettes_like'){
 serialize($value);
 $value= implode(',',$value);
 }

$setlist.=$key .'=\''.$value.'\',';
}
$setlist=substr($setlist, 0, -1);

$result = mysql_query('UPDATE style SET '.$setlist.' WHERE user_id='.$user_id);
if (mysql_affected_rows()==0) {
$result = mysql_query('INSERT INTO style ('.$fieldlist.') VALUES ('.$vallist.')');
}

Edited by ChayaCooper
Link to comment
Share on other sites

Briefly

 

$images = explode(',', $row['silhouettes_like']);
foreach ($images as $im) {
echo "<img src='$path.$im'>
}

 

However the way you are doing it is nor exactly optimal. You would be better having a separate table table to store the images, one per row.

Edited by Barand
Link to comment
Share on other sites

I must be missing something, because when I added it to my code it stopped working. Is this what you meant?

<?php    
$result = mysql_query("SELECT * FROM style WHERE style.user_id=$user_id ") or die(mysql_error());
$row = mysql_fetch_array($result );

$images = explode(',', $row['silhouettes_like']);
foreach ($images as $im) {
    echo "<img src='$path.$im'>
}
?>

 

Since these are images which I supply and there are a finite amount of images, I'm curious why it would be better to have a separate table for the images than to keep them in a folder on my server?

 

Also, just to clarify - each row represents a customer, there are apx. 100 columns in each row, and many of the columns represent arrays where the user can select multiple answers (this particular element has 50+ choices), so I don't believe that they can be on separate rows.

Link to comment
Share on other sites

The page still doesn't load :-( Should I be defining $path somewhere?

 

This is my current code (with the corrections suggested)

 

<?php   
$result = mysql_query("SELECT * FROM style WHERE style.user_id=$user_id ") or die(mysql_error());
$row = mysql_fetch_array($result );

$images = explode(',', $row['silhouettes_like']);
foreach ($images as $im) {
		    echo "<img src='$path.$im'> and ;
}
?>

<?php echo "<img src='files/" . $row['silhouettes_like']. ".png'>"; ?>

Link to comment
Share on other sites

try this...

 

 


<?php   
$user_id= (int)$_POST['user_id'];
$result = mysql_query("SELECT * FROM style WHERE style.user_id='".$user_id."'");
if($result===FALSE) { echo mysql_error();}
$row = mysql_fetch_array($result);

$images = explode(',', $row['silhouettes_like']);
foreach ($images as $im) {
						    echo "<img src='"$path.$im"'>";
}
?>

<?php echo "<img src='files/" . $row['silhouettes_like']. ".png'>"; ?>

Link to comment
Share on other sites

  • 4 weeks later...

I ended up creating an array in my php file with the images

 


$Silhouettes = array(
'Crew_Neck' =>'files/style-Crew_Neck.png',
'Scoop_Neck' =>'files/style-Scoop_Neck.png',
'V-Neck' =>'files/style-V-Neck.png'
...
);

 

And the array is referenced in these 2 places:

 

foreach($Silhouettes as $cname=>$ccode)

style="background-image: url('files/style-<?php echo $cname; ?>.png'); background-repeat: no-repeat; border: none;"

Edited by ChayaCooper
Link to comment
Share on other sites

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.