CodeIgniter Forums
need help with table class - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: need help with table class (/thread-21363.html)

Pages: 1 2


need help with table class - El Forum - 08-07-2009

[eluser]buckboru[/eluser]
I have build a table using the table class, but i would like to change it so that rows are colorized based on specific criteria. For example

data1 SYSTEM1 this would be RED
data2 SYSTEM1 this would be RED
data3 SYSTEM2 This would be BLUE
data4 SYSTEM3 this would be GREEN
data5 SYSTEM3 this would be GREEN

I see info in the userguide about alternating rows, but not sure if i can do what i would like to do.

Thanks


need help with table class - El Forum - 08-07-2009

[eluser]Jondolar[/eluser]
You can use CSS to style your rows. Create CSS styles like this:
.tdSYSTEM1 { color:red }
.tdSYSTEM2 { color:red }
.tdSYSTEM3 { color:blue } ...

Now, in your table, add the style and append the variable that displays the system field.
<tr class='td&lt;? echo $system; ?&gt;'><td>...

When each row is parsed, each <tr> tag will have a class that is specific to the value in your system variable. You'll need to have a css style for every unique "system" value in your db.


need help with table class - El Forum - 08-10-2009

[eluser]buckboru[/eluser]
Thanks for the help.
I kinda understand what you are saying to do, but i'm having trouble trying to figure out exactly how to accomplish this.
Please understand, that i'm trying to get up to speed on PHP, CI, CSS, HTML all at once, so if this is really simple to accomplish, please forgive me.

I think i created my css ok.
i made it like this.

td.rg{color:red}
td.net{color:blue}
td.exch{color:green}

now in my controller, i do this. $row->TAPESYSTEM is the field i want trigger the color change on.

Code:
$this->table->add_row($row->TAPELABEL,$row->TAPEID,$row->TAPESYSTEM,$row->TAPELOCATE,$row->TAPESTATUS,$row->HLDDATE,$row->TAPENOTES,
                anchor('tapeinv/view/'.$row->ID,'view',array('class'=>'view')).' '.
                anchor('tapeinv/update/'.$row->ID,'update',array('class'=>'update')).' '.
                anchor('tapeinv/delete/'.$row->ID,'delete',array('class'=>'delete','onclick'=>"return confirm('Are you sure want to delete this Tape?')")));[code]
$this->table->add_row($row->TAPELABEL,$row->TAPEID,$row->TAPESYSTEM,$row->TAPELOCATE,$row->TAPESTATUS,$row->HLDDATE,$row->TAPENOTES,
anchor('tapeinv/view/'.$row->ID,'view',array('class'=>'view')).' '.
anchor('tapeinv/update/'.$row->ID,'update',array('class'=>'update')).' '.
anchor('tapeinv/delete/'.$row->ID,'delete',array('class'=>'delete','onclick'=>"return confirm('Are you sure want to delete this Tape?')")));[/code]

and in my view, i have
Code:
<div class="content">
        <h1>RGC Tape Inventory List</h1>
        &lt;?php echo anchor('tapeinv/add/','add new data',array('class'=>'add')); ?&gt;
        <div class="data">&lt;?php echo $table; ?&gt;</div>
        <br />
        &lt;?php echo anchor('tapeinv/add/','add new data',array('class'=>'add')); ?&gt;
    </div>



need help with table class - El Forum - 08-10-2009

[eluser]jedd[/eluser]
Hi buckboru,

[quote author="buckboru" date="1249931685"]
Please understand, that i'm trying to get up to speed on PHP, CI, CSS, HTML all at once, so if this is really simple to accomplish, please forgive me. [/quote]

It is, and you are.

The neat thing about Jondolar's suggestion was that you could use SYSTEM1 (as an example) to denote your CSS style as well as the, albeit assumed, variable that you were pulling out of your data. Using the approach you're doing is likely to bite you - rg, exch, net - unless those strings come out of your database too. If they do not, then you need some logic in there to select your CSS TD style based upon those strings - this is perhaps double handling - though it depends how many variations you have to contend with.

Two other small suggestions first - always preview, or at least confirm your message after you post, to make sure it reads well. I think you've mucked up a [ /code ] tag, so it looks like you've posted a code segment twice. Secondly, try to put one question mark somewhere in a post that's asking a question - it makes it easier to narrow in on what your problem is! Wink If you don't know exactly what your problem is (not as weird as it sounds - happens to the best of us) then focus on the 'this is what I expected / wanted to happen' and 'this is what actually did happen' approach - this is useful, too.

