BigBrother Posted November 20, 2008 Share Posted November 20, 2008 Hi all, Just learning PHP and have a problem understanding how to display an image based on a value, below is the script I used to display an image based on another number within the table. The problem I have is, I am attempting to view the whole team and get an image for each player within the loop... any help would be appreciated. $player_query = "SELECT * FROM se_player WHERE player_id=$id"; $player_result = mysql_query($player_query); while($row = mysql_fetch_array($player_result)){ if (($row[5] > 0 and $row[5] <= 9)){$stats = "0.gif";} if (($row[5] > 9 and $row[5] <= 19)){$stats = "1.gif";} if (($row[5] > 19 and $row[5] <= 29)){$stats = "2.gif";} if (($row[5] > 29 and $row[5] <= 39)){$stats = "3.gif";} if (($row[5] > 39 and $row[5] <= 49)){$stats = "4.gif";} if (($row[5] > 49 and $row[5] <= 59)){$stats = "5.gif";} if (($row[5] > 59 and $row[5] <= 69)){$stats = "6.gif";} if (($row[5] > 69 and $row[5] <= 79)){$stats = "7.gif";} if (($row[5] > 79 and $row[5] <= 89)){$stats = "8.gif";} if (($row[5] > 89 and $row[5] <= 99)){$stats = "9.gif";} if (($row[5] == 100)){$stats = "10.gif";} } Quote Link to comment https://forums.phpfreaks.com/topic/133461-new-to-all-this-but-need-help/ Share on other sites More sharing options...
shutat Posted November 20, 2008 Share Posted November 20, 2008 for($id = 1; $id <= MAX_PLAYERS; $id++) { $res = mysql_query("SELECT * FROM se_player WHERE player_id=$id"); while($row = mysql_fetch_assoc($res)) { // generate the image } } I *think* that's what you're after. If you need to format a playing field or something, use the modulus operator against $id... something like if($id % 3 == 0) { echo "<br>"; } Quote Link to comment https://forums.phpfreaks.com/topic/133461-new-to-all-this-but-need-help/#findComment-694176 Share on other sites More sharing options...
BigBrother Posted November 20, 2008 Author Share Posted November 20, 2008 Thanks, I will give it a go and let you know the results... Kind regards BB Quote Link to comment https://forums.phpfreaks.com/topic/133461-new-to-all-this-but-need-help/#findComment-694289 Share on other sites More sharing options...
BigBrother Posted November 20, 2008 Author Share Posted November 20, 2008 As you can see below, I need to display an image from the value within the database. I am using smarty as well. Im still not able to get it working. This is the script for the table, have worked for ages to try to get it working and banging my head against the wall now....: <? $page = "user_squad"; include "header.php"; include "./lang/lang_english_manager.php"; // SET VAR // $id = $_GET['id']; $sql = "SELECT * FROM se_player WHERE player_team='".$user->user_info[user_id]."'"; $res = mysql_query($sql); $results = array(); $i=0; while ($r=mysql_fetch_array($res)) { $tmp = array( 'player_id' => $r['player_id'], 'player_fname'=> $r['player_fname'], 'player_lname'=> $r['player_lname'], 'player_int'=> $r['player_int'], 'player_rating'=> $r['player_rating'], 'player_number'=> $r['player_number'], 'player_pos'=> $r['player_pos'], 'player_value'=> $r['player_value'], ); $results[$i++] = $tmp; } //GET CLUB INFORMATION $club_query = "SELECT team_name FROM se_team WHERE team_manager='".$user->user_info[user_id]."'"; $club_res = mysql_query($club_query); while($row = mysql_fetch_row($club_res)) $club_name = $row[0]; // THIS UPDATES THE PALYER RATING IMAGE (NOT WORKING) $imagequery = "SELECT * FROM se_player"; $resimage = mysql_query($imagequery); while ($row = mysql_fetch_row($resimage)) { if (($row[5] > 0 and $row[5] <= 9)){$stats = "0.gif";} if (($row[5] > 9 and $row[5] <= 19)){$stats = "1.gif";} if (($row[5] > 19 and $row[5] <= 29)){$stats = "2.gif";} if (($row[5] > 29 and $row[5] <= 39)){$stats = "3.gif";} if (($row[5] > 39 and $row[5] <= 49)){$stats = "4.gif";} if (($row[5] > 49 and $row[5] <= 59)){$stats = "5.gif";} if (($row[5] > 59 and $row[5] <= 69)){$stats = "6.gif";} if (($row[5] > 69 and $row[5] <= 79)){$stats = "7.gif";} if (($row[5] > 79 and $row[5] <= 89)){$stats = "8.gif";} if (($row[5] > 89 and $row[5] <= 99)){$stats = "9.gif";} if (($row[5] == 100)){$stats = "10.gif";} } $smarty->assign('results', $results); $smarty->assign('stats', $stats); include "footer.php"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/133461-new-to-all-this-but-need-help/#findComment-694640 Share on other sites More sharing options...
premiso Posted November 20, 2008 Share Posted November 20, 2008 Are you sure that 10.gif is in the right directory? Maybe try adding the site url to the beginning of each image. Like so: $img_url = "http://www.yoursite.com/path/to/image/"; $smarty->assign('results', $results); $smarty->assign('stats', $img_url . $stats); If the picture is not in the directory where the file is being called from that will not work. Unless the template has already taken account for that. Since there is no template I am throwing this up as an option. Quote Link to comment https://forums.phpfreaks.com/topic/133461-new-to-all-this-but-need-help/#findComment-694643 Share on other sites More sharing options...
BigBrother Posted November 20, 2008 Author Share Posted November 20, 2008 I can get it to call the image, but it displays the last result in the table and displays the same image for all the rows... Quote Link to comment https://forums.phpfreaks.com/topic/133461-new-to-all-this-but-need-help/#findComment-694649 Share on other sites More sharing options...
premiso Posted November 20, 2008 Share Posted November 20, 2008 Oh I thought that is how you wanted it...given the code. You would want to store each image in an array like the results, infact attached onto the results array would be ideal, then you could just use that in your smarty to display the image. How you have it currently coded, only the last image is returned because $stats is not an array and gets overwritten with each loop. Hope that helps you get moving. Quote Link to comment https://forums.phpfreaks.com/topic/133461-new-to-all-this-but-need-help/#findComment-694653 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.