开发者

Codeigniter and nested tab markup

开发者 https://www.devze.com 2023-02-25 04:06 出处:网络
I need some guidance. Forgive me for a lengthy post but I need to explain myself. In my world, there is no one who understands what I am talking about so I have to go online for assistance.

I need some guidance. Forgive me for a lengthy post but I need to explain myself. In my world, there is no one who understands what I am talking about so I have to go online for assistance.

I am an architect doing my own website because work has dried up and I plan to use an improved website for a marketing campaign.

I have done what I'd call a "lash-up" of this site which functions OK. But it's nowhere near ready to publish. I am trying to get it reorganised to do this and am moving the whole thing over to Codeigniter. My puzzle relates to views in Codeigniter.

One of the main pages for potential clients is the projects page showing work done. It uses nested tabbing. As I have said, I've made it work OK in ordinary procedural PHP.

Note that the projects are organised by category i.e housing, commercial etc In each category there are projects.

Actually the tabs are dynamically produced with some assistance from jQuery. I mean by this that my homespun php creates markup based on what's returned from the database.

The tab markup is the usual one of an unordered list whose li elements contain anchors whose hrefs reference divs arranged below. To achieve nesting, these divs then contain another ul with a further set of divs re开发者_开发知识库lated to it.

The top tabs correspond to a category e.g housing, commercial. The lower tabs correspond to projects within a category.

I've made this work with four queries before. I think at least one may be redundant but I said it was a "lash-up".

Query 1: "select distinct pcat, pcatid from pcategory inner join projects on pcatid = projcat order by pcat desc"

From this query I get hold of the id used in the href.

Query 2 : same as above but this time the id is used for div id.

The next query is the source of my puzzlement because I don't see how to replicate it with CI.

Query3 :

$jobcat=$row2['pcatid']; 
$queryall3 = "select projid, projtit, projcost, projdate from projects where projcat= '$jobcat'";

This query uses the category id - $jobcat - returned by each iteration of the while clause used to expand the results from query 2. In other words, it runs inside the query 2 while loop so it can get the category id and then get all the projects related to it.

The results of query 3 are used to form the lower tabs, and their href value is the id of the project.

Query4: same as query 3 and used to populate the lower divs with data from the database relating to a specific project.

So, finally my question: it seems to me that query 3 is difficult to manage using the Codeigniter set up. I can imagine an array of results looped over in a view. What I can't conceive is how to make a model call within that loop.

Apologies for a long-winded question and any maladroit coding assumptions exhibited. Assistance would be a blessing.

Tom


I don't really see what you're asking, but it seems that you want to know how to perform queries in CI?

In which case I suggest you take a good read of the docs

$this->db->select('pcat, pcatid');
$this->db->distinct();
$this->db->from('pcategory');
$this->db->join('projects', 'pcategory.pcatid = projects.pid', 'inner');
$this->db->order_by('pcat', 'DESC');
$result = $this->db->get();

I very much doubt this will work as I do not know your table structure but may give you an idea of how to use the active record class in CI.

You can of course just use the query method:

$results = $this->db->query('YOUR QUERY HERE');

the active record class can do a lot of the work for you, however.

As for about being difficult to do in CI, this is simply untrue - you just need a clearer picture and understanding of what you want to achieve.

Edit

$jobcat=$row2['pcatid']; 
$queryall3 = "select projid, projtit, projcost, projdate from projects where projcat= '$jobcat'";

$results=$this->db->query($queryall3);
$data = $results->result_array(); // get the results of the 3rd query as an array


// new query
$this->db->select('query_4_select'); // select whatever you need
$this->db->from('whatever_table');
// this probably isn't the most efficient way, but for examples sake:
foreach($data as $row) // using the result_array from above
{
    $this->db->or_where('query_4_id', $row['id']); // the magic !!
}
$new_results = $this->db->get();

So essentially, you get the ID's from query 3, run it through a foreach and build a where x=x or x=y or x=b type query, which will then (hopefully) return the desired results.

This is one way, you will need to tweak it.

But it sounds like you can just use a join? Perhaps if you could post your entire tables structure.

0

精彩评论

暂无评论...
验证码 换一张
取 消