简体   繁体   中英

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. 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).

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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