Welcome Guest, Not a member yet? Register   Sign In
Nested View: Pass variable so that correct query results returned
#1

[eluser]Tumac[/eluser]
First: I am new to CI but understand already basic MVC concepts.

I think this is what I am trying to accomplish but not sure CI Subview thread

I have a nested view that is part of a tabset. The nested view is returning records of a query to the DB.

I would like to pass a variable from that nested view so that the records returned are only for that "key/variable".

I believe I may be missing something here as my "main()" function of my controller gets query results for the subview but does not use them on the "home" view.

Make sense? (prolly not...)

NESTED VIEW
Code:
<table class="tlctable">
    <tr>
        <th></th>
        <th>PO #</th>
        <th>Req Date</th>
        <th>Mill</th>
        <th>Contact</th>
        <th>Status</th>
    </tr>
    <tr>


&lt;?php
$lastcust=null;
echo $cusID;  
foreach($records as $row) {

?&gt;
  
    <tr>
        <td>&lt;?php echo form_checkbox('record','selected',FALSE); ?&gt;</td>
        <td>&lt;?php echo anchor("po/viewDetail/$row->poID",$row->poID); ?&gt;</td>
        <td>&lt;?php echo $row->shipReqDate; ?&gt;</td>
        <td>&lt;?php echo $row->millName; ?&gt;</td>
        <td>&lt;?php echo $row->conName; ?&gt;</td>
         <td>&lt;?php echo $row->status; ?&gt;</td>
    </tr>
    &lt;?php
    
$lastcust=$row->CusName;
} //end foreach



echo '</table>';

MAIN VIEW
Code:
&lt;?php
//$this->load->view('header');
include ('views/header.php');
?&gt;

<p><h2>Purchase Orders</h2></p>

<div id="tabs">
    <ul id="tabnav" >
&lt;?php    
$n=sizeof($cust);
$i=1;
foreach($cust as $cus) {
//for($i=0;$i<sizeof($cust);$i++) {
    if($i==1) { ?&gt;
        <li><a >cusID;?&gt;" id="tab_&lt;?php echo $cus->cusID;?&gt;" class="tab selected"><span>&lt;?php echo $cus->cusShort;?&gt;</span></a></li>
    &lt;?php
    } else {  ?&gt;
        <li><a >cusID;?&gt;" id="tab_&lt;?php echo $cus->cusID;?&gt;" class="tab"><span>&lt;?php echo $cus->cusShort;?&gt;</span></a></li>
&lt;?php  
    }  
    $i++;
}
?&gt;
    </ul>

&lt;?php
//for($i=0;$i<sizeof($cust);$i++) {
foreach($cust as $cus) {
    ?&gt;
    <div id="panel_&lt;?php echo $cus->cusID;?&gt;" class="panel">
       &lt;?php
       $cusID=$cus->cusID;
       $this->load->view("po/polist",$cusID);
       ?&gt;
    </div>
        
&lt;?php  
}
?&gt;
</div>


&lt;?php  
include ('views/footer.php');
?&gt;

CONTROLLER
Code:
function main() {
        $cusID="";
        if($query=$this->po_model->getPOinfo($cusID)) {
            $data['records']=$query;
            }        

        if($query=$this->po_model->getCustomers()) {
            $data['cust']=$query;
        }
        $this->load->view('po/home',$data);
    }

MODEL
Code:
function getPOinfo($cusID) {
        if($cusID == "") {
            $qry=$this->db->get('model_POHeader');
            return $qry->result();  
        } else {
            $this->db->where('cusID',$cusID);
            $qry=$this->db->get('model_POHeader');
            return $qry->result();
        }
    }
#2

[eluser]jedd[/eluser]
I'm not entirely sure I fully understand what you're asking.

[quote author="Tumac" date="1261030178"]
I have a nested view that is part of a tabset. The nested view is returning records of a query to the DB.
[/quote]

Do you mean your nested view receives records from the DB - and returns some HTML?

Quote:I would like to pass a variable from that nested view so that the records returned are only for that "key/variable".

Hmmm. You keep using the word view to describe things I expect models to do.

Here are the links that I usually throw at people when they say some of these magic words.

[url="http://ellislab.com/forums/viewthread/109698/"]http://ellislab.com/forums/viewthread/109698/[/url]
[url="http://ellislab.com/forums/viewthread/110969/"]http://ellislab.com/forums/viewthread/110969/[/url]

Have a quick read through those and see if either enlightens at all.
#3

[eluser]Tumac[/eluser]
I will look through those in a little more detail. From a "codespeak" standpoint, you are right - I am using html/php views to display data from a model. Now since I am beginning with CI and MVC, I still am processing some code at the view level that eventually could be done in the model. Until I get better, I am going to go with what I know works and is technically acceptable.

Now to try and clarify:

- Model collects data and returns to "main view".
- Main view creates a tabbed container based on number of records sent from model. Tabbed container is JS tabs
- The content area of each tab is query records generated by a model that contain more detail specific to the "customer tab".
- Right now, as the code sits, each Tab content returns ALL the records from the model. I want to display only those records that belong to the customer on the tab.

Does that make more sense. My trouble is that since JS is parsing and creating the tabs, when I call the view to deliver the query, I need to be able to compare the customer with the data from the model to display only that customers records.

