How to convert php program into code igniter?

<?php

include_Once("postgresconnection.php");
include_Once("mongoconnection.php");

$venue    = $_POST['venue'];
$building = $_POST['buildingname'];
$floor    = $_POST['floorlevel'];
$lanmark  = $_POST['landmark'];
$mac      = $_POST['Clientmac'];
$T1       = $_POST['starttime'];
$T2       = $_POST['endtime'];
$d1       = new DateTime($T1);
$d3       = $d1->getTimeStamp();
$d2       = new DateTime($T2);
$d4       = $d2->getTimeStamp();

// check max number of records & take 50% for validity condtition
$maxrecords = ($d4 - $d3) / 5;
$minrecords = $maxrecords / 2;

$data   = $collection->distinct("ap");
$jvalue = count($data);

// Check validity
$dat = $collection->count(array(
    "channel" => array(
        '$lte' => 11
    )
));

$data = $collection->count(array(
    "channel" => array(
        '$gte' => 36
    )
));

// condition to check frequency band and Valid data details insertion in PostgreDB table

if (($dat / $jvalue) > $minrecords && ($data / $jvalue) > $minrecords) {
    $array = file_get_contents('/json/parser.json');
    print_r ($array);
    $multi     = array(
        2.4,
        5.0
    );
    $arrlength = count($multi);
    $searchfor = $Lanmark;
    
    for ($i = 0; $i < $arrlength; $i++) {
        $Val        = $multi[$i];
        $query      = "SELECT * FROM tablename WHERE buildingname = '" . trim($building) . "' AND floorlevel = '" . trim($floor) . "' AND landmark ='" . trim($lanmark) . "' AND radio ='" . trim($Val) . "'"; //Checking wheather data already exist in table
        $findResult = pg_query($query);
        $numrows    = pg_num_rows($findResult);
        if ($numrows > 0) {
            echo " ";
        } else {
            $query = "INSERT INTO tablename(venue, buildingname, floorlevel, starttime, endtime, landmark, mac, radio) VALUES('" . $venue . "', '" . $building . "', '" . $floor . "','" . $T1 . "', '" . $T2 . "', '" . $lanmark . "','" . $mac . "', '" . $Val . "')"; // If data doesn't exist insert in DB
            $ret   = pg_query($query);
            if (!$ret) {
                echo pg_last_error($query);
            } else {
                echo " ";
            }
        }
    }
}

else {
    echo "Not getting data";
}

$condition = array(
    "enc_client" => $mac,
    "timestamp" => array(
        '$gte' => $d3,
        '$lte' => $d4
    ),
    "age" => array(
        '$gte' => 1,
        '$lte' => 5
    )
);

// fetch data from db

$cursor   = $collection->find($condition);
$num_docs = $cursor->count();
//var_dump($num_docs);

foreach ($cursor as $doc) {
    echo " ";
}

?>

A lot of refactoring would be needed, but you should probably split the code into models and controllers. This would mean probably rewriting most part of the code.

I’d advise to read about MVC basics and go through CodeIgniter documentation before starting.

Thanks,

I have read something about Model,view and controller. I have made changes into database and created controller too but getting issues with model and view, not able to create those.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.