0

I would like to print days of month and highlight the days witch are same like dates in my MySQL table.

There are rows of runs in table (day of run, distance, speed ...) and I would like to highlight days of month when I run.

My code:

$user="1";
$dbmonth="201806";
$month = date('m');
$year = date('Y');
$days = date('t');

for($d=1; $d<=$days; $d++)
{
$time=mktime(12, 0, 0, $month, $d, $year);
if (date('m', $time)==$month)
$list=date('Y-m-d', $time);

$select1cal = mysqli_query($conn, "SELECT * FROM runs WHERE user=$user AND dbmonth=$dbmonth ORDER BY date");
while($data=mysqli_fetch_array($select1cal)) {
$dateid1=$data['date'];
$event1=date('Y-m-d', $dateid1);

if ($list == $event1) {$show="<p style=\"color:#0f0;\">$list</p>";} else {$show="<p>$list</p>";}
}
echo $show;
}

This code print all days of month (2018-06-01 to 2018-06-30) but highlighted is only one date. How can I highlight all of dates from table? For example there are 4 rows with date between 2018-06-01 and 2018-06-30 in my table.

Here is output of my code:

<p>2018-06-01</p>
<p>2018-06-02</p>
<p>2018-06-03</p>
<p>2018-06-04</p>
<p>2018-06-05</p>
<p>2018-06-06</p>
<p>2018-06-07</p>
<p>2018-06-08</p>
<p>2018-06-09</p>
<p>2018-06-10</p>
<p>2018-06-11</p>
<p>2018-06-12</p>
<p style="color:#0f0;">2018-06-13</p>
<p>2018-06-14</p>
<p>2018-06-15</p>
<p>2018-06-16</p>
<p>2018-06-17</p>
<p>2018-06-18</p>
<p>2018-06-19</p>
<p>2018-06-20</p>
<p>2018-06-21</p>
<p>2018-06-22</p>
<p>2018-06-23</p>
<p>2018-06-24</p>
<p>2018-06-25</p>
<p>2018-06-26</p>
<p>2018-06-27</p>
<p>2018-06-28</p>
<p>2018-06-29</p>
<p>2018-06-30</p>
Martin
  • 65
  • 1
  • 8
  • Your while loop only goes over the data once - in your second for loop iteration, mysqli_fetch_array returns false at that point right away. You would have to reset the record pointer first, to be able to loop over the same database result again this way. But you should really rather put this data into an array beforehand in the first place. – CBroe Jun 21 '18 at 11:46

1 Answers1

2

Try add all db records to array:

$runs = array();
$select1cal = mysqli_query($conn, "SELECT * FROM runs WHERE user=$user AND dbmonth=$dbmonth ORDER BY date");
while ($data=mysqli_fetch_array($select1cal)) {
    $dateid1=$data['date'];
    $event1=date('Y-m-d', $dateid1);

    array_push($runs, $event1);
}

Then make a loop after all days and check if the day is in array.

if (in_array($list, $runs)) {
    $show="<p style=\"color:#0f0;\">$list</p>";
} else {
    $show="<p>$list</p>
}
webmazz
  • 79
  • 3
  • Waw man, Webmazz, thanks a lot. You solve the problem. I work on it 2 days and try more variants, but do not find solution. Thank you. – Martin Jun 21 '18 at 12:19
  • One more question. How can I bring to this more info from table? I need to bring info about runned distance `$distance1=$data['distance'];` I tried add one more ARRAY `array_push($dist, $distance1);` or expand original ARRAY `array_push($runs, $event1, $distance1);` but it always give me back just first value of distance, not all runned distances. – Martin Jun 22 '18 at 11:34
  • Yes, I would like to display distance next to the selected day. Thank you for helping me, but this code the page do not accept `syntax error, unexpected '{'` – Martin Jun 22 '18 at 12:06
  • Yes, i find missing `}` :) but still same syntax error is show. I try `array_push($runs, $event1); array_push($runs, $distance1);` but it gives me same value for all selected days ... [link](http://cursor.richiedesign.sk/x/dni1.php) – Martin Jun 22 '18 at 12:18
  • Try this: `while ($data=mysqli_fetch_array($select1cal)) { $dateid1=$data['date']; $event1=date('Y-m-d', $dateid1); $distance1=$data['distance']; $run = (object) [ 'date' => $event1, 'distance' => $distance1 ]; array_push($runs, $run); }` – webmazz Jun 22 '18 at 12:24
  • I try it, but now it do not work at all. I have got just `$list` without highlighted (selected) days – Martin Jun 22 '18 at 12:42
  • sorry I have no idea how this work ... can U see my code here? [link](https://dpaste.de/jUqE) – Martin Jun 22 '18 at 12:55
  • Hi Webmazz. Even this code do not show selected day and distance [link](http://cursor.richiedesign.sk/x/dni1.php) but never mind. I decide to go by another (easer) way, just highlight runned days of month [link](http://cursor.richiedesign.sk/x/dni2.php). Thank you very much for your help. – Martin Jun 25 '18 at 12:45