I currently have a fairly simple PHP script that displays a few columns of data, and paginates all the entries. I am trying to get my DECIMAL field (Cost) to display like $123,000,000 with the commas and currency symbols.
I am aware of the PHP currency formatting string, I just don’t know how to implement it in my script. My full script is below.
Thanks for your help ahead of time!
[php]
<?php
//DATABASE SETTINGS
$config['host'] = "localhost";
$config['user'] = "";
$config['pass'] = "";
$config['database'] = "";
$config['table'] = "";
$config['nicefields'] = true; //true or false | "Field Name" or "field_name"
$config['perpage'] = 60;
$config['showpagenumbers'] = true; //true or false
$config['showprevnext'] = true; //true or false
/******************************************/
//SHOULDN'T HAVE TO TOUCH ANYTHING BELOW...
//except maybe the html echos for pagination and arrow image file near end of file.
include 'scripts/Pagination.php';
$Pagination = new Pagination();
//CONNECT
mysql_connect($config['host'], $config['user'], $config['pass']);
mysql_select_db($config['database']);
//get total rows
$totalrows = mysql_fetch_array(mysql_query("SELECT count(*) as total FROM `".$config['table']."`"));
//limit per page, what is current page, define first record for page
$limit = $config['perpage'];
if(isset($_GET['page']) && is_numeric(trim($_GET['page']))){$page = mysql_real_escape_string($_GET['page']);}else{$page = 1;}
$startrow = $Pagination->getStartRow($page,$limit);
//create page links
if($config['showpagenumbers'] == true){
	$pagination_links = $Pagination->showPageNumbers($totalrows['total'],$page,$limit);
}else{$pagination_links=null;}
if($config['showprevnext'] == true){
	$prev_link = $Pagination->showPrev($totalrows['total'],$page,$limit);
	$next_link = $Pagination->showNext($totalrows['total'],$page,$limit);
}else{$prev_link=null;$next_link=null;}
//IF ORDERBY NOT SET, SET DEFAULT
if(!isset($_GET['orderby']) OR trim($_GET['orderby']) == ""){
	//GET FIRST FIELD IN TABLE TO BE DEFAULT SORT
	$sql = "SELECT length FROM `".$config['table']."` LIMIT 1";
	$result = mysql_query($sql) or die(mysql_error());
	$array = mysql_fetch_assoc($result);
	//first field
	$i = 0;
	foreach($array as $key=>$value){
		if($i > 0){break;}else{
		$orderby=$key;}
		$i++;		
	}
	//default sort
	$sort="ASC";
}else{
	$orderby=mysql_real_escape_string($_GET['orderby']);
}	
//IF SORT NOT SET OR VALID, SET DEFAULT
if(!isset($_GET['sort']) OR ($_GET['sort'] != "ASC" AND $_GET['sort'] != "DESC")){
	//default sort
		$sort="desc";
	}else{	
		$sort=mysql_real_escape_string($_GET['sort']);
}
//GET DATA
$sql = "SELECT length,link_to_broker_listing,namelink,year,GD,cost,advertisers,advertiserlink FROM `".$config['table']."` ORDER BY $orderby $sort LIMIT $startrow,$limit";
$result = mysql_query($sql) or die(mysql_error());
//START TABLE AND TABLE HEADER
  echo "
|  | 
\n";
  $array = mysql_fetch_assoc($result);
  $i = 0;
  foreach ($array as $key=>$value) {
      if($config['nicefields']){
        $field = str_replace("_"," ",$key);
        $field = ucwords($field);
        }
      $field = columnSortArrows($key,$field,$orderby,$sort);
      if($i !=2 && $i!=7)
      echo "| " . $field . "\n";
      $i = $i +1;
  }
echo " | 
\n";
//reset result pointer
mysql_data_seek($result,0);
//start first row style
$tr_class = "class='odd'";
//LOOP TABLE ROWS
while($row = mysql_fetch_assoc($result)){
echo "\n";
$i=0;  //used to count fields...
foreach ($row as $field=>$value)
{
		if($i==1){ $namelink=$value; }
		elseif($i==0){ echo "| $value"; }
		elseif($i==2){ echo " | " . $namelink . ""; }
		elseif($i==3){ echo " | $value"; }
		elseif($i==4){ echo " | $value"; }
		elseif($i==5){ echo " | $value"; }
		elseif($i==6){ $advertiserlink=$value; }
		elseif($i==7){ echo " | " . $advertiserlink . ""; }
		$i=$i+1;
}
echo "\n";
	
	//switch row style
	if($tr_class == "class='odd'"){
		$tr_class = "class='even'";
	}else{
		$tr_class = "class='odd'";
	}
	
}
//END TABLE
echo " | 
\n";
if(!($prev_link==null && $next_link==null && $pagination_links==null)){
echo '
'."\n";
echo $prev_link;
echo $pagination_links;
echo $next_link;
echo "
\n";
}
/*FUNCTIONS*/
function columnSortArrows($field,$text,$currentfield=null,$currentsort=null){	
	//defaults all field links to SORT ASC
	//if field link is current ORDERBY then make arrow and opposite current SORT
	
	$sortquery = "sort=ASC";
	$orderquery = "orderby=".$field;
	
	if($currentsort == "ASC"){
		$sortquery = "sort=DESC";
	}
	
	if($currentsort == "DESC"){
		$sortquery = "sort=ASC";
	}
	
	if($currentfield == $field){
		$orderquery = "orderby=".$field;
	}else{	
		$sortarrow = null;
	}
	return '
'.$text.' '. $sortarrow;	
	
}
?>[/php]