homepage Welcome to WebmasterWorld Guest from 54.161.240.10
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Help Needed In Weeding Out Errors In PHP
st_7




msg:4687203
 3:44 pm on Jul 12, 2014 (gmt 0)

Hello, I'm unable to find out a solution to some 'Undefined Notice' & 'Undefined Variable' errors in some of the pages of my website. I did solve many of them by using 'isset' function as advised on various sites on internet, but just couldn't get around on two instances and being an absolute noob in all the web programming languages didn't help the matter. I'm including the code of both the pages,the page I'm executing & the page the Undefined errors are pointed to.

The following is the code of the file to which the errors are being pointed to, I've highlighted the error lines in 'bold' letters & made a comment on the line itself on what error I'm getting.

<?php

class bsiSearch
{
public $checkInDate = '';
public $checkOutDate = '';
public $mysqlCheckInDate = '';
public $mysqlCheckOutDate = '';
public $currency = '';
public $guestsPerRoom = 0;
public $childPerRoom = 0;
public $extrabedPerRoom = false;
public $nightCount = 0;
public $fullDateRange;
public $roomType = array();
public $multiCapacity = array();
public $searchCode = "SUCCESS";
const SEARCH_CODE = "SUCCESS";
function bsiSearch() {
$this->setRequestParams();
$this->getNightCount();
$this->checkSearchEngine();
if($this->searchCode == self::SEARCH_CODE){
$this->loadMultiCapacity();
$this->loadRoomTypes();
$this->fullDateRange = $this->getDateRangeArray($this->mysqlCheckInDate, $this->mysqlCheckOutDate);
$this->setMySessionVars();
}
}
private function setRequestParams() {
global $bsiCore;
$tmpVar = isset($_POST['check_in'])? $_POST['check_in'] : $_SESSION['sv_checkindate'];
$this->setMyParamValue($this->checkInDate, $bsiCore->ClearInput($tmpVar), NULL, true);
$tmpVar = isset($_POST['check_out'])? $_POST['check_out'] : $_SESSION['sv_checkoutdate'];
$this->setMyParamValue($this->checkOutDate, $bsiCore->ClearInput($tmpVar), NULL, true);
$tmpVar = isset($_POST['capacity'])? $_POST['capacity'] : $_SESSION['sv_guestperroom'];
$this->setMyParamValue($this->guestsPerRoom, $bsiCore->ClearInput($tmpVar), 0, true);

$tmpVar = isset($_POST['child_per_room'])? $_POST['child_per_room'] : $_SESSION['sv_childcount'] ;
$this->setMyParamValue($this->childPerRoom, $bsiCore->ClearInput($tmpVar), 0, true);

$tmpVar = isset($_REQUEST['currency'])? $_REQUEST['currency'] : $_SESSION['sv_currency'] ;
$this->setMyParamValue($this->currency, $bsiCore->ClearInput($tmpVar), 0, true);

$this->mysqlCheckInDate = $bsiCore->getMySqlDate($this->checkInDate);
$this->mysqlCheckOutDate = $bsiCore->getMySqlDate($this->checkOutDate);
}

private function setMyParamValue(&$membervariable, $paramvalue, $defaultvalue, $required = false){
if($required){if(!isset($paramvalue)){$this->invalidRequest();}}
if(isset($paramvalue)){ $membervariable = $paramvalue;}else{$membervariable = $defaultvalue;}
}
private function setMySessionVars(){
if(isset($_SESSION['sv_checkindate'])) unset($_SESSION['sv_checkindate']);
if(isset($_SESSION['sv_checkoutdate'])) unset($_SESSION['sv_checkoutdate']);
if(isset($_SESSION['sv_mcheckindate'])) unset($_SESSION['sv_mcheckindate']);
if(isset($_SESSION['sv_mcheckoutdate'])) unset($_SESSION['sv_mcheckoutdate']);
if(isset($_SESSION['sv_nightcount'])) unset($_SESSION['sv_nightcount']);
if(isset($_SESSION['sv_guestperroom'])) unset($_SESSION['sv_guestperroom']);
if(isset($_SESSION['sv_childcount'])) unset($_SESSION['sv_childcount']);
if(isset($_SESSION['sv_currency'])) unset($_SESSION['sv_currency']);

$_SESSION['sv_checkindate'] = $this->checkInDate;
$_SESSION['sv_checkoutdate'] = $this->checkOutDate;
$_SESSION['sv_mcheckindate'] = $this->mysqlCheckInDate;
$_SESSION['sv_mcheckoutdate'] = $this->mysqlCheckOutDate;
$_SESSION['sv_nightcount'] = $this->nightCount;
$_SESSION['sv_guestperroom'] = $this->guestsPerRoom;
$_SESSION['sv_childcount'] = $this->childPerRoom;
$_SESSION['sv_currency'] = $this->currency;
$_SESSION['svars_details'] = array();
}

private function invalidRequest(){
header('Location: booking-failure.php?error_code=9');
die;
}
private function getNightCount() {
$checkin_date = getdate(strtotime($this->mysqlCheckInDate));
$checkout_date = getdate(strtotime($this->mysqlCheckOutDate));
$checkin_date_new = mktime( 12, 0, 0, $checkin_date['mon'], $checkin_date['mday'], $checkin_date['year']);
$checkout_date_new = mktime( 12, 0, 0, $checkout_date['mon'], $checkout_date['mday'], $checkout_date['year']);
$this->nightCount = round(abs($checkin_date_new - $checkout_date_new) / 86400);
}
private function getDateRangeArray($startDate, $endDate, $nightAdjustment = true) {
$date_arr = array();
$day_array=array();
$total_array=array();
$time_from = mktime(1,0,0,substr($startDate,5,2), substr($startDate,8,2),substr($startDate,0,4));
$time_to = mktime(1,0,0,substr($endDate,5,2), substr($endDate,8,2),substr($endDate,0,4));
if ($time_to >= $time_from) {
if($nightAdjustment){
while ($time_from < $time_to) {
array_push($date_arr, date('Y-m-d',$time_from));
array_push($day_array, date('D',$time_from));
$time_from+= 86400; // add 24 hours
}
}else{
while($time_from <= $time_to) {
array_push($date_arr, date('Y-m-d',$time_from));
array_push($day_array, $time_from);
$time_from+= 86400; // add 24 hours
}
}
}
array_push($total_array, $date_arr);
array_push($total_array, $day_array);
return $total_array;
}
private function checkSearchEngine(){
global $bsiCore;
if(intval($bsiCore->config['conf_booking_turn_off']) > 0){
$this->searchCode = "SEARCH_ENGINE_TURN_OFF";
return 0;
}
$diffrow = mysql_fetch_assoc(mysql_query("SELECT DATEDIFF('".$this->mysqlCheckOutDate."', '".$this->mysqlCheckInDate."') AS INOUTDIFF"));
$dateDiff = intval($diffrow['INOUTDIFF']);
if($dateDiff < 0){
$this->searchCode = "OUT_BEFORE_IN";
return 0;
}else if($dateDiff < intval($bsiCore->config['conf_min_night_booking'])){
$this->searchCode = "NOT_MINNIMUM_NIGHT";
return 0;
}
$userInputDate = strtotime($this->mysqlCheckInDate);
$hotelDateTime = strtotime(date("Y-m-d"));
$timezonediff = ($userInputDate - $hotelDateTime);
if($timezonediff < 0){
$this->searchCode = "TIME_ZONE_MISMATCH";
return 0;
}
}
private function loadRoomTypes() {
$sql = mysql_query("SELECT * FROM bsi_roomtype");
while($currentrow = mysql_fetch_assoc($sql)){
$rtimg = isset($_POST['rtimg']) ? $_POST['rtimg'] : '';
array_push($this->roomType, array('rtid'=>$currentrow["roomtype_ID"], 'rtname'=>$currentrow["type_name"]));
}
mysql_free_result($sql);
}
private function loadMultiCapacity() {
$sql = mysql_query("SELECT * FROM bsi_capacity WHERE capacity >= ".$this->guestsPerRoom);
while($currentrow = mysql_fetch_assoc($sql)){
$this->multiCapacity[$currentrow["id"]] = array('capval'=>$currentrow["capacity"],'captitle'=>$currentrow["title"]);

}
mysql_free_result($sql);
}

public function getAvailableRooms($roomTypeId, $roomTypeName, $capcityid){
global $bsiCore;
$currency_symbol = $bsiCore->config['conf_currency_symbol']; //UNDEFINED INDEX: conf_currency_symbol Error//
$searchresult = array('roomtypeid'=>$roomTypeId, 'roomtypename'=>$roomTypeName, 'capacityid'=>$capcityid, 'capacitytitle'=>$this->multiCapacity[$capcityid]['captitle'], 'capacity'=>$this->multiCapacity[$capcityid]['capval'], 'maxchild'=>$this->childPerRoom);
$room_count = 0;
$dropdown_html = '<option value="0" selected="selected">0</option>';
$price_details_html = '';
$total_price_amount = 0;
$calculated_extraprice = 0;
$total_specail_price=0;
$specail_price_flag=0;
$extraSearchParam = "";
$minimum_night_flg=1;
$variable_concat=1;
$searchsql = "SELECT rm.room_ID, rm.room_no
FROM bsi_room rm
WHERE rm.roomtype_id = ".$roomTypeId."
AND rm.capacity_id = ".$capcityid."".$extraSearchParam."
AND rm.room_id NOT IN
(SELECT resv.room_id
FROM bsi_reservation resv, bsi_bookings boks
WHERE boks.is_deleted = FALSE
AND resv.bookings_id = boks.booking_id
AND resv.room_type_id = ".$roomTypeId."
AND (('".$this->mysqlCheckInDate."' BETWEEN boks.start_date AND DATE_SUB(boks.end_date, INTERVAL 1 DAY))
OR (DATE_SUB('".$this->mysqlCheckOutDate."', INTERVAL 1 DAY) BETWEEN boks.start_date AND DATE_SUB(boks.end_date, INTERVAL 1 DAY)) OR (boks.start_date BETWEEN '".$this->mysqlCheckInDate."' AND DATE_SUB('".$this->mysqlCheckOutDate."', INTERVAL 1 DAY)) OR (DATE_SUB(boks.end_date, INTERVAL 1 DAY) BETWEEN '".$this->mysqlCheckInDate."' AND DATE_SUB('".$this->mysqlCheckOutDate."', INTERVAL 1 DAY))))";

$sql = mysql_query($searchsql);
$tmpctr = 1;
$searchresult['availablerooms'] = array();
while($currentrow = mysql_fetch_assoc($sql)){
$dropdown_html.= '<option value="'.$tmpctr.'">'.$tmpctr.'</option>';
array_push($searchresult['availablerooms'], array('roomid'=>$currentrow["room_ID"], 'roomno'=>$currentrow["room_no"]));
$tmpctr++;
}

mysql_free_result($sql);
if($tmpctr >= 1){
$totalDays = $this->getDateRangeArray($this->mysqlCheckInDate, $this->mysqlCheckOutDate);
$totalamt3=0;


$dayName=array_count_values($totalDays);
$_month = date('M',strtotime($this->mysqlCheckInDate));
$month_ = date('M',strtotime($this->mysqlCheckOutDate));
$_color = '#f2f2f2';
$color_ = '#f2f2f2';
if($_month == $month_){
$mon = $_month;
}else{
$mon = $_month.' - '.$month_;
}
$price_details_html='<tr><td bgcolor='.$_color.'><b>'.MONTH.'</b></td>';
foreach($dayName as $days => $totalnum){

$$days=0;
$h1=$days.$variable_concat;
$$h1=0;
}
$total_child_price=0;
$total_child_price2=0;
$price_details_html.='<td bgcolor='.$color_.' align="right"><b>'.$this->nightCount.' Night(s)</b></td></tr>';
foreach($totalDays[0] as $date2 => $val){
$pricesql = mysql_query("SELECT * FROM bsi_priceplan WHERE roomtype_id = ".$roomTypeId." AND capacity_id = ".$capcityid." AND ('".$val."' BETWEEN start_date AND end_date)");
if(mysql_num_rows($pricesql)){
$row=mysql_fetch_assoc($pricesql);
}else{
$pricesql2 = mysql_query("SELECT * FROM bsi_priceplan WHERE roomtype_id = ".$roomTypeId." AND capacity_id = ".$capcityid." AND default_plan=1");
$row=mysql_fetch_assoc($pricesql2);
}



$day=date('D',strtotime($val));
$$day+=$row[strtolower($day)];
//*************************** specail offer ******************
$sql_sp=mysql_query("select * from bsi_special_offer where '".$val."' between `start_date` and `end_date` and (room_type=".$roomTypeId ." or room_type=0)");
$row99=mysql_fetch_assoc($sql_sp);

$h2=$day.$variable_concat;
if(mysql_num_rows($sql_sp)){
$c999=round($row[strtolower($day)]- (($row[strtolower($day)]*$row99['price_deduc'])/100),1);
$$h2+=$c999;
if($row99['min_stay'] <= $minimum_night_flg){ $specail_price_flag=1; }
$minimum_night_flg++;
}else{
$$h2+=$row[strtolower($day)];
}

//echo $$day.'/'.$$h2.' ';
//*************************** specail offer ******************
$chld_row2=mysql_fetch_assoc(mysql_query("SELECT distinct(`no_of_child`) FROM `bsi_room` WHERE `capacity_id`=".$capcityid." and `roomtype_id`=".$roomTypeId.""));
if($chld_row2['no_of_child'] >= $this->childPerRoom && $this->childPerRoom != 0){
$child_flag=true;
$childpricesql = mysql_query("SELECT * FROM bsi_priceplan WHERE roomtype_id = ".$roomTypeId." AND capacity_id =1001 AND ('".$val."' BETWEEN start_date AND end_date)");
if(mysql_num_rows($pricesql)){
$chrow=mysql_fetch_assoc($childpricesql);
}else{
$childpricesql2 = mysql_query("SELECT * FROM bsi_priceplan WHERE roomtype_id = ".$roomTypeId." AND capacity_id =1001 AND default_plan=1");
$chrow=mysql_fetch_assoc($childpricesql2);
}

$day=date('D',strtotime($val));
$$day+=($chrow[strtolower($day)]*$this->childPerRoom);
//*************************** specail offer ******************
$sql_sp1=mysql_query("select * from bsi_special_offer where '".$val."' between `start_date` and `end_date` and (room_type=".$roomTypeId ." or room_type=0)");


//$h22=$day.$variable_concat;
if(mysql_num_rows($sql_sp1)){
$row999=mysql_fetch_assoc($sql_sp1);
$c999=round(($chrow[strtolower($day)]*$this->childPerRoom)- ((($chrow[strtolower($day)]*$this->childPerRoom)*$row999['price_deduc'])/100),1);
//echo $c999;
$$h2+=$c999;
$total_child_price2+=$c999;

}else{

$$h2+=($chrow[strtolower($day)]*$this->childPerRoom);
$total_child_price2+=($chrow[strtolower($day)]*$this->childPerRoom);
}

//echo $$day.'/'.$$h2.' ';
//*************************** specail offer ******************
$total_child_price+=($chrow[strtolower($day)]*$this->childPerRoom);



}
}
$night_count_at_customprice = 0;
$searchresult['prices'] = array();


foreach($dayName as $days => $totalnum){
$totalamt3=$totalamt3+$$days;
$h3=$days.$variable_concat;
$total_specail_price=$total_specail_price+$$h3;
}

$total_price_amount=$totalamt3;
// echo $total_specail_price;
if($bsiCore->config['conf_tax_amount'] > 0 && $bsiCore->config['conf_price_with_tax']==1){
$total_price_amount=$total_price_amount+(($total_price_amount * $bsiCore->config['conf_tax_amount'])/100);
$total_specail_price=$total_specail_price+(($total_specail_price * $bsiCore->config['conf_tax_amount'])/100);
$total_child_price=$total_child_price+(($total_child_price * $bsiCore->config['conf_tax_amount'])/100);
$total_child_price2=$total_child_price2+(($total_child_price2 * $bsiCore->config['conf_tax_amount'])/100);
}

}
if($specail_price_flag){
$searchresult['roomprice'] = $total_specail_price;
}else{
$searchresult['roomprice'] = $total_price_amount;
}
if($tmpctr > 1) array_push($_SESSION['svars_details'], $searchresult);
unset($searchresult);

return array(
'roomcnt' => $tmpctr-1,
'roomdropdown' => $dropdown_html,
'pricedetails' => $price_details_html,
'child_flag'=>$child_flag, //UNDEFINED VARIABLE CHILD_FLAG//
'specail_price_flag'=>$specail_price_flag,
'total_specail_price'=>$total_specail_price,
'total_child_price'=>$total_child_price,
'total_child_price2'=>$total_child_price2,
'totalprice' => $total_price_amount);
}
}
?>

