onthespot Posted July 11, 2009 Share Posted July 11, 2009 Hey guys. I have a folder with 3 images in, first.jpg, second.jpg, third.jpg. These images are going to be used for 1st 2nd and 3rd place on my site. <table align="center" width="100%"style='border: 1px dotted;'bgcolor="#eeeeee" > <tr><td><?echo "<a href=\"userprofile.php?user=$user\"><b>$user</b></a>\n";?></td></tr> <tr><td><?echo "at $final_date on $final_date2\n";?></td></tr> <tr bgcolor="#ffffff"><td><?echo "$comment\n";?></td></tr> <tr><td><? echo "$type\n";?></td></tr> </table><br> So this is the code for the table on my site. The $type variable is what I need the images to be linked to. So when it returns "league1", i need it to show the image "first.jpg", and league 2 , second, league 3 , third. How can I go about doing this? Quote Link to comment https://forums.phpfreaks.com/topic/165619-solved-displaying-images/ Share on other sites More sharing options...
onthespot Posted July 11, 2009 Author Share Posted July 11, 2009 if ($type == 'league1') echo "<img src=\"images/awards/first.jpg\"></img>"; else if ($type == 'league2') echo "<img src=\"images/awards/second.jpg\"></img>"; this works for so far, but how can i add this into a variable or some way where i dont have to write this in every place i want the medal to appear? Quote Link to comment https://forums.phpfreaks.com/topic/165619-solved-displaying-images/#findComment-873638 Share on other sites More sharing options...
wildteen88 Posted July 11, 2009 Share Posted July 11, 2009 but how can i add this into a variable or some way where i dont have to write this in every place i want the medal to appear? create a function instread. function show_league_image($type) { $images = array ( 'league1' => 'first.jpg', 'league2' => 'secound.jpg', 'league3' => 'third.jpg' ); if(array_key_exists($type, $images)) return '<img src="images/awards/'.$images[$type].'" />'; return false; } Now to display the images you call the function <tr><td><? echo show_league_image($type) ?></td></tr> Quote Link to comment https://forums.phpfreaks.com/topic/165619-solved-displaying-images/#findComment-873645 Share on other sites More sharing options...
ignace Posted July 11, 2009 Share Posted July 11, 2009 but how can i add this into a variable or some way where i dont have to write this in every place i want the medal to appear? create a function instread. function show_league_image($type) { $images = array ( 'league1' => 'first.jpg', 'league2' => 'secound.jpg', 'league3' => 'third.jpg' ); if(array_key_exists($type, $images)) return '<img src="images/awards/'.$images[$type].'" />'; return false; } Now to display the images you call the function <tr><td><? echo show_league_image($type) ?></td></tr> Hanging around WordPress to much? Quote Link to comment https://forums.phpfreaks.com/topic/165619-solved-displaying-images/#findComment-873647 Share on other sites More sharing options...
wildteen88 Posted July 11, 2009 Share Posted July 11, 2009 but how can i add this into a variable or some way where i dont have to write this in every place i want the medal to appear? create a function instread. function show_league_image($type) { $images = array ( 'league1' => 'first.jpg', 'league2' => 'secound.jpg', 'league3' => 'third.jpg' ); if(array_key_exists($type, $images)) return '<img src="images/awards/'.$images[$type].'" />'; return false; } Now to display the images you call the function <tr><td><? echo show_league_image($type) ?></td></tr> Hanging around WordPress to much? ??? No, never used it. Quote Link to comment https://forums.phpfreaks.com/topic/165619-solved-displaying-images/#findComment-873649 Share on other sites More sharing options...
ignace Posted July 11, 2009 Share Posted July 11, 2009 but how can i add this into a variable or some way where i dont have to write this in every place i want the medal to appear? create a function instread. function show_league_image($type) { $images = array ( 'league1' => 'first.jpg', 'league2' => 'secound.jpg', 'league3' => 'third.jpg' ); if(array_key_exists($type, $images)) return '<img src="images/awards/'.$images[$type].'" />'; return false; } Now to display the images you call the function <tr><td><? echo show_league_image($type) ?></td></tr> Hanging around WordPress to much? ??? No, never used it. Strange, you then applied the exact same logic WordPress applies to their functions as the way they deploy the functions. You are one kick-ass programmer, then? Quote Link to comment https://forums.phpfreaks.com/topic/165619-solved-displaying-images/#findComment-873653 Share on other sites More sharing options...
onthespot Posted July 11, 2009 Author Share Posted July 11, 2009 So where would i add the width and height i want these images to be? Quote Link to comment https://forums.phpfreaks.com/topic/165619-solved-displaying-images/#findComment-873664 Share on other sites More sharing options...
onthespot Posted July 11, 2009 Author Share Posted July 11, 2009 ok forget that, just realised my school boy Quote Link to comment https://forums.phpfreaks.com/topic/165619-solved-displaying-images/#findComment-873667 Share on other sites More sharing options...
onthespot Posted July 11, 2009 Author Share Posted July 11, 2009 Ok so got all that working, now stumbled across this. Fatal error: Cannot redeclare show_league_image() (previously declared in function show_league_image($type) Thats the code its referring to, any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/165619-solved-displaying-images/#findComment-873668 Share on other sites More sharing options...
wildteen88 Posted July 11, 2009 Share Posted July 11, 2009 How are you using the function? Post your code here. Quote Link to comment https://forums.phpfreaks.com/topic/165619-solved-displaying-images/#findComment-873669 Share on other sites More sharing options...
onthespot Posted July 11, 2009 Author Share Posted July 11, 2009 $type=$row['awardtype']; $comment=$row['awardcomment']; $awarddate=$row['awarddate']; $date=strtotime($awarddate); $final_date=date("g:i a", $date); $final_date2=date("F j Y", $date); function show_league_image($type) { $images = array ( 'league1' => 'first.jpg', 'league2' => 'second.jpg', 'league3' => 'third.jpg' ); if(array_key_exists($type, $images)) return '<img src="images/awards/'.$images[$type].'" width="20" height="45" />'; return false; } ?> <table align="center" width="100%"style='border: 1px dotted;'bgcolor="#eeeeee" > <tr><td><?echo "<a href=\"userprofile.php?user=$user\"><b>$user</b></a>\n";?></td></tr> <tr><td><?echo "at $final_date on $final_date2\n";?></td></tr> <tr bgcolor="#ffffff"><td><?echo "$comment\n";?></td></tr> <tr><td><? echo show_league_image($type) ?></td></tr> </table><br> Quote Link to comment https://forums.phpfreaks.com/topic/165619-solved-displaying-images/#findComment-873672 Share on other sites More sharing options...
wildteen88 Posted July 11, 2009 Share Posted July 11, 2009 Where is this code being used? Is it within a while loop? If it is take this code function show_league_image($type) { $images = array ( 'league1' => 'first.jpg', 'league2' => 'second.jpg', 'league3' => 'third.jpg' ); if(array_key_exists($type, $images)) return '<img src="images/awards/'.$images[$type].'" width="20" height="45" />'; return false; } Outside of the while loop. Quote Link to comment https://forums.phpfreaks.com/topic/165619-solved-displaying-images/#findComment-873673 Share on other sites More sharing options...
onthespot Posted July 11, 2009 Author Share Posted July 11, 2009 Thankyou i shal try this, thats a lesson i didnt know, declare functions outside, cheers Quote Link to comment https://forums.phpfreaks.com/topic/165619-solved-displaying-images/#findComment-873675 Share on other sites More sharing options...
onthespot Posted July 11, 2009 Author Share Posted July 11, 2009 PERFECT, thankyou my friend Quote Link to comment https://forums.phpfreaks.com/topic/165619-solved-displaying-images/#findComment-873676 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.