来自控制器的数据无法查看(PHP Codeigniter)

时间:2015-09-24 16:44:50

标签: php codeigniter model-view-controller

我试图从我的控制器获取$ 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
)

2 个答案:

答案 0 :(得分:0)

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

LIke:

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

在方法&#34; affiliate_relationship&#34; ,接受论点

Redirect方法不会将参数作为要发送到视图的变量。

答案 1 :(得分:-1)

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

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

$ this-&gt; load-&gt; view('your_view_name','您在数组中的数据');