0

<h3><?php echo $name; ?></h3>ループに表示される各項目について、数行のデータを表示しようとしています。

現在のコードでは、アイテムごとに 1 行しか表示されません<h3><?php echo $name; ?></h3>

functions.php ファイルにこれがあります $newdb = new wpdb('login', 'pass', 'db', 'host');

<?php $result=$newdb->get_results('select tbl1.name, tbl2.col1, tbl2.col2, tbl2.col3 from tbl1, tbl2 where tbl1.name=tbl2.tbl1_name');
$names=array();
foreach($result as $row): ?>
<?php $names[$row->name][]=$row;
endforeach; ?>
<?php foreach($names as $name=>$info): ?>

    <h3><?php echo $name; ?></h3>
    <table>
       <tr><th>col1</th><th>col2</th><th>col3</th></tr>
        <?php foreach($info as $n):?>       
        <tr>
            <td><?php echo $n->col1; ?></td>
            <td><?php echo $n->col2; ?></td>
            <td><?php echo $n->col3; ?></td>    
        </tr>
        <?php endforeach; ?>
    </table>    
<?php endforeach; ?>

そのため、ループは見出しの後に、1 行だけでなく数行のレコードを表示します。

<h2>Name</h2>
<table>
<tr><th>col1</th><th>col2</th><th>col3</th></tr>
        col1-value1  col2-value1  col3-value1
        col1-value2  col1-value2  col1-value2
        etc.
</table>

<h2>Name</h2>
<table>
<tr><th>col1</th><th>col2</th><th>col3</th></tr>
        col1-value1  col2-value1  col3-value1
        col1-value2  col1-value2  col1-value2
        etc.
</table>

<h2>Name</h2>
<table>
<tr><th>col1</th><th>col2</th><th>col3</th></tr>
        col1-value1  col2-value1  col3-value1
        col1-value2  col2-value2  col3-value2
        etc.
</table>
.....`
4

1 に答える 1

1

あなたのアイデアを使用することは可能ですが、次のような1つのクエリを使用して実行できます

global $wpdb;
$result=$wpdb->get_results('select a.col, b.col1, b.col2, b.col3 from a,b where a.col=b.col5');

tbl1テーブルがあり、nameフィールドとtbl2テーブルがあり、たとえば、名前フィールドと他のフィールドtbl1_nameの名前値を持つフィールドがあると仮定しますtbl1(重複なし)。したがって、次のように両方のテーブルを結合できます。

$result=$wpdb->get_results('select tbl1.name, tbl2.col1, tbl2.col2, tbl2.col3 from tbl1, tbl2 where tbl1.name=tbl2.tbl1_name');
$names=array();
foreach($result as $row): ?>
    $names[$row->name][]=$row;
<?php endforeach; ?>
foreach($names as $name=>$info):
    echo $name;
    ?><table><?php
    foreach($info as $n):
    ?>
            <tr>
                <td><?php echo $n->col1; ?></td>
                <td><?php echo $n->col2; ?></td>
                <td><?php echo $n->col3; ?></td>
            </tr>
    <?php
    <?php endforeach; ?>
    ?></table><?php
<?php endforeach; ?>

また$wpdb、グローバルオブジェクトであり、テンプレートファイルから利用できることを覚えておいてください。インスタンスを作成するために使用する必要はありません。答えに書いたように、キーワードをnew使用するだけです。global

Class Reference/wpdb and sql join and this one .

于 2012-10-20T19:22:45.913 に答える