Отображать определенное изображение в зависимости от конкретного текста в таблице MySQL с помощью PHP

В моей базе данных MySQL у меня есть таблица с game с колонками: sport_cat , timelive , match_title , selection ,

Я точно этот оператор SELECT запроса MySQL для отображения их в виде сетки на сайте:

 <? $qry = " SELECT sport_cat,match_title,selection, CASE WHEN game.result LIKE '' THEN 'PENDING' WHEN game.result LIKE game.selection THEN 'WON' WHEN game.result NOT LIKE game.selection THEN 'LOST' END AS result FROM game "; $searchText = ""; if($_REQUEST['search_text']!=""){ $searchText = mysql_real_escape_string($_REQUEST['search_text']); $qry .=" WHERE sport_cat.timelive LIKE '%$searchText%' " . " OR game.timelive LIKE '%$searchText%' " . " OR game.match_title LIKE '%$searchText%' "; } $qry .= " ORDER BY timelive DESC"; $obj = new pagination_class($qry,$starting,$recpage); $result = $obj->result; ?> 

В первом столбце sport_cat есть несколько возможных текстовых входов: football , tennis , basketball .

HTML моего файла, где эта часть выглядит следующим образом:

 <table> <?if(mysql_num_rows($result)!=0){ $counter = $starting + 1; while($data = mysql_fetch_array($result)) {?> <tr> <td align="center"><? echo $data['sport_cat']; ?></td> <td align="center"><? echo $data['timelive']; ?></td> <td align="center"><? echo $data['match_title']; ?></td> <td align="center"><? echo $data['selection']; ?></td> </tr> <? $counter ++; } ?> </table> 

То, что я пытаюсь сделать, – отобразить конкретную картинку в этой ячейке в соответствии с этим входом в ячейку. Так что если football хочет отображать football.png tennis.png Если tennis хочет показать tennis.png или если basketball хочет отобразить basketball.png . Кроме того, если есть, возможно, какой-то неизвестный ввод, такой как cricket и у него нет изображения, указанного для этого слова, хотелось бы отобразить unknown.png . Изображения хранятся под images/icons на моем сервере.

Теперь я не уверен, как реализовать этот код, связанный с изображением проблемы, в этом уже существующем. Благодарю.

ДОБАВИТЬ полный примерный код: 03.11.2011 .:

 <?php error_reporting(E_ALL^E_NOTICE); include('pagination_class.php'); mysql_connect('xxx.xxx.xxx.xx', 'xxxxxx', 'xxxxxxxx') or die(mysql_error()); mysql_select_db('xxxxxxx'); ?> <script language="JavaScript" src="pagination.js"></script> <link rel="stylesheet" type="text/css" href="style.css" /> <? $qry = " SELECT sport_cat,bidprice,timelive,match_title,selection,winnings_with_startamount,odds,odds*10 AS gainedodds,username, CASE WHEN game.result LIKE '' THEN 'pending' WHEN game.result LIKE game.selection THEN 'WON' WHEN game.result NOT LIKE game.selection THEN 'LOST' END AS result FROM game "; $searchText = ""; if($_REQUEST['search_text']!=""){ $searchText = mysql_real_escape_string($_REQUEST['search_text']); $qry .=" WHERE game.sport_cat LIKE '%$searchText%' " . " OR game.timelive LIKE '%$searchText%' " . " OR game.match_title LIKE '%$searchText%' " . " OR game.selection LIKE '%$searchText%' " . " OR game.username LIKE '%$searchText%'"; } $qry .= " ORDER BY timelive DESC"; //for pagination $starting=0; $recpage = 10;//number of records per page $obj = new pagination_class($qry,$starting,$recpage); $result = $obj->result; function getStyleColorForStatus($status) { if ($status == 'WON') { return '#99ff99'; } else if ($status == 'LOST') { return '#ff9999'; } else if ($status == 'pending') { return '#e5e5e5'; } return ''; } ?> <form name="form1" action="testpage.php" method="POST" style="background-color:#f9f9f9; -webkit-box-shadow: 0 1px 10px rgba(0,0,0,.1); -moz-box-shadow: 0 1px 10px rgba(0,0,0,.1); box-shadow: 0 1px 10px rgba(0,0,0,.1);"> <table border="0" align="center" width="1000px" padding-left="0px" style="background:#F9F9F9;"> <TD colspan="0" style="padding-left:0px; padding-bottom:5px; padding-top:10px; text-align:center;"> Search: <input type="text" name="search_text" value="<?php echo $searchText; ?>"> <input type="submit" value="Search"> </TD> <tr><TD colspan="0"> <div id="page_contents"> <table width="968px" cellspacing="0" cellpadding="5" align="center" frame="box" rules="none" style="padding-bottom:20px; margin-bottom:8px;margin-top:3px; -webkit-box-shadow: 0 1px 10px rgba(0,0,0,.1); -moz-box-shadow: 0 1px 10px rgba(0,0,0,.1); box-shadow: 0 1px 10px rgba(0,0,0,.1); border: 1px solid #cccccc;"> <tr style="height: 50px;"> <td width="10%" align="center" td class="winheader"><div class="glowtext">Event Category</div></td></td> <td width="5%" align="center" td class="winheader"><div class="glowtext">Bid Cost</div></td></td> <td width="10%" align="center" td class="winheader"><div class="glowtext">Event Start Time</div></td></td> <td width="35%" align="center" td class="winheader"><div class="glowtext">Market/Event</div></td></td> <td width="10%" align="center" td class="winheader"><div class="glowtext">Selection</div></td></td> <td width="10%" align="center" td class="winheader"><div class="glowtext">Winnings</div></td></td> <td width="5%" align="center" td class="winheader"><div class="glowtext">Odds</div></td></td> <td width="5%" align="center" td class="winheader"><div class="glowtext">Gained Odds</div></td></td> <td width="10%" align="center" td class="winheader"><div class="glowtext">Winning Bidder</div></td></td> <td width="10%" align="center" td class="winheader"><div class="glowtext">Final Result</div></td></td> </tr> <? if(mysql_num_rows($result)!=0){ $counter = $starting + 1; $pathImg = "/images/icons/"; while($data = mysql_fetch_array($result)) { //calculate url image $pathFile = $pathImg . $data['sport_cat'] . ".png"; if (!file_exists($pathFile)) { $pathFile = $pathImg . "unknown.png"; } ?> <tr class="initial" onMouseOver="this.className='highlight'" onMouseOut="this.className='normal'"> <td align="center"><img src="<?=$pathFile?>" alt="<?=$data['sport_cat']?>"></TD> <td align="center"><? echo $data['bidprice']; ?></TD> <td align="center"><? echo $data['timelive']; ?></TD> <td align="left"><? echo $data['match_title']; ?></TD> <td align="left"><? echo $data['selection']; ?></TD> <td align="center"><font color="green">&euro; <? echo $data['winnings_with_startamount']; ?></TD> <td align="center"><? echo $data['odds']; ?></TD> <td align="center"><? echo $data['gainedodds']; ?></TD> <td align="center"><? echo $data['username']; ?></TD> <td align="center" style="background-color:<?php echo getStyleColorForStatus($data['result']); ?>"><? echo $data['result']; ?></td> </tr> <? $counter ++; } } ?> <tr><TD align="center" colspan="10" style="padding-bottom:1px; padding-top:10px; color:#333;"><? echo $obj->anchors; ?></TD></tr> <tr><TD align="center" colspan="10" style="padding-bottom:10px; padding-top:5px; color:#333;"><? echo $obj->total; ?></TD></tr> <?}else{?> <tr><TD align="center" colspan="10" style="padding-bottom:10px padding-top:10px; color:red;">No Data Found</TD></tr> <?}?> </TD></tr> </table> </div> </tr> </TD> </form> </table> в <?php error_reporting(E_ALL^E_NOTICE); include('pagination_class.php'); mysql_connect('xxx.xxx.xxx.xx', 'xxxxxx', 'xxxxxxxx') or die(mysql_error()); mysql_select_db('xxxxxxx'); ?> <script language="JavaScript" src="pagination.js"></script> <link rel="stylesheet" type="text/css" href="style.css" /> <? $qry = " SELECT sport_cat,bidprice,timelive,match_title,selection,winnings_with_startamount,odds,odds*10 AS gainedodds,username, CASE WHEN game.result LIKE '' THEN 'pending' WHEN game.result LIKE game.selection THEN 'WON' WHEN game.result NOT LIKE game.selection THEN 'LOST' END AS result FROM game "; $searchText = ""; if($_REQUEST['search_text']!=""){ $searchText = mysql_real_escape_string($_REQUEST['search_text']); $qry .=" WHERE game.sport_cat LIKE '%$searchText%' " . " OR game.timelive LIKE '%$searchText%' " . " OR game.match_title LIKE '%$searchText%' " . " OR game.selection LIKE '%$searchText%' " . " OR game.username LIKE '%$searchText%'"; } $qry .= " ORDER BY timelive DESC"; //for pagination $starting=0; $recpage = 10;//number of records per page $obj = new pagination_class($qry,$starting,$recpage); $result = $obj->result; function getStyleColorForStatus($status) { if ($status == 'WON') { return '#99ff99'; } else if ($status == 'LOST') { return '#ff9999'; } else if ($status == 'pending') { return '#e5e5e5'; } return ''; } ?> <form name="form1" action="testpage.php" method="POST" style="background-color:#f9f9f9; -webkit-box-shadow: 0 1px 10px rgba(0,0,0,.1); -moz-box-shadow: 0 1px 10px rgba(0,0,0,.1); box-shadow: 0 1px 10px rgba(0,0,0,.1);"> <table border="0" align="center" width="1000px" padding-left="0px" style="background:#F9F9F9;"> <TD colspan="0" style="padding-left:0px; padding-bottom:5px; padding-top:10px; text-align:center;"> Search: <input type="text" name="search_text" value="<?php echo $searchText; ?>"> <input type="submit" value="Search"> </TD> <tr><TD colspan="0"> <div id="page_contents"> <table width="968px" cellspacing="0" cellpadding="5" align="center" frame="box" rules="none" style="padding-bottom:20px; margin-bottom:8px;margin-top:3px; -webkit-box-shadow: 0 1px 10px rgba(0,0,0,.1); -moz-box-shadow: 0 1px 10px rgba(0,0,0,.1); box-shadow: 0 1px 10px rgba(0,0,0,.1); border: 1px solid #cccccc;"> <tr style="height: 50px;"> <td width="10%" align="center" td class="winheader"><div class="glowtext">Event Category</div></td></td> <td width="5%" align="center" td class="winheader"><div class="glowtext">Bid Cost</div></td></td> <td width="10%" align="center" td class="winheader"><div class="glowtext">Event Start Time</div></td></td> <td width="35%" align="center" td class="winheader"><div class="glowtext">Market/Event</div></td></td> <td width="10%" align="center" td class="winheader"><div class="glowtext">Selection</div></td></td> <td width="10%" align="center" td class="winheader"><div class="glowtext">Winnings</div></td></td> <td width="5%" align="center" td class="winheader"><div class="glowtext">Odds</div></td></td> <td width="5%" align="center" td class="winheader"><div class="glowtext">Gained Odds</div></td></td> <td width="10%" align="center" td class="winheader"><div class="glowtext">Winning Bidder</div></td></td> <td width="10%" align="center" td class="winheader"><div class="glowtext">Final Result</div></td></td> </tr> <? if(mysql_num_rows($result)!=0){ $counter = $starting + 1; $pathImg = "/images/icons/"; while($data = mysql_fetch_array($result)) { //calculate url image $pathFile = $pathImg . $data['sport_cat'] . ".png"; if (!file_exists($pathFile)) { $pathFile = $pathImg . "unknown.png"; } ?> <tr class="initial" onMouseOver="this.className='highlight'" onMouseOut="this.className='normal'"> <td align="center"><img src="<?=$pathFile?>" alt="<?=$data['sport_cat']?>"></TD> <td align="center"><? echo $data['bidprice']; ?></TD> <td align="center"><? echo $data['timelive']; ?></TD> <td align="left"><? echo $data['match_title']; ?></TD> <td align="left"><? echo $data['selection']; ?></TD> <td align="center"><font color="green">&euro; <? echo $data['winnings_with_startamount']; ?></TD> <td align="center"><? echo $data['odds']; ?></TD> <td align="center"><? echo $data['gainedodds']; ?></TD> <td align="center"><? echo $data['username']; ?></TD> <td align="center" style="background-color:<?php echo getStyleColorForStatus($data['result']); ?>"><? echo $data['result']; ?></td> </tr> <? $counter ++; } } ?> <tr><TD align="center" colspan="10" style="padding-bottom:1px; padding-top:10px; color:#333;"><? echo $obj->anchors; ?></TD></tr> <tr><TD align="center" colspan="10" style="padding-bottom:10px; padding-top:5px; color:#333;"><? echo $obj->total; ?></TD></tr> <?}else{?> <tr><TD align="center" colspan="10" style="padding-bottom:10px padding-top:10px; color:red;">No Data Found</TD></tr> <?}?> </TD></tr> </table> </div> </tr> </TD> </form> </table> 

попробуй это:

 $sql = "SELECT * FROM game"; $result = mysql_query($sql) or die(mysql_error()); $pathImg = "/images/icons/"; while ($row = mysql_fetch_array($result)) { $pathFile = $pathImg . $row['sport_cat'] . ".png"; if (!file_exists($pathFile)) { $pathFile = $pathImg . "unknown.png"; } echo '<img src="' . $pathFile . '">'; } 

Вы редактируете Ask, и я редактирую Anwer: P

 <table> <? if (mysql_num_rows($result) != 0) { $counter = $starting + 1; $pathImg = "/images/icons/"; while ($data = mysql_fetch_array($result)) { //calculate url image $pathFile = $pathImg . $data['sport_cat'] . ".png"; if (!file_exists($pathFile)) { $pathFile = $pathImg . "unknown.png"; } ?> <tr> <td align="center"><img src="<?=$pathFile?>" alt="<?=$data['sport_cat']?>"></td> <td align="center"><? echo $data['sport_cat']; ?></td> <td align="center"><? echo $data['timelive']; ?></td> <td align="center"><? echo $data['match_title']; ?></td> <td align="center"><? echo $data['selection']; ?></td> </tr> <? $counter++; } } ?> </table> 

Что-то вроде следующего должно работать.

 <?php $sql = "SELECT * FROM game"; $result = mysql_query($sql)or die(mysql_error()); while ($row = mysql_fetch_array($result)){ // Show images if($row['sport_cat']=='football'){ print "<img src='football.png'/>"; } elseif($row['sport_cat']=='basketball'){ print "<img src='basketball.png'/>"; } elseif($row['sport_cat']=='tennis'){ print "<img src='tennis.png'/>"; }else{ print "<img src='unknown.png' />"; } } // End while loop ?>