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>
1
  • 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. Commented Jun 21, 2018 at 11:46

1 Answer 1

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>
}
Sign up to request clarification or add additional context in comments.

8 Comments

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.
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.
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 '{'
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
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); }
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.