![]() |
Formatting columns in a table - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22) +--- Thread: Formatting columns in a table (/showthread.php?tid=39402) |
Formatting columns in a table - El Forum - 03-09-2011 [eluser]skypanther[/eluser] I'm pulling data from a database then outputting it in an HTML table. That part's fine. But what I can't figure out is how to apply formatting to the individual columns in the results. So, the first column is a part number, then a product name, then a price, etc. I want to format the price column as money (applying the money_format() or similar function). My controller: Code: // call my model's function to grab the data: And my view: Code: echo $this->table->generate($results); Works fine but how can I format columns? I tried manually adding rows with $this->table->add_row() but I can't figure out how to extract the data returned by my model. BTW, it's just calling $query = $this->db->get('parts'); I could do the formatting with jQuery but there are a lot of rows and that would be slow I'm guessing. Thanks, Tim Formatting columns in a table - El Forum - 03-09-2011 [eluser]oldmatt[/eluser] Well, you could do it in MySQL(if the is what DB you are using). Or you could use a template, or you could prepare the data before you pass it to the generate method. Formatting columns in a table - El Forum - 03-09-2011 [eluser]skypanther[/eluser] I guess it's the "prepare the data before you pass it to the generate method" that I figured was the best way. But that's what I can't figure out. I never thought about using MySQL, but you're right -- CONCAT('$', FORMAT(mynumberfield, 2)) I'd still like to know how to do the "prep the data" technique. Formatting columns in a table - El Forum - 03-09-2011 [eluser]InsiteFX[/eluser] You can add a css class to the table! You need to use the Table Classes table template. Code: class MY_Controller extends CI_Controller I am using a MY_Controller but should work in a regular Controller. For formatting you can use the php sprintf method. InsiteFX Formatting columns in a table - El Forum - 03-10-2011 [eluser]skypanther[/eluser] Thanks, adding a class as you showed would help me format rows. But I need to format the content in the third column. I don't see how that can be done with the table templates. As for the printf function, how do I apply it to the third column of data using the table library? Formatting columns in a table - El Forum - 03-10-2011 [eluser]InsiteFX[/eluser] Can you show your view code. InsiteFX Formatting columns in a table - El Forum - 03-10-2011 [eluser]skypanther[/eluser] Sure, it's really just the table->generate call. The rest is just HTML/CSS to pretty it up. Code: <div class="button" style="width:150px;float:right;margin-right:20px;"><a href="import" title="Import new prices">Import new prices</a></div> Formatting columns in a table - El Forum - 03-10-2011 [eluser]InsiteFX[/eluser] to format the your column you would need to loop through the returned result set either in your model or your controller. Code: $data['results'] = $this->productsmodel->get_products(); Then you could format the fields you need formatted. InsiteFX Formatting columns in a table - El Forum - 03-10-2011 [eluser]skypanther[/eluser] When I print_r($results) in my view, this is what I get: Code: CI_DB_mysql_result Object ( [conn_id] => Resource id #27 [result_id] => Resource id #42 What do I loop over within that to access the data? Or are you saying I need to dispense with the automated techniques offered with the Table library and build my own? I'm certainly familiar with PHP enough to query a DB, loop through a result set, and output an HTML table. What I'm trying to figure out is what & how CI can do the heavy lifting for me. The automated table-building library is a great timesaver. I just figured there was some callback or other means built-in for me to manipulate rows and columns prior to output. It seems not, so I'll go the roll-my-own route. I don't mean that to be snotty. And I really appreciate your help! If there's not a built-in way to do what I want, I'm fine with doing it more manually. I'm just trying to learn the CI way (since I obviously have not used it very much). Thanks, Tim Formatting columns in a table - El Forum - 06-07-2011 [eluser]Julian[/eluser] Hello, good post, please visit my post and see how you can become a millionaire in the forex. [URL="http://millionaireinforex.com"]Become a Millionaire in Forex[/URL] |