Categories > TinyButStrong general >

Support columns with spaced names

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Col
Date: 2009-02-24
Time: 02:08

Support columns with spaced names

I have dynamic columns with spaced names. Since the space character is a separator in TBS how do I get it to treat the space as a literal?

This thread (http://tinybutstrong.com/forum.php?msg_id=6574#msg_6574) says it's been available since version 3.2.0 but I can't find any details in the manual or change log.

Thanks for any advice.
Col
By: Skrol29
Date: 2009-02-24
Time: 23:34

Re: Support columns with spaced names

Hi Col,

Column names with spaces could be supported by TBS, but they aren't yet.
It's only 2 lines of code to change, but I have not committed to do so.

Because, in fact, you can always avoid spaces in column names by using an alias (except if you are calling a stored procedure).
By: Col
Date: 2009-02-25
Time: 01:31

Re: Support columns with spaced names

Unfortunately aliases doesn't work as this array isn't coming straight from a database.

Example of data:
Percentage of something happening per month for the last 6 months:

                       Sep 08 | Oct 08 | Nov 08 | Dec 08 | Jan 09 | Feb 09
X_happening    21%   |            |   24%   |             |             |   28%
Y_happening              |    43% |             |             |             |   41%
Z_happening    87%   |             |  80%    |   82%   |             |

PHP array of the above:
$arr = array(3) {
   ["X_happening"]=>
   array(3) {
      ["Sep 08"] => 21,
      ["Nov 08"] => 24,
      ["Feb 09"] => 28
   }
   ["Y_happening"]=>
   array(2) {
      ["Oct 08"] => 43,
      ["Feb 09"] => 41
   }
   ["Z_happening"]=>
   array(3) {
      ["Sep 08"] => 87,
      ["Nov 08"] => 80,
      ["Dec 09"] => 82
   }
}

So unfortunately the key block (months) needs to maintain the sequence and therefore must be numbered rather than using something like Sep_08 as the key.

Since spaced keys hasn't been implemented I guess this problem is quite unique. I see two possible solutions:
1) If there is some way to edit the format of the key prior to Show(). So the values of the column block could be Sep_08 but on display the underscore is removed. Possible via the onformat feature? (I look into it.)
2) Change the array above so the key of the data above is not a string (Sep 08) but an integer which matches the integer value of the column block. Do-able but not as simple as using a string key.
By: Skrol29
Date: 2009-02-25
Time: 01:38

Re: Support columns with spaced names

Hi,

You maybe can perform your (1) by doing a str_replace() on $TBS->Source.

But when you say the "array isn't coming straight from a database", I guess you mean that the array structure cannot be modified. That would be my first direction.
By: Col
Date: 2009-02-25
Time: 01:57

Re: Support columns with spaced names

Actually the structure can be changed and I'm open to suggestions.

Sorry. "array isn't coming straight from a database" isn't quite correct. It is not being extracted in a single request. The data comes from about 3 tables which, as a single request, was too time consuming. Instead I pull an array from 1 of the tables and reference against it. Also I need to do the percentage calculation which is not coming from the database query.
By: TomH
Date: 2009-02-26
Time: 06:04

Re: Support columns with spaced names

A possibility to get the field names with spaces from MySQL is to use the DESCRIBE query.
Here's a table struct
Field                 Type             Null     Key     Default    
id                   int(10)         NO PRI     NULL     auto_increment
President's Name     varchar(64)     NO           NULL     
Company Name         varchar(64)     NO           NULL     

Using the DESCRIBE
Query [1] -- [DESCRIBE nameswithspaces]

Query Result..

    (row)    string 16
    Field    blob 11
    Type    string 2
    Null    string 3
    Key    blob 0
    Default    string 14
    Extra
    1    id    int(10)    NO    PRI        auto_increment
    2    President's Name    varchar(64)    NO           
    3    Company Name    varchar(64)    NO

html look like
                <tr>
                <th class="">[blk.Field;block=td]</th>
                </tr>

Maybe a help,
TomH
By: Anonymous
Date: 2009-02-26
Time: 06:39

Re: Support columns with spaced names

Thanks for the reply and the thoughts Tom.

The problem is that I can't then use those field names as keys to an array referenced by TBS. TBS breaks the field name on the space and says the 'Presidents' or 'Company' is not a valid key in the array.

I decided to go with my option 2 above which was a little clunky but not too bad.

Using my example from the first post I had a column block like so:
$colArr = array(0 => "Sep 08", 1 => "Oct 08", 2 => "Nov 08", 3 => "Dec 08", 4 => "Jan 09", 5 => "Feb 09");
(I know the numeric keys aren't necessary; just including them for clarity).

Then in my script I flipped it:
$flipColArr = array_flip($colArr); // keys of $colArr becomes values of $flipColArr and values of $colArr become keys of $flipColArr

Then, during the creation of $arr (as per original post) instead of keys of the ?_happening arrays I used:
$month = "Sep"; // calculated from the data coming from the database
$year = "08"; // calculated from the data coming from the database
$arr["X_happening"][$flipColArr["$month $year"]] = 21 // ends up being $arr["X_happening"][0] = 21

Then my html code looks like this:
<table>
   <tr>
      <td>Happening</td>
      <td>[blkCols.val;block=td]</td>
   </tr>
   <tr>
      <td>[blkData.$;block=tr]</td>
      <td>[blkData.[blkCols2.$;block=td];.;noerr]</td>
   </tr>
</table>
By: Col
Date: 2009-02-26
Time: 06:40

Re: Support columns with spaced names

Oops, last post was by me.
By: TomH
Date: 2009-02-26
Time: 07:27

Re: Support columns with spaced names

Yes, no matter it is a little bit clunky ;)

I was using
    foreach($query AS $key=>$val){
    $data[] = array ("id"=>$val[id],"name"=>$val["President's Name"], "company"=>$val["Company Name"]);
    }

to access / rename the fields.