PHP: Most efficient way to display a variable within text when the text could be one of many possibilities

Below is a link to my original question:

PHP: How to display a variable (a) within another variable(b) when variable (b) contains text

Ok here's more to the problem, all your suggestions work but now I'm looking for the most efficient method to my specific problem.

In my database I have several blocks of text. When a user(described as $teamName) logs in to the site, they are randomly assigned one of these blocks of text. Each block of text is different and may have different variables in it.

The problem is I don't have knowledge of which block of text is assigned to the user without actually viewing the database or running a query. So at the moment I have to query the database and select the $newsID that corresponds to the block of text that the user has been assigned.

Because I have preset the blocks of text, I know what they contain so I can know do a switch($newsID) and depending on the value of the $newsID I then run the correct values inserted into the sprintf() function.

There is however, many many blocks of text so there will be many instances of case "": and break;. I wish to have the site working so that if at any stage I change a block of text to something different, then the variables within sprintf() are automatically updated, rather than me manually updating sprintf() within the switch() case:.

Sorry for the long post, hope it makes sense.

EDIT: I have these predetermined blocks of text in my database in my teamNews table:

For $newsID = 1:

"$teamName is the name of a recently formed company hoping to take over the lucrative hairdryer design
$sector"

For $newsID = 2:

"The government is excited about the potential of ".$teamName.", after they made an annoucement that they have hired $HoM"

For $newsID = 3:

"It is rumored that $teamName are valuing their hairdryer at $salePrice. People are getting excited.

When a user($teamName) logs into the game they are randomly assigned one of these blocks of text with $newsID of 1,2 or 3. Lets say the user is assigned the block of text with $newsID = 2. So now their username($teamName) is inserted into the database into the same row as their selected text.

Now I want to display the text corresponding to this user so I do the following:

$news = news ($currentStage,$teamName);

switch ($ID)
{

    case "1":

    sprintf($teamName,$sector)
    echo $news."<br/><hr/>";    

    break;


    case "2":
    sprintf($teamName,$Hom)
    break;

    case "3":
        sprintf($teamName,$saleprice)
    break;

}



$currentStage--;

}

With the function

function news($period,$teamName)
{


$news = mysql_query("

    SELECT `content`,`newsID` FROM `teamnews` WHERE `period` = '$period' && `teamName` = '$teamName'

    ") or die($news."<br/><br/>".mysql_error());


    $row = mysql_fetch_assoc($news);
    $news = $row['content'];
    $ID = $row ['newsID']; 
    return $news,$ID;

}

The problem is that in reality there are about 20 different blocks of text that the user could be assigned to. So I will have many case:'s. Also if I want to change all the text blocks in the database I would have to also manually change all the variables in the sprintf's in each `case:

I am wondering is there a better way to do this so that if I change the text in the database then the paramaters passed to sprintf will change accordingly.

So if I use

$replaces = array(
 'teamName' => 'Bob the team',
 'sector' => 'murdering',
 'anotherSector' => 'giving fluffy bunnies to children'
);

is it possible to do this:

$replaces = array(
 '$teamName' => '$teamName',
 '$sector' => '$sector',
 '$anotherSector' => '$anothersector'
);

Answers


I suggest you have fixed set of named placeholders, and use either the str_replace() or eval() (evil) methods of substitution.

So you would (for example) always have a $teamName and a $sector - and you might only sometimes use $anotherSector. And you have these two strings:

1 - $teamName, is the name of a recently formed company hoping to take over the lucrative $sector.
2 - The people at $teamName hate working in $sector, they would much rather work in $anotherSector

If you were to do:

$replaces = array(
  '$teamName' => 'Bob the team',
  '$sector' => 'murdering',
  '$anotherSector' => 'giving fluffy bunnies to children'
);
$news = str_replace(array_keys($replaces),array_values($replaces),$news);

You would get

1 - Bob the team, is the name of a recently formed company hoping to take over the lucrative murdering.
2 - The people at Bob the team hate working in murdering, they would much rather work in giving fluffy bunnies to children

As long as your placeholders have known names, they don't all have to be present in the string - only the relevant ones will be replaced.


Need Your Help

Installing mongodb in mac

osx mongodb

I am trying to install mongodb on my system and following these steps(http://docs.mongodb.org/manual/tutorial/install-mongodb-on-os-x/) in doing so. I have installed Homebrew and MacPorts and have

Vim Scrolling Slowly

performance vim scroll

Vim is acting slow when I scroll. The cursor skips some lines when I'm pressing j/k continually.