Jump to content

progress bar chart in PHP


KarthikOnIT

Recommended Posts

Hi Folks,

    I am newbie in PHP, we are designing a simple quiz game in PHP.

I am trying to draw a horizontal progress bar in PHP.This is like a

rectangle of length (10*1)and divided into ten equal parts of size(1*1).

The quiz will have ten questions so the blocks will be filled with GREY colour

initially, when the user answers the questions it will turn green or red depending on

whether it's correct or not.

 

  I couldn't find a open source library which could do this. Should I use PHP GD

and draw it?

 

Thanks in advance.

 

Karthik

Link to comment
https://forums.phpfreaks.com/topic/223141-progress-bar-chart-in-php/
Share on other sites

One way to do it would be to use variables and loops to control how many of each is displayed up to the total number needed.

Pseudo-code:

<?php
$total_q = 10;
$curr_q = 4;
$remain = ( $total_q - $curr_q );
for( $i = 0; $i < $curr_q; $i++) {
   echo '<img src="green.jpg">';
}
for( $r = 0; $r < $remain; $r++) {
   echo '<img src="grey.jpg">';
}
?>

 

There might be better ways, but that's what came to mind immediately.

just take a solid image and strech it.

 

 

<?php
$qnum = (int)$_POST['qnum']; // for example 5 (50%)
$total = 10; // Total questions
if($qnum > $total)
$qnum = $total;

$width = ($qnum / $total) * 100;

echo '<form action="" method="post">Question Number:<input type="text" name="qnum" /></form>';
echo '<div style="width:500px;border:solid 1px #000000;height:10px;">';
echo '<img src="blue.jpg" height="10px" width="'.$width.'%" />';
echo '</div>';
?>

 

You will get an error because I have strict mode on, but here is an example:

http://96.42.108.211:3333/test.php

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.