Welcome Guest, Not a member yet? Register   Sign In
Efficiency with arrays, help please!
#1

[eluser]Jbeasley6651[/eluser]
I like being as efficient as possible, and i'm trying to apply these principles in my programming.

I have a page with lets say 5 different tables, each table shows results based on an If statement in a foreach loop

example of table showing result for cat-> == 1


Code:
<div id="dashA" class="column">
    &lt;?php foreach($dash->result() as $row) :?&gt;
    &lt;?php if ($row->cat == 1) : ?&gt;
    <div id="port_&lt;?=$row->id?&gt;" class="portlet">
        <div class="portlet-header">&lt;?=$row->name?&gt;</div>
        &lt;? endif;?&gt;
        &lt;?php endforeach; ?&gt;

example of table showing result for cat-> == 2


Code:
<div id="dashB" class="column">
    &lt;?php foreach($dash->result() as $row) :?&gt;
    &lt;?php if ($row->cat == 2) : ?&gt;
    <div id="port_&lt;?=$row->id?&gt;" class="portlet">
        <div class="portlet-header">&lt;?=$row->name?&gt;</div>
        &lt;? endif;?&gt;
        &lt;?php endforeach; ?&gt;

I'm only showing 2 above, pretend i'm showing 5 for the example below


Now, lets say i have 100 database rows in $dash, and i'm having to run this loop 5 times. Doesn't that seem like overkill?


So here's my question

Can't i just loop though once and use an array to do my if statements on?

How would i do this and would i do this in the controller? I'm not currently using models.

And if you were wondering, i do NOT want to run separate queries to return different sets of data.

Help would be appreciated.

Thanks,

JBeasley
#2

[eluser]John_Betong[/eluser]
&nbsp;
Try this in your controller or model
Code:
function get_loop($data, $dash)
{
  $result = array();

  foreach($dash->result() as $row):

    $result[] = "<div id='port_"  .$row->id  ."' class='portlet'>"
              .   "<div class='portlet-header'>";  
              .     $row->name
              .   '</div>'
              . '</div>';
        
  endforeach;

  $data['loop'] = $result;

  return $data;
}
&nbsp;
In your View
[code]

<= $loop ?&gt;

[code]
&nbsp;
&nbsp;
Modified: added $dash parameter to get_loop(...)
&nbsp;
&nbsp;
#3

[eluser]Thorpe Obazee[/eluser]
What you could do is NOT to retrieve the rows that you don't need instead of using conditional statements.
#4

[eluser]Jbeasley6651[/eluser]
I ended up with this, with the help of a friend.

It allowed everything to be 100% flexible so if i add new status or anything it would populate.

Code:
[removed]
$(document).ready(function() {
    
    &lt;?php
           $lineIds = '';
           $comma = '';
           foreach($query_p_status->result() as $statusRow){
                   $lineIds .= $comma . '#' . $statusRow->id;
                   $comma = ', ';
           }
    ?&gt;
    
    $("&lt;?php echo $lineIds; ?&gt;").sortable({
        placeholder: 'ui-state-highlight',
            connectWith: ['.connect'],
            over: function(event, ui) {
                    var order = 'status=' + $(this).attr('id') + '&id;=' + $(ui.item).attr('id');
                    $.post("projects/jupdate", order, function(theResponse){
                        $("#contentRight").html(theResponse);
                    });                                                          
            }
        
        });

});
[removed]

&lt;?=anchor('','New Project',array('class' => 'newProject'))?&gt;
&lt;?php foreach($query_p_status->result() as $statusRow) : ?&gt;
    &lt;!-- &lt;?php echo $statusRow; ?&gt; --&gt;
    <h3 class="projectStatus p&lt;?php echo str_replace(' ', '', $statusRow->name); ?&gt;">&lt;?php echo $statusRow->name; ?&gt;</h3>
    <ul class="projectHeading">
        <li class="pColHeading pName">Name</li>
        <li class="pColHeading pClient">Client</li>
        <li class="pColHeading pCat">Category</li>
        <li class="pColHeading pStarted">Started</li>
        <li class="pColHeading pTotal">Total</li>
        <li class="pColHeading pPaid">Paid</li>
        <li class="pColHeading pOwed">Owed</li>
        <div class="clearall"></div>
    </ul>
    <ul id="&lt;?php echo $statusRow->id; ?&gt;" class="connect">
        &lt;?php $rowCount = false; ?&gt;
        &lt;?php foreach($query_projects->result() as $row) :?&gt;
        &lt;?php if ($row->status == $statusRow->id) : ?&gt;    
        &lt;?php $rowCount = true; ?&gt;
            <li id="joel_&lt;?=$row->id?&gt;">
        <ul class="project">
        <li class="pCol pName">&lt;?=$row->name?&gt;</li>
        <li class="pCol pClient">&lt;?=$row->client?&gt;</li>
        <li class="pCol pCat">&lt;?=$row->cat?&gt;</li>
        <li class="pCol pStarted">&lt;?=date($row->start_date)?&gt;</li>
        <li class="pCol pTotal">$&lt;?=number_format($row->amount_total,2,NULL,",")?&gt;</li>
        <li class="pCol pPaid">$&lt;?=number_format($row->amount_paid,2,NULL,",")?&gt;</li>
        <li class="pCol pOwed">$&lt;?=number_format($row->amount_owed,2,NULL,",")?&gt;</li>
        <div class="clearall"></div>
    </ul></li>
    &lt;?php endif; ?&gt;
    &lt;?php endforeach; ?&gt;
    &lt;?php if(!$rowCount) : ?&gt;
        <li id="joel_">
                <ul class="project">
                <li class="pCol pName">No Results </li>
                <li class="pCol pClient">Drag Item Here</li>
                <li class="pCol pCat"></li>
                <li class="pCol pStarted"></li>
                <li class="pCol pTotal"></li>
                <li class="pCol pPaid"></li>
                <li class="pCol pOwed"></li>

                <div class="clearall"></div>
            </ul></li>
    
    &lt;?php endif; ?&gt;
    
    </ul>
&lt;?php endforeach; ?&gt;

I included the JS since the list are drag and drop you need all the dymanic list id's in the JS.

Thanks for your responses




Theme © iAndrew 2016 - Forum software by © MyBB