Steppio Posted March 20, 2007 Share Posted March 20, 2007 I have the following piece of code that doesnt work, the problem i have is that i 1. want to take a value out of the database (newsuser), - working 2. turn it into a variable ($user) - working 3. depending on which user has added the article to the news page (out of 5) i would like to define a <td style=""> tag with, what i have so far is a call to a custom function passing the variable ($user), in which the custom function should decide which background image to display depending on which user the table belongs to, if that makes sense. I think it may be something to do with my parenthesis with the echo command, but im probably wrong: <?php echo "<td class='dboxmain' colspan='3' style='get_news_bg($user);'>";?> <?php echo $news;?> </td> this call occurs in a seperate custom function that i can send the entirity of if neccesary. The custom function looks like: function get_news_bg($user) { if ($user == 'keith'){ echo "background-image: url('dbox/gbg.jpg');background-position: top left;background-repeat: repeat;"; } if ($user == 'latchy'){ echo "background-image: url('dbox/bg.jpg');background-position: top left;background-repeat: repeat;"; } if ($user == 'dave.fingers'){ echo "background-image: url('dbox/gbg.jpg');background-position: top left;background-repeat: repeat;"; } if ($user == 'squarejaw'){ echo "background-image: url('dbox/gbg.jpg');background-position: top left;background-repeat: repeat;"; } if ($user == 'dyson.ben'){ echo "background-image: url('dbox/gbg.jpg');background-position: top left;background-repeat: repeat;"; } } Any help would be most appreciated. Thanks. Link to comment https://forums.phpfreaks.com/topic/43485-solved-parentheses-problems-i-think/ Share on other sites More sharing options...
kenrbnsn Posted March 20, 2007 Share Posted March 20, 2007 I see at least two problems with your code: The first echo statement is wrong. You need to concatenate the value returned by the function to the string being echoed: <?php echo '<td class="dboxmain" colspan="3" style="'. get_news_bg($user) . '">'; ?> [*]In your function, you need to return the value, not echo it: <?php function get_news_bg($user) { switch ($user) { case 'latchy': $bgi = 'bg'; break; default: $bgi = 'gbg'; } return ('background-image: url("dbox/' . $bgi . '.jpg");background-position: top left;background-repeat: repeat;'); } ?> You'll notice that I replaced the "if" statementS with a "switch" statement. Also, you don't have to leave PHP and re-enter it on consecutive statement. You're first two echo statement can be written as: <?php echo '<td class="dboxmain" colspan="3" style="'. get_news_bg($user) . '">'; echo $news; ?> Ken Link to comment https://forums.phpfreaks.com/topic/43485-solved-parentheses-problems-i-think/#findComment-211191 Share on other sites More sharing options...
trq Posted March 20, 2007 Share Posted March 20, 2007 Change all occrances of echo within your function to return. eg; if ($user == 'keith'){ return "background-image: url('dbox/gbg.jpg');background-position: top left;background-repeat: repeat;"; } Then call your function using.... <?php echo "<td class='dboxmain' colspan='3' style='". get_news_bg($user) ."'>$news</td>"; ?> Link to comment https://forums.phpfreaks.com/topic/43485-solved-parentheses-problems-i-think/#findComment-211192 Share on other sites More sharing options...
Steppio Posted March 20, 2007 Author Share Posted March 20, 2007 Thanks Ken and Thorpe, its been bothering me for some time, i'll try this when i get home, hopefully another weight off my mind Thanks for your time. Link to comment https://forums.phpfreaks.com/topic/43485-solved-parentheses-problems-i-think/#findComment-211512 Share on other sites More sharing options...
Steppio Posted March 20, 2007 Author Share Posted March 20, 2007 Worked perfectly, thank you very much! Link to comment https://forums.phpfreaks.com/topic/43485-solved-parentheses-problems-i-think/#findComment-211599 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.