The goal in following code is the get a select filled up accordingly to another select selected option.
CategoryList.php
<?phpclass CategoryList extends CI_Controller{ public function __construct() { parent::__construct(); $this->load->model( 'categories_model' ); } public function index() { } public function list_products() { //Get all the categories in the system $data["categories"] = $this->categories_model->get_categories(); //Loading the view for the record edition $this->load->view( 'templates/header', $data ); $this->load->view( 'list_products', $data ); $this->load->view( 'templates/footer' ); }}
<?phpclass SubcategoryList extends CI_Controller{ public function __construct() { parent::__construct(); $this->load->model( 'subcategories_model' ); } public function index() { } public function list_dropdown() { $cat_id = $this->input->post( 'cat_id' ); //Read the category id sent by POST $subcategories = $this->subcategories_model->get_subcategories_by_category( $cat_id ); //Get the matches list for that tournament from the DB $data["subcategories"] = $subcategories; $this->load->view( 'list_dropdown_view', $data ); }}
$(document).ready(function(){ var subcategories = []; $(document).on('change', 'select#category_list', function() { var cat_id = $('select#category_list').val(); //Get the id of the tournament selected in the list $.ajax( { type: 'POST', url: baseurl+'subcategorylist/list_dropdown', //We are going to make the request to the method "list_dropdown" in the match controller data: 'cat_id='+cat_id, //POST parameter to be sent with the tournament id success: function(resp) { //When the request is successfully completed, this function will be executed //Activate and fill in the matches list subcategories = resp; $('select#subcategory_list').html(resp); //With the ".html()" method we include the html code returned by AJAX into the matches list } }); }); $('select#category_list').trigger('change');});
What's the problem, now? I'd like to use knockout.js and I have two distinc questions about:
1. I have the doubt using knockout.js (or similar javascript framework) within a php framework ibe useless or at lease less useful than using it without a php framework: after all, the above code get the same result, maybe a better result in a whole perspective: more robust, more secure (it's server side), a bit less elegant, maybe, but concise and clear. So the question is: does it make sense to try to integrate a javascript framework with a php frame work and more specifically knockout.js with CodeIgniter? If the answer is 'No', the second question can just be skipped.
2. The second question is: how to implement knockout.js logic? I tried to modify the list_product.php code this way:
<div class="slider-wrapper"></div><div class="container marketing"> <!-- Three columns of text below the carousel --> <div class="row"> <div class="col-lg-12"> <strong>Categories:</strong> <select id="category_list"> <?php foreach ( $categories as $category ) { echo "<option value='{$category["category_id"]}'>{$category["category_name"]}</option>"; } ?> </select> <br /> <strong>Match:</strong> <select id="subcategory_list" data-bind="options: subcategories"> </div> </div></div>
But I can't fighure out how to modify views and I'm just blocked here.
I know this is can be a hard question and I apologize if it's too theoretical or confused: I'm confused me too :-)
As usual, thanks in advance to all.
As a side note: can I win something for the most long question posted at EE? lol
PHPJavaScriptjQuery
Last Comment
Rob
8/22/2022 - Mon
Ray Paseur
Marco: No points for this, because it's not an answer, but we've been colleagues long enough for me to feel OK making a suggestion. Ditch CodeIgniter and learn about Laravel. It's an amazing framework. Fast, powerful, fluent, easy to use, with many of the "convention over configuration" practices of Ruby on Rails, but in a PHP environment. I've been on a Laravel project for a couple of months and I'm in awe of the framework.
Give yourself one hour to watch the Harvard video and you'll "get it" and you'll be able to start developing in Laravel.
All the best, and Happy New Year, ~Ray
Marco Gasi
ASKER
Hi Ray, How do you do? It's a long time we don't meet here at EE: I confess I'm too busy to stay here mutch time, but usually when I post a comment, later or soon you post a comment you too :-)
I heard about Laravel and I yet read something about from you here at EE. Only a question: I need to install composer or such a software in the server or I just have to use them in my computer and then I can just upload the ite to the server? Working on shared server usually makes difficult install extra-software...
Happy New Year to you too
Marco
Rob
Hi Marco / Ray
I've also heard good things about Laravel but couldn't get it working (on ubuntu mind you). But the principles are the same (mvc) but it's php as much as codeigniter except it does require being installed along with its own package manager so you may have trouble with your host (as I found out recently trying to get node.js installed on my host)
Codeigniter is also better documented at this point in time. I found their reference material terrible compared with other frameworks and that makes a huge difference when you run into issues.
There are multiple frameworks out there that all do a good job. You just have to pick one that suits your needs and skill level. Keep it simple!
Hi Rob. Your words are conforting, since I'd just setup a project succeeding in having within CodeIgniter the same things I had in another framework. I'm also installing Laravel in my machine (Windows) and I get in some trouble: overall, I have the impresion, as you confirm, that to get it working you need a your server where you can install and uninstall and configure what you want when you want. Anyway, I just love to learn, so I'll study it.
But to come back to my question, what about the relationship between CodeIgniter and Knockout.js? And what about the code? :-)
Rob
I'll open a discussion on it as it would be good to get Ray's input on it given he's got it working.
As for your code:
Firstly, yes you can use knockout with codeigniter and most other mvc frameworks but it's more about why you would. It depends on your project.
Rob
It depends on the content you're showing. If your views are mainly static or with minimal dynamic content then you may not need knockout.
On the other hand, if you find that you want an heavily driven ajax page or your view becomes messy with php code with loops displaying data etc, then it's time to use knockout as it separates the logic from the markup. Your markup is cleaner and it's easier to understand what's going on. I think that's how you got here in the first place right? Because one of your php pages was so messy?
Sure, Rob. It's up to you! ;-) I was terribly fascinated by your knockout example and I felt in love with it. I only thought that with the MVC pattern, logic and look are yet separated, but with a lot of dynamic content, i'd end with a lot od messy code again for sure!
So, the answer to my first question is 'Yes'. Now, can you give me some suggestion about the code above? Don't want you wtire my code, only a little suggestion...
I should also add you could do the above without knockout, however you would have to handle the modifying / recreation of the markup when new data (subcategories) are selected. e.g.
Simple example above but hopefully you can see that with knockout, the markup is already in your view, without it, your markup is in your javascript and in my opinion a messy way and confusing way to create your content. It also marks it harder to apply styles on the fly. As an example, you can test the way your page looks with subcategories once a category has been selected by simply adding dummy data to the subcategory: ko.observableArray(['subcat1','subcat2', ...]);
Marco Gasi
ASKER
Wow. Thank you Rob. Give me the time to digest all (my wife requires my active collaboration to prepare the celebration of 2015 so I'll have to stay away from my beloved pc) and I'll come back to let you know. Thank you vary mutch for your effort in helping me. :-)
I trivially followed the linked tutorial: I have to learn a lot about MVC: only one controller per site, uh? I'll try to get it working later...
Anyway, I think to have understood the point o I can give you the points :-) On to the next question (I'm sure I'll have a lot to do...)
Just to revise my one controller comment as you can obviously have more than one, however they would want to be logically separated if you did. E.g. authentication controller that just manages everything to do with authentication. Your home controller (for want of another name) to provide functions for the content of your site.
So getting categories and sub categories would be in the same controller as they both related to content for your site.
My pleasure and hi from 2015! You must be almost in 2015 now eh?
Marco Gasi
ASKER
less than four hours and I'll be in: you live in the future!
Marco Gasi
ASKER
Hi Rob. Now I'm in 2015 me too.
Idecided to defer my knockout.js training because I'm not intelligent enough to learn both mvc and knockout at the same time realizing a project with a three weeks deadline: I'll be happy if I'll do a good job with CodeIgniter and a bit of my old messy code ;-)
And here a question which has no comments yet: https://www.experts-exchange.com/questions/28589380/CodeIgniter-dynamic-menu-and-routes.html
Cheers
Marco
Plus, they teach it online at Harvard University!
https://www.youtube.com/watch?v=XwhZ4xX7Qmc
Give yourself one hour to watch the Harvard video and you'll "get it" and you'll be able to start developing in Laravel.
All the best, and Happy New Year, ~Ray