CodeIgniter Forums

Full Version: PHP Scrit in C.I Method - Convertion
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello Guys,

Can someone please help me to convert this procedural php script in the C.I method?

I would be immensely grateful

I'm trying, but i'm doing something wrong.


Thank you in advance.

This is the procedural script PHP:

Code:
<?php
   if ($_POST["sorte"] == "N")
   {
      $zahlung = $_POST["liter"] * 1.35;
      $text = "Normal";
   }
   else
   {
      $zahlung = $_POST["liter"] * 1.40;
      $text = "Super";
   }
   echo $_POST["liter"] . " Liter $text kosten $zahlung &euro;";
?>


This is the html Form:

Code:
<form action = "u_ifelse1.php" method = "post">
    <p><input name = "liter"> Menge in Liter</p>
    <p><input name = "sorte"> Sorte (S oder N)</p>
    <p><input type = "submit">
    <input type = "reset"></p>
</form>


Now there comes my try - 1° CI_Controller:

Code:
class U_ifelse1 extends CI_Controller{
    
    function __construct()
    {
        parent::__construct();
        // Your own constructor code
    }

    public function index($page = 'home_23')
        $Form_data['form_fields'] = $this->input->post();

        $payment = "";
        $text = "";

        if ($Form_data['form_fields'] == "N")
        {                               
                $payment = $Form_data['liter'] * 1.35;
                $text = "Normal";               
                $this->load->view('pages/'.$page, $Form_data);               
        }
        else
        {       
                $payment = $Form_data['form_fields'] * 1.40;
                $text = "Super";
                $this->load->view('pages/home_24', $Form_data);
        }

    }
}

2° The form - home_23.php

Code:
<body>
    <!-- class U_ifelse1 extends CI_Controller -->
    
    <h3><p>Please enter quantity and type - sorte</p></h3>

    <?php echo form_open('u_ifelse1/index'); ?>

    <p><input name = "liter"> Menge in Liter</p>

    <p><input name = "sorte"> Sorte (S oder N)</p>
        
        <p><input type="submit" name="senden">
            <input type="reset"></p>
    
    <?php echo $form_fields["liter"] . " Liter $text kosten $payment &euro;"; ?>

    :: Ergebnis des Tankvorgangs
    <!-- https://stackoverflow.com/questions/7020873/show-hide-div-if-if-statement-is-true -->
    <?php echo form_close(); ?>

</body>


3° - Third file home_24.php

Code:
<body>
    <!-- class U_ifelse1 extends CI_Controller -->

    <?php echo $form_fields["liter"] . " Liter $text cost $payment &euro;"; ?>


</body>
Give all your inputs names, including the submit button. You can use the name "submit".
Your controller should load the form view only if "submit" wasn't posted.

The common logic for a function like that is:

PHP Code:
if (! $this->input->post('submit')) {
  //show the form
}
else {
  //show the view with results

In a later version, you can add form_validation.

The line
PHP Code:
if ($Form_data['form_fields'] == "N" 

doesn't make sense, because $form_data['form_fields'] is an array with all post values.
To determine if $sorte == "N", do this:
PHP Code:
if ( $this->input->post('sorte') == 'N') {
  ...