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
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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.