CodeIgniter 3从3个表中获取数据并保存为xml

yxyvkwin  于 2022-12-06  发布在  其他
关注(0)|答案(1)|浏览(120)

我用下面代码编写模型:

public function generate_google(){
        $this->db->select('*'); // <-- There is never any reason to write this line!
$this->db->from('products');
$this->db->join('product_details', 'products.id = product_details.product_id');
$this->db->join('images', 'products.id = images.product_id');

$query = $this->db->get()->result_array();

    
$xml = '<root>';
foreach($query as $row){
  $xml .= '<item>
             <sku>'.$row['sku'].'</sku>
             <price>'.$row['price'].'</price>
             <stock>'.$row['stock'].'</stock>
             <title>'.$row['title'].'</title>
           </item>';
}
$xml .= '</root>';
$this->output->set_content_type('text/xml');
$this->output->set_output($xml);
        
        
    }

1.如何在浏览器中添加保存xml文件而不显示输出的选项?
1.最后如何添加规则WHERE products.stock >= 1
@update请看下面是我的更新代码为model controller和view
sitemap_model.php

public function get_all_products() {
        $this->db->select('products.*, product_details.*, images.*')
        $this->db->from('products');
        $this->db->join('product_details','product_details.product_id = products.id');
        $this->db->join('images','images.product_id = products.id');
        $this->db->where('products.stock >=' 1);

        $query = $this->db->get();

        return $query->result_array();
    }

sitemap_controller.php

public function generate_google_post()
{
$this->load->model('sitemap_model');

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

foreach ($query as $row) {
    $xml .= '<item>
        <sku>'.$row['sku'].'</sku>
        <price>'.$row['price'].'</price>
        <stock>'.$row['stock'].'</stock>
        <title>'.$row['title'].'</title>
    </item>';
}

$xml .= '</root>';

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

echo $xml;
}

view.php

<?php echo form_open('sitemap_controller/generate_google_post'); ?>
            
            <!-- /.box-body -->
            <div class="box-footer">
                <button type="submit" name="process" value="generate" class="btn btn-primary pull-right" style="margin-bottom: 5px;"><?php echo trans('download_sitemap'); ?></button>
                
            </div>
            <!-- /.box-footer -->

        <?php echo form_close(); ?><!-- form end -->

我不确定这个错误是从哪里来的,但是当我点击“生成”时,我得到了这个错误,我把它粘贴在评论中。

q43xntqr

q43xntqr1#

查询应该如下所示:

$this->db->select('products.*, product_details.*, images.*');
$this->db->from('products');
$this->db->join('product_details', 'product_details.product_id = products.id');
$this->db->join('images', 'images.product_id = products.id');
$this->db->where('products.stock >=', 1);

$query      = $this->db->get();
$mysql_data = $query->result_array();

要下载XML,请使用以下行:

$this->output->set_content_type('text/xml');
$this->output->set_output($xml);

试试这个:

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

echo $xml;

顺便说一下,我建议您将数据库查询包含到模型中:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Products_Model extends CI_Model
{
    public function get_all_products() {
        $this->db->select('products.*, product_details.*, images.*');
        $this->db->from('products');
        $this->db->join('product_details', 'product_details.product_id = products.id');
        $this->db->join('images', 'images.product_id = products.id');
        $this->db->where('products.stock >=', 1);

        $query = $this->db->get();

        return $query->result_array();
    }
}

然后从控制器中调用它:

public function generate_google() {
    $this->load->model('products_model');

    $query = $products_model->get_all_products();
    $xml   = '<root>';

    foreach ($query as $row) {
        $xml .= '<item>
            <sku>'.$row['sku'].'</sku>
            <price>'.$row['price'].'</price>
            <stock>'.$row['stock'].'</stock>
            <title>'.$row['title'].'</title>
        </item>';
    }

    $xml .= '</root>';

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

    echo $xml;
}

相关问题