2014-11-22 1 views
-3

Мне нужно сделать, чтобы значения в списке $ отображались в отдельных столбцах, а заголовки столбцов находились внутри таблицы в отдельной строке.

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)) 
+0

что/где проблема ур/вопрос? – user3510665

+0

данные отображаются некорректно в таблице xhtml :( – harris

ответ

0

Хорошо, что касается ваших комментариев ниже я готовлю вам пример с 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>'; 
+0

okay i обновил мою и не отобразил, но никаких ошибок в основном, если флажок «echo» '; отмечен галочкой, все скрытые тоже должны указывать так, чтобы он представлял все элементы. Когда я делаю print_r ($ _ POST), он отображает только последнюю строку таблицы. – harris

+0

это только работает со всеми значениями в одном флажке, поэтому я держу его таким образом, просто пытаясь заставить его отделить с взрывом, сложно, потому что я получаю сообщение об ошибке, указывающее, что его массив не является строкой – harris

Смежные вопросы