Dear visitors. There is a question. The essence of the situation - you need to display data from the database in a table on the page. If there is an entry in the database, output the contents to a cell; if not, draw an empty cell in the table.

Now the data is drawn only by the query that returns, i.e. the data for which is in the database, the rest is ignored. I tried several output options.

There is another feature - the number of days that shows corresponds to the data from the database, and if you look at the source code of the page, then data about a specific day were added to the desired id, but again there are no empty cells.

Understand the problem: the arr array is multidimensional, associative. I use it with the wrong function and look for the value of $ id in it. Now another difficulty is how to find a dynamically changing $ id in a multidimensional associative array. Found several articles on this topic, but alas ...

I attach the screen. enter image description here

$id = strtoupper($info[$i]['id'])."-".$gluing_date; // дСлаю ΡƒΠ½ΠΈΠΊΠ°Π»ΡŒΠ½Ρ‹ΠΉ id для ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ элСмСнта <td>, Ρ‡Ρ‚ΠΎΠ±Ρ‹ Π·Π°ΠΏΠΈΡΡ‹Π²Π°Ρ‚ΡŒ Π΄Π°Π½Π½Ρ‹Π΅ Π² Π½ΡƒΠΆΠ½ΡƒΡŽ ячСйку $sql = "SELECT id, work_date, cause FROM month_detail WHERE id = '".strtoupper($info[$i]['id'])."' AND work_date = '".$gluing_date."' "; $query = mysqli_query($connect,$sql); //Π’Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ запроса Π² Π‘Π”; // Ρ†ΠΈΠΊΠ» отрисовки ячССк Ρ‚Π°Π±Π»ΠΈΡ†Ρ‹ $t=0; while ($row = mysqli_fetch_assoc($query)) { $arr[$t]['cause'] = $row['cause']; $arr[$t]['concat'] = $row['id']."-".$row['work_date']; if (in_array($id,$arr)){ //ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° $id ΠΊΠ»Π΅Ρ‚ΠΊΠΈ <td> Π² ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌΠΎΠΌ массивС, Ρ‡Ρ‚ΠΎΠ±Ρ‹ Π·Π°ΠΏΠΈΡΠ°Ρ‚ΡŒ ΠΊΠΎΠ½ΠΊΡ€Π΅Ρ‚Π½ΡƒΡŽ запись ΠΈΠΌΠ΅Π½Π½ΠΎ Π² этот id echo "<td class='day'; id='".$id."'> ".$arr[$t]['cause']." </td>"; // Ссли $id Π΅ΡΡ‚ΡŒ Π² массивС $arr, Ρ‚ΠΎ ΠΎΡ‚Ρ€ΠΈΡΠΎΠ²Π°Ρ‚ΡŒ <td> с содСрТимым ΠΈΠ· Π‘Π” } else { echo "<td class='day'; id='".$id."'> &nbsp; </td>"; // Ссли Π½Π΅Ρ‚ - Ρ€ΠΈΡΠΎΠ²Π°Ρ‚ΡŒ ΠΏΡƒΡΡ‚ΡƒΡŽ ΠΊΠ»Π΅Ρ‚ΠΊΡƒ } $t++; } 
  • output table? not displayed? what exactly works incorrectly? - Jurij Jazdanov
  • Now it turns out that it displays only those records that are in the database, the other cells simply do not draw - kstkt

3 answers 3

Have you watched the source of the page that is displayed? If there is

 <td></td> 

then empty cells are displayed, they are simply not visible in the browser.

Try to substitute some letter instead of a non-breaking space and see if it is displayed.

  • I watched the source, does not render. Adding characters instead of & nbsp; - does not help - kstkt
  • logically, in this case the condition else is not satisfied. Run the corresponding variables to the output via var_dump (), somewhere an empty variable or a non-working condition will be revealed. - labris
  • I realized the trouble, my arr is a multidimensional associative array, and in_array does not work as it should. Those. I need to find the dynamic variable $ id in a multidimensional associative array - kstkt
  • var_dump () always opens a lot of unexpected :) - labris

Set the width and height of cells with css, you can set the radius. And it will work <td style="border: 1px solid red; width: 50px; height: 50px;">&nbsp;</td> iron draws a cell

  • does not work, does not display data - kstkt
  • You asked to draw an empty cell. This is for her - Konstantin Efimov
  • Konstantin, I perfectly understood what you wrote, however, if you pay attention, then in the code I gave there is a similar line, this class simply does not have descriptions of the style in the php code. The problem was different. - kstkt

The selection, assignment and logic are logically correct. Incorrectly matched function that searches for values ​​in the array.

In condition

 if (in_array($id,$arr)){ //ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° $id ΠΊΠ»Π΅Ρ‚ΠΊΠΈ <td> Π² ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌΠΎΠΌ массивС, Ρ‡Ρ‚ΠΎΠ±Ρ‹ Π·Π°ΠΏΠΈΡΠ°Ρ‚ΡŒ ΠΊΠΎΠ½ΠΊΡ€Π΅Ρ‚Π½ΡƒΡŽ запись ΠΈΠΌΠ΅Π½Π½ΠΎ Π² этот id echo "<td class='day'; id='".$id."'> ".$arr[$t]['cause']." </td>"; // Ссли $id Π΅ΡΡ‚ΡŒ Π² массивС $arr, Ρ‚ΠΎ ΠΎΡ‚Ρ€ΠΈΡΠΎΠ²Π°Ρ‚ΡŒ <td> с содСрТимым ΠΈΠ· Π‘Π” } else { echo "<td class='day'; id='".$id."'> &nbsp; </td>"; // Ссли Π½Π΅Ρ‚ - Ρ€ΠΈΡΠΎΠ²Π°Ρ‚ΡŒ ΠΏΡƒΡΡ‚ΡƒΡŽ ΠΊΠ»Π΅Ρ‚ΠΊΡƒ } 

The use of the in_array function is unacceptable, since the search is performed in a multidimensional associative array. Therefore, the described condition is not satisfied.