strberg Posted August 9, 2012 Share Posted August 9, 2012 Hello! Allright so i want taken upon a task were i should make a gauge. But i don't know how. I've been googleing alot about this and i found this: http://jpgraph.net/download/manuals/chunkhtml/ch20s02.html But for my it only says how to make it. Which is a great start but i want the gauge to look into a database to see what precent the meter should point on. Example: The question is, Do you watch the olympics? Two answers, Yes and No. The database saves these and when you press View results, the speedometers arrow should point at, lets say 60%. The you know that 60% watches the olympics and 40% doesn't. (it is a half circle) If you have any better guides than the one i linked please enlighten me. If you haven't notice i'm new to PHP so use your easy words. Quote Link to comment Share on other sites More sharing options...
Barand Posted August 9, 2012 Share Posted August 9, 2012 Rather than have your gauge look into the database so that it performs only a single function it is much better to have a gauge that just lets you pass a percentage figure and draw itself accordingly. That way it becomes reusable on many other occasions. Query the database to get the percentage. Another approach would be a basic gauge class which you can then subclass for your olympics question. Quote Link to comment Share on other sites More sharing options...
strberg Posted August 9, 2012 Author Share Posted August 9, 2012 You mean i should go another design? Because the whole point of making this is that i want the speedometer look, and that the arrow moves according to the database vaulues. Quote Link to comment Share on other sites More sharing options...
Barand Posted August 9, 2012 Share Posted August 9, 2012 If you separate the gauge from the question you can then have dozens of questions all utilizing the same gauge to show the results. Quote Link to comment Share on other sites More sharing options...
strberg Posted August 9, 2012 Author Share Posted August 9, 2012 Oh okay. I have bad english as you may have noticed thats why i didin't get it from the beginning. And how do I do that? Quote Link to comment Share on other sites More sharing options...
Barand Posted August 9, 2012 Share Posted August 9, 2012 Create a php script to draw your gauge using the GD library, say "gauge.php". This would expect the percentage to be passed to it so it can draw the pointer in the right position. guage.php $percent = $_GET['percent']; // get the percentage to be displayed // draw gauge and output Query the database and calc the percentage to be displayed, say $pcent. Display the gauge using an img tag <img src='gauge.php?percent=$pcent' /> You can repeat this for as many questions as you like. Quote Link to comment Share on other sites More sharing options...
strberg Posted August 10, 2012 Author Share Posted August 10, 2012 I don't quite understand, is there any way you could expand your guide? Quote Link to comment Share on other sites More sharing options...
xyph Posted August 10, 2012 Share Posted August 10, 2012 There are many tutorials on how to use GD with PHP to create custom images. Chances are no one here is going to write another one just for your issue. If you can be more specific as to what you're having problems with, we might be able to help. As of now, the questions you're asking are too generic. Are you having problems getting the percentage from the database? Once you have that number, the class you listed should do the rest for you quite easily. $odo->needle->Set($percent); Quote Link to comment Share on other sites More sharing options...
Barand Posted August 10, 2012 Share Posted August 10, 2012 Here's an example using one of my image php files to draw a bar graph (you would substitute your gauge image). I am attaching the code for img_bar.php to get you started. <style type='text/css'> th {background-color: #369; color:#FFF; font-family: arial; font-size: 10pt;} td {background-color: #ccc; color:#000; font-family: arial; font-size: 9pt;} </style> <?php /*** * Suppose you had these questions and reults */ $questions = array ( array ( 'q' => 'Do you watch the Olympic Games?', 'pcent' => 70 ), array ( 'q' => 'Do you normally watch sport?', 'pcent' => 55 ), array ( 'q' => 'Do you watch soaps?', 'pcent' => 80 ) ); /*** * and you want to display them */ echo "<table border='0' cellpadding='4' cellspacing='2'><tr><th>Question</th><th colspan='2'>Percent</th>"; foreach ($questions as $qarr) { echo "<tr><td>{$qarr['q']}</td> <td>{$qarr['pcent']}</td> <td><img src='img_bar.php?val={$qarr['pcent']}&max=100' /></td></tr>"; } echo "</table>"; ?> 18829_.php Quote Link to comment Share on other sites More sharing options...
strberg Posted August 11, 2012 Author Share Posted August 11, 2012 Thanks for that barand it gave me alot more understanding! Just how have one question at the moment, how do i collect the value in the database and how do i make it work with this? Quote Link to comment Share on other sites More sharing options...
Barand Posted August 11, 2012 Share Posted August 11, 2012 That would depend on your database. Assuming you have a table of questions and a table of responses question -> qid, question_text response-> rid, userid, qid, answer SELECT q.qid, q.question_text, COUNT(*) as total, SUM(IF(r.answer='yes', 1, 0) as yes FROM response r INNER JOIN question q USING (qid) GROUP BY qid Quote Link to comment Share on other sites More sharing options...
strberg Posted August 11, 2012 Author Share Posted August 11, 2012 The value is decided here right? $questions = array ( array ( 'q' => 'Do you watch the Olympic Games?', 'pcent' => 70 ), array ( 'q' => 'Do you normally watch sport?', 'pcent' => 90 ), array ( 'q' => 'Do you watch soaps?', 'pcent' => 80 ) How do i get so the "pcent" is decided by the code you replied with for getting the database value? Quote Link to comment Share on other sites More sharing options...
Barand Posted August 11, 2012 Share Posted August 11, 2012 You calculate the percents from the yes counts and the totals Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted August 11, 2012 Share Posted August 11, 2012 It's a pretty cool img_bar.php file. I'm going to use it in the future Quote Link to comment Share on other sites More sharing options...
Barand Posted August 11, 2012 Share Posted August 11, 2012 Just send me a note of thanks on the back of a $50.00 bill Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted August 11, 2012 Share Posted August 11, 2012 Just send me a note of thanks on the back of a $50.00 bill No problem Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.