【问题标题】:Data from controller not going to view (PHP Codeigniter)来自控制器的数据不会查看(PHP Codeigniter)
【发布时间】:2015-12-22 09:01:03
【问题描述】:

我正试图从我的控制器中获取 $data 以进入我的视图,但它似乎不起作用。我不断收到“变量不存在”错误。这是控制器代码(视图加载在不同的部分)。

            $affiliate_id = $this->input->get_post('affiliate_id');
        $product_id = $this->input->get_post('product_id');
        $data = array();

        if (empty($affiliate_id)
            && empty($product_id))
        {
            $this->session->set_flashdata('error', 'Please enter an affiliate ID, a product ID, or both.');
            redirect('admin/affiliate_relationship');
        }

        if (empty($affiliate_id))
        {
            $data['affiliate_relationship'] = $this->AffiliateRelationship->search_for_affiliate_by_product_id($product_id);
        }
        elseif (empty($product_id))
        {
            $data['affiliate_relationship'] = $this->AffiliateRelationship->search_for_affiliate_by_affiliate_id($affiliate_id);
            $affiliate = $affiliate_id;
        }
        elseif (!empty($affiliate_id)
                && !empty($product_id))
        {
            $data['affiliate_relationship'] = $this->AffiliateRelationship->search_for_affiliate($affiliate_id, $product_id);
            $affiliate = $affiliate_id . '/';
        }

        $data['affiliate_id'] = $affiliate_id;
        $data['product_id'] = $product_id;

        redirect_and_continue_processing('admin/affiliate_relationship/' . $affiliate . $product_id, $data);

当我 pr($data) 时,我得到了正确的数组,所以我知道数据就在那里。只是在视图中使用它时它甚至不存在。

我做错了吗?我已经以或多或少相同的方式完成了其他控制器和视图,并且以前从未遇到过问题。

编辑:查看代码。

<?php
$affiliateRelationshipRows = NULL;

if (!empty($affiliate_relationship))
{
$class = NULL;
$i = 0;

foreach ($affiliate_relationship->result_array() as $affiliate)
{
    if (++$i%2 == 0)
    {
        $class= ' class="odd"';
    }
    else
    {
        $class = NULL;
    }

    $affiliateRelationshipRows .= <<<END
    <tr $class>
        <td class="text-left">{$affiliate['id']}</td>
        <td class="text-left">{$affiliate['product_id']}</td>
        <td class="text-left">{$affiliate['user_id']}</td>
        <td class="text-left">{$affiliate['affiliate_status_id']}</td>
        <td class="text-left">{$affiliate['created']}</tD>
        <td class="text-left">{$affiliate['custom_payout']}</td>
        <td class="text-left">{$affiliate['delayed']}</td>
        <td class="text-left">{$affiliate['sales_page_url']}</td>
        <td class="text-left">{$affiliate['comments']}</td>
    </tr>
END;
}
}
?>

<?php echo form_open($this->uri->uri_string()); ?>

<div class="box-search">
<div class="grid-2" style="width:200px; margin-left:25px;">
    <span class="label" style="float:none;">
        Affiliate ID:
    </span>
    <span class="field" style="float:none;">
        <input type="text" name="affiliate_id" value="<?php if (!empty($affiliate_id)) { echo $affiliate_id; } ?>" style="width: 75px;"/>
    </span>
    <?php echo form_error('affiliate_id'); ?>
</div>

<div class="grid-2" style="width:200px; margin-left:0px;">
    <span class="label" style="float:none; ">
        Product ID:
    </span>
    <span class="field" style="float:none;">
        <input type="text" name="product_id" value="<?php if (!empty($product_id)) { echo $product_id; } ?>" style="width:75px;"/>
    </span>
    <?php echo form_error('product_id'); ?>
</div>

<a href="/admin/affiliate_relationship" class="btn btn-mini-submit btn-blue btn-search" style="margin-right: 100px;">RESET</a>
<input type="submit" name="search" class="btn btn-green btn-mini-submit btn-search" value="SEARCH"/>
<div class="clear"></div>
</div>

<?php echo form_close(); ?>

<div class="box">
<div class="top">
    Affiliate Relationship
</div>
<div class="main-table">
    <table class="style1" cellpadding="2" cellspacing="0" width="100%" border="0">
        <thead>
            <tr>
                <th>ID</th>
                <th>Product ID</th>
                <th>User ID</th>
                <th>Affiliate Status ID</th>
                <th>Created</th>
                <th>Custom Payout</th>
                <th>Delayed</th>
                <th>Sales Page URL</th>
                <th>Comments</th>
            </tr>
        </thead>
        <tbody>
            <?php echo $affiliateRelationshipRows; ?>
        </tbody>
    </table>
</div>

数组:

Array
(
[affiliate_relationship] => Array
    (
        [0] => stdClass Object
            (
                [id] => 11615304
                [created] => 2015-09-17 00:00:00
                [product_id] => 175538
                [user_id] => 393598
                [comments] => 
                [affiliate_status_id] => 2
                [custom_payout] => 
                [delayed] => 0
                [sales_page_url] => 
            )

    )

[affiliate_id] => 11615304
[product_id] => 175538
)

【问题讨论】:

  • 在你看来试试这个--> echo $affiliate_id;
  • 您是如何尝试检索视图中的数据的?你能显示视图代码吗?
  • 回声给了我同样的错误,不明变量。
  • 请打印
     标签内的数组并发送结果

标签: php codeigniter model-view-controller


【解决方案1】:

而不是使用 redirect_and_continue_processing('admin/affiliate_relationship/' . $affiliate . $product_id, $data);

你可以使用简单的方法来做到这一点

$this->load->view('your_view_name','你的数组中的数据');

【讨论】:

    【解决方案2】:

    我认为您不能在 codeigniter 中的 redirect_and_continue_processing() 或 redirect() 中发送这样的数据。唯一的方法是将数据作为参数发送给您尝试重定向到的函数。然后在该函数中,使用数据生成视图。

    喜欢:

     redirect_and_continue_processing('admin/affiliate_relationship/' . $affiliate . $product_id/$data);
    

    并且在 "affiliate_relationship" 方法中,接受参数

    重定向方法不将参数作为变量发送到视图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-22
      • 2017-10-28
      • 2014-04-13
      • 1970-01-01
      相关资源
      最近更新 更多