Jump to content

[SOLVED] Parentheses problems i think


Steppio

Recommended Posts

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

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

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>";

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.