Oh, a third suggestion - that you probably won't like - but a lot of CI users move away from the Table Class pretty early on, not least because of this kind of problem - it has a few annoying limitations that are messy to work around, it's easy enough (easier perhaps) to just write your own table generation view partials that do exactly what you want, and/or because generating HTML within the Controller, which is what this Class implies, goes against the MVC approach.

So you may be better off - and I reckon it'd be far less confusing for you, actually - to write your own view partial (stick it in a subdirectory under application/views/ - say snippets, partials, common, tables, - whatever floats your boat) that generates this one table for you.

Finally, can I clarify - do you know what the HTML code you're seeking to generate should look like? As in, for example, do you understand what class= attribute will do for you, and where Jondolar was heading with the suggestion made above?


need help with table class - El Forum - 08-10-2009

[eluser]buckboru[/eluser]
Hey Jedd,
Thanks for the reply and the constructive criticism.
Believe it or not i actually did a preview, but was just focusing on whether or not I actually got my code snippets inserted correctly, and didn't catch the fact that i screwed it up and had a snippet in their twice.

Now on to the meat of your reply.
I was using a CRUD application I found as an example to try and create my application. This application was using the table class, so i just went with the flow. Not that creating the table manually is that much simpler(for a newbie like me), but i have created them manually, and i could probably convert the table if that is that makes the most sense.

As far as the CSS Class, I sort of understand it and what its intent is(after looking it up today). So if i have td.net and td.net and td.exch, I can have different colors assigned to my html <td> based on the class. I currently have my table working, but the person that is going to use this application, has asked to make each 'SYSTEM' a different color. Although i'm not sure he will like it once he sees it.

Regarding how the 'SYSTEM' is defined. On my ADD and UPDATE pages, the 'SYSTEM' is hard coded in a drop down box(i'd like to pull these from a table, but that is a lesson for another day). The values RG, NET, EXCHG are then stored in my database upon saving and retrieved directly from the database. The number of values is pretty stable and unlikely to change.

So, in your opinion, should i switch from the table class to writing my table manually(thats likely to take me a week :-) ) or can i accomplish this in the table class? If i can do this with the table class, then based on my previous post, can you give me some specific details to do this?

Thanks for all the help
buckboru


need help with table class - El Forum - 08-10-2009

[eluser]jedd[/eluser]
Well, as I say, a lot of people move away from the CI Table Class. I used to use it, but found, like you, some limitations .. and after a few questions on the forums realised that a) I wasn't alone, and b) it's okay to just ignore bits of CI if they don't suit your particular requirement.

