2

I'm learning CodeIgniter framework. I have difficulty in PHP to convert the script from CodeIgniter.

How to write a script that I made the change to CodeIgniter?

Here's the code that I have done so far:

<?php 
    date_default_timezone_set('Asia');
    $u = $row->jadwalkal;
    $tgl1 = $u;
    $tgl2 = date("Y-m-d");
    $selisih = strtotime($tgl1) -  strtotime($tgl2);
    $hari = $selisih/(60*60*24);
    if ($hari < 0){
    ?>
    <div class="label label-danger"><?php echo "Telat $hari  hari";?></div>
    <?php
    }elseif ($hari < 7) {
    ?>
    <div class="error message"><?php echo "Tinggal $hari hari";?></div>
    <?php
    }else{
    ?>
    <div class="error message"><?php echo "Masih $hari hari";?></div>
    <?php }
?>
5
  • Exactly what are you looking for? Commented Dec 15, 2015 at 6:11
  • You need to refer user guide codeigniter.com/user_guide Commented Dec 15, 2015 at 6:14
  • @rahul, Yes, that is true ,... Commented Dec 15, 2015 at 6:18
  • @DishaV. Thank you but I still do not understand the writing php dicodeigniter Commented Dec 15, 2015 at 6:19
  • Well, you have to study but still here is link for you where you can convert your php form into codeigniter code standards ellislab.com/codeigniter/user-guide/helpers/form_helper.html Commented Dec 15, 2015 at 6:21

2 Answers 2

2

Codeigniter is built by PHP. So, there's no such work as conversion.

However, You can do some good practises available in Codeigniter to make your code look like Native Codes of Codeigniter which utilises helpers.

Here's the good way to start from here

Coming to your question

I would suggest you to pass those values from the Controller to the views like this

$data = array(
    'value' => $row->jadwalkal,
    'someotherkey' => 'someothervalue',
);

$this->load->view('results_view', $data);

And inside the view i would do the conditions like this

You can retrieve the values that is passed form the controller by

echo $value;

The Operation shall look like this from your view

Here's the Clear Syntax

<?php if ($username == 'sally'): ?>

   <h3>Hi Sally</h3>

<?php elseif ($username == 'joe'): ?>

   <h3>Hi Joe</h3>

<?php else: ?>

   <h3>Hi unknown user</h3>

<?php endif; ?>

Here's the conversion for you ;)

<?php if ($value < 0): ?>
<div class="label label-danger"><?php echo "Telat $hari  hari";?></div>
<?php elseif ($value < 7): ?>
<div class="error message"><?php echo "Tinggal $hari hari";?></div>
<?php else: ?>
<div class="error message"><?php echo "Masih $hari hari";?></div>
<?php endif; ?>

Note :

I have given a simple example to use if .. else operations

You shall modify according to your need.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks @Sulthan Allaudeen, I already know the plot , but there was an error on $selisih = strtotime($tgl1) - strtotime($tgl2);
You want the result in seconds ?
You shall also do echo round((strtotime($tgl2) - strtotime($tgl1))/3600, 1); to make it simple ;)
1

In your case you must use the Controller And View.. Declare all your variables in your Controller something like this here's the sample:

Controller: main.ph

class Main extends CI_Controller {

    function __construct() {
        parent::__construct();
        date_default_timezone_set('Asia/Manila');
    }

    public function sample()
    {
       $u = date("Y-m-d"); //I just make sample value here
       $tgl1 = $u;
       $tgl2 = date("Y-m-d");
       $selisih = strtotime($tgl1) -  strtotime($tgl2);
       $data['hari'] = $selisih / (60*60*24);    

       $this->load->view('sample',$data)       
    }
}

View: sample.php

    if ($hari < 0){

       echo '<div class="label label-danger">Telat '. $hari .' hari</div>';

   }else if($hari < 7) {

       echo '<div class="error message">Tinggal '. $hari. ' hari</div>';

   }else{

       echo '<div class="error message">Masih '. $hari . 'hari</div>';
   }

12 Comments

thanks @user5666946, i can understand the script you wrote , but there is an error in the script $selisih = strtotime($tgl1) - strtotime($tgl2); Message: strtotime() expects parameter 1 to be string, array given
can I see the value you put in strtotime()
ok i get $u from model, $u = $this->mcrud->getDate(); this script public function getDate(){ $this->db->where('jadwalkal'); $query = $this->db->get('tbdetail'); return $query->result(); }
This return $query->result() returns array list. Change it to this return $query->rows()->column_name;
sorry it's only $query->row()->jadwalkal; hehe
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.