Reputation: 21
I'm working on getting values from the database and convert it into json object. It is working fine, but the problem is if the value is blank(Not assigned 'null' value), it is showing null in json output. I tried if condition to check for it and skip it if the value is blank. But it is not working. what can I do for if condition so that if the value is blank it should skip that one. Please suggestion some solution to it. I'm a newbie to php
<?php
$connect = mysql_connect("localhost","plinlt","lajisdfla");
mysql_select_db("plinlt");
$result = mysql_query("SELECT field_id_6 FROM exp_channel_data") or die(mysql_error());
// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["events"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$product = array();
if($row["field_id_6"]==null)
{
Echo "";
}
else
{
$product["event"] = $row["field_id_6"];
// push single product into final response array
array_push($response["events"], $product);
}
}
// success
$response["success"] = 1;
$preserved = array_reverse($response, true);
// echoing JSON response
echo json_encode($preserved);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No products found";
// echo no users JSON
echo json_encode($response);
}
?>
Upvotes: 1
Views: 2579
Reputation: 215
Instead of
if($row["field_id_6"]==null)
{
Echo "";
}
try
if($row["field_id_6"]==null || $row["field_id_6"]=="null" || empty($row["field_id_6"]) || trim($row["field_id_6"])=="")
{
continue;
}
Upvotes: 0
Reputation: 11450
continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration.
To check for an empty variable, the better option is usually empty()
PHP Manual: empty
So, you can use something like this:
while ($row = mysql_fetch_array($result)) {
$product = array();
// if the field is blank, skip this row
if( empty($row["field_id_6"]) )
continue;
// it's not blank, do what we need
$product["event"] = $row["field_id_6"];
array_push($response["events"], $product);
}
Upvotes: 2
Reputation: 45124
Use is_null for this. It checks whether a variable is NULL. Also isset checks determine if a variable is set and is not NULL.
Examples could be found on the web.
Upvotes: 0
Reputation: 3078
Try in if condition,
if($row["field_id_6"]==null || $row["field_id_6"] == "" || $row["field_id_6"] == " ")
{
//your code
}
Upvotes: 0