Face Code Face Code - 3 months ago 16
PHP Question

How can I make a strtr with values coming from database

I have the following code:

$books = $fetchPageContents->books;

echo strtr($books, array('sacrifice' => '<a href="#"> sacrifice </a>','sacrificing' => '<a href="#"> sacrificing </a>'));


I want to transfer this data to a db for cleaner handling of the code. How could this be accomplished?

These are my thoughts (Please assume
$db
is connected):

$content = $db->query("SELECT keyword, link FROM keywords");

while($fetch_content = mysqli_fetch_array($content)) {
$keyword = $fetch_content["keyword"];
$link = $fetch_content["link"];
}


This is the part where I'm stuck. I want to push the content into an array. But, the array is inside a PHP function. How do I push to the PHP array in a correct way?

Thanks...

Answer

Put it into a variable that you use with strtr.

$translations = array();
while($fetch_content = mysqli_fetch_array($content)) {
    $keyword = $fetch_content["keyword"];
    $link = $fetch_content["link"];
    $translations[$keyword] = $link;
}

Then use $translations as an argument to the function that calls strtr().

Comments