【问题标题】:PHP get $list to display each value in seperate columnsPHP 获取 $list 以在单独的列中显示每个值
【发布时间】:2014-11-22 11:05:53
【问题描述】:

我需要做的是让 $list 中的值显示在单独的列中,并让列的标题在表内的单独行中显示。

   if (isset($_POST['check_list'][0])) {
    // form was submitted, checkbox was checked
     ?>
     <table>
     <table border='1'>
    <tr>
        <th>Artist</th>
        <th>Composer</th>
        <th>Genre</th>
        <th>Title</th>
        <th>Album</th>
        <th>Label</th>
        <th>Price</th>
        <th>Description</th>
    </tr>
    <tr>
   <?php
    foreach ($_POST['check_list'] as $item) {
        echo '<td>' . $item . '</td>'; // here we add the table row and fill there all data from    $getColumn array, each one has own table cell
    }
    echo '</table>';
  } else if (isset($_POST['submit'])) {
     // form was submitted, checkbox wasn't checked
    echo 'Form was submitted and checked wasn\'t checked';
    }
    ?>
    </table>

数据库页面:

    print '<input type="hidden" name="checkbox1" value="'. $getColumn[1].'" />';
    print '<input type="hidden" name="checkbox2" value="'. $getColumn[2].'" />';
    print '<input type="hidden" name="checkbox3" value="'. $getColumn[3].'" />';
    print '<input type="hidden" name="checkbox4" value="'. $getColumn[4].'" />';
    print '<input type="hidden" name="checkbox5" value="'. $getColumn[5].'" />';
    print '<input type="hidden" name="checkbox6" value="'. $getColumn[6].'" />';
    print '<input type="hidden" name="checkbox7" value="'. $getColumn[7].'" />';
    print '<input type="hidden" name="checkbox8" value="'. $getColumn[8].'" />';
    print '<input type="hidden" name="checkbox8" value="'. $getColumn[9].'" />';
    print '<td><input type="checkbox" name="check_list[]"value="'. $getColumn[0].'"</td>';

连接:

  $conn = pg_connect("host=**** port=****
  dbname=teaching user=csguest password=****);

    $res = pg_query ($conn, "SELECT ref, artist, composer, genre, title, album, label, price,  description FROM music");
    print "<table border='1'>";
    print "<th>Check box</th><th>Artist</th><th>Composer</th><th>Genre</th><th>Title</th><th>Album</th><th>Label</th><th>Price</th><th>Description</th></tr>";
    while($getColumn = pg_fetch_row($res))

【问题讨论】:

  • 你的问题/疑问是什么/在哪里?
  • xhtml 表格中的数据显示不正确 :(

标签: php foreach xhtml html-table


【解决方案1】:

好的,关于下面的 cmets,我也为你准备了 PHP 和 HTML 的示例。

您可以尝试发送表单。如果您选中复选框,则会呈现带有值的表格,在另一种情况下,您会收到未选中复选框的消息。只是为了让您了解它是如何工作的。

<?php

// this array you have rendered and filled elsewhere, it's just for this exaple
$getColumn = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j');

if (isset($_POST['check_list'][0])) {
    // form was submitted, checkbox was checked
?>
    <table>
        <tr>
            <th>Artist</th>
            <th>Composer</th>
            <th>Genre</th>
            <th>Title</th>
            <th>Album</th>
            <th>Label</th>
            <th>Price</th>
            <th>Description</th>
            <th>Last one</th><!-- in your question you have only these 8 THs, in form you have 9 values. In this case it's just to have the same THs and TDs -->
        </tr>
        <tr>
<?php
        foreach ($_POST['check_list'] as $item) {
            echo '<td>' . $item . '</td>'; // here we add the table row and fill there all data from $getColumn array, each one has own table cell
        }
    echo '</table>';
} else if (isset($_POST['submit'])) {
    // form was submitted, checkbox wasn't checked
    echo 'Form was submitted and checked wasn\'t checked';
}

// and the simply form for testing
echo '<form method="post">';
    echo '<input type="checkbox" name="check_list[0]" value="' . $getColumn[0] . '">';
    echo '<input type="hidden" name="check_list[1]" value="' . $getColumn[1] . '">';
    echo '<input type="hidden" name="check_list[2]" value="' . $getColumn[2] . '">';
    echo '<input type="hidden" name="check_list[3]" value="' . $getColumn[3] . '">';
    echo '<input type="hidden" name="check_list[4]" value="' . $getColumn[4] . '">';
    echo '<input type="hidden" name="check_list[5]" value="' . $getColumn[5] . '">';
    echo '<input type="hidden" name="check_list[6]" value="' . $getColumn[6] . '">';
    echo '<input type="hidden" name="check_list[7]" value="' . $getColumn[7] . '">';
    echo '<input type="hidden" name="check_list[8]" value="' . $getColumn[8] . '">';
    echo '<input type="submit" name="submit">';
echo '</form>';

【讨论】:

  • 好的,我更新了我的,它没有显示,但基本上没有错误,当复选框 echo '';已勾选所有隐藏的也需要勾选,因此它提交所有元素。当我做 print_r($_POST);它只显示表格的最后一行。
  • 它只适用于一个复选框中的所有值,所以我保持这种方式只是试图让它与爆炸分开很难,因为我收到一个错误,说它是一个数组而不是一个字符串
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-04
相关资源
最近更新 更多