I am still hacking at it, but thought I would throw it out here. It is almost like when I click the tab, the tab throws a new variable back to the model that creates the subview. My thinking is that maybe I have multiple datasets from the model (multiple functions returning data), sending the array of datasets to the model to parse which one it needs and somehow determine which one to show.

I am sure that just made it go from murk to mud.
#4

[eluser]jedd[/eluser]
Did you notice you didn't use the C word at all in that message? I know you're using a controller - you showed some code earlier! Smile


[quote author="Tumac" date="1261055821"]
... I still am processing some code at the view level that eventually could be done in the model.
[/quote]

Sure, though I think you'd find it easier if you try it now - un-learning bad habits is more difficult, and the framework will fight against you (again, making things more difficult). OTOH it's quite okay to do 'display logic'] in the view, of course. And arguably (some much-smarter-than-me types around here will assert this) it's fine for a view to consult a model - but it may be less confusing if you try to avoid that while you're learning the framework.


Quote:- Model collects data and returns to "main view".
- Main view creates a tabbed container based on number of records sent from model. Tabbed container is JS tabs
- The content area of each tab is query records generated by a model that contain more detail specific to the "customer tab".
- Right now, as the code sits, each Tab content returns ALL the records from the model. I want to display only those records that belong to the customer on the tab.

Okay - models, for most of us, return data to the controller. The controller manages which views get which data at which time, and also any hierarchy structures within the final view (if you're doing partials). Amongst other things, of course.

Tabs are going to confuse the issue here. Are you doing AJAX-ish calls on tab focus, or are you loading up each tab with all the information it needs on every page load? (The former is trickier, the latter is easier.)

Do not return ALL the records from the model - grab ONLY the information you need (from the database, in the model) and return that. You do this because a) it's more elegant, b) it's faster, c) it's less load on the DB and web server, d) it's more secure, e) it's easier to debug, and probably a few more.

Quote:Does that make more sense. My trouble is that since JS is parsing and creating the tabs, when I call the view to deliver the query, I need to be able to compare the customer with the data from the model to display only that customers records.

I can understand it, but I think you're moving the 'which customer am I looking at now?' question from the controller / model (which is where it should be) to the view (which is where it shouldn't).


Quote:I am still hacking at it, but thought I would throw it out here. It is almost like when I click the tab, the tab throws a new variable back to the model that creates the subview.

Each tab should likely be hitting a controller/method that can return the (new) content for the tab (wrapped in a div, presumably). The method will take parameters that let you specify the nature of that content.
#5

[eluser]Tumac[/eluser]
"Each tab should likely be hitting a controller/method that can return the (new) content for the tab (wrapped in a div, presumably). The method will take parameters that let you specify the nature of that content."


Correct - Right now I have it load a view, but would rather pass the variable back to a controller that would use the model to query for "that" customer, return results to controller and send to view. I know how to pass that back via an anchor, but not automatically. Do I need to pass it back via JS?


Here is the routine that dynamically creates the tabs. If I uncomment the " echo $cusID", the cusId will display within the content.

The cusID variable will display, I just need to pass this back now so that the controller can run the query and return the results back to the view.

Code:
foreach($cust as $cus) {
    ?&gt;
    <div id="panel_&lt;?php echo $cus->cusID;?&gt;" class="panel">
       &lt;?php
       $cusID=$cus->cusID;
      // echo $cusID;
      $this->load->view('po/polist',$cusID)
      
       ?&gt;
    </div>
#6

[eluser]Tumac[/eluser]
Almost got this working. But here is the quick rundown on my tabs. Instead of my tab calling a # section lower on the view page, I can have the tab reference an external link. The link includes the cusID in segment(3), which is read by a new controller function. passes the cusID to the model and returns the records properly to the view.

Now the trick is to get the view to result within the tab and not jump to an external page. I know there are code examples to do this, I just need to go out and find em!

Thanks for the help and insight. I am sure there is a way to do this within AJAX/JSON/JQuery but I am better at php right now than those elements.
#7

[eluser]jedd[/eluser]
Ahh .. good. Cos I'm right on the edge of what I'm comfortable with - but there are plenty of other people around here who can pop in with some insights if you get stuck a bit further on.

I'm starting to do some work with jQuery and tabs & accordions at the moment, but having mixed success. So far I've been okay with reloading the page, as I want to have portable (REST-ish) URL's - but I'm about to start pushing this stuff into a site where I don't care about the URL, and prefer the responsiveness of updating each tab on the fly.

If you check the [url="/wiki/FAQ"]FAQ[/url] here you'll see there's one bit about AJAX - and it's worth experimenting with that code partial in there, so that your controllers can be smart about what kind of data they return, based on who is asking them. As you say, there are plenty of code examples floating about - so while it can get a touch complicated, at least you have the benefit of walking down a well-trodden path.
#8

[eluser]Tumac[/eluser]
True on the results. The controller is sending in some mixed results so far or the page is not refreshing the tab. Whan I click on my tabs, I am getting some residuals from the prior tab, but it is at least sending data back to the tab and I can go from there. I am using JQuery UI tabs and Ajax(built in) as my model.

Getting closer! Will post code when complete.




Theme © iAndrew 2016 - Forum software by © MyBB