简体   繁体   English

如何从后视图中获取数据 mysql 到 controller | Codeigniter 3

[英]how to get data mysql from post view to controller | Codeigniter 3

This is part of function controller:这是 function controller 的一部分:

    public function generate_google_post()
        {
$query = $this->sitemap_model->get_all_products();
    foreach ($query as $row) {
            $xml .= '<item>
                <g:id>'.$row['id'].'</g:id>
    </item>';
    }
        echo $xml;
        }

From view.php In above function I can submit to controller and generate correct XML file with data from mysql.view.php在上面的 function 中,我可以提交给 controller 并使用来自 mysql 的数据生成正确的 XML 文件。

Issue start from here:问题从这里开始:

now I want do some customization to write xml content directly in storefront (view) and then call input data to controller and generate xml.现在我想做一些定制,直接在店面(视图)中写入 xml 内容,然后将输入数据调用到 controller 并生成 xml。

For this I try:为此,我尝试:

public function generate_google_post2()
    {

    $xml   = '<channel>';
    $query = $this->sitemap_model->get_all_products();

    foreach ($query as $row) {
        $xml .= $this->input->post('add-list');
    }

    $xml .= '</channel>';

    header('Content-type: text/xml');
    header('Content-Disposition: attachment; filename="file.xml"');

    echo $xml;
    }
}

and in view page I try:在视图页面中,我尝试:

<?php echo form_open('sitemap_controller/generate_google_post2'); ?>
            <textarea name="add-list" id="add-list" rows="10" cols="75"> </textarea>
<button type="submit" name="process" value="generate" class="btn btn-primary pull-right" style="margin-bottom: 5px;"><?php echo trans('download_feed'); ?></button>
<?php echo form_close(); ?><!-- form end -->

and now in storefront text area form I try post:现在在店面文本区域表单中,我尝试发布:

 <item>
 <gid>'.$row['id'].'</gid>
</item>

XML file generate sucess but the problem is I get output XML: XML 文件生成成功但问题是我得到 output XML:

<channel> <item>
 <gid>'.$row['id'].'</gid>
</item> <item>
 <gid>'.$row['id'].'</gid>
</item> </channel>

So missing data from mysql. Is any way post from text are and in this way get data from database and then generate to xml?所以缺少来自 mysql 的数据。是否有任何方式从文本发布并以这种方式从数据库获取数据然后生成到 xml?

text area view post文本区域查看帖子

I try add if($_POST) then $query = $this->sitemap_model->get_all_products();我尝试添加if($_POST)然后$query = $this->sitemap_model->get_all_products(); and

foreach ($query as $row) {
        $xml .= $this->input->post('add-list');
    }

But this not resolved this issue.但这并没有解决这个问题。 Full code:完整代码:

public function generate_google_post2()
    {
  if($_POST){
      $id = $this->input->post('add-list');
      $query = $this->sitemap_model->get_all_products();
      $xml   = '<channel>';
   
    foreach ($query as $row) {
        $xml .= $this->input->post('add-list');
    }

    $xml .= '</channel>';

    header('Content-type: text/xml');
    header('Content-Disposition: attachment; filename="file.xml"');

    echo $xml;
    }
}

The $row variable is being interpreted as text. $row变量被解释为文本。 You wouldn't want to do this anyway as you would be very vulnerable to malicious code submitted in the form.无论如何您都不想这样做,因为您很容易受到表单中提交的恶意代码的攻击。 Essentially you are trying to provide some template to the backend to fill with data.本质上,您正在尝试向后端提供一些模板来填充数据。

Provide a placeholder in your form.在您的表单中提供一个占位符。

<item><gid>{id}</gid></item>

In your foreach loop, replace the placeholder with your data.在您的 foreach 循环中,将占位符替换为您的数据。

$template = $this->input->post('add-list');
// Replace your placeholder
$template = str_replace('{id}',            $row['id'], $template);
$template = str_replace('{title}',         $row['title'], $template);
$template = str_replace('{sku}',           $row['sku'], $template);
$template = str_replace('{somethingelse}', $row['thing'], $template);
$xml .= $template;

Anyone who uses this form who is not you would need to be instructed what specific text to use as the placeholder.任何使用此表格的人都需要被告知要使用什么特定文本作为占位符。 So you would need to know, or control, what placeholders the user enters.因此,您需要知道或控制用户输入的占位符。

You could control the placeholders on the front end with buttons that insert the text on the cursor. https://jsfiddle.net/xhgk8cz7/1/您可以使用在 cursor 上插入文本的按钮来控制前端的占位符。https://jsfiddle.net/xhgk8cz7/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM