Welcome Guest, Not a member yet? Register   Sign In
Count how many notification doesn't read by users
#1

Hello guys, cotinue my last question yesterdays !
here is my images design database, please click here

as you can see this table store who user and announcement already read,

this is my way how to ge which one announcement not read yet
Code:
public function notif()
    {
        $items = array(); //make variable array
        $cu_id_user='4';  //get id user
        $data=$this->Notif_model->get_all_notif_not_read($cu_id_user);  //select
    foreach ($data as $row)
       {
            $items[] = $row->cu_anoun_id; //save array cu_anoun_id from announcement
       }
    $rs=$this->db->where_not_in('cu_anoun_id',$items)->get('cu_announcement'); //select where announ cement where not reading yet
    if($rs->num_rows()>0)
        {
            $data=$rs->result_array();
            print_r($data);die;
        }
    }

but not good cause i don't know how to get each data based on this $data=$rs->result_array();
i tried <?php echo $data->cu_anoun_id ?> always get errors Message: Trying to get property of non-object

then i change to be echo $data['cu_anoun_id']; got errors like this Message: Undefined index: cu_anoun_id

might be there is easy way to get how many announcement for each users which one already read or whihc one doesn't read,


Note : All i want too is count how many announcement alreadty read , thanks
Reply
#2

When you pass the array with result_array, it point the array as zero pointing. 
When you print_r($data) you can see it in model.

So to access some of value inside the $data you have to point zero as well

Code:
echo $data[0]['cu_anoun_id'];


And this $data[0] is required. 

Quote:As well as this is only valid inside model an controller only.
Reply
#3

(This post was last modified: 02-02-2016, 06:50 PM by freddy.)

(02-02-2016, 12:45 PM)abdullacool04 Wrote: When you pass the array with result_array, it point the array as zero pointing. 
When you print_r($data) you can see it in model.

So to access some of value inside the $data you have to point zero as well

Code:
echo $data[0]['cu_anoun_id'];


And this $data[0] is required. 

Quote:As well as this is only valid inside model an controller only.

Hi thanks for answer this, but when i only code like this
Code:
echo $data[0]['cu_anoun_id'];
Only show 1 cu_anoun_id, my question is how can i show all data ? means when i should show all data should code like this
Code:
echo $data[0]['cu_anoun_id'];
echo $data[1]['cu_anoun_id'];

Means it repeatation, how can i code with clean code, thanks
Reply
#4

(02-02-2016, 06:49 PM)freddy Wrote:
(02-02-2016, 12:45 PM)abdullacool04 Wrote: When you pass the array with result_array, it point the array as zero pointing. 
When you print_r($data) you can see it in model.

So to access some of value inside the $data you have to point zero as well

Code:
echo $data[0]['cu_anoun_id'];


And this $data[0] is required. 

Quote:As well as this is only valid inside model an controller only.

Hi thanks for answer this, but when i only code like this
Code:
echo $data[0]['cu_anoun_id'];
Only show 1 cu_anoun_id, my question is how can i show all data ? means when i should show all data should code like this
Code:
echo $data[0]['cu_anoun_id'];
echo $data[1]['cu_anoun_id'];

Means it repeatation, how can i code with clean code, thanks


Well please check this out, is it spageti code or not !
Code:
public function notif()
    {
        $items = array();
        $cu_id_user='4';  
        $data=$this->Notif_model->get_all_notif_not_read($cu_id_user);  
    foreach ($data as $row)
       {
            $items[] = $row->cu_anoun_id;
       }
      
      
    $rs=$this->db->where_not_in('cu_anoun_id',$items)->get('cu_announcement');
    if($rs->num_rows()>0)
        {
            $data=$rs->result_array();
            foreach($data as $key => $value)
                {  
                    echo $value['cu_anoun_id']."<br>";
                
                }  
        }
    }

i can get all data but think still spageti code Exclamation
Reply




Theme © iAndrew 2016 - Forum software by © MyBB