I'm fetching practice_string_id and program_string_id from a table
$project_type = DB::table('project')
->where('code',$asset_request->project_code)
->select('practice_string_id','program_string_id')
->first();
print_r($project_type); //output
Output:
stdClass Object ( [practice_string_id] => PRACTICE0028
[program_string_id] => )
I want to check $project_type->program is set or not in if condition
if(isset($project_type->program_string_id)){
//nothing in $project_type->program but reached here now
}
I want to how to check the value is set or not in php if condition.now if(isset($project_type->program_string_id)) is passed and if condition is working.I want to skip if condition.
As this is a database query, and you select the field program_string_id, it will be set always and any time. The question is, if there is any value. So you might want to use empty as check:
if (!empty($project_type->program_string_id)) {
// ...
}
if(isset($project_type-> program_string_id) && !empty($project_type-> program_string_id) )
}
You have to use both isset() and empty() other wise it may throw error in some cases
$project_type = DB::table('project')
->where('code',$asset_request->project_code)
->select('practice_string_id','program_string_id')
->first();
if(count($project_type) > 0 && $project_type != ""){
echo $project_type->program_string_id;
}
Related
Its' about a form to enter results of a soccer tournament.
The form got the already inputed data from the db and writes it into the value argument of the html form. If the value in the db NULL so in the html i got
value=""`
It's important that games with no inputs doesn't make a change in the db so i filter it before i do the query. But now could it happen that a game ends 0 : 0 But the db won't safe that. How can i say the system its not empty/NULL it is 0?
if(!empty($_POST[$tore_heim] OR !empty($_POST[$tore_gast]))){
$spiele_save = "UPDATE spiele SET tore_heim = '".$_POST[$tore_heim]."', tore_gast = '".$_POST[$tore_gast]."' WHERE id_spiele = ".$spiele_id[$i]."";
$spiele_save = mysqli_query($con, $spiele_save);};
};
Thank you deceze
if(isset($_POST[$tore_heim]) && strlen($_POST[$tore_heim]) > 0
OR
isset($_POST[$tore_gast]) && strlen($_POST[$tore_gast]) > 0)
The Problem is solved!
Check if value is '0': !empty($_POST[$tore_gast])) || $_POST[$tore_gast] === '0'
look at this example.
function isEmpty( $variable ){
return (
!isset($_POST[$variable]) ||
( empty($_POST[$variable]) && $_POST[$variable] !== "0" )
);
}
if ( !isEmpty($tore_heim) ){
// run your code here...
}
I need to use where operator to check some ids in my table as the following
$subScIDs = SubjectSchedule::where('subject_start_id',Input::get('substID'))
->whereNull('deleted_at')
->select('id')->get();
$subScIDsArray = array() ;
foreach ($subScIDs as $id)
{
$subScIDsArray [] = $id->id ;
}
// here my issue
$subScInStudAttends = StudentAttendees::whereIn('subject_schedule_id',$subScIDsArray)
->select('subject_schedule_id')->get();
if ($subScInStudAttends)
{
return " true " . $subScInStudAttends;
}else{
return "false";
}
My issue in this code
$subScInStudAttends = StudentAttendees::whereIn('subject_schedule_id',$subScIDsArray)
->select('subject_schedule_id')->get();
whereIn working well it fetch any id in $subScIDsArray, but i need to check each id of ids if one id in $subScIDsArray not equal subject_schedule_id' return false ;
How I Can do it ?
Any Suggestions ?
You can check the length of the array that contains the ids against the length of the records returned.
if( count($subScIDsArray) == count($subScInStudAttends) ){
// all IDs in the array have a corresponding record in the database
}
Or better, if your application logic permits it, simply get the count of the records and then compare with the length of the ids array.
$subScInStudAttendsCount = StudentAttendees::whereIn('subject_schedule_id', $subScIDsArray)->count('subject_schedule_id');
if( count($subScIDsArray) == $subScInStudAttendsCount ){
// all IDs in the array have a corresponding record in the database
}
This code assumes the ids in your database are all unique.
I'm having trouble trying to remove null values from an array using values from the database. These null values are usually found within the 'answers'.. Code below:
$getQuestions = mysql_logging_query("SELECT fq.`question_id`, fq.`ques_form_id`, fq.`question_body`, fq.`type`, fq.`answer1`, fq.`answer2`, fq.`answer3`, fq.`answer4`, fq.`answer5`, fq.`answer6`, fq.`min_validation`, fq.`max_validation`
FROM QuestionnaireFormQuestions fq
LEFT JOIN QuestionnaireForms f
ON f.`form_id` = fq.`ques_form_id`
WHERE f.`active` = 1
AND f.`form_id` = '".mysql_real_escape_string($form_id,$this->dbcon)."'
",$this->dbcon);
if($getQuestions && mysql_num_rows($getQuestions)>0 && mysql_error($this->dbcon)=="")
{
$get_questions_array = array();
while($getQuestions && $getQuestions_rec=mysql_fetch_assoc($getQuestions))
{
$get_questions_array[] = array('question_id' => $getQuestions_rec['question_id'],
'related_form_id' => $getQuestions_rec['ques_form_id'],
'question_body' => $getQuestions_rec['question_body'],
'question_type' => $getQuestions_rec['type'],
'possible_answer1' => $getQuestions_rec['answer1'],
'possible_answer2' => $getQuestions_rec['answer2'],
'possible_answer3' => $getQuestions_rec['answer3'],
'possible_answer4' => $getQuestions_rec['answer4'],
'possible_answer5' => $getQuestions_rec['answer5'],
'possible_answer6' => $getQuestions_rec['answer6'],
'min_validation' => $getQuestions_rec['min_validation'],
'max_validation' => $getQuestions_rec['max_validation']
);
}
if(is_array($get_questions_array) && count($get_questions_array)>0)
{
foreach($get_questions_array as $key=>$array)
{
if($array === null) {
unset($get_questions_array[$key]);
}
$return['data']['questions'][] = $array;
}
}
}
else
//error
A return for example; would look like this:
"question_id":"3",
"related_form_id":"4",
"question_body":"Do you like this content?",
"question_type":"radio",
"possible_answer1":"Disagree",
"possible_answer2":"Neutral",
"possible_answer3":"Agree",
"possible_answer4":null,
"possible_answer5":null,
"possible_answer6":null,
"min_validation":"1",
"max_validation":"1"
I've tried unsetting the key using empty and isnull but to no avail. Any help would be appreciated.
You are not testing the values inside the array, you need to:
foreach($get_questions_array as $array){
foreach($array as $key=>$element){
if($element===null)
unset($array[$key]);
}
$return['data']['questions'][] = $array;
}
I think it's possible you're looping through your nested data structure at the wrong level.
You have this $get_questions_array, each element of which is an associative array that came from one row in your MySQL result set. But your php code loops over the rows of the result set, not over the columns.
I think you want something more like this, with another nested foreach.
if(is_array($get_questions_array) && count($get_questions_array)>0)
{
foreach($get_questions_array as $row)
{
foreach ($row AS $colname=>$colvalue)
{
if ($colvalue === null || $colvalue =='')
{
unset($row[$colname]);
}
}
}
}
See what's going on? Your code was throwing away whole rows that were null, but there weren't any of those, so it wasn't doing anything.
why don't you query the data that does not contain nulls, instead of removing the nulls by yourself ? let the database do this for you something like:
select * from table where possible_answer IS NOT NULL
Try this loop through the array and set them to null with the key's if the value is empty
foreach($getQuestions_rec as $key => $value){
if($value == ''){
$getQuestions_rec[$key] = null;
}
}
Use the IFNULL(ColumnName,"") to replace the null values to empty string in SQL query.
Example: IFNULL(answer6,"")
refer this this How to remove null values from an array? or you ca alter the query to select the null values from table.
I'm running the following query but $new returns 0:
$count = mysql_query("SELECT COUNT(*) FROM flagged WHERE status=0") or die(mysql_error());
$new = mysql_fetch_row($count);
if ($new != 0) {
echo "<script language=\"javascript\">$.titleAlert(\"New Logs - ($new[0])\");</script>";
}
Problem is the if-condition keeps getting met when it shouldn't when $new != 0
I even tried:
if ($new > 0) {
//update title about new logs
}
Either way, the title is still updated and I'm not sure why.
New Logs - (0)
just do
var_dump($new);
and you'll see why your if doesn't work
Not sure what you mean. $new should be an array not a scalar value per the PHP documentation.
mysql_fetch_row documentation
this code
$new = mysql_fetch_row($count);
makes $new becomes an array
try to compare it like this:
if ($new[0] != 0)
or even better, so You would be 100% sure it's what You need.
if (count($new) == 1 && intval($new[0]) != 0)
mysql_fetch_row should return an array or FALSE.
Maybe adjust your conditional to:
if ( !$new ) {
// some code here
}
$query = 'select column from table ...';
...
$row = mysql_fetch_assoc($result);
//now i have the result
//but how to check if it's null or 0?
What Pandiya was meant to say was is_null.
try
$res=mysql_query(" SELECT NULL AS anullcol, 0 AS anum, '' AS emptystring ");
var_dump(mysql_fetch_assoc($resource));
and read up on the '===' and '!==' operators.
C.
if($var == '' || $var == NULL){
//Code
}
You can use isset():
$foo = 0;
echo isset($foo) ? 'yes' : 'no';
echo "<br>";
$foo = null;
echo isset($foo) ? 'yes' : 'no';
will result in
yes
no
Maybe you are asking if the Query has returned anything. So try using:
mysql_num_rows()
from PHP:
Retrieves the number of rows from a result set. This command is only valid for statements like SELECT or SHOW that return an actual result set. To retrieve the number of rows affected by a INSERT, UPDATE, REPLACE or DELETE query, use mysql_affected_rows().
<?php
if ( mysql_num_rows ( $query ) == 0){
echo "duh, nothing!";
}
?>
maybe replace the null with an identifier in the select:
select coalesce(column, -1) as column
from table ...
that way you can test for 0 or -1 (NULL).