• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Questions regarding <dl> in CI.

#1
[eluser]überfuzz[/eluser]
How should I display a <dl>-list in a view-file? I'm very fond of dl-tags and rather new with CI.

I've got a method in a model that fetches rows from a sql-table. With my limited knowledge of CI I take out the result in an array. (Could it be done with result()? I'm used to fall back on arrays etc when I feel unsure.) In the controller calling the get-method I have to tinker with the array.
sql-array from model:
Code:
Array
(
    [0] => Array
        (
            [when] => 2008
            [what] => Lorem ipsum...
        )

    [1] => Array
        (
            [when] => 2008
            [what] => Lorem ipsum...
        )

    [2] => Array
        (
            [when] => 2009
            [what] => Lorem ipsum...
        )

    [3] => Array
        (
            [when] => 2009
            [what] => Lorem ipsum...
        )

    [4] => Array
        (
            [when] => 2009
            [what] => Lorem ipsum...
        )
)

The array altered by the controller:
Code:
Array
(
    [2008] => Array
        (
            [0] => Lorem ipsum...
            [1] => Lorem ipsum...
        )

    [2009] => Array
        (
            [0] => Lorem ipsum...
            [1] => Lorem ipsum...
            [2] => Lorem ipsum...
        )

)
This is to be able to echo out a nice <dl>-tag, like this one:
Code:
<dl class="nice_dl">

        <dt>2008</dt>

        <dd>Lorem ipsum...</dd>
        <dd>Lorem ipsum...</dd>

        <dt>2009</dt>
        <dd>Lorem ipsum...</dd>
        <dd>Lorem ipsum...</dd>
        <dd>Lorem ipsum...</dd>
    </dl>

Could my sql-array be rendered in a better way? How should I muster the output in the view-file. Right now I'm using a foreach-loop.

#2
[eluser]bretticus[/eluser]
You can't really get a hierarchical result from a database query as you intend. Personally, I see no problem with building an hierarchical array as long as it's not massive (I would assume not just because it's obviously meant to be sent to your view.)

#3
[eluser]Colin Williams[/eluser]
I think you brought up several issues. First, there's nothing wrong with altering the format of a SQL result. But, you should definitely let the model do this formatting. Then in your view, you just need to foreach the array, then foreach the items of that array within that first foreach. Nothing wrong with that either

#4
[eluser]überfuzz[/eluser]
I did a model with no altering functionality. The result was that I had to alter the array in every controller calling for it. I guess I'll fix it so it's nice and tidy when it comes from the model.

Tnx, both of you!

#5
[eluser]beemr[/eluser]
That would be fine if you wanted to limit the way you use <dl>'s to a single term. However the spec for <dl>'s can have many terms for many definitions. I modified the html_helper a while back so it could produce a <dl> from an associative array like so:
Code:
array(array('dt'=>'term','term2'),array('dd'=>'definition','def2'))

It can be nested inside of a call to the ol or ul helper, so you could produce definitions bounded by list items like so:
Code:
<ol>
<li>item 1
<dl>
<dt>term</dt>
<dt>term2</dt>
<dd>definition</dd>
<dd>def2</dd>
</dl>
</li>
</ol>

I posted it to the wiki under definition list helper

#6
[eluser]überfuzz[/eluser]
@beem At the top of the thread you'll see the correct, i Believe, way of using <dl>-tags. (Not sure of what it is you're trying to show...) I've already got a working system. Now with some help the code even looks good.

#7
[eluser]beemr[/eluser]
Just trying to show you what a <dl> is and that
multiple terms are part of the spec

Your solution only allows for a single term.

#8
[eluser]überfuzz[/eluser]
@beamer - Guess what. It's because I only need one. ;-)

#9
[eluser]beemr[/eluser]
... until you need two :wow:

#10
[eluser]überfuzz[/eluser]
I just had a glans at the helper you referred to. I see a lot of foot work, but it seem to be over weighted. I get my stuff using a handful of lines.
Code:
$temp_array = $query->result_array();
            foreach($temp_array AS $value)
            {
                $custom_array[$value['what']][] = $value['this'];
            }
Thats fine for now.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.