• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problems With My Code Extra Set of Eyes Needed!!

#1
[eluser]codedoode[/eluser]
I'm having a problem getting a getting two separate MySQL tables to display on one page.

This is my controller code
Code:
function index()
    {
        $submissions = array();
        
        if($query = $this->site_model->get_records())
        {
            $submissions['records'] = $query;
        }
        
        $votes = array();
        
        if($query = $this->site_model->get_votes())
        {
            $votes['data'] = $query;
        }
        
        $this->load->view('list_view', $submissions, $votes);
    }


This is my model code
Code:
function get_records()
    {
        $this->db->order_by('id', 'desc');
        $query = $this->db->get('submissions');
        return $query->result();
    }
    
    function get_votes()
    {
        $this->db->order_by('id', 'desc');
        $query = $this->db->get('votes');
        return $query->result();
    }

This is my view
Code:
<!-- start post -->
        <div class="post">
        
        &lt;?php if(isset($records)) : foreach($records as $row) : ?&gt;
        
            <h1>&lt;?=anchor('http://example.com/'.$row->username, $row->username); ?&gt;</h1>
            <p>
                &lt;?php echo $row->message; ?&gt;
            </p>
            
            <ul id="info">
                <li><a href="#">2 days ago</a></li>
            </ul>
            &lt;?php if(isset($data)) : foreach($data as $row) : ?&gt;
            <ul id="voting">
                <li><a href="#">&lt;?php echo $row->hilarious; ?&gt; Hilarious</a></li>
                <li><a href="#">&lt;?php echo $row->boring; ?&gt; Boring</a></li>
                <li><a href="#">&lt;?php echo $row->retweet; ?&gt; I'd ReTweet</a></li>
                <li><a href="#">&lt;?php echo $row->favorite; ?&gt; Favorite</a></li>
            </ul>
            &lt;?php endforeach; ?&gt;

            &lt;?php else : ?&gt;    
            <ul id="voting">
                <li>No Votes were found.</li>
            </ul>    
            &lt;?php endif; ?&gt;

            <div id="like">
                [removed][removed]
                <fb:like href="&lt;?php echo 'http://example.com/staging/index.php/site/post/'.$row->id ?&gt;" layout="button_count" show_faces="false" width="150"></fb:like>
            </div>
            
            <img src="images/divider.png" class="divider" alt="divider" />
        
        &lt;?php endforeach; ?&gt;

            &lt;?php else : ?&gt;    
            <p>No submissions were found.</p>    
            
        &lt;?php endif; ?&gt;
        
        </div>
        &lt;!-- end post --&gt;

I'm assuming it's a problem with the way I'm writing my If statements on the view page, but then again I have no idea. This is one of maybe issues I'm running into when putting the finishing touches on this new website I'm building. Can anyone help me get this working or provide insight as to why this isn't working? THANK YOU TO ANYONE WHO HELPS IN ADVANCE!

#2
[eluser]mikegeorgeff[/eluser]
Try this:

Controller
Code:
function index()
{
     $data['records'] = $this->site_model->get_records();
     $data['votes'] = $this->site_model->get_votes();
    
     $this->load->view('list_view', $data);
}

View
Code:
<div class="post">

&lt;?php foreach ($records as $record): ?&gt;

<p> &lt;?php echo $record['message']; ?&gt; </p>

&lt;?php endforeach; ?&gt;



&lt;?php foreach ($votes as $vote): ?&gt;
<ul id="voting">
     <li>&lt;?php echo $vote['hilarious']; ?&gt; </li>
     <li>&lt;?php echo $vote['boring']; ?&gt;</li>
     <li>&lt;?php echo $vote['retweet']; ?&gt;</li>
     <li>&lt;?php echo $vote['favorite']; ?&gt;</li>
</ul>
&lt;?php endforeach; ?&gt;

I think the problem is when you call $this->load->view() it only accepts one variable so you need to tie both your model functions to on variable, which is the $data variable in the above example.

#3
[eluser]crnalajna[/eluser]
First of all, it's not a proper way of using view call. Vars are not transferred to your view as it should.
Please read carefully http://ellislab.com/codeigniter/user-gui...views.html

For debugging you can always use var_dump() in your view or controller file.

#4
[eluser]codedoode[/eluser]
[quote author="mikegeorgeff" date="1297153045"]Try this:

Controller
Code:
function index()
{
     $data['records'] = $this->site_model->get_records();
     $data['votes'] = $this->site_model->get_votes();
    
     $this->load->view('list_view', $data);
}

View
Code:
<div class="post">

&lt;?php foreach ($records as $record): ?&gt;

<p> &lt;?php echo $record['message']; ?&gt; </p>

&lt;?php endforeach; ?&gt;



&lt;?php foreach ($votes as $vote): ?&gt;
<ul id="voting">
     <li>&lt;?php echo $vote['hilarious']; ?&gt; </li>
     <li>&lt;?php echo $vote['boring']; ?&gt;</li>
     <li>&lt;?php echo $vote['retweet']; ?&gt;</li>
     <li>&lt;?php echo $vote['favorite']; ?&gt;</li>
</ul>
&lt;?php endforeach; ?&gt;

I think the problem is when you call $this->load->view() it only accepts one variable so you need to tie both your model functions to on variable, which is the $data variable in the above example.[/quote]

This didn't work at all

Anyone else have any ideas

#5
[eluser]jakub[/eluser]
@codedoode, what is the actual 'issue' are you having a code problem? As in bug? Or are you trying to wrap your head around how to do what you want (which is output data from 2 tables?).

Please clarify your question, tell us what the issue is, and what you expect.

#6
[eluser]codedoode[/eluser]
@jakub. I'm trying to wrap my head around how to do what I want to do (which is output the data from 2 tables)

The only thing that is not outputting is what's in the votes ul. It's returning "No votes were found." instead of the db data

#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.

&lt;?php if ($records): ?&gt;

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

&lt;?php endif; ?&gt;

&lt;?php if ($votes): ?&gt;

&lt;?php foreach($votes $vote): ?&gt;
&lt;?php echo $vote->hilarious; ?&gt;
&lt;?php endforeach; ?&gt;

&lt;?php endif; ?&gt;

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.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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