0

I'm pulling data from a database table using php. I'm creating a simple string that provides information to a chart.

Here is the current code and out put:

$tsq7a = "SELECT * FROM FactDayUtilv1"; $stmt7a = sqlsrv_query( $conn, $tsq7a); $data7a = ""; while( $row = sqlsrv_fetch_array( $stmt7a, SQLSRV_FETCH_NUMERIC)){ $data7a .= "{name: '"; $data7a .= "$row[2]"; $data7a .= "', y: "; $data7a .= "$row[3]"; $data7a .= ", drilldown: 'D"; $data7a .= "$row[2]"; $data7a .= "'},"; } $data7a = substr($data7a,0,strlen($data7a)-1); 

Creates the following output.

{name: '1', y: 65, drilldown: 'D1'},{name: '2', y: 66, drilldown: 'D2'},{name: '3', y: 54, drilldown: 'D3'},{name: '4', y: 71, drilldown: 'D4'},{name: '5', y: 43, drilldown: 'D5'},{name: '6', y: 12, drilldown: 'D6'},{name: '7', y: 8, drilldown: 'D7'}

I'm trying to replace the '1' '2' '3' '4' '5' '6' '7' in the output to the days of the week as below:

{name: 'Mon', y: 65, drilldown: 'D1'},{name: 'Tue', y: 66, drilldown: 'D2'},{name: 'Wed', y: 54, drilldown: 'D3'},{name: 'Thu', y: 71, drilldown: 'D4'},{name: 'Fri', y: 43, drilldown: 'D5'},{name: 'Sat', y: 12, drilldown: 'D6'},{name: 'Sun', y: 8, drilldown: 'D7'}

I've been trying str_replace or str_ireplace with no luck.

Any help appreciated.

Thanks Rob

1
  • Sidenote: There's a special function - json_encode that will encode you data properly. Commented Oct 23, 2014 at 19:06

1 Answer 1

2

create array;

$days = array(1=>'Mon',2=>'Tue'); //.. 

then change

$data7a .= "$row[2]"; 

to

$data7a .= $days[$row[2]]; 
Sign up to request clarification or add additional context in comments.

4 Comments

Baaah! That's too easy ;-)
sorry, shall i write the OOP version using the command line call to cobol?
Yes please, static class using exec to call PERL regex for replacement.
That worked - It really was that simple! as a newbe I think I'm expecting things to be harder than they are! cheers Dagon

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.