Searching....

Help
ST Ooi
2014-03-14
2014-03-20
  • ST Ooi
    ST Ooi
    2014-03-14

       function search() {
          $field=$this->txt_field->getNewValue();
          $operator=$this->txt_operator->getNewValue();
          $string=$this->txt_data->getNewValue();
    
          if ((!$field=="") & (!$operator=="") & (!$string=="")) {
             if ($operator=="like") {
                $sql=$field." ".$operator." '".$string."%'";
             } else {
                $sql=$field." ".$operator." '".$string."'";
             }
    
             $this->data->setWhere($sql);
             $this->data->firstRow();
    
               $num_rows=$this->data->getNumRows();      
    
            if (!$num_rows) {
                $this->error->setValue("No results were found");
                   $this->data->setWhere(null);
                   $this->data->firstRow();
               }
          } else {
             $this->error->setValue("Fill in all fields and click Search button");
             $this->txt_field->setValue("");
             $this->txt_operator->setValue("");
             $this->txt_data->setValue("");
          }
       }
    

    I have above code for searching of my database. 3 controls are required, field, operator and search string, it is suppose to construct the sql from where onwards, however it does not work.

    Could anyone help?

    Thanks

     
    • André Pignata
      André Pignata
      2014-03-14

      Hi, where you create the $this->data? You have done the $this->data->load()?
      Att

      André Luiz Martins Pignata
      Universidade de São Paulo - Campus Ribeirão Preto
      Faculdade de Economia e Administração (FEA)
      Analista de Sistemas

      2014-03-14 4:10 GMT-03:00 ST Ooi ooisootuck@users.sf.net:

      function search() {
      $field=$this->txt_field->getNewValue();
      $operator=$this->txt_operator->getNewValue();
      $string=$this->txt_data->getNewValue();

        if ((!$field=="") & (!$operator=="") & (!$string=="")) {
           if ($operator=="like") {
              $sql=$field." ".$operator." '".$string."%'";
           } else {
              $sql=$field." ".$operator." '".$string."'";
           }
      
           $this->data->setWhere($sql);
           $this->data->firstRow();
      
             $num_rows=$this->data->getNumRows();
      
          if (!$num_rows) {
              $this->error->setValue("No results were found");
                 $this->data->setWhere(null);
                 $this->data->firstRow();
             }
        } else {
           $this->error->setValue("Fill in all fields and click Search button");
           $this->txt_field->setValue("");
           $this->txt_operator->setValue("");
           $this->txt_data->setValue("");
        }
      

      }

      I have above code for searching of my database. 3 controls are required,
      field, operator and search string, it is suppose to construct the sql from
      where onwards, however it does not work.

      Could anyone help?

      Thanks

      Searching....https://sourceforge.net/p/p4a/discussion/340765/thread/9a0efe35/?limit=25#75b6

      Sent from sourceforge.net because you indicated interest in
      https://sourceforge.net/p/p4a/discussion/340765/

      To unsubscribe from further messages, please visit
      https://sourceforge.net/auth/subscriptions/

       
    • everything seems right, $this->data is the internal name for the default data source so it should be right.
      i suggest you to enable the db_profile and check the generated query... sorry not to have a better answer at the moment

       
  • ST Ooi
    ST Ooi
    2014-03-18

       function create_search() {
            $this->build("p4a_fieldset","fs_search");
            $this->fs_search->setLabel("Search");
    
          $field_value=array();
          $field_value[]=array("id"=>"", "desc"=>"Select a field");
          $field_value[]=array("id"=>"hp_no", "desc"=>"Hanphone No.");
          $field_value[]=array("id"=>"name", "desc"=>"Name");
          $field_value[]=array("id"=>"plate_no", "desc"=>"Plate No.");
    
          $this->build("p4a_array_source", "field_source")
               ->setPk("id")
               ->load($field_value);
    
          $this->build("p4a_field", "txt_field")
               ->setType("select")
               ->setSource($this->field_source)
               ->setSourceDescriptionField("desc")
               ->setSourceValueField("id")
                 ->addAction("onReturnPress")
                 ->setLabel("Field")
                 ->implement("onreturnpress", $this, "search");
    
          $operator_value=array();
          $operator_value[]=array("id"=>"", "desc"=>"Select an operator");
          $operator_value[]=array("id"=>"=", "desc"=>"=");
          $operator_value[]=array("id"=>">", "desc"=>">");
          $operator_value[]=array("id"=>">=", "desc"=>">=");
          $operator_value[]=array("id"=>"<", "desc"=>"<");
          $operator_value[]=array("id"=>"<=", "desc"=>"<=");
          $operator_value[]=array("id"=>"like", "desc"=>"Contains");
    
          $this->build("p4a_array_source", "operator_source")
               ->load($operator_value)
               ->setPk("id");
    
          $this->build("p4a_field", "txt_operator")
               ->setType("select")
               ->setSource($this->operator_source)
               ->setSourceDescriptionField("desc")
               ->setSourceValueField("id")      
                 ->addAction("onReturnPress")
                 ->setLabel("Operator")
                 ->implement("onreturnpress", $this, "search");
    
          $this->build("p4a_field", "txt_data")
                 ->addAction("onReturnPress")
                 ->setLabel("String")
                 ->implement("onreturnpress", $this, "search");
    
            $this->build("p4a_button", "cmd_search")
                 ->implement("onclick", $this, "search")
                 ->setLabel("Search");
    
          $this->build("p4a_button", "cmd_clear")
               ->implement("onclick", $this, "clear_search")
               ->setLabel("Clear");
    
            $this->fs_search->anchor($this->txt_field);
          $this->fs_search->anchorLeft($this->txt_operator);
          $this->fs_search->anchorLeft($this->txt_data);
            $this->fs_search->anchorLeft($this->cmd_search);
          $this->fs_search->anchorLeft($this->cmd_clear);
       }
    
       function search() {
          $field=$this->txt_field->getNewValue();
          $operator=$this->txt_operator->getNewValue();
          $string=$this->txt_data->getNewValue();
          $s=$field.$operator.$string;
    
          $this->error($s);
    
          if ((!$field=="") & (!$operator=="") & (!$string=="")) {
             if ($operator=="like") {
                $sql=$field." ".$operator." '".$string."%'";
             } else {
                $sql=$field." ".$operator." '".$string."'";
             }
    
             $this->data->setWhere($sql);
             $this->data->firstRow();
             $this->data->load();
    
               $num_rows=$this->data->getNumRows();      
    
            if (!$num_rows) {
                $this->error->setValue("No results were found");
                   $this->data->setWhere(null);
                   $this->data->firstRow();
                $this->data->load();               
               }
          } else {
             $this->error->setValue("Fill in all fields and click Search button");
             $this->txt_field->setValue("");
             $this->txt_operator->setValue("");
             $this->txt_data->setValue("");
             $this->data->load();         
          }
       }
    
       function clear_search() {
          $this->txt_field->setValue("");
          $this->txt_operator->setValue("");
          $this->txt_data->setValue("");
    
          $this->data->setWhere("");
          $this->data->firstRow();
          $this->data->load();
    
            $num_rows=$this->data->getNumRows();      
    
            if (!$num_rows) {
                $this->error->setValue("No results were found");
                $this->data->setWhere(null);
                $this->data->firstRow();
             $this->data->load();           
            }
       }
    

    Above is more complete code for my problem. BTW, how to enable db_profile, forgive my ignorant.

    Thanks

     
  • type

    define("P4A_DB_PROFILE", true);

    in your index.php file, before including p4a :)

     
  • ST Ooi
    ST Ooi
    2014-03-18

    Hi Balliano,

    I have enable db_profile. However, there isn't anything shown when I click the search button, please shed some light.

    Thanks

     
  • ST Ooi
    ST Ooi
    2014-03-18

    FYI, there are plenty of information shown after I login

     
  • every query should be shown as a p4a notification on the browser, did you restart the browser?

     
  • ok so you should check all the queries after you press the search button

     
  • ST Ooi
    ST Ooi
    2014-03-18

    alright, thanks

     
  • ST Ooi
    ST Ooi
    2014-03-20

    ~~~
    function search() {
    $field=$this->txt_field->getNewValue();
    $operator=$this->txt_operator->getNewValue();
    $string=$this->txt_data->getNewValue();

      if ((!$field=="") & (!$operator=="") & (!$string=="")) {
         if ($operator=="like") {
            $sql="$field $operator '$string%'";
         } else {
            $sql="$field $operator '$string'";
         }
    
         $this->source->setWhere($sql);
         $this->source->firstRow();
           $num_rows=$this->source->getNumRows();
    
        if (!$num_rows) {
            $this->error("No results were found");
               $this->data->setWhere(null);
               $this->data->firstRow();
            $this->data->load();               
           }
      } else {
         $this->error("Fill in all fields and click Search button");
         $this->txt_field->setValue("");
         $this->txt_operator->setValue("");
         $this->txt_data->setValue("");
         $this->data->load();         
      }
    

    }

    ~~~

    I have change my code like above and it worked perfectly.

    $this->data become $this->source

    Thanks