R
R
Roman Sergeev2017-05-16 21:38:08
PHP
Roman Sergeev, 2017-05-16 21:38:08

How to display a table on a page from a database?

There is a code:

<?php
$db_host = 'localhost';
$db_name = 'schedule';
$db_username = 'root';
$db_password = '';
$db_table_to_show = 'Exam_schedule';

$mysqli = new mysqli($db_host, $db_username, $db_password, $db_name);
//------------------------------------------------------
$mysqli->query('SET NAMES utf8');
$result = $mysqli->query('SELECT * FROM ' . $db_table_to_show );
$qr_results = array(
  'Дисциплина' => array(),
  'Дата экзамена' => array(),
  'Время экзамена' => array(),
  'Аудитория' => array(),
  'Преподаватель' => array()
);
$mon_rows = 0; $tue_rows = 0;$thu_rows = 0;$wed_rows = 0;$fri_rows = 0;
while ($row = $result->fetch_assoc()){
    switch($row['discipline']){
      case 'Дисциплина':
        $qr_results['Дисциплина'][] = $row;
        $mon_rows++;
        break;
      case 'Дата экзамена':
        $qr_results['Дата экзамена'][] = $row;
        $tue_rows++;
        break;
      case 'Время экзамена':
        $qr_results['Время экзамена'][] = $row;
        $wed_rows++;
        break;
      case 'Аудитория':
        $qr_results['Аудитория'][] = $row;
        $thu_rows++;
        break;
      case 'Преподаватель':
        $qr_results['Преподаватель'][] = $row;
        $fri_rows++;
        break;
      default:
        break;
    }
    
}
$rows = max($mon_rows,$tue_rows,$thu_rows,$wed_rows,$fri_rows);
?>


<style type="text/style.css" media="all"> body { margin: 0; padding: 0;}  </style>
<table  align="center" border="1" cellpadding="1" cellspacing="1" height="200" width="650">
<thead>
<tr>
<th><b>Дисциплина</b></th>
<th><b>Дата экзамена</b></th>
<th><b>Время экзамена</b></th>
<th><b>Аудитория</b></th>
<th><b>Преподаватель</b></th>
</tr>
</thead>
<tbody>
<?php for($i=0;$i<$rows;$i++){ ?>
  <tr>
    <?php if(isset($qr_results['Дисциплина'][$i])){ ?> 
    <td> <?php echo $qr_results['Дисциплина'][$i]['discipline']; ?><br><?php echo $qr_results['discipline'][$i]['discipline']; ?><br><?php echo $qr_results['Дисциплина'][$i]['discipline']; ?></td>
    <?php }else{ ?>
    <td> - </td>
    <?php } ?>
    <?php if(isset($qr_results['Вторник'][$i])){ ?> 
    <td> <?php echo $qr_results['Вторник'][$i]['subject']; ?><br><?php echo $qr_results['Вторник'][$i]['classroom']; ?><br><?php echo $qr_results['Вторник'][$i]['teacher']; ?></td>
    <?php }else{ ?>
    <td> - </td>
    <?php } ?>
    <?php if(isset($qr_results['Среда'][$i])){ ?> 
    <td> <?php echo $qr_results['Среда'][$i]['subject']; ?><br><?php echo $qr_results['Среда'][$i]['classroom']; ?><br><?php echo $qr_results['Среда'][$i]['teacher']; ?></td>
    <?php }else{ ?>
    <td> - </td>
    <?php } ?>
    <?php if(isset($qr_results['Четверг'][$i])){ ?> 
    <td> <?php echo $qr_results['Четверг'][$i]['subject']; ?><br><?php echo $qr_results['Четверг'][$i]['classroom']; ?><br><?php echo $qr_results['Четверг'][$i]['teacher']; ?></td>
    <?php }else{ ?>
    <td> - </td>
    <?php } ?>
    <?php if(isset($qr_results['Пятница'][$i])){ ?> 
    <td> <?php echo $qr_results['Пятница'][$i]['subject']; ?><br><?php echo $qr_results['Пятница'][$i]['classroom']; ?><br><?php echo $qr_results['Пятница'][$i]['teacher']; ?></td>
    <?php }else{ ?>
    <td> - </td>
    <?php } ?>
  </tr>
<?php } ?>
</tbody>
</table>

c3b788617e1d47fcbdafb5d77e602a67.jpg
Displays only the title, but no cell
ef4c76f1e834470da953acf6952d6cf0.jpg
What's the problem?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question