CodeIgniter Forums
Cannot test if a field is empty or not - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Cannot test if a field is empty or not (/showthread.php?tid=37890)

Pages: 1 2


Cannot test if a field is empty or not - El Forum - 01-24-2011

[eluser]caperquy[/eluser]
Hello
I am implementing an application where I have to test if a field is empty or not. That field is defined within PhpMySQL as follows :
Field name : DIVERS
Type : varchar(80)
Interclassement : latin1_swedish_ci
Null : No (non)
Defaut : None (aucun)
There are cases where nothing has been put into the field. Nevertheless whenever I test that field in the controller using an instruction such as
Code:
if (empty($DIVERS))
    {
        echo "DIVERS empty<br />";
    }else{
        echo "DIVERS not empty<br />";
}
the Else part of the if condition is always executed.
The same occurs if I use is_null
Could someone give me a clue ? Thanks in advance.
CapErquy


Cannot test if a field is empty or not - El Forum - 01-24-2011

[eluser]toopay[/eluser]
Can you post the code, which you use to retrieve fields from database.


Cannot test if a field is empty or not - El Forum - 01-24-2011

[eluser]caperquy[/eluser]
Thanks for answering that fast. In fact I simplified the code when I put my first question on line. The exact code is a little bit more complex since I retrieve other fields as well. The code in the model is as follows ($critere represents the word I am looking for in the database) :

Code:
[b]function recherche($critere)[/b]
this->db->select('COTE, TITRE, EDITEURS, RESUME, DESCRIPTEURS, MOTSCLES, DIVERS');
$this->db->from ('catalogue_dvd');
this->db->like ('TITRE',$critere);
$this->db->or_like ('EDITEURS',$critere);
$this->db->or_like ('RESUME',$critere);
$this->db->or_like ('DESCRIPTEURS',$critere);
this->db->or_like ('MOTSCLES',$critere);
$this->db->or_like ('DIVERS',$critere);
[b]return $this->db->get();
[/b]


The code in the controller is as follows :

Code:
$data['resultats'] = $this->catalogue_model->recherche($critere)->result();
$i=count($data['resultats']);
$j=0;
while ($j<$i)
    {
    if (is_null($data['resultats'][$j]->DIVERS))
        {
            echo "Divers empty<br />";
    }else{
            echo "Divers not empty<br />";
    }
    j++;
}

Obviously when I will get the solution to my problem the echo instructions will be replaced by more sophisticated ones.
I hope this will be useful tp you


Cannot test if a field is empty or not - El Forum - 01-24-2011

[eluser]toopay[/eluser]
I assume, that you are trying to find a single row of data, with specific keywords.

First, on your Model change to this
Code:
function recherche($critere)
{
this->db->select('COTE, TITRE, EDITEURS, RESUME, DESCRIPTEURS, MOTSCLES, DIVERS');
$this->db->from ('catalogue_dvd');
this->db->like ('TITRE',$critere);
$this->db->or_like ('EDITEURS',$critere);
$this->db->or_like ('RESUME',$critere);
$this->db->or_like ('DESCRIPTEURS',$critere);
this->db->or_like ('MOTSCLES',$critere);
$this->db->or_like ('DIVERS',$critere);
$result = $query->result_array();
//if there are a match result, bring first row. Else, Null
return (count($result) > 0 ? $result[0] : NULL);
}
Now, you can test if a field is empty or not, on your Controller like this
Code:
//I assume, in this stage, that your model class have properly loaded
$resultats = $this->catalogue_model->recherche($critere);

if(is_null($resultats))
{
//if there are no result, give a warning
echo "No result for'" . $critere . "'. Try another criteria!" ;
}
else
{
//if there are 1 result, test it
  $data=array();
  $data['resultats']=$resultats['DIVERS'];
    if ($data['resultats']=='')
        {
            echo "Divers empty<br />";
    }else{
            echo "Divers not empty<br />";
    }
}
}

Let me know, if it work for you...
Btw, i think maybe next time you should post your question at proper forum, because this is Bug Report section i believe.


Cannot test if a field is empty or not - El Forum - 01-24-2011

[eluser]toopay[/eluser]
forgot one line...replace above model, with this
Code:
function recherche($critere)
{
this->db->select('COTE, TITRE, EDITEURS, RESUME, DESCRIPTEURS, MOTSCLES, DIVERS');
$this->db->from ('catalogue_dvd');
this->db->like ('TITRE',$critere);
$this->db->or_like ('EDITEURS',$critere);
$this->db->or_like ('RESUME',$critere);
$this->db->or_like ('DESCRIPTEURS',$critere);
this->db->or_like ('MOTSCLES',$critere);
$this->db->or_like ('DIVERS',$critere);
//the forgotten line
$query = $this->db->get();
$result = $query->result_array();
//if there are a match result, bring first row. Else, Null
return (count($result) > 0 ? $result[0] : NULL);
}



Cannot test if a field is empty or not - El Forum - 01-24-2011

[eluser]caperquy[/eluser]
Many thanks for your answer. I will not be able to test it before tomorrow but when done I will let you know.
CapErquy


Cannot test if a field is empty or not - El Forum - 01-25-2011

[eluser]caperquy[/eluser]
I am back to my application. I am trying to implement what you said but I am confused with the instruction

Code:
return (count($result) >0 ? $result[0] :NULL);

If in my controller if I execute the following instructions :

Code:
$resultats=$this->catalogue_model->recherche($critere, $autrechoix);
print_r($resultats);

the array shows only one result (I expect 13) and the number found is not shown.

I must be missing something but I do not see what.

Caperquy


Cannot test if a field is empty or not - El Forum - 01-25-2011

[eluser]caperquy[/eluser]
Sorry but forget the parameter $autrechoix which I plan to use in the future. For the time being it is not implemented. I only have :

Code:
$resultats=$this->catalogue_model->recherche($critere);
print_r($resultats);

CapErquy


Cannot test if a field is empty or not - El Forum - 01-25-2011

[eluser]toopay[/eluser]
like i said, I assume, that you are trying to find a single row of data (not array), with specific keywords.
now tell me, are you try to find a list or single row?


Cannot test if a field is empty or not - El Forum - 01-25-2011

[eluser]caperquy[/eluser]
Yes I am trying to get a list, ie zero or several rows.
CapErquy