Display Nested categories |
[eluser]shailendra[/eluser]
I want to display various project categories and no. of projects posted for each catgory. For example Web & Programming(2) -PHP(1) -ASP(1) Graphic Designing(2) -Logo(2) -Website Design(0) I did this in php using following code. How do I do this in codeigniter? Code: <?php Function to list all parent category Code: /* Function to list all parent category*/ Display all projects in a parent category Code: //Display all projects in a parent category
[eluser]toopay[/eluser]
First, you need to move your database abstraction into a separated class : read CI Model Spec to do that. Once you've done with that, create a Controller to handle the request, and when you need to interact with your database, call your model class. In CI, you do Code: // This is actually instantiate a model class Code: // This probably still works, if you include the file 'bar_model.php' before this line, but it is not the way CI works.
[eluser]shailendra[/eluser]
Basically, I want to know how to pass data from 3 different model functions to a view(via controller) and access it using foreach loop? Model Code: function list_parent_categories() I want to call above functions in a controller and pass the data to a single view so that I get this output Web & Programming(2) -PHP(1) -ASP(1) Graphic Designing(2) -Logo(2) -Website Design(0) Please help?
[eluser]toopay[/eluser]
In controller... Code: public function index()
[eluser]shailendra[/eluser]
Based on the above code given I tried doing this In Controller Code: function index() In view Code: <?php But I get following error: ---------------------------- A PHP Error was encountered Severity: Notice Message: Trying to get property of non-object Filename: views/category_view.php Line Number: 3
[eluser]toopay[/eluser]
If everythings goes smooth, in your view you just need to parse the "categories" variable as array (not object). So you do... Code: foreach($categories as $category)
[eluser]shailendra[/eluser]
I get this error now with the above view code: ------------------------------ A PHP Error was encountered Severity: Notice Message: Undefined index: category_name Filename: views/category_view.php Line Number: 4
[eluser]shailendra[/eluser]
I tried your previous code you gave on 15 July. I modified the controller code to include sub category In controller Code: function index() In View Code: <?php I get following error: Fatal error: Cannot use object of type stdClass as array in C:\Program Files\Apache Group\Apache2\htdocs\codeigniter\application\views\category_view.php on line 4 Below is var_dump($categories) output ------------------------------------- array(11) { [0]=> object(stdClass)#17 (2) { ["category_id"]=> string(2) "10" ["category_name"]=> string(13) "Admin Support" } [1]=> object(stdClass)#18 (2) { ["category_id"]=> string(2) "15" ["category_name"]=> string(8) "Coaching" } [2]=> object(stdClass)#19 (2) { ["category_id"]=> string(2) "12" ["category_name"]=> string(27) "Engineering & Manufacturing" } [3]=> object(stdClass)#20 (2) { ["category_id"]=> string(1) "2" ["category_name"]=> string(17) "Graphic Designing" } [4]=> object(stdClass)#21 (2) { ["category_id"]=> string(2) "13" ["category_name"]=> string(5) "Legal" } [5]=> object(stdClass)#22 (2) { ["category_id"]=> string(2) "14" ["category_name"]=> string(6) "Others" } [6]=> object(stdClass)#23 (2) { ["category_id"]=> string(2) "11" ["category_name"]=> string(17) "Sales & Marketing" } [7]=> object(stdClass)#24 (2) { ["category_id"]=> string(1) "1" ["category_name"]=> string(17) "Web & Programming" } [8]=> object(stdClass)#25 (2) { ["category_id"]=> string(1) "9" ["category_name"]=> string(22) "Writing & Translations" } ["sub_category"]=> array(9) { [0]=> array(0) { } [1]=> array(0) { } [2]=> array(1) { [0]=> object(stdClass)#26 (2) { ["category_id"]=> string(2) "16" ["category_name"]=> string(14) "Product Design" } } [3]=> array(3) { [0]=> object(stdClass)#16 (2) { ["category_id"]=> string(1) "6" ["category_name"]=> string(4) "Logo" } [1]=> object(stdClass)#28 (2) { ["category_id"]=> string(1) "7" ["category_name"]=> string(7) "Website" } [2]=> object(stdClass)#29 (2) { ["category_id"]=> string(1) "8" ["category_name"]=> string(10) "Stationery" } } [4]=> array(0) { } [5]=> array(0) { } [6]=> array(0) { } [7]=> array(3) { [0]=> object(stdClass)#27 (2) { ["category_id"]=> string(1) "3" ["category_name"]=> string(3) "PHP" } [1]=> object(stdClass)#31 (2) { ["category_id"]=> string(1) "4" ["category_name"]=> string(3) "JSP" } [2]=> object(stdClass)#32 (2) { ["category_id"]=> string(1) "5" ["category_name"]=> string(3) "ASP" } } [8]=> array(0) { } } ["proj_counts"]=> array(3) { [0]=> string(1) "2" [1]=> string(1) "1" [2]=> string(1) "3" } }
[eluser]toopay[/eluser]
In your view, just convert the object into an array, before looping it... Code: <?php |
Welcome Guest, Not a member yet? Register Sign In |