简体   繁体   English

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

[英]Fetch complex table data from database

I have a table in database and I want to fetch the data from it. 我在数据库中有一个表,我想从中获取数据。 I know how to fetch data from database using php. 我知道如何使用php从数据库中获取数据。 But the problem is that this table is different in structure. 但是问题在于该表的结构不同。 For example 例如

在此处输入图片说明 and so on... 等等...

As you can see this table is like a pivot table. 如您所见,此表就像数据透视表。 That is the real problem for me. 这对我来说是真正的问题。 All the entries of a single person should be in single row. 一个人的所有条目都应放在一行中。 But it is in different rows and single column (all entries of single person is in value column). 但是它在不同的行和单个列中(一个人的所有条目都在value列中)。

I have tried to fetch table with regular way like this 我试图以这种常规方式获取表格

<?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();?>

But with the above code it's showing all the results in a single column and that's what I expect from above code. 但是上面的代码将所有结果显示在一个列中,这就是我对上面的代码的期望。

So I don't know the correct code to represent this table as single row entries of single person. 因此,我不知道将这张表表示为单人的单行条目的正确代码。 I searched a lot but didn't get much. 我搜索了很多,但没有得到太多。

You use foreign key entries_id so you can retrieve from that table ie 您使用外键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 }
}

I'm pretty sure you looking for something like this: 我很确定您正在寻找这样的东西:

horizontal representation: (by Person) 水平表示:(按人)

<?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();?>

vertical representation (by field slug) 垂直表示 (按段塞)

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