开发者

Codeigniter check if values exist with database

I want to compare values with what's in the database.

This is my database

            Id  xone     xtwo   yone    ytwo
            1   519      819    64      364

The name of the database is num.

 public function coord_check() {

               $X = $this->input->post('X');
        $Xm = $this->input->post('Xm');
         $Y = $this->input->post('Y');
         $Ym = $this->input->post('Ym');

        $query = $this->db->where('xone',$X)->select('xone')->get('num');
         $query2 = $this->db->where('xtwo',$Xm)->select('xtwo')->get('num');
         $query3 = $this->db->where('yone',$Y)->select('yone')->get('num');
         $query4 = $this->db->where('ytwo',$Ym)->select('ytwo')->get('num');

         if($query->num_rows() > 0 && $query->num_rows() > 0 )        {

             echo "xerror";
                               }
         elseif($query3->num_rows() > 0 || $query4->num_rows() > 0 )        {

             echo "yerror";  }

               else{
                   echo "noerror";
               }



 } 

My code at the moment only echo's no error even if i ake the values 开发者_如何学运维exactly the same with what's in the database.. Can anyone see where the problem is?

What i really want is to compare lets say if $xm is in the range of database values xone and xtwo. Is that possible? Many Thanks in advance..


I'm not sure if you're following MVC Architecture or not because your post is actually kind of confusing.

However, this is what I would do -- in Codeigniter. I'll try and match your code style as best as possible. Also there are a ton of ways you can do this, some might be way more efficient than this, but this will get the job done. I never ran this script, so it might have errors or require a little debugging:

<?php
/* Controller
*************************/

class Something extends CI_Controller {

function coord_check() {

    //Form Validation -- if necessary
    $this->form_validation->set_rules('X', 'X', 'required|xss_clean');
    $this->form_validation->set_rules('Xm', 'Y', 'require|xss_clean'); 
    $this->form_validation->set_rules('Y', 'Y', 'require|xss_clean'); 
    $this->form_validation->set_rules('Ym', 'Ym', 'require|xss_clean'); 

    if ($this->form_validation->run() == FALSE) {
        $this->load->view('your_view'); 
    } else {
        $this->load->model('num');

        $X = $this->input->post('X');
        $Xm = $this->input->post('Xm');
        $Y = $this->input->post('Y');
        $Ym = $this->input->post('Ym');

        $X_result = $this->num->check_if_coord_thingy_exists('xone', $X);
        $Xm_result = $this->num->check_if_coord_thingy_exists('xtwo', $Xm);
        $Y_result = $this->num->check_if_coord_thingy_exists('yone', $Y);
        $Ym_result = $this->num->check_if_coord_thingy_exists('ytwo', $Ym);

        /*
        if ($X_result == TRUE && $Xm_result == TRUE && $Y_result == TRUE && $Ym_result == TRUE) {
            //all things inputed match database
        } else {
            //all values don't match database
        }


        if ($X_result == TRUE && $Xm_result == TRUE) :
            //all X things inputed match database
                    endif;

        if ($Y_result == TRUE && $Ym_result == TRUE) :
            //all X things inputed match database
                    endif; */


        $data['X_repsonse'] = ($X_result == TRUE ? 'X exist' : 'X doesn\'t exist';
        $data['Xm_repsonse'] = ($Xm_result == TRUE ? 'Xm exist' : 'Xm doesn\'t exist';
        $data['Y_repsonse'] = ($Y_result == TRUE ? 'Y exist' : 'Y doesn\'t exist';
        $data['Ym_repsonse'] = ($Ym_result == TRUE ? 'Ym exist' : 'Ym doesn\'t exist';

        $this->load->view('your_view', $data);
    }
}
?>

<?php
/* Model
*************************/  

class Num extends CI_Model {

    function check_if_coord_thingy_exists($value, $variable) {
        $this->db->select($value);
        $this->db->where($value, $variable);

        $query = $this->db->get('num');

        if ($query->num_rows() > 0) {
            //Value exists in database
            return TRUE;
        } else {
            //Value doesn't exist in database
            return FALSE;
        }
    }   
}

?>

<?php 
/* View -> your_view.php
*************************/  

    echo validation_errors('<div class="message">', '</div>');

    if (!empty($X_response)) echo '<div class="message">X: '.$X_response.'</div>';
    if (!empty($X_response)) echo '<div class="message"Xm: >'.$Xm_response.'</div>';
    if (!empty($X_response)) echo '<div class="message">Y: '.$Y_response.'</div>';
    if (!empty($X_response)) echo '<div class="message">Ym: '.$Ym_response.'</div>';

?>

<?php echo form_open('something/coord_check'); ?>

<?php echo form_label('First Coord', 'X'); ?><br>
<?php $first_coord = array('name' => 'X', 'id' => 'X', 'value' => set_value('X')); ?>
<?php echo form_input($first_coord); ?><br>

<?php echo form_label('Second Coord', 'Xm'); ?><br>
<?php $second_coord = array('name' => 'Xm', 'id' => 'Xm', 'value' => set_value('Xm')); ?>
<?php echo form_input($second_coord); ?><br>

<?php echo form_label('Third Coord', 'Y'); ?><br>
<?php $third_coord = array('name' => 'Y', 'id' => 'Y', 'value' => set_value('Y')); ?>
<?php echo form_input($third_coord); ?><br>

<?php echo form_label('Fourth Coord', 'Ym'); ?><br>
<?php $fourth_coord = array('name' => 'Ym', 'id' => 'Ym', 'value' => set_value('Ym')); ?>
<?php echo form_input($fourth_coord); ?><br>

<?php echo form_submit('submit', 'Coord Check or Whatever'); ?>
<?php echo form_close(); ?>

Hopefully this helps. This is all assuming you autoload your database and form_validation libraries.

Lastly, your database is strange. X and Y I feel should be different tables. I don't know the extent of your project though. Good luck!


Will the table have more than 1 value?

If not, then it's better to use a single query to pull the entire record first. Then you can compare if the values fall within the range of the x's and y's.

Something like this:

public function coord_check() {

  $X = $this->input->post('X');
  $Xm = $this->input->post('Xm');
  $Y = $this->input->post('Y');
  $Ym = $this->input->post('Ym');

  $query = $this->db->where('Id',1)->get('num');
  if ($query->num_rows())
  {
    $row = $query->row();

    if ($row->xone > $X || $row->xtwo < $X || $row->xone > $Xm || $row->xtwo < $Xm) {
      echo "xerror";
    }
    elseif ($row->xone > $Y || $row->xtwo < $Y || $row->xone > $Ym || $row->xtwo < $Ym) {
      echo "yerror";
    }
    else {
      echo "noerror";
    }
  }
}


function check_record($db, $table, $where) {
    try {
        $db->where($where);
        return $db->count_all_results($table);
    } catch (Exception $e) {
        return false;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