简体   繁体   中英

Check if any of the value in resultant array similar to values of existing array with codeigniter

I really need help to display a month list view in which deposit received month should be green in color and no-deposit month should be in red color. I have stuck in comparing the array values. and I am using codeigniter.

I have a table with start-date and end-date by using this I have created an array to display all months in between these dates. Please find the code which I have used to do it below:

$datefrom = strtotime($showrangecalendar['chitdate_start']);
$dateto = strtotime($showrangecalendar['chitdate_end']);

$start_date = date('Y-m-d', $datefrom);
$end_date =  date('Y-m-d', $dateto);
$day = 2.628e+6; // Day in Months  
$format = 'Y-F'; // Output format (see PHP date funciton)  
$sTime = strtotime($start_date); // Start as time  
$eTime = strtotime($end_date); // End as time  
$numDays = round(($eTime - $sTime) / $day) + 1;  
$days = array();  
for ($d = 0; $d < $numDays; $d++) {  
    $days[] = date($format, ($sTime + ($d * $day)));  
}

This code got me the Days list and I have displayed this using foreach statement to a table.

Now I have another table (accounts) with columns such as ID, Book_number, Deposit_Amount and Deposit_Month. Here the problem starts I have fetched data from "accounts" and loaded to my view using the below code from controller

$data['show_account']= $this->Accounts_model->get_all_accounts($sess_data);

My Model (Accounts_Model) is as shown below:

public function get_all_accounts($id)
{
    $this->db->select("*");
    $this->db->order_by('book_id','desc');
    $this->db->from("accounts");
    $this->db->where('bh_id',$id);
    $query = $this->db->get();
    return $query->result();
}

If I run the belowcode:

foreach($show_account as $values){

  echo $values->deposit_month;
 }

Its getting me the array result of all deposit month. Suppose I have data as 2018-Sep and 2018-Oct, These 2 months column should turn green in the above mentioned $days array.

Hope I have explained my requirement clearly. Please help me with this as I am already spent long hours in this.

Thanks in Advance.

Updated:

Now could you please check my model as follows:

public function get_dep_month($bh_id)
{
    $this->db->select('deposit_month');
    $this->db->from('accounts');
    $this->db->where('bh_id',$_SESSION['bh_id']);
    $this->db->order_by('deposit_month','asc');
    $query = $this->db->get();
    return $query->result_array();
}

And My Controller is as follows:

$sess_data = $this->session->userdata('bh_id');
    $data['depM_array'] = $this->Accounts_model->get_dep_month($sess_data);

Now please check my View as follows:

 <?php

                // option 2
                foreach($days as $day) { // using your already exist $day for-loop for display
                    if (!in_array($day, $depM_array)){
                       $calhilight = "calgreen";
                    }
                    else{
                      $calhilight = "calred";
                    }

            ?>
              <li class="<?php echo $calhilight; ?>"><?= $day ?></li>
            <?php      
                }
            ?>

But as my doposit_month column having only 2 values ie: 2018-Sep and 2018-Oct, Instead of getting green color for those 2 values, I am getting Green for who li element. Not getting where I have done wrong.

This is the Current page view which I am getting, Actually I am expecting a calendar view with 2 green fields for 2018-Sep and 2018-Oct and all other fields in Red

Performed Var Dumb in arrays:

Pls check this screenshot

FYI: Below is the code from where I am getting $days array.

 <?php    
$datefrom = strtotime($showrangecalendar['chitdate_start']);
$dateto = strtotime($showrangecalendar['chitdate_end']);

$start_date = date('Y-m-d', $datefrom);
$end_date =  date('Y-m-d', $dateto);
$day = 2.628e+6; // Day in Months  
$format = 'Y-F'; // Output format (see PHP date funciton)  
$sTime = strtotime($start_date); // Start as time  
$eTime = strtotime($end_date); // End as time  
$numDays = round(($eTime - $sTime) / $day) + 1;  
$days = array();  
for ($d = 0; $d < $numDays; $d++) {  
    $days[] = date($format, ($sTime + ($d * $day)));  
}

?>

Here $start_date & $end_date is fetching from another table named chitdate.

Thanks Again.

You can use array-diff or simple for-loop .

Consider the following simple example:

$days = array("2018-September", "2018-October", "2018-November", "2018-December");
$deposit_month = array("2018-September", "2018-October");

// option 1:
$diff = array_diff($days, $deposit_month);
// now $diff has: "2018-November" and "2018-December"

// option 2
foreach($days as $day) { // using your already exist $day for-loop for display
    if (!in_array($day, $deposit_month))
        // color in red as not found in "deposit_month"
    else 
        // color in green
}

Nicer writing for that if can be as:

$color = in_array($day, $deposit_month) ? "Green" : "Red";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM