CodeIgniter Forums

Full Version: formatting query results
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]gafro[/eluser]
Hey Guys and Gals,

Looking for some info with the results received from a query.

overview example:

DB (table name = items)

id name
1 titleone
2 titletwo
3 titlethree
4 titlefour

Controller

$this->db-get('items');

view

<ul>
&lt;?php foreach($items-result() as $item):?&gt;

<li>&lt;?=$item->titleon?&gt;</li>
&lt;?php endforeach;?&gt;
</ul>


What I'd like to do is add a class to the li on every 3rd row.

<ul>
<li>itemone</li>
<li>itemtwo</li>
<li class="classname">itemthree</li>
<li>itemfour</li>
</ul>

I'm not sure how to get access to the keys from the array the active record has returned.

Can anyone shed some light or point me in the right direction.

Cheers all
Gafroninja

El Forum

[eluser]Steve Grant[/eluser]
[quote author="gafro" date="1223907308"]&lt;?php foreach($items-result() as $item):?&gt;

<li>&lt;?=$item->titleon?&gt;</li>
&lt;?php endforeach;?&gt;
</ul>


What I'd like to do is add a class to the li on every 3rd row.

<ul>
<li>itemone</li>
<li>itemtwo</li>
<li class="classname">itemthree</li>
<li>itemfour</li>
</ul>

I'm not sure how to get access to the keys from the array the active record has returned.

Can anyone shed some light or point me in the right direction.

Cheers all
Gafroninja[/quote]
Your best bet is probably to use a for loop rather than foreach, as you then effectively create your own array indexing.

Something like...
Code:
<ul>
&lt;?php for($i = 0; $i < count($items); $i++): ?&gt;
  &lt;?php $item = $items->result(); ?&gt;
  <li&lt;?php if(($i + 1) % 3 == 0): ?&gt; class="classname"&lt;?php endif; ?&gt;>&lt;?php echo $item->name; ?&gt;
&lt;?php endfor; ?&gt;
</ul>

El Forum

[eluser]John_Betong[/eluser]
&nbsp;
Try:
Code:
&lt;?php foreach($items-result() as $item):?&gt;
    </php if ('itemthree' == $item) { ?&gt;  
      <li class='classname'>&lt;?=$item->titleon?&gt;</li>
    &lt;?php }else{ ?&gt;    
      <li>&lt;?=$item->titleon?&gt;</li>
    &lt;?php } ?&gt;
  &lt;?php endforeach;?&gt;
  
OR this may work...

  &lt;?php foreach($items-result() as $item): ?&gt;

    &lt;?php $class = ('itemthree' == $item) : "class='classname'" : '' ?&gt;  

    &lt;?= "<li $class" .'<'  .$item->titleon .'</li>' ?&gt;

  &lt;?php endforeach; ?&gt;
&nbsp;
&nbsp;

El Forum

[eluser]gafro[/eluser]
@Steve grant - Cheers I'll give that a go.

@John Betong - That is a solution for that particular example. I would need to do a check for itemsix, itemnine and so on. Thanks for the help.


If there's another way to actually access the array index let me knowBig Grin



Cheers everyone.

Gafro

El Forum

[eluser]John_Betong[/eluser]
&nbsp;
Hi Gafro,
&nbsp;
Try this:
&nbsp;
Controller:

Code:
//========================================================  
  function index($data=array())  {
    for($i2=0; $i2<10; $i2++) {
      $data['customers'][] = array(
                                    'Name',
                                    'Title: 1',
                                    'Title: 2',
                                    'Title: 3',
                                    'email',
                                    'address: 1',
                                    'address: 2',
                                    'address: 3',
                                    'address: 4',
                                    'City:',
                                    'Country:',
                                    'Zip code',
                                    'Tel:',
                                    'Fax:',
                                    'Source:'
                                  );
    }//endforeach
    
    $total_fields = count($data['customers'][0]);
    for($i2=0; $i2<$total_fields; $i2++) {
        switch($i2) {
              case 0: $style="font-size:14px; font-weight:bold";              break; // Name
              case 1:
              case 2:
              case 3: $style="font-size:12px; color:#00f'";   break; // Title
              case 4: $style="font-size:16px; color:#f00";    break; // Email
              case 5:
              case 6:
              case 7:
              case 8: $style="font-size:14px; color:#060";    break; //  Tel
              case 9:
              case 10: $style="font-size:16px; color:#00f";    break; // City, Country
              case 11: $style="font-size:18px; color:#060";    break; // Zip code
              case 12:
              case 13: $style="font-size:12px; color:#009";    break; // Tel, Fax
              case 14: $style="font-size:22px; color:#009";    break; // Source
              break;   echo 'Should never get here';
        }//endswitch      
        $data['style'][$i2] = $style;
    }//endfornext loop  
      
      if (FALSE) {
        echo '<pre>';
          print_r($data);
        echo '</pre>';
      }  
    // }//endfornext14:44 10/10/2008;
    // die;
    $data['footer'] = $this->load->view('_footer',  $data, TRUE);
    $output         = $this->load->view('_ci_forums_viewthread_93697',    $data, TRUE);
    echo $output;
  }//endfunc
&nbsp;
View:
Code:
<div style='width:500px; margin:1em auto 14em; scroll:auto; border:solid 1px #ddd; text-align:left'>
      
      <div style='float:left; width:60%; margin:1em auto 0 1em; border:solid 1px #bbb'>
      
        <p style='width:90%; margin:2em auto''>
          &lt;?php foreach($customers as $customer): ?&gt;
            &lt;?php for($i2=0; $i2<count($customer); $i2++) { ?&gt;
              &lt;?= "<span style='$style[$i2]'>" .$customer[$i2] .'</span><br />'?&gt;
            &lt;?php } ?&gt;
            &lt;?= br(3) ?&gt;
          &lt;?php endforeach    ?&gt;
        </p>  
        
      </div>

    </div>
&nbsp;
&nbsp;
You can see the results Here

&nbsp;
&nbsp;

El Forum

[eluser]xwero[/eluser]
[quote author="Steve Grant" date="1223910304"]
Your best bet is probably to use a for loop rather than foreach, as you then effectively create your own array indexing.

Something like...
Code:
<ul>
&lt;?php for($i = 0; $i < count($items); $i++): ?&gt;
  &lt;?php $item = $items->result(); ?&gt;
  <li&lt;?php if(($i + 1) % 3 == 0): ?&gt; class="classname"&lt;?php endif; ?&gt;>&lt;?php echo $item->name; ?&gt;
&lt;?php endfor; ?&gt;
</ul>
[/quote]
You can do it with a foreach loop by using the key.
Code:
<ul>&lt;?php foreach($items as $i=>$item): ?&gt;
<li&lt;?php if(($i+1) % 3 == 0) echo ' class="classname"' ?&gt;>&lt;?php echo $item->name ?&gt;</li>&lt;?php endforeach ?&gt;
</ul>

El Forum

[eluser]gafro[/eluser]
Excellant. I think that's what I was looking for.

Thanks for the help all