• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Looping data

#1
[eluser]snowstar[/eluser]
Hi All

Im trying find a better way to do this

My Controller:

Code:
function printview($uniqueid)
{
  $members = $this->entry_model->get_members_forprintview($uniqueid);
  $data['members'] = $members;

  for($i = 0; $i < count($members); $i++){
  
   $member[$i] = $members[$i]['school'];
  
  }
  
  if (($member[0] == $member[1]) && ($member[1] == $member[2]) && ($member[2] == $member[3])) {
     echo 'They are all the same';
  }
  
  $this->load->view('print_view', $data);
}

Basically im looping through assigning a value from the database to a variable. Then im checking to see if the variable matches. This technically works how-ever its not dynamic (the band member amount will change each time)

Can any one recommend a better way of doing this? im out of ideas

#2
[eluser]Aken[/eluser]
Doing a foreach() on $members is better than a for() loop.

What's your end goal for trying to see if the band members are all the same? Are you just trying to prevent duplicates, or what? I'm sure your code could be optimized a lot better than as-is.

#3
[eluser]Patrick Spence[/eluser]
Well, personally I wouldnt iterate over an array like that, I would use the foreach construct..

As for checking if they are all identical, you could set a flag variable to TRUE and keep track of the last piece of data through and compare to the current one, then switch it to FALSE if it is different..

But it is kinda klunky.

If you have control over the database, and want to prevent duplicates, you should consider using a unique flag on your id field.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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