Pair Array Results to Replace Images

They have: 3 posts

Joined: Dec 2010

hi, i am trying to visualize a table by replacing the fixed values with images.
For example, i have a visitor statistics table on mysql. Values on the config_os table are always in three letters. WI7 for Windows7, MAC for Macintosh. I hope I can tell this correctly.
I have a display page which retrieves the values from database and displays them website statistics.
But in order to make it more cool, i am telling to PHP that When you echo to divtable WI7, replace them with specified Windows7 icon image which has identical name as WI7.gif. Here is the code:

        include("conn.php");
       $sql="SELECT *, INET_NTOA(location_ip) AS location_ip FROM xx_log_visit AS location_ip ORDER BY idvisit DESC LIMIT 10";
// I am selecting all so you can't see config_os but it is there . I cropped unnecessary code for keeping it easy to read.

       $result=mysql_query($sql);
        $numofrows = @mysql_num_rows($result);
  
      
       while($row = mysql_fetch_array($result))
       {

$system = $row['config_os'];
                $SysName = array ("AND", "IPH", "LIN", "MAC", "UNK", "W2K", "W95", "W98", "WI7", "WME", "WNT", "WOS", "WS3", "WVI", "WXP");
                $SysRename = array ("<img src='images/os/AND.gif'>","<img src='images/os/IPH.gif'>","<img src='images/os/LIN.gif'>","<img src='images/os/MAC.gif'>","<img src='images/os/UNK.gif'>","<img src='images/os/W2K.gif'>","<img src='images/os/W95.gif'>","<img src='images/os/W98.gif'>","<img src='images/os/wi7.gif'>","<img src='images/os/WME.gif'>","<img src='images/os/WNT.gif'>","<img src='images/os/WOS.gif'>","<img src='images/os/WS3.gif'>","<img src='images/os/WVI.gif'>","<img src='images/os/WXP.gif'>");
                $showsystem= str_replace($SysName, $SysRename, $system);

What I do here is simply use strreplace function.

$showsystem = str_replace($SysName, $SysRename, $system);

and in the display table, continue from code:

            echo ("
                <div class='sub15'>$showsystem</div>
            " );      
   }

So for example instead of writing WI7 as visitors os, it shows WI7.gif, cool hmm Smiling

How can i automate this instead of writing each of them separately?

Thanks for any help..

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

Hi erturanya, welcome to TWF.

What I would do is output the HTML and then insert the MySQL field as the image name. So when you are ready to output the image, do something like this:

<img src="images/os/<?=$row['config_os']?>.gif">

If you don't have short tags enabled, something like this will work as well:

<img src="images/os/<?php echo $row['config_os']; ?>.gif">

So as long as you have an image in that directory with the file name matching all the database fields (case sensitive), it will work w/o a problem.

If you want to catch any possible file not found errors, you can do something like this:

<?php
if (file_exists('images/os/' . $row['config_os'] . '.gif'))
{
    echo
'<img src="images/os/' . $row['config_os'] . '.gif">';
else
{
    echo
'<img src="images/os/na.gif">';
}
?>

The na.gif file can be some sort of generic icon instead of it being a broken/404 image link.

Doing it this way will allow you to add operating systems options without changing any code in this area. All you would have to do would be to create it as an option in your form and upload the image icon to the icon folder.

They have: 3 posts

Joined: Dec 2010

Thanks for welcoming, pr0gr4mm3r.

Exteremely helpful. Thank you so much.

So i applied as follows:

echo ("
  <div class='sub15'><img src='images/os/$system.gif' /></div>
  /* Where $system = $row['config_os']; */

it worked nicely. Thanks for help.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.