第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

使用 PHP MySQL 將數(shù)組放入 html 表

使用 PHP MySQL 將數(shù)組放入 html 表

PHP
紅顏莎娜 2022-07-09 10:05:16
我在一個(gè)數(shù)據(jù)庫中有 2 個(gè)表,我正在加入一個(gè) PDO 執(zhí)行,我讓它以一種表的形式輸出,但我需要它以另一種格式。我需要某種形式的動(dòng)態(tài)支點(diǎn)嗎?2張桌子是這樣的:我希望從中得到下表:我用來整理數(shù)據(jù)的當(dāng)前查詢是:$sql3 = $pdo->prepare('SELECT b.title, a.nameFROM poll_summary a INNER JOIN poll_answers b ON a.answers_id = b.id WHERE a.poll_id = ? ORDER BY title');$sql3->execute([$_GET['id']]);$testing = $sql3->fetchAll(\PDO::FETCH_GROUP|\PDO::FETCH_ASSOC);    這是它給出的示例數(shù)組:Array ( [Knight ] => Array ( [0] => Array ( [name] => Dave ) [1] => Array ( [name] => Simon ) ) [Lieutenant ] => Array ( [0] => Array ( [name] => Tom ) ) )我可以使用以下方法從標(biāo)題列中獲取表頭:<table border=1>  <thead>    <tr>        <?php            foreach($testing as $key => $val):            ?>                <th><?php echo $key;?></th>             <?php endforeach; ?>    </tr>    </thead>    <tbody>        <tr>            </tbody></table>但我不知道如何將名稱值放入正確的列中。我嘗試使用此代碼,但無法讓它按我想要的方式工作。foreach($testing as $key => $val) {    $arrlength = count($val);    for($x = 0; $x < $arrlength; $x++) {    //echo $key;    echo $key;    echo $val[$x]['name'];     }}輸出:Knight Dave Knight Simon Lieutenant Tom
查看完整描述

1 回答

?
茅侃侃

TA貢獻(xiàn)1842條經(jīng)驗(yàn) 獲得超22個(gè)贊

在 $testing 中已經(jīng)有了這個(gè)結(jié)構(gòu):


Array ( [Knight ] => Array ( [0] => Array ( [name] => Dave ) [1] => Array ( [name] => Simon ) ) [Lieutenant ] => Array ( [0] => Array ( [name] => Tom ) ) )

做表


<?php

    // Just to keep same variable

    $output = $testing;

?>

<table border=1>

  <thead>

    <tr>

<?php

    // We need to know wich $title has more names

    $max = 0;

    foreach($output as $title => $names) {

       if(count($names) > $max) {

          $max = count($names);

       }

?>

                <th><?php echo $title;?></th>

<?php } // end foreach ?>

    </tr>

    </thead>

    <tbody>

<?php

    // Loop for creating table rows

    for($i = 0; $i < $max; $i++) {

?>

    <tr>

<?php

        // Loop for creating table cells

        foreach($output as $title => $names) {

?>

        <td><?php

             // Echo only if name exists

             if(isset($names[$i]['name'])) {

                echo $names[$i]['name'];

             }

        ?></td>

<?php

        } // end foreach

?>

    </tr>

<?php

    } // end for

?>

    </tbody>

</table>


查看完整回答
反對(duì) 回復(fù) 2022-07-09
  • 1 回答
  • 0 關(guān)注
  • 160 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)