Welcome Guest, Not a member yet? Register   Sign In
->result() not showing all rows/data
#1

(This post was last modified: 07-09-2019, 07:13 PM by acebay.)

Hi all, I have a question

Let say we have this 2 tables in our database

first table : category_lists

category_lists_id   |    category
============================
1                         |    Accident Management
2                         |    Aluminium
3                         |    Brass
4                        |    Car Services
5                         |    Car Towing
6                        |    Car Wreckers
7                         |    Cash For Cars


second table: data_category


id                        |   list_data_id        |       category_lists_id 
===================================
1                         |    3                     |          5
2                         |    3                     |          6
3                         |    3                     |          7  
4                        |    2                      |          8
5                         |    4                     |          5
6                        |    1                      |          6
7                         |    1                     |          7


so if list_data_id is equal to 1 meaning that it has 2 data if you look at data_category table which is 6 and 7

Now what I want is to query from category_list table so that 6 and 7 will echo Car Wreckers and Cash For Cars.

This is what my code looks like:

below is my controller
PHP Code:
public function index($listing_name)
    {

        
$this->load->view('layouts/head_layout_seo');
        
$this->load->view('layouts/head_layout');

        
$data['main_view'] = 'listings/main_view';
        
$data['listing_data'] = $this->Listing_model->get_detail_listing($listing_name);

        
$list_id $data['listing_data']->list_data_id// this is what I get list_data_id is equal to 1

        
$data['category_ads'] = $this->Ads_model->get_ads($list_id); // this is what you need to look

        
$this->load->view('layouts/main'$data);

        
$this->load->view('layouts/footer_layout');

    } 


below is my model
PHP Code:
    public function get_ads($list_id)
    {
        
$this->db->where('list_data_id'$list_id);
        
$query $this->db->get('data_category');
        
$query $query->result();
        
//return $query;

        
if (count($query) > 0) {    
            for (
$i=0$i count($query); $i++) { 
                foreach (
$query as $value) {
                    
$this->db->where('category_lists_id'$value->category_lists_id);
                    
$querys $this->db->get('category_lists');
                    
//print_r($query);
                    
return $querys->result();
                }
            }    
        }
    } 



below is my view:

Code:
<?php foreach ($category_ads as $value) { ?>
    <p><a href=""><?php echo $value->categories; ?></a></p>
<?php } ?>


with the code above I get only 1 data which is Car Wreckers as you can see from the table it suppose to show 2 data Car Wreckers and Cash For Cars

Can anyone help me with this?

Thank You
Reply
#2

(This post was last modified: 07-09-2019, 11:42 PM by neuron.)

[quote pid='365920' dateline='1562724737']
PHP Code:
                            foreach ($query as $value) {
                    
$this->db->where('category_lists_id'$value->category_lists_id);
                    
$querys $this->db->get('category_lists');
                    
//print_r($query);
                    
return $querys->result();
                } 

[/quote]

I did not read all your question BUT why there is return statement in your foreach loop?

Also you should learn SQL, this is not the right of doing such a query. Learn about JOIN clause in SQL, this is basics and power of RDBMS.
It will take some time to learn, but it is what everybody has to do.
Reply
#3

Have a look at the Database Reference in the CI documentation.
Use the JOIN statement to get the result from 2 tables.
Reply
#4

(07-09-2019, 07:12 PM)acebay Wrote: Hi all, I have a question

Let say we have this 2 tables in our database

first table : category_lists

category_lists_id   |    category
============================
1                         |    Accident Management
2                         |    Aluminium
3                         |    Brass
4                        |    Car Services
5                         |    Car Towing
6                        |    Car Wreckers
7                         |    Cash For Cars


second table: data_category


id                        |   list_data_id        |       category_lists_id 
===================================
1                         |    3                     |          5
2                         |    3                     |          6
3                         |    3                     |          7  
4                        |    2                      |          8
5                         |    4                     |          5
6                        |    1                      |          6
7                         |    1                     |          7


so if list_data_id is equal to 1 meaning that it has 2 data if you look at data_category table which is 6 and 7

Now what I want is to query from category_list table so that 6 and 7 will echo Car Wreckers and Cash For Cars.

This is what my code looks like:

below is my controller
PHP Code:
public function index($listing_name)
    {

        
$this->load->view('layouts/head_layout_seo');
        
$this->load->view('layouts/head_layout');

        
$data['main_view'] = 'listings/main_view';
        
$data['listing_data'] = $this->Listing_model->get_detail_listing($listing_name);

        
$list_id $data['listing_data']->list_data_id// this is what I get list_data_id is equal to 1

        
$data['category_ads'] = $this->Ads_model->get_ads($list_id); // this is what you need to look

        
$this->load->view('layouts/main'$data);

        
$this->load->view('layouts/footer_layout');

    } 


below is my model
PHP Code:
    public function get_ads($list_id)
    {
        
$this->db->where('list_data_id'$list_id);
        
$query $this->db->get('data_category');
        
$query $query->result();
        
//return $query;

        
if (count($query) > 0) {    
            for (
$i=0$i count($query); $i++) { 
                foreach (
$query as $value) {
                    
$this->db->where('category_lists_id'$value->category_lists_id);
                    
$querys $this->db->get('category_lists');
                    
//print_r($query);
                    
return $querys->result();
                }
            }    
        }
    } 



below is my view:

Code:
<?php foreach ($category_ads as $value) { ?>
    <p><a href=""><?php echo $value->categories; ?></a></p>
<?php } ?>


with the code above I get only 1 data which is Car Wreckers as you can see from the table it suppose to show 2 data Car Wreckers and Cash For Cars

Can anyone help me with this?

Thank You
hai,
    This is called under the topic of dynamic dependent drop down list. Search in the google
https://www.webslesson.info/2018/06/code...-ajax.html
It helps you.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB