Page 1 of 1

Change returned query data to an image

Posted: Tue Aug 05, 2008 9:30 pm
by ardan16
Hi all.
I have a query return its results into a table eg

Code: Select all

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        echo '<tr><td align="center">' . $row['name'] . '</td><td align="left">' . $row['com'] . '</td><td align="center">' . $row['date']. '</td><td align="center">' . $row['face'] . '</td></tr>
        ';
Can anyone please tell me a way to substitute

Code: Select all

' . $row['face'] . '
if value returned is pos an image of happy face
or neg sad face if I have the images say includes/happy.jpg includes/sad.jpg

the returned results are correct so far as in pos or neg text

If not possible can the image be sent to the database originally.

thanks lost and need guidance.

Re: Change returned query data to an image

Posted: Wed Aug 06, 2008 1:42 am
by RobertGonzalez

Code: Select all

<?php
//Default value of this var for now
$img = null;
 
// Check the value of the field you are checking
if ($row['face'] == 'pos') {
  $img = 'smile';
} elseif ($row['face'] == 'neg') {
  $img = 'sad';
}
 
if ($img) {
  $fullimg = 'pathtoimage' . $img . '.png'; // You name the image you are using here, or above, whichever is easier
}
?>
This is probably more complex than you want, but really all you need to do is check the value of the row you are looking for and if it is one thing, do one thing with it and if it is another then do another with it.

Re: Change returned query data to an image

Posted: Wed Aug 06, 2008 2:54 am
by ardan16
Thanks Everah,

I tried it several ways and could not get it to work,
Not saying it wont work just I couldn't get it to work.

Code: Select all

if ($row['face'] == 'pos') {
$img = 'include/smile.jpg';
} elseif ($row['face'] == 'neg') {
  $img = 'include/sad.jpg';
}
 
    echo "<p>You are viewing $num user comments at a time.</p>\n";
 
    // Table header.
    echo '<table align="center" border="1" cellpadding="2" id="view_com">
    <tr><td align="left"><b>Name of Bank</b></td><td align="left" ><b>Comments</b></td><td align="left" ><b>Date</b></td><td align="left" ><b>Pos/Neg</b></td></tr>
';
// another oh oh MYSQL_ASSOC and the associated array can also fetch row or number num
  // Fetch and print all the records.
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        echo '<tr><td align="center">' . $row['name'] . '</td><td align="left">' . $row['com'] . '</td><td align="center">' . $row['date']. '</td><td align="center">'.$row['face'].'</td></tr>
        ';
    }
That is what I ended up with however don't know if I made it worse.
Thanks for your help.

Re: Change returned query data to an image

Posted: Wed Aug 06, 2008 4:05 am
by onion2k
Everah's code isn't something you can just drop into your script and it'll magically work. You need to think about what Everah's code is doing and apply the same principle to your own code. He showed you how to do it. You're the one who actually has to write the final code.

Re: Change returned query data to an image

Posted: Wed Aug 06, 2008 9:22 am
by ardan16
Thanks onion2k,

I would not have guessed that - if I knew what I was doing I wouldn't have asked for help.

Re: Change returned query data to an image

Posted: Wed Aug 06, 2008 10:09 am
by onion2k
ardan16 wrote:Thanks onion2k,

I would not have guessed that - if I knew what I was doing I wouldn't have asked for help.
You asked for help and you got help. Now you're asking for people to do the work for you. That's not what this forum is about. You need to put a little more effort in.

What you're trying to do is replace the text you have now, $row['face'], with an image. To do that you need to add the code for an HTML image tag with a source of either the happy face or the sad face. You need to figure out how to integrate Everah's code into your script so that instead of $row['face'] it outputs the $fullimg value.

Re: Change returned query data to an image

Posted: Wed Aug 06, 2008 10:23 am
by ghurtado
Ardan, you are missing something fundamental.

You are trying to display an image, but you forgot (or just didnt know) to use the "img" HTML tag.

Developers on these forums are more than happy to help you go 90% of the way - so long as you show you are capable (or at least willing) to go the other 10%.

Re: Change returned query data to an image

Posted: Wed Aug 06, 2008 10:46 am
by RobertGonzalez
Lets make this a little easier for you...

Code: Select all

<?php
// Loop the result to build our output
while ($row = mysql_fetch_array($result)) {
    // Starting with an image for the face field
    $img = null;
 
    // If the face is 'pos' show a smile, if it is 'neg' show a sad
    if ($row['face'] == 'pos') {
        $img = '<img src="images/smile.jpg" />';
    } elseif ($row['face'] == 'neg') {
        $img = '<img src="images/sad.jpg" />';
    }
 
    // Now build the table row using the face image instead of the face field value
    echo '<tr><td align="center">' . $row['name'] . '</td><td align="left">' . $row['com'] . '</td><td align="center">' . $row['date']. '</td><td align="center">' . $img  .'</td></tr>';
}
?>
The logic is very simple in this case: If the 'face' field is 'pos' show the HTML code for the smile image. If the 'face' is 'neg' show the HTML code for the sad image. If the 'face' field is neither of these show nothing. That is what I was getting at in the first snippet I offered.

Re: Change returned query data to an image

Posted: Wed Aug 06, 2008 12:13 pm
by ardan16
Thank You All,
Your help was greatly appreciated, with the limited knowledge I have of php I spent most of yesterday trying to resolve this problem. And spent hours looking in books and on the web for a solution. Posting the question on the forum was not my first thought.
The code I was originally given sent me looking back through the books I have to find what was missing. Yes as pointed out I had missed something fundamental and it was through lack of knowledge; not because I just expect to come to the forum and get what I want.
With the knowledge you have yes this may have seemed very simple however I had tried every possible combination that I could think of to get this to work.
Once again thank you all for your help.

Re: Change returned query data to an image

Posted: Wed Aug 06, 2008 12:18 pm
by RobertGonzalez
Try not to let us scare you away ;) We are a rather friendly bunch. We just get a lot of people coming here asking us to write their code for them. We are not about that. We are about helping people become better at coding PHP. We will always offer to help you. We will almost never do anything for you.

Make sure to come back.