Welcome Guest, Not a member yet? Register   Sign In
Problems With My Code Extra Set of Eyes Needed!!
#7

[eluser]d1a8lo24[/eluser]
The following example is following your code to correct the output not the logic.

First lets do the model
Code:
function get_records()
    {
        $query = $this->db
                      ->order_by('id', 'desc')
                      ->get('submissions');

        // Usually we try to test before returning anything
        // but we are just following the code
        return $query->result();
    }
    
    function get_votes()
    {
        $query = $this->db
                      ->order_by('id', 'desc')
                      ->get('votes');
        return $query->result();
    }

Now lets work with the controller
Code:
function index()
    {
        $data = array();
        
        $data['records'] = $this->site_model->get_records();
        
        $data['votes'] = $this->site_model->get_votes()
        
        // Remember the load view can only accept an array as a second parameter.
        $this->load->view('list_view', $data);
    }

Now on your view you can do the following.
Code:
// Now you can loop through your arrays.

<?php if ($records): ?>

<?php foreach($records as $record): ?>
<?php echo $record->username; ?>
<?php endforeach; ?>

<?php endif; ?>

<?php if ($votes): ?>

<?php foreach($votes $vote): ?>
<?php echo $vote->hilarious; ?>
<?php endforeach; ?>

<?php endif; ?>

Now it seems that you want to get all the users records and also get each user's votes, if this is the case then your coding logic is wrong.

In the other hand if you're trying to get all users records and display some voting options then your code looks ok.

If your case is the first one this is why your code logic is wrong.

When executing your first loop everything looks fine but your second inner loop is the one that is going to cause you problems. When the second loop executes it will loop through every vote record but it will do this for every user record. This means that every user will have a list of everyone's votes. And i think this is not what you want.

To fix this you need to change your code logic in your model.


Messages In This Thread
Problems With My Code Extra Set of Eyes Needed!! - by El Forum - 02-07-2011, 07:24 PM
Problems With My Code Extra Set of Eyes Needed!! - by El Forum - 02-07-2011, 08:17 PM
Problems With My Code Extra Set of Eyes Needed!! - by El Forum - 02-07-2011, 08:29 PM
Problems With My Code Extra Set of Eyes Needed!! - by El Forum - 02-07-2011, 08:30 PM
Problems With My Code Extra Set of Eyes Needed!! - by El Forum - 02-07-2011, 08:55 PM
Problems With My Code Extra Set of Eyes Needed!! - by El Forum - 02-07-2011, 08:57 PM
Problems With My Code Extra Set of Eyes Needed!! - by El Forum - 02-07-2011, 08:58 PM



Theme © iAndrew 2016 - Forum software by © MyBB