Part of the EllisLab Network
   
 
formatting query results
Posted: 13 October 2008 05:15 AM   [ Ignore ]  
Summer Student
Total Posts:  14
Joined  03-16-2007

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>
<?php foreach($items-result() as $item):?>

  <li><?=$item->titleon?></li>
<?php endforeach;?>
</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

Profile
 
 
Posted: 13 October 2008 06:05 AM   [ Ignore ]   [ # 1 ]  
Summer Student
Total Posts:  20
Joined  03-17-2008
gafro - 13 October 2008 09:15 AM

<?php foreach($items-result() as $item):?>

  <li><?=$item->titleon?></li>
<?php endforeach;?>
</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

Your best bet is probably to use a for loop rather than foreach, as you then effectively create your own array indexing.

Something like…

<ul>
<?php for($i 0$i count($items); $i++): ?>
  <?php $item 
$items->result(); ?>
  
<li<?php if(($i 1) % == 0): ?> class="classname"<?php endif; ?>><?php echo $item->name?>
<?php 
endfor; ?>
</ul
Profile
 
 
Posted: 13 October 2008 06:09 AM   [ Ignore ]   [ # 2 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  749
Joined  12-26-2006

 
Try:

<?php foreach($items-result() as $item):?>
    
</php if ('itemthree' == $item{ ?>   
      
<li class='classname'><?=$item->titleon?></li>
    
<?php }else{ ?>     
      
<li><?=$item->titleon?></li>
    
<?php } ?>
  <?php 
endforeach;?>
  
OR this may work...

  
<?php foreach($items-result() as $item): ?>

    <?php $class 
= ('itemthree' == $item) : "class='classname'" '' ?>   

    <?
"<li $class.'<'  .$item->titleon .'</li>' ?>

  <?php 
endforeach; ?> 

 
 

 Signature 

Joke of the day - Bulletin Board Ideas     (ongoing development site)

My Hippy Trail    Source code   

Latest Project

Profile
 
 
Posted: 13 October 2008 06:16 AM   [ Ignore ]   [ # 3 ]  
Summer Student
Total Posts:  14
Joined  03-16-2007

@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 know:D

Cheers everyone.

Gafro

Profile
 
 
Posted: 13 October 2008 08:44 AM   [ Ignore ]   [ # 4 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  749
Joined  12-26-2006

 
Hi Gafro,
 
Try this:
 
Controller:

//========================================================  
  
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',  $dataTRUE);
    
$output         $this->load->view('_ci_forums_viewthread_93697',    $dataTRUE);
    echo 
$output;
  
}//endfunc 

 
View:

<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''>
          <?php foreach($customers as $customer): ?>
            <?php for($i2=0; $i2<count($customer); $i2++) { ?>
              <?= "<span style='
$style[$i2]'>" .$customer[$i2] .'</span><br />'?>
            <?php } ?>
            <?= br(3) ?>
          <?php endforeach    ?>
        </p>  
        
      </div>

    </div> 

 
 
You can see the results Here

 
 

 Signature 

Joke of the day - Bulletin Board Ideas     (ongoing development site)

My Hippy Trail    Source code   

Latest Project

Profile
 
 
Posted: 13 October 2008 08:55 AM   [ Ignore ]   [ # 5 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4785
Joined  07-14-2006
Steve Grant - 13 October 2008 10:05 AM

Your best bet is probably to use a for loop rather than foreach, as you then effectively create your own array indexing.

Something like…

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

You can do it with a foreach loop by using the key.

<ul><?php foreach($items as $i=>$item): ?>
<li<?php if(($i+1) % == 0) echo ' class="classname"' ?>><?php echo $item->name ?></li><?php endforeach ?>
</ul
Profile
 
 
Posted: 13 October 2008 09:24 AM   [ Ignore ]   [ # 6 ]  
Summer Student
Total Posts:  14
Joined  03-16-2007

Excellant. I think that’s what I was looking for.

Thanks for the help all

Profile