NameSilo

Create a table/css/div bar dynamically with php

Spaceship Spaceship
Watch

axilant

Account Closed
Impact
28
Alright with my new project, i needed to use a bar to show a user how much space he/she is using... and i thought i would share this great function.
PHP:
<?
	function create_bar($width, $show_text = '')
	{
    	$bar = '';    
		if ( strstr($width, '%') )
    	{
        	$width = intval(str_replace('%', '', $width));
			if($width > 0)
        	{
            	$width = ( intval($width) <= 100 ) ? $width . '%' : '100%';
        	}
    	}
    
    	if($width > 0)
    	{
        	$bar = "<table width=\"100\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" height=\"10\">\n";
        	$bar .= "\t<tr>\n\t\t<td width=\"100%\" align=\"left\" style=\"border: 1px solid #AAAAAA; background-color: #CECECE;\" nowrap=\"nowrap\">";
    		$bar .= "\n\t\t\t<div style=\"background-color: #26E81C; margin: 2px; height: 10px; width: {$width}px;\">";
        	if ($show_text != "")
        	{
            	$bar .= "\n\t\t\t\t<span style=\"font-size: 10px;\">$show_text</span>";
        	}    
        	$bar .= "\n\t\t\t</div>\n\t\t</td>\n\t</tr>\n</table>";
    	}    
   		return $bar;
	}
	print(create_bar("10","10/100"));
?>
Create_bar(int percentage used..., string for text in the middle of the bar)

all comments/suggestions welcome...
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Cool, thanks for sharing!
 
0
•••
before i use i wanna look at this in demo, is there anywhere you have installed this so that i can view.
 
0
•••
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back