My if statement doesn't work

Hello Everyone,
I want to extract data from the DB and display it in a line graph.
Here is the function which gets the data from the DB :
(There are 2 similar variables I show here just one to be as short as possible)

function get_Single_Couple_Short_profit($item, $user_id, $start_date, $end_date, $hour_start, $hour_end, $days)
{	
	global $db;
	
	try
	{
		$sql = "SELECT DATE_FORMAT(`c_time`, '%m-%y') AS time, `c_time`, `o_time`, `item`, `user_id`, `profit`, `hideshow` 
		        FROM data 
				WHERE DATE(`o_time`) BETWEEN :start_date AND :end_date 
			    AND TIME(`o_time`) BETWEEN :hour_start AND :hour_end
				AND WEEKDAY(`o_time`) IN ($days)
				AND `hideshow` = 'hide'
				AND type LIKE 'sell%'
				AND `user_id` = :user_id
				AND `item` = :item
				ORDER BY `c_time` ASC";
		 $stmt = $db->prepare($sql);
		 $stmt->bindParam(':item', $item, PDO::PARAM_STR);
		 $stmt->bindParam(':user_id', $user_id, PDO::PARAM_STR);
		 $stmt->bindParam(':start_date', $start_date, PDO::PARAM_STR);				 $stmt->bindParam(':end_date', $end_date, PDO::PARAM_STR);
		 $stmt->bindParam(':hour_start', $hour_start, PDO::PARAM_STR);
		 $stmt->bindParam(':hour_end', $hour_end, PDO::PARAM_STR);
		 $stmt->execute();

		 if($stmt->rowCount() == 0) 
		   return false;
		else
		  return $stmt->fetchAll(PDO::FETCH_ASSOC);
		
	}
	catch(Exception $e) 
	{
	   return false;        
	}
}// End function

Here I get it in a variable and check it (for this post:

$data_ls = get_Single_Couple_Short_profit($couple, $user_id, $start_date, $end_date, $hour_start, $hour_end, $days);
var_dump($data_ls );
die();

Here is a ccreenshot the result when there is data to extract from DB :

And Here is the result when there is no data to extract from DB:

C:\wamp64\www\A_website_for_test\pages\instrument_c.inc.php:34:boolean false

Here is the code which transfers the variabls’f data to a javascript variable
and check it(for this post:

////////       Convert PhP $data_ll array to JavaScript array Short    //////////
	  var datum_ls = <?php echo json_encode($data_ls); ?>;
console.log('value of datum_ls');
console.log(datum_ls);

Here is a screenshot of the result :slight_smile:
with_data_js

And Here is a screenshot when there is no data to display :slight_smile:
no_data

My problem is that the code which works well when there is data to display fails when there is no data to display.

Here are my if statements
this is one :

	   if(datum_ls != false && datum_ll != false){  
//Callback for line chart long
	    google.charts.load('current', {'packages':['corechart']});
      google.charts.setOnLoadCallback(drawChart_l);
//Callback for line chart short
	    google.charts.load('current', {'packages':['corechart']});
      google.charts.setOnLoadCallback(drawChart_s);
	   }// End check if there is only 1 trade type

and here is another :slight_smile:

  if(datum_ls != false && datum_ll != false){

 ////////////////////////    Drawing the line graph long   //////////////////////
   function drawChart_l() {

      var data_ll = new google.visualization.DataTable();
      data_ll.addColumn('string', ' ');
      data_ll.addColumn('number', 'acc_profit');

      for(var i = 0; i < datum_ll.length; i++){
		  		var obj_l = datum_ll[i];
				data_ll.addRow([obj_l.time, obj_l.profit]);
				
			}

	  

      var options = {
          
            title:'<?php echo "Accumulated Profit of  ".$couple.'  Trades'.$addition; ?>',
            subtitle: 'in  [USD]',
		    backgroundColor: '#d7bde2',
			colors: ['black'],
		    vAxis: {title: 'Balance', gridlines: { count: 5 }},
		    
	      animation: {'startup': 'true', 'duration': 1000, 'easing': 'out'},
		  bar: { groupWidth: '90%' },
	      hAxis: { showTextEvery: 10, minTextSpacing: 10}
        };

      var chart = new google.visualization.LineChart(document.getElementById('line_div_l'));
	  
      chart.draw(data_ll, options);
    }//END CHART line long


 ////////////////////////    Drawing the line graph short   //////////////////////
   function drawChart_s() {

      var data_ls = new google.visualization.DataTable();
      data_ls.addColumn('string', ' ');
      data_ls.addColumn('number', 'acc_profit');

      for(var i = 0; i < datum_ls.length; i++){
		  		var obj_l = datum_ls[i];
				data_ls.addRow([obj_l.time, obj_l.profit]);
				
			}

	  

      var options = {
          
            title:'<?php echo 'Accumulated Profit of  '.$couple.'short Trades'.$addition; ?>',
            subtitle: 'in  [USD]',
		    backgroundColor: '#f9e79f',
			colors: ['black'],
		    vAxis: {title: 'Balance', gridlines: { count: 5 }},
		    
	      animation: {'startup': 'true', 'duration': 1000, 'easing': 'out'},
		  bar: { groupWidth: '90%' },
	      hAxis: { showTextEvery: 10, minTextSpacing: 10}
        };

      var chart = new google.visualization.LineChart(document.getElementById('line_div_s'));
	  
      chart.draw(data_ls, options);
    }//END CHART line short
} // End check if there is only 1 trade type

I tried datum_ls[0] instead of just datum_ls

Here is the code for the divs where the graphs are supposed to be displayed :

<?php
				 if(($data_ls != false) && ($data_ll !=false)){
				echo 
				'<div id="line_div_l" class="data_unit"></div>
			    <div id="line_div_s" class="data_unit"></div>';}
				?>

when there is data to display, the divs are created but they are empty

When I remove the javascript if statement and there is data to display, everything works well
what is wrong with the javascript if statement ?

1 Like

You could have “echo json_encode($data_ls);” return something more reliable when there is no data, or try a positive if (if there is data, do something, else don’t).
if (datum_ls) {do your stuff}

Work out the logic using only simple JS code and variables, then apply the working code to your project.

You have doubled ifs - one on PHP and one on JS - double check the logic of that.

1 Like