ashvillerob
asked on
Follow_up to last question!
snoyes jw- this is a follow_up question to the one you just answered for me. Here is the same page with all the db field included. how would I edit this page to apply the formating to just specefic db fields. For example both 'rsvc' and 'scai' are stored as decimals (.012345) and I need them to be output as a two deciaml place percentage (1.23%).
var TABLE_CONTENT = [
<?php
$_db_host = 'localhost';
$_db_user = 'xxxxxx';
$_db_pass = 'xxxx';
$_db_name = 'prodenddec182004';
$_db_table = 'prodenddec182004copy';
// table fields to fetch
$_db_field = 'tsr, name, jobtitle, supervisor, reghrs, othrs, tothrs, totpts, pph, rsvc, scai';
// bad characters - characters making a JavaScript string to be split apart
$_chars_no = array('"',"\n","\r");
// strings to replace bad characters correspondently
$_chars_ok = array('\\"','\n','\r');
// trying to connect
@mysql_connect ($_db_host, $_db_user, $_db_pass) or mydie ();
// trying to select database
@mysql_select_db ($_db_name) or mydie ();
// trying to send a query
($qh = @mysql_query ("SELECT $_db_field FROM $_db_table WHERE Location like 'Central' ")) or mydie ();
// fetching result
while ($row = mysql_fetch_row($qh)) {
unset ($all);
// processing result cells
foreach ($row as $cell) {
// replacing bad charecters and making up an array of a row
$all[] = '"' . str_replace($_chars_no, $_chars_ok, $cell) . '"';
}
// joining cells up into a string
$lines[] = '['.join (',', $all).']';
}
// joining lines up and printing the result
if (is_array($lines)) echo join(",\n", $lines);
// error reporting function
function mydie () {
?>]; alert("Unable to get Tigra Table PRO data: <?=addslashes(mysql_error( ))?>");<?
exit;
}
?>];
var TABLE_CONTENT = [
<?php
$_db_host = 'localhost';
$_db_user = 'xxxxxx';
$_db_pass = 'xxxx';
$_db_name = 'prodenddec182004';
$_db_table = 'prodenddec182004copy';
// table fields to fetch
$_db_field = 'tsr, name, jobtitle, supervisor, reghrs, othrs, tothrs, totpts, pph, rsvc, scai';
// bad characters - characters making a JavaScript string to be split apart
$_chars_no = array('"',"\n","\r");
// strings to replace bad characters correspondently
$_chars_ok = array('\\"','\n','\r');
// trying to connect
@mysql_connect ($_db_host, $_db_user, $_db_pass) or mydie ();
// trying to select database
@mysql_select_db ($_db_name) or mydie ();
// trying to send a query
($qh = @mysql_query ("SELECT $_db_field FROM $_db_table WHERE Location like 'Central' ")) or mydie ();
// fetching result
while ($row = mysql_fetch_row($qh)) {
unset ($all);
// processing result cells
foreach ($row as $cell) {
// replacing bad charecters and making up an array of a row
$all[] = '"' . str_replace($_chars_no, $_chars_ok, $cell) . '"';
}
// joining cells up into a string
$lines[] = '['.join (',', $all).']';
}
// joining lines up and printing the result
if (is_array($lines)) echo join(",\n", $lines);
// error reporting function
function mydie () {
?>]; alert("Unable to get Tigra Table PRO data: <?=addslashes(mysql_error(
exit;
}
?>];
sorry,
> $all[] = '"' . ( ( (int) ($cell*1000) ) / 100 ) . '%"';
should be :
$all[] = '"' . ( ( (intval) ($cell*1000) ) / 100 ) . '%"';
> $all[] = '"' . ( ( (int) ($cell*1000) ) / 100 ) . '%"';
should be :
$all[] = '"' . ( ( (intval) ($cell*1000) ) / 100 ) . '%"';
ASKER
Sorry ldbkutty, this does not work!
What is the problem, errors ?
Have you changed mysql_fetch_row($qh) to mysql_fetch_array($qh) as I mentioned ?
Here's my another better try than the previous (storing the decimal fields in array so you can add dynamically whenever you wish) :
var TABLE_CONTENT = [
<?php
$_db_host = 'localhost';
$_db_user = 'xxxxxx';
$_db_pass = 'xxxx';
$_db_name = 'prodenddec182004';
$_db_table = 'prodenddec182004copy';
// table fields to fetch
$_db_field = 'tsr, name, jobtitle, supervisor, reghrs, othrs, tothrs, totpts, pph, rsvc, scai';
$_db_field_decimal = array("rsvc", "scai");
// bad characters - characters making a JavaScript string to be split apart
$_chars_no = array('"',"\n","\r");
// strings to replace bad characters correspondently
$_chars_ok = array('\\"','\n','\r');
// trying to connect
@mysql_connect ($_db_host, $_db_user, $_db_pass) or mydie ();
// trying to select database
@mysql_select_db ($_db_name) or mydie ();
// trying to send a query
$qh = @mysql_query ("SELECT $_db_field FROM $_db_table WHERE Location like 'Central' ") or mydie ();
while ($row = mysql_fetch_array($qh)) {
unset ($all);
// processing result cells
foreach ($row as $key=>$cell) {
// replacing bad charecters and making up an array of a row
if( in_array($key, $_db_field_decimal) )
$all[] = '"' . (floatval) ( ( (intval) ($cell*1000) ) / 100 ) . '%"';
else
$all[] = '"' . str_replace($_chars_no, $_chars_ok, $cell) . '"';
}
// joining cells up into a string
$lines[] = '['.join (',', $all).']';
}
// joining lines up and printing the result
if (is_array($lines)) echo join(",\n", $lines);
// error reporting function
function mydie () {
?>]; alert("Unable to get Tigra Table PRO data: <?=addslashes(mysql_error( ))?>");<?
exit;
}
?>];
Have you changed mysql_fetch_row($qh) to mysql_fetch_array($qh) as I mentioned ?
Here's my another better try than the previous (storing the decimal fields in array so you can add dynamically whenever you wish) :
var TABLE_CONTENT = [
<?php
$_db_host = 'localhost';
$_db_user = 'xxxxxx';
$_db_pass = 'xxxx';
$_db_name = 'prodenddec182004';
$_db_table = 'prodenddec182004copy';
// table fields to fetch
$_db_field = 'tsr, name, jobtitle, supervisor, reghrs, othrs, tothrs, totpts, pph, rsvc, scai';
$_db_field_decimal = array("rsvc", "scai");
// bad characters - characters making a JavaScript string to be split apart
$_chars_no = array('"',"\n","\r");
// strings to replace bad characters correspondently
$_chars_ok = array('\\"','\n','\r');
// trying to connect
@mysql_connect ($_db_host, $_db_user, $_db_pass) or mydie ();
// trying to select database
@mysql_select_db ($_db_name) or mydie ();
// trying to send a query
$qh = @mysql_query ("SELECT $_db_field FROM $_db_table WHERE Location like 'Central' ") or mydie ();
while ($row = mysql_fetch_array($qh)) {
unset ($all);
// processing result cells
foreach ($row as $key=>$cell) {
// replacing bad charecters and making up an array of a row
if( in_array($key, $_db_field_decimal) )
$all[] = '"' . (floatval) ( ( (intval) ($cell*1000) ) / 100 ) . '%"';
else
$all[] = '"' . str_replace($_chars_no, $_chars_ok, $cell) . '"';
}
// joining cells up into a string
$lines[] = '['.join (',', $all).']';
}
// joining lines up and printing the result
if (is_array($lines)) echo join(",\n", $lines);
// error reporting function
function mydie () {
?>]; alert("Unable to get Tigra Table PRO data: <?=addslashes(mysql_error(
exit;
}
?>];
ASKER
With your most recent suggestion, I get parse error on line 30
Try changing :
> $all[] = '"' . (floatval) ( ( (intval) ($cell*1000) ) / 100 ) . '%"';
to :
$all[] = '"' . ( ( (intval) ($cell*1000) ) / 100 ) . '\%"';
If you get error, please show the full line of the error. Something Like :
parse error : expecting ) in ........
> $all[] = '"' . (floatval) ( ( (intval) ($cell*1000) ) / 100 ) . '%"';
to :
$all[] = '"' . ( ( (intval) ($cell*1000) ) / 100 ) . '\%"';
If you get error, please show the full line of the error. Something Like :
parse error : expecting ) in ........
ASKER
Same error:
Parse error: parse error in c:\inetpub\wwwroot\ttp_fil es\content central.ph p on line 30
Is there anyhing I need to turn on/off in order to get more detailed error ?
Parse error: parse error in c:\inetpub\wwwroot\ttp_fil
Is there anyhing I need to turn on/off in order to get more detailed error ?
Sorry for late.
Replace :
>> $all[] = '"' . (floatval) ( ( (intval) ($cell*1000) ) / 100 ) . '%"';
with :
$all[] = '"' . number_format(($cell*100), 2, '.','') . '%"';
I tried and this works now. :=)
Replace :
>> $all[] = '"' . (floatval) ( ( (intval) ($cell*1000) ) / 100 ) . '%"';
with :
$all[] = '"' . number_format(($cell*100),
I tried and this works now. :=)
ASKER
Looks like we are getitng closer. This one does return records but it duplicates all records. Here is one of the record outputs:
["341100.00%","3411","Abel , Daniel P","Abel, Daniel P","Broadband Tech I (trainee)","Broadband Tech I (trainee)","Harris, Derrick A","Harris, Derrick A","152","152","16","16"," 168","168" ,"77","77" ,"0.46","0 .46","0.08 33","8.33% ","","0.00 %"]
As you can see it returned every record twice for some reason.
Also I only want to format 'rsvc' and 'scai' as percentages, but if you look above 'tsr'(3411) was formatted as 341100.00%.
["341100.00%","3411","Abel
As you can see it returned every record twice for some reason.
Also I only want to format 'rsvc' and 'scai' as percentages, but if you look above 'tsr'(3411) was formatted as 341100.00%.
I think you modified something in the code, can you show me your full code again ?
ASKER
var TABLE_CONTENT = [
<?php
$_db_host = 'localhost';
$_db_user = '';
$_db_pass = '';
$_db_name = 'prodenddec182004';
$_db_table = 'prodenddec182004copy';
// table fields to fetch
$_db_field = 'tsr, name, jobtitle, supervisor, reghrs, othrs, tothrs, totpts, pph, rsvc, scai';
$_db_field_decimal = array("rsvc", "scai");
// bad characters - characters making a JavaScript string to be split apart
$_chars_no = array('"',"\n","\r");
// strings to replace bad characters correspondently
$_chars_ok = array('\\"','\n','\r');
// trying to connect
@mysql_connect ($_db_host, $_db_user, $_db_pass) or mydie ();
// trying to select database
@mysql_select_db ($_db_name) or mydie ();
// trying to send a query
$qh = @mysql_query ("SELECT $_db_field FROM $_db_table WHERE Location like 'Central' ") or mydie ();
while ($row = mysql_fetch_array($qh)) {
unset ($all);
// processing result cells
foreach ($row as $key=>$cell) {
// replacing bad charecters and making up an array of a row
if( in_array($key, $_db_field_decimal) )
{$all[] = '"' . number_format(($cell*100), 2, '.','') . '%"';}
else
{$all[] = '"' . str_replace($_chars_no, $_chars_ok, $cell) . '"';}
}
// joining cells up into a string
$lines[] = '['.join (',', $all).']';
}
// joining lines up and printing the result
if (is_array($lines)) echo join(",\n", $lines);
// error reporting function
function mydie () {
?>]; alert("Unable to get Tigra Table PRO data: <?=addslashes(mysql_error( ))?>");<?
exit;
}
?>];
<?php
$_db_host = 'localhost';
$_db_user = '';
$_db_pass = '';
$_db_name = 'prodenddec182004';
$_db_table = 'prodenddec182004copy';
// table fields to fetch
$_db_field = 'tsr, name, jobtitle, supervisor, reghrs, othrs, tothrs, totpts, pph, rsvc, scai';
$_db_field_decimal = array("rsvc", "scai");
// bad characters - characters making a JavaScript string to be split apart
$_chars_no = array('"',"\n","\r");
// strings to replace bad characters correspondently
$_chars_ok = array('\\"','\n','\r');
// trying to connect
@mysql_connect ($_db_host, $_db_user, $_db_pass) or mydie ();
// trying to select database
@mysql_select_db ($_db_name) or mydie ();
// trying to send a query
$qh = @mysql_query ("SELECT $_db_field FROM $_db_table WHERE Location like 'Central' ") or mydie ();
while ($row = mysql_fetch_array($qh)) {
unset ($all);
// processing result cells
foreach ($row as $key=>$cell) {
// replacing bad charecters and making up an array of a row
if( in_array($key, $_db_field_decimal) )
{$all[] = '"' . number_format(($cell*100),
else
{$all[] = '"' . str_replace($_chars_no, $_chars_ok, $cell) . '"';}
}
// joining cells up into a string
$lines[] = '['.join (',', $all).']';
}
// joining lines up and printing the result
if (is_array($lines)) echo join(",\n", $lines);
// error reporting function
function mydie () {
?>]; alert("Unable to get Tigra Table PRO data: <?=addslashes(mysql_error(
exit;
}
?>];
A quick question. Doesn't it duplicates the values if you dont have the IF ELSE condition ?
ASKER
No, here is a smaple output using the orginal code:
["1175","Adams, Phillip D","Broadband Tech I (trainee)","Winegardener, Charles F","152","0","152","343"," 2.26","0.3 000","0.13 33"]
["1175","Adams, Phillip D","Broadband Tech I (trainee)","Winegardener, Charles F","152","0","152","343","
Ok, please give ,me an hour or two. I cannot test the code now, I'll come back with solution.
ASKER
No problem, thanks for your help!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here the reason :
mysql_fetch_array($res) unlike mysql_fetch_row($res) gets the result and store it in both "numeric array and associative array" . That is both
$row[0] and $row["tsr"]
$row[1] and $row["name"]
// etc..
are returned for mysql_fetch_array($res).
If the MYSQL_ASSOC argument is passed to mysql_fetch_array() , then it returns just the associative array ( no numeric array) . That is only :
$row["tsr"]
$row["name"]
// etc..
are returned for mysql_fetch_array($res, MYSQL_ASSOC).
mysql_fetch_array($res) unlike mysql_fetch_row($res) gets the result and store it in both "numeric array and associative array" . That is both
$row[0] and $row["tsr"]
$row[1] and $row["name"]
// etc..
are returned for mysql_fetch_array($res).
If the MYSQL_ASSOC argument is passed to mysql_fetch_array() , then it returns just the associative array ( no numeric array) . That is only :
$row["tsr"]
$row["name"]
// etc..
are returned for mysql_fetch_array($res, MYSQL_ASSOC).
ASKER
worked perfectly!
ASKER
Jagadeesan-
I am about to post another question using the same script but with a variation, I would apprecieate your help with it!
I am about to post another question using the same script but with a variation, I would apprecieate your help with it!
while ($row = mysql_fetch_array($qh)) {
unset ($all);
// processing result cells
foreach ($row as $key=>$cell) {
// replacing bad charecters and making up an array of a row
if($key=="rsvc" || $key=="scai" )
$all[] = '"' . ( ( (int) ($cell*1000) ) / 100 ) . '%"';
else
$all[] = '"' . str_replace($_chars_no, $_chars_ok, $cell) . '"';
}
// joining cells up into a string
$lines[] = '['.join (',', $all).']';
}