简体   繁体   English

控制器中的数据将无法查看(PHP Codeigniter)

[英]Data from controller not going to view (PHP Codeigniter)

I'm trying to get the $data from my controller to go into my view but it does not seem to be working. 我正在尝试从控制器获取$ data进入我的视图,但它似乎无法正常工作。 I keep getting a "variable does not exist" error. 我不断收到“变量不存在”错误。 Here's the Controller code (view is loaded in a different part). 这是Controller代码(视图加载在不同的部分)。

            $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);

When I pr($data) I get the array correctly, so I know the data is all there. 当我pr($ data)我正确地获得数组,所以我知道数据就在那里。 It's just when it is used in the view it does not even exist. 只是在视图中使用它时,它甚至不存在。

Am I doing something wrong? 难道我做错了什么? I've done other controller and views more-or-less the same way and never got problem this before. 我已经用相同的方式完成了其他控制器和视图的工作,以前从未遇到过问题。

EDIT: View code. 编辑:查看代码。

<?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>

The array: 数组:

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
)

I don't think you can send data like that in redirect_and_continue_processing() or redirect() in codeigniter. 我认为您无法在codeigniter中的redirect_and_continue_processing()或redirect()中发送类似的数据。 The only way to do that is by sending the data as an argument to the function that you are trying to redirect to. 唯一的方法是将数据作为参数发送给您要重定向到的函数。 Then in that function, use the data to generate the view. 然后在该函数中,使用数据生成视图。

LIke : 喜欢 :

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

And in the method "affiliate_relationship" , accept the argument 在方法“ affiliate_relationship”中,接受参数

Redirect method does not take an argument as the variable to send to view. 重定向方法不将参数作为要发送到视图的变量。

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

you can use simple way to do this 你可以用简单的方法做到这一点

$this->load->view('your_view_name','your data in array'); $ this-> load-> view('your_view_name','your data in array');

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

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