Good afternoon, dear users. Please help in the situation.

I am writing a page for entering information about employee schedules. The number of tables depends on the number of sectors in which employees are registered. The number of entries in each table corresponds to the number of employees in the sector. The number of columns in the table depends on the month, i.e. how many days, so many columns.

On the page in the pop-up window, information on a specific day is entered, transferred to the handler using AJAX, data is written to the database (there are no problems here), the problem is that it is necessary each time the page is refreshed to color the table cell with a certain color depending on the selected parameters i.e. I should add an id to the td with the class .day in the format: TABLE_NUMER_YE_MODIC_DATE and then on a specific id that contains the value I need, I will color the cells using JQuery.

Please help in solving the problem. Code for dynamic table rendering:

//Выбор всех операторов, сортировка по рабочему сектору $sql = "SELECT t1.tabNo, t1.sector, t2.otdelname, t1.name, t3.date, t3.cause FROM operators as t1 JOIN otdel as t2 on t1.napravlenie = t2.id LEFT JOIN day_detail as t3 on t1.tabNo= t3.tabNo WHERE rol = '1' AND admin = 0 ORDER BY sector, name ASC "; $query = mysqli_query($connect,$sql); //Выполнение запроса в БД; //Результат выполнения загоняем в массив peopInfo $i=0; $j=0; $sector1 = -1; $cols = $countDay; // количество столбцов, td - Определяется динамически, зависит от месяца (сколько дней, столько столбцов) while ($auxiliaryArr = mysqli_fetch_assoc($query)) { //массив peopleInfo содержит данные: табельный номер оператора, имя оператора, № сектора, имя сектора, дату по которой вносится информация, причину изменения записи $peopleInfo[$i]['tabNo'] = $auxiliaryArr['tabNo']; $peopleInfo[$i]['name'] = $auxiliaryArr['name']; $peopleInfo[$i]['sector'] = $auxiliaryArr['sector']; $peopleInfo[$i]['otdel'] = $auxiliaryArr['otdel']; $peopleInfo[$i]['date'] = $auxiliaryArr['date']; $peopleInfo[$i]['cause'] = $auxiliaryArr['cause']; if ($auxiliaryArr['sector'] != $sector1) { if ($i!=0) { echo '</table>'; } echo '<table class = "tableEmployee" ; <colgroup> <col width = "400px"; > </colgroup>'; echo "<br>"; echo '<caption id="sector";> '.$peopleInfo[$j]['otdel'].' </caption>'; echo '<tr> <td id="fio"; rowspan = "2";> ФИО </td> <td id="tabNo"; rowspan="2";> Табельный номер </td> <td id="day"; colspan="31"; style="background:#fffff";> Дата </td> </tr>'; //Выводим в таблицу строку с датой (прим.: 01, 02, 03) $dateArr = Array(); //Вспомогательный массив $d = 1; //Нумерация дня с 1-го числа каждого месяца while ($d <= $countDay): $dateArr[$d]['day'] = $d; echo '<td id="chislo"> '.htmlspecialchars($dateArr[$d]['day']).' </td> '; $sector1= $auxiliaryArr['sector']; //присвоение "пустой" переменной значения id сектора из массива $auxiliaryArr $d++; endwhile; //цикл динамичной отрисовки таблиц } echo "<tr>"; for ($td=1;$td<$cols+2;$td++) { $id[$j] = strtoupper($peopleInfo[$j]['tabNo']); if($td == 1) { echo "<th class='fio'; title='Внести информацию по сотруднику'>".$peopleInfo[$j]['name']."</th>"; echo "<th class='login';> ".strtoupper($peopleInfo[$j]['tabNo'])." </th>"; } else { echo "<td class='day' >".substr($peopleInfo[$j]['cause'],0,1)."</td>"; } } echo "</tr>"; $i++; $j++; } echo "</table>"; 

