0

情况如下:我有 2 个不同的功能和一个视图。我需要将数据从函数 1 发送到视图(2 个数组),然后从该视图发送数据到函数 2(1 个数组)。

将数据从函数 1 发送到视图是一项简单的工作,但我不知道如何使用函数 2 来完成,因为我想发送给它的信息包含函数 1 的数组以及用户输入的其他新数据。

我知道没有机会通过 URL 发送数组,但我没有想法。

哪个是传递数据的最佳选择?

我将这些数据发送到视图:

管理员:(功能1的一部分)

//last part of the code
$this->data['conditions'] = $conditions;
$this->data['notselectedconditions'] = $notselectedconditions;
$this->parser->parse('admin/tools/showReport.tpl',$this->data);

在视图中,我使用这些数组的信息,用户可以输入一些新条目。

看法:

<dt>Tipo de Usuario<dt>
    <dd><a href="{site_url()}admin/admin/reloadConditions/1">Pre Registrado</a></dd>

reloadConditions 是函数 2,它需要函数 1 提供的内容以及用户的选择,以便继续过滤结果。

我需要在函数 2 上获得的信息是:数组“条件”和用户新条目

4

2 回答 2

2

经过长时间的讨论和聊天中的一些澄清,这是我想出的解决方案:

<?php

function addQueryParameters($url, $params) {
    $paramsStr = http_build_query($params);

    $fragment = parse_url($url, PHP_URL_FRAGMENT);
    $query = parse_url($url, PHP_URL_QUERY);

    $interUrl = '';
    if ($query === NULL) {
      $interUrl = '?';
    }
    if ($fragment !== NULL) {
        $interUrl = '&';
    }

    $interUrl .= $paramsStr;

    if ($fragment !== NULL) {
        $pos = strpos($url, '#' . $fragment);
        $url = substr($url, 0, $pos) . $interUrl . substr($url, $pos);
    }

    return $url;
}

function linkifyFilterConditions($url, $conditions) {
    return addQueryParameters($url, array('conditions' => $conditions));
}

// Tests
// TODO: replace by unit tests
var_dump(linkifyFilterConditions('http://example.com/blub', [1,2,3]));
var_dump(linkifyFilterConditions('http://example.com/blub#a', [1,2,3]));
var_dump(linkifyFilterConditions('http://example.com/blub?a=b#a', [1,2,3]));

用法(在 OP 看来):

$href = 'yourscript?conditions[]=a'; 
$href = linkifyFilterConditions($href, $conditions); 

echo '<a href="' . $href . '">Test</>';
于 2013-11-14T17:21:15.297 回答
-1

把你的数组放进去怎么样$_SESSION

见这里http://www.phpriot.com/articles/intro-php-sessions/7

于 2013-11-14T16:23:32.587 回答