Link to home
Start Free TrialLog in
Avatar of ashvillerob
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;
}
?>];



Avatar of ldbkutty
ldbkutty
Flag of India image

Try changing your while loop like this :

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).']';
}
sorry,

> $all[] = '"' . ( ( (int) ($cell*1000) ) / 100 ) . '%"';

should be :

$all[] = '"' . ( ( (intval) ($cell*1000) ) / 100 ) . '%"';
Avatar of ashvillerob
ashvillerob

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;
   }
   ?>];
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 ........
Same error:

Parse error: parse error in c:\inetpub\wwwroot\ttp_files\contentcentral.php on line 30

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. :=)
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.0833","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%.
I think you modified something in the code, can you show me your full code again ?
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;
   }
   ?>];

A quick question. Doesn't it duplicates the values if you dont have the IF ELSE condition ?
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.3000","0.1333"]
Ok, please give ,me an hour or two. I cannot test the code now, I'll come back with solution.
No problem, thanks for your help!
ASKER CERTIFIED SOLUTION
Avatar of ldbkutty
ldbkutty
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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).
worked perfectly!
Jagadeesan-

I am about to post another question using the same script but with a variation, I would apprecieate your help with it!