Welcome Guest, Not a member yet? Register   Sign In
Variable in one query is getting into another query in view
#1

[eluser]jshultz[/eluser]
I have two foreach statements. The variable from one one is somehow getting into another and i'm not sure how to fix it.

Here's my controller:

Code:
// Categories Page Code
    function categories($id)
    {
        $this->load->model('Business_model');
        $data['businessList']     = $this->Business_model->categoryPageList($id);
        $data['catList']    = $this->Business_model->categoryList();
        $data['featured']     = $this->Business_model->frontPageList();
        $data['user_id']    = $this->tank_auth->get_user_id();
        $data['username']    = $this->tank_auth->get_username();
        $data['page_title'] = 'Welcome To Jerome - Largest Ghost Town in America';
        $data['page'] = 'category_view'; // pass the actual view to use as a parameter
        $this->load->view('container',$data);
        
    }

What happens is categories will only show the businesses in a certain category.

The businesses are pulled from the database using the categoryPageList($id) function.

Here is that function:

Code:
function categoryPageList($id) {
    $this->db->select('b.id, b.busname, b.busowner, b.webaddress, p.thumb, v.title, c.catname, s.specname, p.thumb, c.id');
    $this->db->from ('business AS b');
    $this->db->where('b.category', $id);
    $this->db->join('photos AS p', 'p.busid = b.id', 'left');
    $this->db->join('video AS v', 'v.busid = b.id', 'left');
    $this->db->join('specials AS s', 's.busid = b.id', 'left');
    $this->db->join('category As c', 'b.category = c.id', 'left');
    $this->db->group_by("b.id");
    return $this->db->get();
}

And here is the view:

Code:
<h2>Welcome to Jerome, Arizona</h2>
  
<p>Choose the Category of Business you are interested in:<br/> &lt;?php foreach ($catList->result() as $row): ?&gt;
            <a >id?&gt;">&lt;?=$row->catname?&gt;</a>, &nbsp;
            &lt;?php endforeach; ?&gt;</p>

        
<table id="businessTable" class="tablesorter">
    <thead><tr><th>Business Name</th><th>Business Owner</th><th>Web</th><th>Photos</th><th>Videos</th><th>Specials</th></tr></thead>
        &lt;?php if(count($businessList) > 0) : foreach ($businessList->result() as $crow): ?&gt;

            <tr>
                <td><a >id?&gt;">&lt;?=$crow->busname?&gt;</a></td>
                <td>&lt;?=$crow->busowner?&gt;</td>
                <td><a >webaddress?&gt;">Visit Site</a></td>
                <td>
                    &lt;?php if(isset($crow->thumb)):?&gt;
                    yes
                    &lt;?php else:?&gt;
                    no
                    &lt;?php endif?&gt;
                    
                </td>
                
                <td>
                    &lt;?php if(isset($crow->title)):?&gt;
                    yes
                    &lt;?php else:?&gt;
                    no
                    &lt;?php endif?&gt;
                    
                
                </td>
                <td>
                    &lt;?php if(isset($crow->specname)):?&gt;
                    yes
                    &lt;?php else:?&gt;
                    no
                    &lt;?php endif?&gt;
                    
                
                </td>
            </tr>
        &lt;?php endforeach; ?&gt;
        
        &lt;?php else : ?&gt;
        <td colspan="4"><p>No Category Selected</p></td>
        
        &lt;?php endif; ?&gt;

</table>


The problem occurs here. &lt;?=$crow->id?&gt; should be showing the row id from the business table. Instead, it's showing the row ID of the category table. so, if i'm viewing /site/categories/6 &lt;?=$crow->id?&gt; will show 6 when it should be showing 10 (the row ID of the only business in that category at this time.

How can I fix this?
#2

[eluser]jshultz[/eluser]
I figured out the problem. I needed to rename the id column in categories from id to cid. I updated my functions accordingly and it's all working now.




Theme © iAndrew 2016 - Forum software by © MyBB