我如何将变量值保存在数据库中?

时间:2017-03-20 09:42:18

标签: php codeigniter

我已经在mysql数据库中保存了数据,并且我在一列中创建了模板html标签。现在我想在php中显示模板数据,但它不起作用。

MYSQL数据库图片点击听众显示数据库 enter image description here

控制器

class Project extends CI_Controller{

    function template(){
        $query = $this-db->query("select * from templete where id='11' ");
        $result = $query->result_array();
        $data['result'] = $result[0];

        $this->load->view('template_view',$data);
    }
}

查看

<html>
<head></head>
<body>
<?php
    extract($result);

    echo $template_data;
?>
</body>
</html> 

输出:

{$ticker}
{$address1}
{$address2}
{$city}
{$phone}
{$website}

我想要输出

HSPG.OL
Sparebankgården
Bjørkelangen
http://www.hsbank.no

模板数据动态 它消失了

<p>{$ticker}</p><p>{$address1}</p><p>{$address2}</p><p>{$city}</p><p>{$phone}</p><p>{$website}</p>

<p>{$ticker}</p><p>{$address1}</p><p>{$address2}</p><p>{$city}</p>

任何一个帮助,我发布第二次完整编码的问题。

2 个答案:

答案 0 :(得分:0)

尽管Codeigniter作为一个框架并没有阻止你或限制你留在MVC边界,但我们真的很感激并且应该这样做。

最佳实践

  

创建模型并将查询放入该模型的函数中。呼叫   在您的控制器中的该功能,获取数组/对象中的数据   然后将其发送到MVC应该如何的视图。

在你的情况下,你正在使用php extract()函数从$ result数组中获取数据,它完美地完成了但你打印的不是正确的索引。

Read this用于php提取信息

所以不要打印$ template_data而是使用以下内容;

<html>
<head></head>
 <body>
 <?php
    extract($result);

    echo $ticker.'<br>';
    echo $address1.'<br>';
    echo $city.'<br>'; // and so on
 ?>
</body>
</html>

答案 1 :(得分:0)

此代码出了很多问题。您不应该从数据库中获取应该位于视图中的模板数据。您应该从表中删除template_data列。

class Project extends CI_Controller{

    function template(){
        $query = $this-db->query("select * from templete where id='11' ");
        $data['result'] = $query->result_array();

        $this->load->view('template_view',$data);
    }
}

查看:

<html>
<head></head>
<body>
<?php echo $result['ticker'];   ?>
<?php echo $result['address1'] . ' ' . $result['address2'];   ?>
<?php echo $result['city'];   ?>
<?php echo $result['phone'];   ?>
<?php echo $result['website'];   ?>
?>   
</body>
</html> 
相关问题