how to draw a graphical bar line using MySql and PHP?

397 Views Asked by At

I am trying to show a graphical bar to show the available seats against total seats for a particular date in a single bar using MySql and PHP in a wordpress site.

My form mark up:

<strong>Check your desired date if it is available already in the system.</strong><br/><br/>
<form action="<?php echo get_permalink();?>" method="post" class="van_sharing_reference">
    <script type="text/javascript">
        jQuery(document).ready(function() {
            jQuery('.search_input_text_van').datepicker({
                dateFormat : 'yy-mm-dd'
            });
        });
    </script>
    <input type="text" placeholder="Select the date" required name="search_text" class="search_input_text_van">
    <span class="arrow">
        <input type="submit" value="CHECK" name="search" class="search_button">
    </span>
</form>

and the action:

<?PHP 
    if (isset($_POST['search'])){
    $serch_text = $_POST["search_text"]; 
    $con=mysqli_connect("localhost","db_name","db_username","db_password");
            // Check connection
            if (mysqli_connect_errno()) {
              die ("Failed to connect to MySQL: " . mysqli_connect_error());
            }       
        $booking_id_serch_text = $serch_text;
        $searchroute = "select * from van_sharing where date = '$booking_id_serch_text'";
        $result = $con->query($searchroute);
        $row = $result->fetch_assoc();
        if ($row == 0) { echo '<div style="color: red;">No one planned yet on the date <b>' . $serch_text . '</b>. to go</div>' ; 
        } else {
            // getting number of rows for a specific date
            $wpdb->get_results( $wpdb->prepare('select * from van_sharing where date = %s', $booking_id_serch_text));
            $total_seats = 10;
            $people_travel_on_the_date = $wpdb->num_rows;
            $available_seats = $total_seats - $people_travel_on_the_date;
            echo 'Total Seats ' . $total_seats . '<br/>';
            echo 'Number of people who are planning to share on the date '.$booking_id_serch_text .' : '.$wpdb->num_rows;

            echo '</br>Available seats : '.$available_seats;
            mysqli_close($con);
        }
    }
    ?>

Now I am getting all the details I want to show to the visitors.

  1. Total seats from $total_seats
  2. Number of people who are willing to share on the date from $people_travel_on_the_date
  3. And the balance / available seats from $available_seats

But rather than showing this in just some lines I want to show these in a graphical bar. Like:

  1. If there is no any one booked / not willing to share on the date. That means $people_travel_on_the_date == 0 Red
  2. If there are two or three people booked / willing to share on the date. That means $people_travel_on_the_date == 2 or 3 Green + red
  3. If there are 5 people booked / willing to share on the date. That means $people_travel_on_the_date == 5 Green + yellow
  4. If there are 10 people booked / willing to share on the date. That means $people_travel_on_the_date == 10 enter image description here

I googled and refered php.net but all the PHP drwaing was about to create charts, the whole chart like pie, or bar chart but not about to show these results in a graphical bar.

How can I create a bar as I explained above using php? It's a wordpress site. So better do it by custom PHP but not by using libraries. Also I am not willing to use plugins.

0

There are 0 best solutions below