I think you might be over-estimating the work required to generate your own partial view that'll generate your table. Sure, you seem to be trying to pick up a bunch of techs at the same time, but tables aren't that tricky, and your data set seems relatively small here anyway. It's just a TABLE, and then lots of TR/TD pairings encased in a loop, with either a conditional or, as Jondolar suggested, just calling in the class based on a piece of the data you are already working with. As you have observed, the CI Table class doesn't lend itself to this (it's do-able, but not straightforward, from memory).

Aside: some people suggest, and I can see their point, that you are better off learning 'raw' PHP before learning a framework. Each of us can only ever experience one of those two permutations, but my gut feel is that it probably would be easier - if for no other reason than providing a clearer idea of where PHP stops and CI picks up. /Aside

Quote:As far as the CSS Class, I sort of understand it and what its intent is(after looking it up today). So if i have td.net and td.net and td.exch, I can have different colors assigned to my html <td> based on the class. I currently have my table working, but the person that is going to use this application, has asked to make each 'SYSTEM' a different color. Although i'm not sure he will like it once he sees it.

Correct. CSS is generally case insensitive, but nonetheless in this case I'd stick with the exact string that you're getting out of your DB - so if it's net/exch/rg - then go for that, but if it's upper case, then use upper-case in your CSS. Be aware that you can't use upper and lower case of a string and expect it'll differentiate, though (net = NET as far as CSS classes are concerned). And yes, there's scope for some garish colour activity going on there - do you have just the three you've cited so far, or are there more?

If you provide a bit more information - rough layout of the table, the bits of data you're extracting - we can doubtless point you in the right (or at least a) direction.

Btw, do you have your CSS loading properly in your main or primary view file? Are you okay with the idea of generating view partials and feeding them back into a main view?


need help with table class - El Forum - 08-10-2009

[eluser]buckboru[/eluser]
Thanks for the input.
I think i will proceed with moving away from the TABLE class, if for no other reason, then it will give me more experience.

I've heard that i should just learn raw PHP first, and I actually started down that road, but having the HTML embedded in the PHP, just seemed ugly, so i just decided to go the other direction.

My biggest drawback to learning, is that i don't get to work on CI/PHP all the time. I'm an RPG programmer and do most of my work as a green screen programmer. I'm just trying to keep from going the way of the dinosaur.

Quote:Btw, do you have your CSS loading properly in your main or primary view file? Are you okay with the idea of generating view partials and feeding them back into a main view?

I have to admit that i'm not exactly sure what you mean by generating view partials. Can you expound on that or give me an example?

Thanks
Buckboru.


need help with table class - El Forum - 08-10-2009

[eluser]jedd[/eluser]
Wow .. a real live RPG programmer? I recall nearly learning this language at college about .. well, quite some years ago. Happily I took the C option instead. Wink

Generating view partials - consider that 99% of web pages can be thought of as having a header, and a footer, and a bit in the middle - the header and footer tend to stay the same on each page, with the bit in the middle changing depending on what you're looking at. Often as not you'll have two sub-bits in the middle - a menu, say, and the actual interesting bit. A generalisation, but it usually bears out.

Anyhoo, every 24 hours some new CI user poses the question 'how do I generate my headers and footers?' - so this might be a first - suggesting to a new user that they want to consider this problem before they realise it's a problem. Wink

In your case it's quite relevant, as you (probably) want to generate a lump of HTML code, using a call to load->view(), with that lump starting with <table> and ending with </table> - which is exactly the same problem, just from a different angle. One common approach to this problem is to have a view-partial - something that generates a sub-set of a valid HTML document - which you call from your controller, using the (,,TRUE) option of load->view (this will cause the view to generate the HTML, but rather than displaying it, it will return it and assign it to a variable). Later on, in your very simple primary view, you just echo these variables out - and the <table>...</table> gets dumped in the right place.

I'd suggest you want to read up on $this->load->view() section of the [url="http://ellislab.com/codeigniter/user-guide/libraries/loader.html"]Loader Class[/url] or the absolute last paragraph on the [url="http://ellislab.com/codeigniter/user-guide/general/views.html"]Views page[/url] - they say much the same thing.

I'd also point you at the two usual threads I cite for this stuff: [url="http://ellislab.com/forums/viewthread/109698/"]http://ellislab.com/forums/viewthread/109698/[/url] and [url="http://ellislab.com/forums/viewthread/110969/"]http://ellislab.com/forums/viewthread/110969/[/url]


need help with table class - El Forum - 08-11-2009

[eluser]buckboru[/eluser]
RPG is really a good business language(as they say- its not your fathers RPG), but its not great for Web programming. There are ways to do it, but i wanted to learn something a little more main stream.
Once i get more comfortable, i want to use CI and RPG together if possible(call stored procs from CI).

I think i understand what you are talking about with the view partials and i'll review the threads you referenced.

It will be a few days before i get back to this, as i have some other RPG projects pressing.

Thanks


need help with table class - El Forum - 08-13-2009

[eluser]buckboru[/eluser]
OK, i'm still having a little trouble with my colorizing.
I have moved away from the table class and created the table myself. I haven't yet separated the headings, but that is next.

I can get 1 color to show up, i just can't figure out exactly how to make different colors show up.

here is my css
Code:
td.RG{
color:red
}
td.NET{
color:blue
}
td.EXCH{
color:green
}
here is the loop for my table. I realize, i am only selecting 'RG', I just can't figure out the proper syntax for allowing multiple colors.
Code:
<tbody>
&lt;?php foreach ($tape as $row):  ?&gt;
<tr >
<td>&lt;?=$row['TAPELABEL']?&gt;</td>
<td>&lt;?=$row['TAPEID']?&gt;</td>


<td class="RG">&lt;?=$row['TAPESYSTEM']?&gt;</td>


<td>&lt;?=$row['TAPELOCATE']?&gt;</td>
<td nowrap>&lt;?=$row['TAPESTATUS']?&gt;</td>
<td nowrap>&lt;?=$row['HLDDATE']?&gt;</td>
<td>&lt;?=$row['TAPENOTES']?&gt;</td>
<td>&lt;?=anchor('tapeinv/view/'.$row['ID'],'view',array('class'=>'view'))?&gt;</td>
<td>&lt;?=anchor('tapeinv/update/'.$row['ID'],'update',array('class'=>'update')).' '?&gt;</td>
<td>&lt;?=anchor('tapeinv/delete/'.$row['ID'],'delete',array('class'=>'delete','onclick'=>"return confirm('Are you sure want to delete this Tape?')"))?&gt;</td>
</tr>  
&lt;?php endforeach; ?&gt;
</tbody>

Thanks
buckboru