0

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"}
4
  • Is my answer helpful ??? Commented Dec 7, 2013 at 22:15
  • You don't need status: ok/no. the 200 OK HTTP status code of the response is sufficient - in case there are no records to display, just return []. Commented Dec 7, 2013 at 22:19
  • @moonwave99 how would I check for that? can you show me an example? Commented Dec 8, 2013 at 18:39
  • Set http response code // first google result for REST status codes. Commented Dec 8, 2013 at 22:48

2 Answers 2

1

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>";
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

17 Comments

hi. thank you for your help. I tried it(see my edit please), but it only works if length is not empty. If I add more arguments, it returns false ... any idea why? another thing ... how do I debug variables from the model? how would I var_dump or print_r $query_and?
I suggest you echo $query_str and test that in your mysql GUI (ie phpMyAdmin). Can see output in browser console by inspecting request itself and copy response body
can also use echo $this->db->last_query() after any db query to see what query string was
wow ... you just helped me so much. I had no idea how to see queries. thank you so much! However there is still something weird going on ... I can't seem to figure out. If I add a length, width, and height it works. If I add length and height, the height argument is missing. Same if I add width and height.
weird ... i ll keep trying.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.