0

Currently, I'm working on a project that requires displaying data in a timeline view and I'm using FullCalendar plugin for that. I used PHP and MySQL to fetch data that need to be displayed from database.

This is the js script

<script>
document.addEventListener('DOMContentLoaded', function() {
var calendarEl = document.getElementById('calendar');

var calendar = new FullCalendar.Calendar(calendarEl, {
  timeZone: 'UTC',
  aspectRatio: 1.5,
  scrollTime: '00:00',

  headerToolbar: {
    left: 'prev,next',
        center: 'title',
        right: 'resourceTimelineDay,resourceTimelineThreeDays,resourceTimelineWeek,resourceTimelineMonth'
  },
  initialView: 'resourceTimelineDay',
  
  views: {
    resourceTimelineThreeDays: {
      type: 'resourceTimeline',
      duration: { days: 3 },
      buttonText: '3 days'
    }
  },
  resourceAreaWidth: '15%',
  resourceAreaHeaderContent: 'Components',

  resources:  
  [
    
    { id: '1', title: 'Company', eventColor: 'orange' },
    { id: '3', title: 'Driver', eventColor: 'purple' },
    { id: '4', title: 'Summon', eventColor: 'red' },
    { id: '5', title: 'Summon Tracking', eventColor: 'green' }
  ],
  
  events: 
  [
    
   
   <?php  echo implode(",",$company); ?>,
   <?php  echo implode(",",$driver); ?>,
   <?php  echo implode(",",$summon); ?>,
   <?php  echo implode(",",$tracking); ?>
    
    ]
});

calendar.render();

});

And this is the php code I used to change color of event

  // Summon Tracking
      

        $sql = $connection->prepare("SELECT * FROM saman 
        WHERE vehicle_no = '".$_POST['vehicle']."'");
        $sql->execute();

        $result_track = $sql->get_result();
        $i = 1;
        
        $count_track = $result_track->num_rows;
        if($count_track > 0){
        
        while($row = $result_track->fetch_assoc())
        {
          $sqlfound = "SELECT status, driver_name from saman WHERE status='Found'";
          $sqlnotfound = "SELECT status, driver_name from saman WHERE status='Not Found'";

          if($result = mysqli_query($connection, $sqlfound)){

        $tracking[] = " { id: '".$i."', 
            resourceId: '5', 
            description: 'description for All Day Event',
            start: '".$row['datetime']."', 
            end: '".$row['datetime']."', 
            title: '".$row['status']."' }";

            // echo "<pre>";
            // print_r($trackingresource);
            // echo "</pre>";

            $i++;
        }else if($result = mysqli_query($connection, $sqlnotfound)){
          $tracking[] = " { id: '".$i."', 
            resourceId: '5', 
            start: '".$row['datetime']."', 
            end: '".$row['datetime']."', 
            title: '".$row['status']."' }";


            $i++;
        }
        }
      }else 
      {
          $tracking[] = " { id: '', 
              resourceId: '5', 
              start: '', 
              end: '',   
              title: '' }";
      }


        $sql->close();
    }

What I want to do is something like this but in timeline view.

enter image description here

Yamin
  • 9
  • 1
  • 5
  • **Warning:** Your code is vulnerable to SQL Injection attacks. You should use prepared statements **and parameters** to help prevent attackers from compromising your database by using malicious input values. http://bobby-tables.com gives an explanation of the risks, as well as some examples of how to write your queries safely using PHP / mysqli. **Never** insert unsanitised data directly into your SQL. The way your code is written now, someone could easily steal, incorrectly change, or even delete your data. – ADyson Dec 01 '22 at 09:57
  • https://phpdelusions.net/mysqli also contains good examples of writing safe SQL using mysqli. See also the [mysqli documentation](https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php) and this: [How can I prevent SQL injection in PHP?](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) . Parameterising your queries will also greatly reduce the risk of accidental syntax errors as a result of un-escaped or incorrectly quoted input values. If you learnt your current technique from a tutorial or book, please don't use it again. – ADyson Dec 01 '22 at 09:57
  • I also highly recommend not generating JSON by hand...it's very error prone potentially, and fiddly to write. Instead, build your data structure in a PHP object/array and then use `json_encode()` to turn it into valid JSON at the end - that's much more reliable, simplier, and results in more readable, maintainable and debuggable code. – ADyson Dec 01 '22 at 09:58
  • `this is the php code I used to change color of event`...I don't see anything in there which affects the colour of an event. Which bit do you think is supposed to do that? It's unclear what you think the problem is. As far as I can see, you just haven't attempted it. If you want to set the colour of an individual event, set one or more of its colour properties when you're generating the event data, as defined in the documentation: https://fullcalendar.io/docs/event-parsing – ADyson Dec 01 '22 at 09:59
  • You're setting the colour of events in each resource, obviously - but presumably you want to do more than that? You mentioned changing the colour based on a condition, but didn't tell us what that condition is, didn't show any code where you attempt to implement that, and didn't explain where you're stuck. So it's really difficult to help you because there is no clear requirement and no clear problem. See also [ask] and how to make a [mre] of your issue. – ADyson Dec 01 '22 at 10:06
  • Thank you for your response @ADyson I just notice that there are backgroundColor properties for events. My timeline is working fine now as I wanted. Also thanks for the information about SQL injection :) – Yamin Dec 02 '22 at 07:37

0 Answers0