繁体   English   中英

从数据库中获取复杂的表数据

[英]Fetch complex table data from database

我在数据库中有一个表,我想从中获取数据。 我知道如何使用php从数据库中获取数据。 但是问题在于该表的结构不同。 例如

在此处输入图片说明 等等...

如您所见,此表就像数据透视表。 这对我来说是真正的问题。 一个人的所有条目都应放在一行中。 但是它在不同的行和单个列中(一个人的所有条目都在value列中)。

我试图以这种常规方式获取表格

<?php
/*
Template Name: Registration
*/
get_header();

$results = $wpdb->get_results( "SELECT * FROM wp_cf_form_entry_values", ARRAY_A  );
$count = $results->num_rows;
if(!empty($results)) {
    echo "<table width='100%' border='1' cellspacing='1'>";
    echo "<tbody>";

    foreach($results as $row){
        echo "<tr>";              
        echo "<td><center>" . $row['value'] . "</center></td>";              
        echo "</tr>";
    }
    echo "</tbody>";
    echo "</table>"; 
} else {
    echo "<table width='100%' border='1' cellspacing='1'><tr><td>No Records Found</td></tr></table>";
}

get_footer();?>

但是上面的代码将所有结果显示在一个列中,这就是我对上面的代码的期望。

因此,我不知道将这张表表示为单人的单行条目的正确代码。 我搜索了很多,但没有得到太多。

您使用外键entries_id,以便可以从该表中检索,即

$results=$wpdb->get_results("select * from entries_table");
foreach($results as $result){
    $entries_id=$result->id;
    $entry_detail=$wpdb->get_results(SELECT * from form_field_table where entries_id='$entries_id' ");
        foreach($entry_detail as $ent_detail){?>
            <td><?php echo $ent_detial->slug;?></td><td><?php echo $ent_detial->value;?></td>
        <?php }
}

我很确定您正在寻找这样的东西:

水平表示:(按人)

<?php
/*
Template Name: Registration
*/
get_header();

global $wpdb;

//Create a mulit dimensional array for each person.
$persons = array();
$fields = $wpdb->get_results( 'SELECT * FROM wp_cf_form_entry_values');
foreach($fields as $field){
    $persons[$field->entry_id][$field->slug] = $field->value;
}

//the table
echo '<table width="100%" border="1" cellspacing="1">';
$i=0;
foreach($persons as $id=>$values){

    //the table header (only in the first loop)
    if($i==0){
        echo '<tr>';
        foreach($values as $key=>$val){
            echo '<th>'.$key.'</th>';
        }
        echo '</tr>';
    }

    // one line per person
    echo '<tr id="'.$id.'">';
    foreach($values as $key=>$val){
        echo '<td>'.$val.'</td>';
    }
    echo '</tr>';
    $i++;
}

if($i==0){
   echo "<table width='100%' border='1' cellspacing='1'><tr><td>No Records Found</td></tr></table>";
}

echo '</table>';

get_footer();?>

垂直表示 (按段塞)

<?php
/*
Template Name: Registration
*/
get_header();

global $wpdb;

//Create a mulit dimensional array for each slug.
$field_slugs = array();
$fields = $wpdb->get_results( 'SELECT * FROM wp_cf_form_entry_values');
foreach($fields as $field){
    $field_slugs[$field->slug][$field->entry_id] = $field->value;
}

//the table
echo '<table width="100%" border="1" cellspacing="1">';
$i=0;
foreach($field_slugs as $slug=>$values){

    //the table header (only in the first loop)
    if($i==0){
        echo '<tr>';
        foreach($values as $person_id=>$val){
            echo '<th></th>';
            echo '<th>Person '.$person_id.'</th>';
        }
        echo '</tr>';
    }

    // one line per person
    echo '<tr id="'.$slug.'">';
    echo '<td><b>'.$slug.'</b></td>';
    foreach($values as $person_id=>$val){
        echo '<td>'.$val.'</td>';
    }
    echo '</tr>';
    $i++;
}

if($i==0){
   echo "<table width='100%' border='1' cellspacing='1'><tr><td>No Records Found</td></tr></table>";
}

echo '</table>';

get_footer();?>

暂无
暂无

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

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