Ciprian
Ciprian

Reputation: 3226

Return multiple rows with ajax from database - Codeigniter

View: Trying to retrieve items based on length, width and height.

<form method="post" action="">

  <label for="title">L</label>
  <input type="text" name="length" id="length" value="" />
</br>
  <label for="title">W</label>
  <input type="text" name="width" id="width" value="" />
</br>
  <label for="title">H</label>
  <input type="text" name="height" id="height" value="" />

  <input type="submit" name="submit" id="submit" />
</form>

js file:

$("#submit").click(function (e) {
event.preventDefault();

    var length = $('#length').val();
    var width = $('#width').val();
    var height = $('#height').val();

    $.ajax({
        url: "http://domain.com/index.php/welcome/get_items",
        data: {
            length: length, width: width, height: height
        }
    }).done(function(data) {
        $('div#cont').text(data.content);
    }).fail(function() {
        alert('Failed!')
    });

});

Controller: I m not sure how to structure this. If only one value is typed in, it should look for that, if two it should match the row that has the two dimensions, and the same if all three are present. Do I check and see which value is given and create multiple model functions?

I also seem to have a problem with the foreach statement below. It won't return multiple rows.

public function get_items()
{
        $this->load->model('home_model');

            $this->load->library('form_validation');
            $this->form_validation->set_rules('length', 'Length', 'trim|required|xss_clean');
            $this->form_validation->set_rules('width', 'Width', 'trim|required|xss_clean');
            $this->form_validation->set_rules('height', 'Height', 'trim|required|xss_clean');

            $length = $_GET['length'];
            $width = $_GET['width'];
            $height = $_GET['height'];

            $one_exp = $this->home_model->one_exp($length, $width, $height);

        if($one_exp != false){
                //$html = '<ul>';
                foreach($one_exp as $exp) {
                        $html = $exp->width . $exp->color . $exp->filename;
                }
                //$html .= '</ul>';
                $result = array('status' => 'ok', 'content' => $html);
                header('Content-type: application/json');
                echo json_encode($result);
                exit();
        }else{
                $result = array('status' => 'no', 'content' => 'nothing here');
                header('Content-type: application/json');
                echo json_encode($result);
                exit();
        }

}

Model: The below function is just for testing.

function one_exp($length, $width, $height) {
       $query_str = "SELECT * FROM files WHERE length =? || width=? || height=?";
       $query = $this->db->query($query_str, array($length, $width, $height));

    if($query->num_rows() > 0) {
        foreach($query->result() as $item) {
            $data[] = $item;
        }
            return $data;
    } else {
            return false;
    }
}

* Edit

function one_exp($length, $width, $height) {
       //$query_str = "SELECT * FROM files WHERE length =? || width=? || height=?";
$query_str = "SELECT * FROM files WHERE ";

$query_and=array();

  if( !empty($length)){
      $query_and[]= ' length =?';
  }
  if( !empty($width)){
      $query_and[]=  ' width=?';
  }
  if( !empty($height)){
       $query_and[]=  ' height=?';
   }

$query_str .= implode(' AND ', $query_and);

    $query = $this->db->query($query_str, array($length, $width, $height));

    if($query->num_rows() > 0) {
        foreach($query->result() as $item) {
            $data[] = $item;
        }
            return $data;
    } else {
            return false;
    }
}

Response

Array
(
[0] =>  width=?
[1] =>  height=?
)
string(7) "Width50"
string(8) "Height60"
SELECT * FROM files WHERE width=''AND height='50'{"status":"no","content":"nothing here"}

Upvotes: 0

Views: 1719

Answers (2)

charlietfl
charlietfl

Reputation: 171669

As far as the query goes you can create the query string based on the arguments , something like:

function one_exp($length, $width, $height) {
    $query_str = "SELECT * FROM files WHERE ";

    $query_and=array();
      if( !empty($length)){
          $query_and[]= ' length =?';
      }
      if( !empty($width)){
          $query_and[]=  ' width=?';
      }
      if( !empty($height)){
           $query_and[]=  ' height=?';
       }
    $query_str .= implode(' AND ', $query_and);
    /* do query */

}

Just make sure at least one of the arguments has a valid value

Upvotes: 0

Sanjog Kumar Dash
Sanjog Kumar Dash

Reputation: 341

There is a problem in this section

foreach($one_exp as $exp) {
    $html = $exp->width . $exp->color . $exp->filename;
}

The varriable $html is reinitialized each time

So make a update

foreach($one_exp as $exp) {
    $html .= $exp->width . $exp->color . $exp->filename."<br>";
}

Upvotes: 1

Related Questions