ChayaCooper Posted November 13, 2012 Share Posted November 13, 2012 (edited) 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 November 13, 2012 by ChayaCooper Quote Link to comment https://forums.phpfreaks.com/topic/270612-displaying-an-image-instead-of-value-for-the-results-of-a-mysql_fetch_array-query/ Share on other sites More sharing options...
Barand Posted November 13, 2012 Share Posted November 13, 2012 What does the data like when there is more than one value in the column? Quote Link to comment https://forums.phpfreaks.com/topic/270612-displaying-an-image-instead-of-value-for-the-results-of-a-mysql_fetch_array-query/#findComment-1391925 Share on other sites More sharing options...
ChayaCooper Posted November 13, 2012 Author Share Posted November 13, 2012 The data looks the same as the way it is displayed as text. For this particular column the sample values when displayed as text are: turtleneck, crew, v-neck Quote Link to comment https://forums.phpfreaks.com/topic/270612-displaying-an-image-instead-of-value-for-the-results-of-a-mysql_fetch_array-query/#findComment-1391927 Share on other sites More sharing options...
trq Posted November 13, 2012 Share Posted November 13, 2012 Why would there ever be more than 1 value in a column in the first place? Quote Link to comment https://forums.phpfreaks.com/topic/270612-displaying-an-image-instead-of-value-for-the-results-of-a-mysql_fetch_array-query/#findComment-1391928 Share on other sites More sharing options...
ChayaCooper Posted November 13, 2012 Author Share Posted November 13, 2012 (edited) 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 November 13, 2012 by ChayaCooper Quote Link to comment https://forums.phpfreaks.com/topic/270612-displaying-an-image-instead-of-value-for-the-results-of-a-mysql_fetch_array-query/#findComment-1391931 Share on other sites More sharing options...
Barand Posted November 13, 2012 Share Posted November 13, 2012 (edited) 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 November 13, 2012 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/270612-displaying-an-image-instead-of-value-for-the-results-of-a-mysql_fetch_array-query/#findComment-1391933 Share on other sites More sharing options...
ChayaCooper Posted November 13, 2012 Author Share Posted November 13, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/270612-displaying-an-image-instead-of-value-for-the-results-of-a-mysql_fetch_array-query/#findComment-1391936 Share on other sites More sharing options...
newmarket Posted November 13, 2012 Share Posted November 13, 2012 you are missing " and ; from the end of the echo statement. Quote Link to comment https://forums.phpfreaks.com/topic/270612-displaying-an-image-instead-of-value-for-the-results-of-a-mysql_fetch_array-query/#findComment-1391937 Share on other sites More sharing options...
ChayaCooper Posted November 13, 2012 Author Share Posted November 13, 2012 I'm not quite sure where that should go :-( Would you mind showing me? you are missing " and ; from the end of the echo statement. Quote Link to comment https://forums.phpfreaks.com/topic/270612-displaying-an-image-instead-of-value-for-the-results-of-a-mysql_fetch_array-query/#findComment-1391950 Share on other sites More sharing options...
darkfreaks Posted November 13, 2012 Share Posted November 13, 2012 (edited) <?php echo "<img src='$path.$im'>"; ?> Edited November 13, 2012 by darkfreaks Quote Link to comment https://forums.phpfreaks.com/topic/270612-displaying-an-image-instead-of-value-for-the-results-of-a-mysql_fetch_array-query/#findComment-1391951 Share on other sites More sharing options...
ChayaCooper Posted November 13, 2012 Author Share Posted November 13, 2012 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'>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/270612-displaying-an-image-instead-of-value-for-the-results-of-a-mysql_fetch_array-query/#findComment-1391953 Share on other sites More sharing options...
darkfreaks Posted November 13, 2012 Share Posted November 13, 2012 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'>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/270612-displaying-an-image-instead-of-value-for-the-results-of-a-mysql_fetch_array-query/#findComment-1391955 Share on other sites More sharing options...
ChayaCooper Posted November 13, 2012 Author Share Posted November 13, 2012 darkfreaks -That option also prevents the page from loading :-( Quote Link to comment https://forums.phpfreaks.com/topic/270612-displaying-an-image-instead-of-value-for-the-results-of-a-mysql_fetch_array-query/#findComment-1391956 Share on other sites More sharing options...
Pikachu2000 Posted November 13, 2012 Share Posted November 13, 2012 You shouldn't be developing with error reporting off. Quote Link to comment https://forums.phpfreaks.com/topic/270612-displaying-an-image-instead-of-value-for-the-results-of-a-mysql_fetch_array-query/#findComment-1391958 Share on other sites More sharing options...
ChayaCooper Posted December 10, 2012 Author Share Posted December 10, 2012 (edited) 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 December 10, 2012 by ChayaCooper Quote Link to comment https://forums.phpfreaks.com/topic/270612-displaying-an-image-instead-of-value-for-the-results-of-a-mysql_fetch_array-query/#findComment-1398617 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.