1

I'm trying to make a 'log' for a litte project.

The info what has to be in the log is in a other table in the database, I would like to get the html form from the database with the variable info in it which it takes from the other table.

Code which is in the Database:

<div class="container">
<ul class="timeline">
    <li>
        <div class="timeline-panel">
            <div class="timeline-heading">
                <h4 class="timeline-title"><?php echo $player->name ?></h4>
                <p class="text-muted"><?php echo $player->time ?></p>
            </div>
            <div class="timeline-body">
                <p>While slaying <?php echo $player->npcName ?> I found an <?php echo $player->drop ?>!</p>
            </div>
        </div>
    </li>
</ul>

Class with functions:

class player
{
    public $name;
    public $time;
    public $npcName;
    public $drop;
    public $level;

    public $code;


    function getPlayer(){
        $db = new dbCon();
        $sql = 'SELECT * from player where id = 1';
        foreach($db->getCon()->query($sql) as $row ){
            $this->name = $row['name'];
            $this->time = $row['time'];
            $this->npcName = $row['npcName'];
            $this->drop = $row['drop'];
            $this->level = $row['level'];
            $db = null;
        }
    }

    function getLogForm(){
        $db = new dbCon();
        $sql = 'SELECT * from htmltemplate WHERE id = 1';

        foreach($db->getCon()->query($sql) as $row){
            $this->code = $row['code'];
        }    
    }      
}

Echo in index.php

<?php echo $player->code ?>

form code in database

Current output

1 Answer 1

2

As $row['code'] is a string, it won't be parsed and variables won't be replaced. For such cases use either sprintf or str_replace functions.

For example, you can store your html-template as:

<div class="timeline-panel">
    <div class="timeline-heading">
        <h4 class="timeline-title">#PLAYER_NAME#</h4>
        <p class="text-muted">#PLAYER_TIME#</p>

And then output like:

<?php echo str_replace(
    array('#PLAYER_NAME#', '#PLAYER_TIME#'), 
    array($player->name, $player->time), 
    $player->code
);?>

And of course you should have $player object available.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.