Nikos Kostis
Nikos Kostis

Reputation: 9

Update database entries

I have a project made using CodeIgniter 3 where I have created 2 view pages. On the 1st page, the user sees a table with the columns (Order Number, Customer, Status of the order, Date Created, and comments), but I show only the pending orders. On the second page, I have the exact columns but I show only the completed orders.

Here's what the 1st page looks like:
Here's what the 1st page looks like

Now what I want to do is on the 1st page when I check the check boxes in each row I want and after pressing the Change Status button to be able to change the status in the database. I have managed to pass the id's of the rows checked by doing this:

function change_status(x){
         var vals = [];
         jQuery('.checkorders:checked').each(function(){
                                          
             vals.push(jQuery(this).val());                                      
                                                     });
         $.ajax({
                type: "POST",
            url: '<?php echo base_url();?>change_status.php' ,
            data:{'myData':vals,'type':x},
            success: function(response)
            {
                 alert('ok');
            }
        });
        
        }

Now the code in the change_status.php is

<?php
include('application/views/backend/admin/dbc.php');
mysqli_query($conn,"SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'");
$type=$_POST['type'];
$mydat= $_POST['myData'];
foreach($mydat as $dt)
{
    $sql0 = "SELECT * FROM orders WHERE id=".$dt." order by id ASC";
        $result0 = $conn->query($sql0);
        if ($result0->num_rows > 0) {
            while($row0 = $result0->fetch_assoc()) {
            $orders_id[]=array("id"=>$row0['id']);
            echo $row0['id']."<br/>";
        }
    }
}
if($type==1)
$out = "**";
{
    foreach($orders_id as $ID)
    {
        $out.= $elispisID['id']." - ";
        //$this->db->where('id', $elispisID['id']);
        //$this->db->update('elipsis', array('status' => 0));
    }
    echo $out;
    $output1="csv";
    $output2="csv/patoures/";
    $output3=$output2.date("Y")."/";
    $output4=$output3.date("m");
    if (!is_dir($output1)) {mkdir($output1,0777);}
    if (!is_dir($output2)) {mkdir($output2,0777);}
    if (!is_dir($output3)) {mkdir($output3,0777);}
    if (!is_dir($output4)) {mkdir($output4,0777);}
    $output_forder=$output4."/".date("dHi");
    file_put_contents($output_forder,$out);
    
}

$conn->close();
 ?>

The $out variable is to help me see if i pass the id's correctly, as every time i check the checkboxes and press the Change Status button it creates a file with the id's selected. But if I uncomment the 2 lines that I try to update the database it doesn't update it or prints anything

Does anyone have any idea what I am doing wrong?

Upvotes: 0

Views: 56

Answers (1)

Kinglish
Kinglish

Reputation: 23664

Ok, heres your page in codeigniter

Keep everything as it is in your JS, but change the url to this

  url: '<?php echo base_url();?>changestatus/change',

Then create this file, called Changestatus.php in your controllers folder. I saw that you were selecting and looping through your DB just to get the list of id's that you're sending through the ajax (did I get that wrong)? All you need to do is update your elipses table and set status=0 where id in (array of ids).

<?php
// controllers/Changestatus.php

class Changestatus extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->library('db'); // this should already be autoloaded in config/config.php but just in case
    }

    function change() {
        $type = $this->input->post('type');
        $mydat = $this->input->post('myData');

        $this->db->set('status', '0');
        $this->db->where_in('id', $mydat);
        $done = $this->db->update('elipsis');  
        if (!$done) {
            die("error: <pre>".print_r($this->db->error(),1)."</pre>");
        }

        // I have no idea what all this is....
        $output1 = "csv";
        $output2 = "csv/patoures/";
        $output3 = $output2 . date("Y") . "/";
        $output4 = $output3 . date("m");
        if (!is_dir($output1)) {mkdir($output1, 0777);}
        if (!is_dir($output2)) {mkdir($output2, 0777);}
        if (!is_dir($output3)) {mkdir($output3, 0777);}
        if (!is_dir($output4)) {mkdir($output4, 0777);}
        $output_forder = $output4 . "/" . date("dHi");
        file_put_contents($output_forder, $out);

    }
}

?>

Upvotes: 1

Related Questions