[1][edited by: coopster at 4:24 pm (utc) on Jul 17, 2014]
[edit reason] "undefined" update. Please do not use pastebin, doesn't help future readers. Thanks [/edit]

 

st_7




msg:4687209
 4:36 pm on Jul 12, 2014 (gmt 0)

There is a mistake in the original post, I wrongly wrote 'Unindexed Index' & 'Unindexed Variable', instead of writing 'Undefined Index' & 'Undefined Variable'. I request moderators to please correct the same as I couldn't edit it myself(can't find any edit button).

penders




msg:4687254
 11:12 pm on Jul 12, 2014 (gmt 0)

What is $bsiCore?

We could easily "hide" these "notices" (they are really just informative messages, not really "errors"), however, we could just be hiding a more serious error. If these variables or array indices should be defined at this point in the code then it is indication of an error - a misspelling, typo, configuration error, etc. However, it could simply be sloppy programming... relying on the "default value" that PHP assigns (only in these situations can isset() be used to resolve the issue).

$currency_symbol = $bsiCore->config['conf_currency_symbol']; //UNDEFINED INDEX: conf_currency_symbol Error//


Ordinarily it would seem strange that this array index is not defined - as it looks like an important configuration value is not set? However,
$currency_symbol is not used in any of the code you have posted, so this line would appear to be entirely redundant!? Just delete it (read: comment it out)! (?)

