我有一个简单的联接表,看起来像这样。

NO   CLASS     NAME    STATUS
 1      1A     JOHN         1
 2      1A     SARA         1
 3      1A     LYOD         1
 4      1B     JOHN         1
 5      1B     SHIN         1


我使用textbox显示CLASS,并使用TEXTAREA显示NAMECLASS 1A的输出应如下所示:

Class : 1A
Name  : JOHN, SARA, LYOD


仅使用联接表(而不是多个查询)怎么做?

这就是我循环结果的方式:

$query  = $this->xxx->yyy($class_name); //JOINED RESULT

    $data = array(
        'titlepage'     => APP_TITLEPAGE,
        'record'        => $query, //This is the result that is sent to the view
        'complete'      => 'true',
        'loadmethod'    => 'add',
        'contentpage'   => 'test_page/detail'
    );

    $this->load->view('shared/master_app', $data);

最佳答案

所以您的查询数组是这样的:

$query = [
    ['NO' => 1, 'CLASS' => '1A', 'NAME' => 'JOHN', 'STATUS' => '1'],
    ['NO' => 2, 'CLASS' => '1A', 'NAME' => 'SARA', 'STATUS' => '1'],
    ['NO' => 3, 'CLASS' => '1A', 'NAME' => 'LYOD', 'STATUS' => '1'],
    ['NO' => 4, 'CLASS' => '1B', 'NAME' => 'JOHN', 'STATUS' => '1'],
];


您可以创建将其排列或分组的功能
让我们将其命名为groupResults($ query)

function groupResults($query) {
    $result = [];
    foreach($query as $queryItem) {
        $result[$queryItem['CLASS']][] = $queryItem['NAME'];
    }
    return $result;
}


现在的结果是这样的:

$result = [
    '1A' => ['JOHN', 'SARA', 'LYOD'],
    '1B' => ['JOHN']
]


这是我得到的快照:

php - 如何从联接表结果中分离出值?-LMLPHP

所以现在你可以像这样使用它

$query  = $this->xxx->yyy($class_name); //JOINED RESULT
$data = array(
    'titlepage'     => APP_TITLEPAGE,
    'record'        => groupResults($query), //This is the result that is sent to the view
    'complete'      => 'true',
    'loadmethod'    => 'add',
    'contentpage'   => 'test_page/detail'
);

$this->load->view('shared/master_app', $data);


现在在视图中,您可以在此数组上循环为:

foreach($record as $class => $names) {
    echo "<input type='text' value='" . $class . "'>";
    echo "<textarea>" . implode(', ', $names) . "</textarea>";
}

关于php - 如何从联接表结果中分离出值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46700867/

10-09 08:26