Javascript – Static progress bar as background of table cell

javascriptPHPprogress-bar

Does anyone know the best way to set the background of a row or cell as a 'progress bar'. For example if the 'percent used' cell value is 50% the bar fills half the background of the row or cell:

╔══════════════════════════════════════════════════════════╗
║░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░78%░░░░░░░░░░░░░░░          ║
╚══════════════════════════════════════════════════════════╝

I am using PHP to generate the table, so maybe I could use a single colour image in the cell and set the width of the img. How would I get the text of the cell to sit on top? How would I know if the text had made the column wider… would I have to use fixed column widths?

Would Javascript be better as it would be able to detect how wide the columns were rendered as?

EDIT:
Just to clarify, the progress does not change live… just when the page is loaded.

Best Answer

This should be pretty easy to accomplish without javascript, since the width is set as a percentage, it wouldn't matter what table width you have:

<table style="width:200px; border: #777 2px solid;">
 <tr>
  <td style="background-color:#f00; height: 10px; width: <?php echo $_GET['percent'] . "%"; ?>;"></td>
  <td style="background-color:#555;"></td>
 </tr>
</table>
Related Topic