'child_flag'=>$child_flag, //UNDEFINED VARIABLE CHILD_FLAG//


This looks like it is relying on PHP defaulting the value. When a variable is not defined, it defaults to NULL (which evaluates to false in a boolean expression). Earlier in the code (inside an IF construct) it is explicitly being assigned the value (bool)true; so this is consistent. Ideally this variable should be initialised to (bool)false at the start of the script (inside the getAvailableRooms() method I think - although without proper indentation it's difficult to tell where methods start and end).

or, to hack that one line...

'child_flag' => (isset($child_flag) ? $child_flag : false),


However, if the calling function is expecting NULL specifically, then you will need to change the above references to false to null!

st_7




msg:4687414
 8:56 pm on Jul 13, 2014 (gmt 0)

@penders,

Thank you very much for replying, especially the detailed technical explaination that you have given is(& will be) very useful to gain some knowledge.

although without proper indentation it's difficult to tell where methods start and end

here is a pastebin link[pastebin.com ] to the code that I pasted in my original post(I should've provided it in the original post itself along with the one that I've provided for), this may help you to understand code better, because the indentation is there in pastebin.

The Unindexed error I'm getting for the following line
$currency_symbol = $bsiCore->config['conf_currency_symbol'];
is for 'conf_currency_symbol'. The error reads like this, Undefined index: conf_currency_symbol in so & so line of so & so page. Searching through other class files, whether conf_currency_symbol is anywhere defined or not, I found it is defined(if I'm right to assume so) in another class file which is the main class file. It is commented though(I don't know why). Here are those particular lines of the code:
//echo "htmlentities('".$_POST['currency_symbol']."','ENT_COMPAT','utf-8')";
$this->configure_update('conf_notification_email', mysql_real_escape_string($_POST['email_notification']));
//$this->configure_update('conf_currency_code', mysql_real_escape_string($_POST['currency_code']));
//$this->configure_update('conf_currency_symbol', htmlentities(mysql_real_escape_string($_POST['currency_symbol']),ENT_COMPAT,'utf-8'));
$this->configure_update('conf_booking_turn_off', mysql_real_escape_string($_POST['booking_turn']));

I've uncommented those lines & after uncommenting them, I tried to import the definition or the value of that 'conf_currency_symbol' to the present file(to which the error is pointed to), I've used "include_once" & done like this:
public function getAvailableRooms($roomTypeId, $roomTypeName, $capcityid){
global $bsiCore;
include_once("includes/admin.class.php");
$currency_symbol = $bsiCore->config['conf_currency_symbol'];
$searchresult = array('roomtypeid'=>$roomTypeId, 'roomtypename'=>$roomTypeName, 'capacityid'=>$capcityid, 'capacitytitle'=>$this->multiCapacity[$capcityid]['captitle'], 'capacity'=>$this->multiCapacity[$capcityid]['capval'], 'maxchild'=>$this->childPerRoom);
$room_count = 0;
........
but still I had that error being reported.

I agree that $currency_symbol is not used elsewhere in the file. Anyway I find few references to 'currency_symbol' & 'bsiconfig' in other class files(configclass file & mainclassfile). Here are the pastebin links to mainclass file([pastebin.com ]), configclass file([pastebin.com ]) & serachclass file([pastebin.com ], this is the file that are errors are pointed to).

And as far as 'child_flag' error is concerned, my assessment was also same, infact I added a line $child_flag=false; just above 'return array' line at the end of the page(where the error is being pointed out). The errors were gone. But I wasn't sure if it was a right solution. That's why I wanted to know from more knowledged people like you. And now I followed your direction, used 'isset' instead & changed $child_flag=false; to 'child_flag' => (isset($child_flag) ? $child_flag : false). Though I'm not sure if it should be 'False' or 'Null'.

Once again thank you for the information.

penders




msg:4687419
 10:02 pm on Jul 13, 2014 (gmt 0)

I added a line $child_flag=false; just above 'return array' line at the end of the page(where the error is being pointed out). The errors were gone. But I wasn't sure if it was a right solution.


Yes, that would not have been correct. In fact that could have caused more problems, since
'child_flag' would always be (bool)false, even when it should have been true.

Though I'm not sure if it should be 'False' or 'Null'.


It should probably work with either. (bool)false would generally be more correct. I would be surprised if it only worked with NULL (bad code smell if it did). A "flag" (from the variable name) is generally a value that is either true or false.

include_once("includes/admin.class.php");


That just looks wrong to be honest. I would assume that "admin.class.php" contains a class definition. Simply including it would do nothing (or result in an error if already included).
$_POST['currency_symbol']) might not even be set anyway?
Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved