Duwi irwanto
Duwi irwanto

Reputation: 139

Syntax PHP to CodeIgniter

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 }
?>

Upvotes: 2

Views: 620

Answers (2)

Felix Kamote
Felix Kamote

Reputation: 342

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>';
   }

Upvotes: 1

Sulthan Allaudeen
Sulthan Allaudeen

Reputation: 11310

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.

Upvotes: 2

Related Questions