Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6878685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:45:15+00:00 2026-05-27T04:45:15+00:00

Below is a link to my original question: PHP: How to display a variable

  • 0

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'
);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:45:15+00:00Added an answer on May 27, 2026 at 4:45 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

UPDATE: Problem Solved. Darin Dimitrov's answer contains a link to another, related question. One
Below is the css for a text link in 4 different states - link,visited,hover,
Note: Full working example now below. Original question follows: I'm having problems using ld's
The original words $string = '<a href=/home/top>here is some text</a>. <a href=/links/abc>some links here</a>...';
I have a problem similar to the one described in the link below. NSHTTPURLResponse
The original question below has been overtaken by time: these days you do not
I am studying snapshot isolation level of SQL Server 2008 from the below link.
The link below is an image URL where the extension has been stripped. I
The link below shows a listview composite control in its most basic form however
I have a site (link below) where the clients work displays on a long

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.