CodeIgniter Forums
Foreach loop only calling model properly in last loop. Why? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Foreach loop only calling model properly in last loop. Why? (/showthread.php?tid=19380)

Pages: 1 2


Foreach loop only calling model properly in last loop. Why? - El Forum - 06-05-2009

[eluser]Zac G.[/eluser]
Hi Folks,

I'm stuck, and hoping there is a simple solution. I am uploading a csv file, looping through each line and looking up the id number of the people in the file.

The problem is that the model method is only working on the last loop. When I change the order of the names, it does the same thing.


Controller
Code:
function import_english_classes($file_path)
    {
        $english_classes = file($file_path);
        foreach ($english_classes as $class)
        {
            list($student_id, $period, $teacher_last_name, $teacher_first_name) = explode(',', $class);
            $teacher_id = $this->Users_model->get_user_id_by_name($teacher_last_name, $teacher_first_name);
            echo $teacher_id . '<br />';
        }  
    }

The Model
Code:
function get_user_id_by_name($last_name, $first_name)
        {
            $this->db->from('users');
            $this->db->select('id');
            $this->db->where('first_name', $first_name);
            $this->db->where('last_name', $last_name);            
            $this->db->limit(1);
            $query = $this->db->get();
            foreach ($query->result() as $row)
            {
                return $row->id;                
            }    
        }

Since I know that the method works, I am thinking it has something to do with being in this loop. Any idea why?

Thanks!
Zac


Foreach loop only calling model properly in last loop. Why? - El Forum - 06-05-2009

[eluser]mddd[/eluser]
The file() function should read a file into an array : each line of the file becomes one item in the array.
This only works if PHP can find the line endings. If not, the entire file will become a single item in the array.
And your loop will therefore only work once.

I would check the size of $english_classes with
Code:
echo 'size of array = ' . sizeof($english_classes)
to see if this is the case.

To solve it, you can set 'auto_detect_line_endings' in the php.ini.


Foreach loop only calling model properly in last loop. Why? - El Forum - 06-05-2009

[eluser]TheFuzzy0ne[/eluser]
I'm not sure what you mean, but I do know that if you're only expecting a single result, you'd probably be better of using $query->row() instead.

Code:
function get_user_id_by_name($last_name, $first_name)
{
    $this->db->select('id');
    $this->db->where(array(
        'first_name' => $first_name,
        'last_name' => $last_name
    );
    $this->db->limit(1);
    $res = $this->db->get('users');
    $res = $res->row();

    return res->id;                
}

I can't help thinking that you might be better off using a join on your tables, however. You could save yourself some database queries.


Foreach loop only calling model properly in last loop. Why? - El Forum - 06-05-2009

[eluser]Zac G.[/eluser]
@mddd - Thanks! It seems to be the proper length for the array.

@TheFuzzy0ne - Good tip Smile I can see how that is more efficient.

The problem I have is that if I want to read through each line and get the id of the teacher. Here is what it should display:

Code:
379113,05,Frankeberger,Eliot
Id: 126
993236,05,Crouse,Shaun
Id: 127
970786,02,Leddy,Alan
Id: 128

*This is using slightly different code than I posted above, but only by some echo statements

However, this is what I get

Code:
379113,05,Frankeberger,Eliot

993236,05,Crouse,Shaun

970786,02,Leddy,Alan
Id: 128

Does that make sense?


Foreach loop only calling model properly in last loop. Why? - El Forum - 06-05-2009

[eluser]mddd[/eluser]
I thought you only got one teacher back, as you said 'it only works on the last loop'..
Seeing your new info, it is obvious that the loop itself is working. So the database lookup is having a problem.

Looking up this way is very fault sensitive. Any typo in the names in your text file results in not being able to find the
teacher's id. I don't know where the info in your text file is coming from, but it would be better to replace the 'lastname,
firstname' with some kind of id directly. Something like:
Code:
379113,05,EF
993236,05,SC
Of course, the best way would be to use some kind of naming that is already in place in the organisation. Like an
employee number, naming code that is used for rostering, etc.


Foreach loop only calling model properly in last loop. Why? - El Forum - 06-05-2009

[eluser]Zac G.[/eluser]
You are very right, and this is part of the frustration I have with this.... The teachers have ids in the database but this file I have to import only has the teachers' first and last names on it. Therefore I am left trying to find out the teacher id based on the first and last names.


Foreach loop only calling model properly in last loop. Why? - El Forum - 06-05-2009

[eluser]TheFuzzy0ne[/eluser]
If I were you I'd enable the profiler by adding:
Code:
$this->output->enable_profiler(TRUE);

to your constructor. Then you'll be able to see the queries that are being run, and that should help you get a grip on the problem. I suspect that all but the last query is actually returning results. Perhaps you need to trim the first and last names first.


Foreach loop only calling model properly in last loop. Why? - El Forum - 06-05-2009

[eluser]Zac G.[/eluser]
@TheFuzzy0ne - Sweet!!!! Once I did that I realized that I wasn't stripping the line breaks, although I thought I was... all set to go now Smile

Thanks everyone!


Foreach loop only calling model properly in last loop. Why? - El Forum - 06-05-2009

[eluser]mddd[/eluser]
I should have remembered that.. File() leaves the newlines at the end of each line..
Good call TheFuzzyOne.


Foreach loop only calling model properly in last loop. Why? - El Forum - 06-05-2009

[eluser]TheFuzzy0ne[/eluser]
To save you the extra work, you can simply call file() with an extra parameter:
Code:
$english_classes = file($file_path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);