Scrolling through the months and, accordingly, changing the number of columns is done using a function in the same file:

 <?PHP $countDay = my_calendar(array(date("Ymd"))); function my_calendar($fill=array()){ $month_names=array("Январь","Февраль","Март","Апрель","Май","Июнь","Июль","Август","Сентябрь","Октябрь","Ноябрь","Декабрь"); if (isset($_GET['y'])) $y=$_GET['y']; if (isset($_GET['m'])) $m=$_GET['m']; if (!isset($y) OR $y < 1970 OR $y > 2037) $y=date("Y"); if (!isset($m) OR $m < 1 OR $m > 12) $m=date("m"); $month_stamp=mktime(0,0,0,$m,1,$y); $prev=date('?\m=m&\y=Y',mktime (0,0,0,$m-1,1,$y)); $next=date('?\m=m&\y=Y',mktime (0,0,0,$m+1,1,$y)); $i=0; ?> <table class="tableMonth"> <tr> <td> <table> <tr> <td align="left"><a href="<? echo $prev . " " ?>"><<<&nbsp;&nbsp;</a></td> <td id="month_year" align="center"><? echo $m.".".$y ?></td> <td align="right"><a href="<? echo $next ?>">&nbsp;&nbsp;>>></a></td> </tr> </table> </td> </tr> </table> <?PHP echo '<hr>'; if ($m == 4 || $m == 6 || $m == 9 || $m == 11){ $countdays = 30; return $countdays; } elseif ($m == 2){ $countdays = 28; return $countdays; } else { $countdays = 31; return $countdays; } } ?> 
  • one
    Your text does not understand the problem. Write, please, more clearly, 1) what you are doing, 2) what effect is required to get, 3) what effect you actually get. - a-bobkov
  • I open a modal window for a specific employee for a specific number, I enter the data. Then the data is recorded in the database, when the page is updated, it is necessary that the information be recorded strictly in the cell by which it was clicked. In fact, it turns out that no matter by which cell the information was entered, it is recorded in all cells by this employee, and not limited to one day - kstkt

2 answers 2

That you can not?

 $id=$peopleInfo[$j]['tabNo'].'_'.$year.'_'.$mounth.'_'.($td-4); echo "<td class='day' id='$id'>".substr($peopleInfo[$j]['cause'],0,1)."</td>"; 

Theoretically, ID is not needed in principle. You can easily find out which cell was clicked:

 $('#table td').click(function(){ //определяем координаты ячейки: var x=$(this).index()+1, y=$(this).closest('tr').index()+1; console.log('Кликнули ячейку ',x,y) //используя координаты находим нужный с помощью jQuery: $('#table tr:nth-child('+y+') td:nth-child('+x+')').toggleClass('active') }) 
 #table {width: 100%} #table td { border: 1px solid #ccc} #table td.active { border: 1px solid red} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id=table> <tr> <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td> </tr> <tr> <td>&nbsp;</td><td></td><td></td><td></td> </tr> <tr> <td>&nbsp;</td><td></td><td></td><td></td> </tr> <tr> <td>&nbsp;</td><td></td><td></td><td></td> </tr> </table> 

  • $ id = $ peopleInfo [$ j] ['tabNo']. ' '. $ year.' '. $ mounth .'_'. $ td-4; echo "<td class = 'day' id = '$ id'>". substr ($ peopleInfo [$ j] ['cause'], 0,1). "</ td>"; This construct, unfortunately, does not work, writes cell IDs as -4. And regarding the example with jQuery, thanks, but as far as I understand, the changes made on the page will not be fixed. I thought to implement this, but I faced the fact that it is necessary to record information strictly according to the day_month_god_otrudniku, but after updating the page, I will “paint” these cells on JQuery. - kstkt
  • @kstkt was wrong, ($td-4) minus 4 had to be bracketed - Crantisz
  • Yes, I fixed it on ($ td-1), that is necessary, but now the month and the year ... - kstkt

Solved.

In the function that draws the scrolling of the date, it is necessary to return not only the number of days of the month, but also the month + year.

 <?php if ($m == 4 || $m == 6 || $m == 9 || $m == 11){ $d = 30; return array($d,$m,$y); } elseif ($m == 2){ $d = 28; return array($d,$m,$y); } else { $d = 31; return array($d,$m,$y); } 

}?>

Then when sticking the elements of the array:

 $gluing_date = $countDay[2]."-".$countDay[1]."-".($td-1); $gluing_date_to_id = date("Ymd", strtotime($gluing_date)); $id = strtoupper($peopleInfo[$j]['tabNo'])."-".$gluing_date_to_id; 

And then the ready id to insert the variable into the div element

 echo "<td class='day'; id='".$id."'>".substr($peopleInfo[$j]['cause'],0,1)."</td>";