English 中文(简体)
编码核对
原标题:Codeigniter check if values exist with database

我想将价值与数据库中的数值进行比较。

这是我的数据库。

            Id  xone     xtwo   yone    ytwo
            1   519      819    64      364

该数据库的名称是南的。

 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";
               }



 } 

我的法典目前只字不提,即使把价值与数据库中的价值完全相同。 谁看问题在哪里?

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..

问题回答

我不敢肯定你会重新接过MVC 结构或不是因为你的职位实际上令人困惑。

然而,这是我在《法典》中所做的事。 我尽量尝试和与你的法典风格保持一致。 此外,还有你能够做些什么,有些可能比这更有效率,但这项工作将完成。 我从来没有听过这一文字,因此可能会有错误,或要求稍作改动:

<?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(); ?>

希望这一帮助。 所有这一切都假定你自动卸下了你的数据库,并组成了“打造图书馆”。

最后,你的数据库很奇怪。 我认为,应该有不同的表格。 我不知道你的项目范围。 亲爱!

该表是否有1个以上价值?

否则,最好利用单一询问,首先把整个记录拉开。 然后,你可以比较这些数值是否属于x和x的范畴。

与此类似:

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;
    }
}




相关问题
PHP Framework: Ebay Like Site

I am going to be builiding a site like ebay - with all the features of ebay. Please note my payment method is limited to paypal. What would be the best PHP framework to use to build this quickly, ...

What s the proper MVC way to do this....?

Quick question about general MVC design principle in PHP, using CodeIgniter or Kohana (I m actually using Kohana). I m new to MVC and don t want to get this wrong... so I m wondering if i have ...

Check session from a view in CodeIgniter

What is the best way to check session from a view in CodeIgniter, it shows no way in their user guide, otherwise I will have to make two views on everything, which is kinda weird...still a newbie to ...

Using SimplePie with CodeIgniter and XAMPP

I am using CodeIgniter 1.7.2 with XAMPP 1.7.2 on a Windows computer. I am trying to make use of SimplePie. I followed all the instructions I could find: a copy of simplepie.inc is in my applications/...

CodeIgniter adding semicolons

How do I stop CodeIgniter adding semicolons ; to data sent via POST that contains ampersand &? For example it is converting "a=1&b=2&c=3" into "a=1&b;=2&c;=3". From looking on the forums ...

Best way to make Admin pages in CodeIgniter?

I m working on an app in CodeIgniter, and I want to have admin pages for several of the objects in the application, and I m wondering what would be the better way to put these into an MVC structure. ...

CodeIgniter form verification and class

I m using the form validation library and have something like this in the view <p> <label for="NAME">Name <span class="required">*</span></label> <?...

热门标签