This topic is locked
[SOLVED]

 If Else Statement On Button

2/19/2013 3:56:56 AM
PHPRunner General questions
K
kyrie76 author

I want to add a condition on a button. In case the value (AvailableSeats) is empty to show data from another table. Code below

if I remove the if statement then it works fine. Can u please tell me what am I doing wrong
ON SERVER TAB

$datevalue = "";

$montharr = Array();

$montharr["Jan"] = 1;

$montharr["Feb"] = 2;

$montharr["Mar"] = 3;

$montharr["Apr"] = 4;

$montharr["May"] = 5;

$montharr["Jun"] = 6;

$montharr["Jul"] = 7;

$montharr["Aug"] = 8;

$montharr["Sep"] = 9;

$montharr["Oct"] = 10;

$montharr["Nov"] = 11;

$montharr["Dec"] = 12;

if ($params["RouteDate"]){

$arr = explode(" ", $params["RouteDate"]);

$mm = $montharr[$arr[1]];

$dd = $arr[2];

$yyyy = $arr[3];

$datevalue = date("Y-m-d h:i:s", mktime(0, 0, 0, $mm, $dd, $yyyy));

}

$sql = "SELECT dbo.Customers.Route, dbo.Customers.RouteDate, (Seats-(SUM(NoOfPersons+Children))) AS AvailableSeats FROM dbo.Customers RIGHT OUTER JOIN dbo.BusRoutes ON dbo.BusRoutes.Route = dbo.Customers.Route AND dbo.BusRoutes.RouteDate = dbo.Customers.RouteDate WHERE (dbo.Customers.PendingStatus =1) AND dbo.Customers.Route = '" . $params["Route"] . "' AND dbo.Customers.RouteDate = '" . $datevalue . "' GROUP BY dbo.Customers.Route, dbo.Customers.RouteDate, dbo.Customers.PendingStatus, dbo.BusRoutes.Seats";

$rs = CustomQuery($sql);

$data = db_fetch_array($rs);

$result["txt"] = "Available Seats: " . $data["AvailableSeats"];
if($data["AvailableSeats"] == '')

$sql1 = "SELECT Route, RouteDate, Seats FROM BusRoutes WHERE Route = '" . $params["Route"] . "' AND RouteDate = '" . $datevalue . "' GROUP BY dbo.Customers.Route, dbo.Customers.RouteDate, dbo.Customers.PendingStatus, dbo.BusRoutes.Seats";

$rs1 = CustomQuery($sql1);

$data1 = db_fetch_array($rs1);

$result["txt"] = "test: " . $data1["Seats"];
ON CLIENT AFTER TAB

var message = result["txt"];

ctrl.setMessage(message);

C
cgphp 2/19/2013

Try to add curly brackets as below:

$datevalue = "";

$montharr = Array();

$montharr["Jan"] = 1;

$montharr["Feb"] = 2;

$montharr["Mar"] = 3;

$montharr["Apr"] = 4;

$montharr["May"] = 5;

$montharr["Jun"] = 6;

$montharr["Jul"] = 7;

$montharr["Aug"] = 8;

$montharr["Sep"] = 9;

$montharr["Oct"] = 10;

$montharr["Nov"] = 11;

$montharr["Dec"] = 12;

if ($params["RouteDate"]){

$arr = explode(" ", $params["RouteDate"]);

$mm = $montharr[$arr[1]];

$dd = $arr[2];

$yyyy = $arr[3];

$datevalue = date("Y-m-d h:i:s", mktime(0, 0, 0, $mm, $dd, $yyyy));

}

$sql = "SELECT dbo.Customers.Route, dbo.Customers.RouteDate, (Seats-(SUM(NoOfPersons+Children))) AS AvailableSeats FROM dbo.Customers RIGHT OUTER JOIN dbo.BusRoutes ON dbo.BusRoutes.Route = dbo.Customers.Route AND dbo.BusRoutes.RouteDate = dbo.Customers.RouteDate WHERE (dbo.Customers.PendingStatus =1) AND dbo.Customers.Route = '" . $params["Route"] . "' AND dbo.Customers.RouteDate = '" . $datevalue . "' GROUP BY dbo.Customers.Route, dbo.Customers.RouteDate, dbo.Customers.PendingStatus, dbo.BusRoutes.Seats";

$rs = CustomQuery($sql);

$data = db_fetch_array($rs);

$result["txt"] = "Available Seats: " . $data["AvailableSeats"];
if($data["AvailableSeats"] == '')

{

$sql1 = "SELECT Route, RouteDate, Seats FROM BusRoutes WHERE Route = '" . $params["Route"] . "' AND RouteDate = '" . $datevalue . "' GROUP BY dbo.Customers.Route, dbo.Customers.RouteDate, dbo.Customers.PendingStatus, dbo.BusRoutes.Seats";

$rs1 = CustomQuery($sql1);

$data1 = db_fetch_array($rs1);

$result["txt"] = "test: " . $data1["Seats"];

}
K
kyrie76 author 2/19/2013

I tried but it doesn't give me anything

C
cgphp 2/19/2013

Enable firebug and check if you get errors.

K
kyrie76 author 2/19/2013



Enable firebug and check if you get errors.


I do not get any errors

C
cgphp 2/19/2013

Print the $data (print_r($data)) array before the if statement and check the value in firebug.

K
kyrie76 author 2/19/2013



Print the $data (print_r($data)) array before the if statement and check the value in firebug.


if empty I get ''

Othersise I get 'Array'

That is why I added the condition

if($data["AvailableSeats"] == '')

C
cgphp 2/19/2013

You should always get an array. Try to print the $data1 array in the if statement.

K
kyrie76 author 2/19/2013



You should always get an array. Try to print the $data1 array in the if statement.


Ok now it works
I had an error on sql1 (Group By)

$sql1 = "SELECT Route, RouteDate, Seats FROM BusRoutes WHERE Route = '" . $params["Route"] . "' AND RouteDate = '" . $datevalue . "' GROUP BY dbo.Customers.Route, dbo.Customers.RouteDate, dbo.Customers.PendingStatus, dbo.BusRoutes.Seats";
Stupid mistake

Sorry.
Thanks for everything though Christian