• We just launched and are currently in beta. Join us as we build and grow the community.

How to Create Graph using Database Driven in PHP/MySQL

betTD

Event Loop Designer
B Rep
0
0
0
Rep
0
B Vouches
0
0
0
Vouches
0
Posts
96
Likes
86
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial, we are going to learn how to create Graph using Database Driven in PHP/MySQL. This tutorial helps you make a simple graph and your data came from your database. This simple graph shows the different kind of motorcycle and their corresponding quantity. We are going to use jQuery and PHP/MySQL to create this simple graph.

Creating our Database Connection
  1. <?php

  2. mysql_select_db

    (

    'product'

    ,

    mysql_connect

    (

    'localhost'

    ,

    'root'

    ,

    ''

    )

    )

    or die

    (

    mysql_error

    (

    )

    )

    ;

  3. ?>

This script is used to display the quantity of different kinds of motorcycle.
  1. <

    script type=

    "application/javascript"

    >
  2. var

    motorcycle_chart =

    new

    AwesomeChart(

    'motorcycle_graph'

    )

    ;
  3. motorcycle_chart.data

    =

    [
  4. <?

    php
  5. $query =

    mysql_query(

    "select * from product"

    )

    or die(

    mysql_error(

    )

    )

    ;
  6. while (

    $row =

    mysql_fetch_array(

    $query)

    )

    {
  7. ?>
  8. <?

    php echo $row[

    'Qty'

    ]

    . ','

    ;

    ?>
  9. <?

    php }

    ;

    ?>
  10. ]

    ;
  11. </

    script>

Next, this script is to display the name of different kinds of motorcycle.
  1. <

    script type=

    "application/javascript"

    >
  2. motorcycle_chart.labels

    =

    [
  3. <?

    php
  4. $query =

    mysql_query(

    "select * from product"

    )

    or die(

    mysql_error(

    )

    )

    ;
  5. while (

    $row =

    mysql_fetch_array(

    $query)

    )

    {
  6. ?>
  7. <?

    php echo "'"

    . $row[

    'name'

    ]

    . "'"

    . ','

    ;

    ?>
  8. <?

    php }

    ;

    ?>
  9. ]

    ;
  10. </

    script>

To change the color of the graph, this is the sour code to help you to change one by one.
  1. <

    script type=

    "application/javascript"

    >
  2. motorcycle_chart.colors

    =

    [

    'gold'

    ,

    'skyblue'

    ,

    '#FF6600'

    ,

    'pink'

    ,

    'darkblue'

    ,

    'lightpink'

    ,

    'green'

    ]

    ;
  3. motorcycle_chart.randomColors

    =

    true

    ;
  4. motorcycle_chart.animate

    =

    true

    ;
  5. motorcycle_chart.animationFrames

    =

    20

    ;
  6. motorcycle_chart.draw

    (

    )

    ;
  7. </

    script>

The complete source code.
  1. <

    script type=

    "application/javascript"

    >
  2. var

    motorcycle_chart =

    new

    AwesomeChart(

    'motorcycle_graph'

    )

    ;
  3. motorcycle_chart.data

    =

    [
  4. <?

    php
  5. $query =

    mysql_query(

    "select * from product"

    )

    or die(

    mysql_error(

    )

    )

    ;
  6. while (

    $row =

    mysql_fetch_array(

    $query)

    )

    {
  7. ?>
  8. <?

    php echo $row[

    'Qty'

    ]

    . ','

    ;

    ?>
  9. <?

    php }

    ;

    ?>
  10. ]

    ;

  11. motorcycle_chart.labels

    =

    [
  12. <?

    php
  13. $query =

    mysql_query(

    "select * from product"

    )

    or die(

    mysql_error(

    )

    )

    ;
  14. while (

    $row =

    mysql_fetch_array(

    $query)

    )

    {
  15. ?>
  16. <?

    php echo "'"

    . $row[

    'name'

    ]

    . "'"

    . ','

    ;

    ?>
  17. <?

    php }

    ;

    ?>
  18. ]

    ;
  19. motorcycle_chart.colors

    =

    [

    'gold'

    ,

    'skyblue'

    ,

    '#FF6600'

    ,

    'pink'

    ,

    'darkblue'

    ,

    'lightpink'

    ,

    'green'

    ]

    ;
  20. motorcycle_chart.randomColors

    =

    true

    ;
  21. motorcycle_chart.animate

    =

    true

    ;
  22. motorcycle_chart.animationFrames

    =

    20

    ;
  23. motorcycle_chart.draw

    (

    )

    ;
  24. </

    script>

This is the result.
1_123.png


Hope that this tutorial will help you a lot.

Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,341

323,350

Top