Good afternoon. All, I ask for help .. I can not correctly display the data in the table.
There are three tables in the database:
city(id, name)week_days(id, name)report(id, city_id, week_day_id, value)
Content of the report table:
1 | 1 | 1 | 10 2 | 1 | 2 | 08 3 | 1 | 3 | 05 4 | 1 | 1 | 02 There is a code that pulls out this data:
$db = new PDO('mysql:host=localhost;dbname=test','root',''); $getDays = $db->prepare("SELECT * FROM week_days");//Достаем дни недели $getDays->execute(); $resultDays = array(); $resultDays = $getDays->fetchAll();//Создаем массив дней $getCity = $db->prepare("SELECT * FROM city");//Достаем города $getCity->execute(); $resultCity = array(); $resultCity = $getCity->fetchAll();//Создаем массив городов $getTables= $db->prepare("SELECT r.*, c.name as city_name FROM report as r INNER JOIN city as c ON(r.city_id = c.id) INNER JOIN week_days as wd ON(r.week_day_id = wd.id) "); $getTables->execute(); $result = $getTables->fetchAll(); $resultTables =$result; $result1=$result; for($i=0;$i<count($resultTables);$i++){ $ress[$i]=$resultTables[$i]; foreach($resultDays as $day){ $ress[$i]["days"][$day['id']]=""; if($day['id'] == $resultTables[$i]['day']){ $ress[$i]["days"][$day['id']]=$resultTables[$i]['value']; }else{ foreach($result1 as $kr=>$table1){ if($day['id']==$table1['week_day_id'] && $table1['city_id']==$resultTables[$i]['city_id'] && $resultTables[$i]['id'] != $table1['id'] ){ $ress[$i]["days"][$day['id']]=$table1['value']; } } } } } <!-- Main jumbotron for a primary marketing message or call to action --> <div class="jumbotron"> <div class="container"> <div class="row"> <div class="col-lg-9"> <h1>Таблица</h1> </div> <div class='col-lg-3'> <div class="thumbnail"> <div class="caption"> <h3>Добавить В Календарь</h3> <p><select class='filial form-control'> <option value='0'>--Филиал--</option> <?php if(!empty($resultCity )){//если массив не пустой?> <?php foreach($resultCity as $city){?> <option value="<?=$city['id']?>"><?=$city['name']?></option> <?php }?> <?php }?> </select></p> <p><select class='day form-control'> <?php if(!empty($resultDays)){//если массив не пустой?> <?php foreach($resultDays as $day){?> <option value="<?=$day['id']?>"><?=$day['name']?></option> <?php }?> <?php }?> </select></p> <p><input type='text' class='time_fil form-control' placeholder='Время'></p> <p><button class="save_this btn btn-primary" role="button">Добавить</button> </p> </div> </div> </div> </div> </div> </div> <div class="container"> <!-- Example row of columns --> <div class="row"> <div class="col-lg-12"> <h2>Календарь</h2> <table class='table table-striped'> <thead> <tr> <th>Филиал</th> <?php if(!empty($resultDays)){//если массив не пустой?> <?php foreach($resultDays as $day){?> <th><?=$day['name']?></th> <?php }?> <?php }?> </tr> </thead> <tbody> <?php if(!empty($ress)){?> <?php foreach($ress as $table){?> <tr> <td><?=$table['id'].")".$table['city_name']?></td> <?php if(!empty($table['days'])){ foreach($table['days'] as $day){?> <td><?=$day?></td> <?php } ?> </tr> <?php }}?> <?php } ?> </tbody> </table> </div> </div> With this request, the table will be released:
ГОРОД | Понедельник | Вторник | Среда | Четверг | Пятница | Суббота | Воскресенье Москва | 10 | | | | | | | Москва | | 08 | | | | | | Москва | | | 05 | | | | | Москва | 02 | | | | | | | And this is not correct, it is necessary to get this:
ГОРОД | Понедельник | Вторник | Среда | Четверг | Пятница | Суббота | Воскресенье Москва | 10 | 08 | 05 | | | | | Москва | 02 | | | | | | | Tell me, please, I'm already fighting more than one day ...