Jump to content

Problem with foreach()


patcheen

Recommended Posts

Hello,

 

I can't seem to figure this out. I mean it should work. Any help you could give would be very much appreciated.

 

I am getting the following errors on my webpage

 

Warning: Invalid argument supplied for foreach() in /home/patcheen/public_html/booking/includes/classes/Order.php on line 231

Warning: Invalid argument supplied for foreach() in /home/patcheen/public_html/booking/includes/classes/Order.php on line 235

 

The code it refers to is this

 

    foreach($event_stat as $event_id=>$count){

      if(!Event_stat::dec($event_id,$count)){return FALSE;}

    }

     

    foreach($category_stat as $cat_id=>$count){

      if(!Category_stat::dec($cat_id,$count)){return FALSE;}

      }

 

and here is where $event_stat and $category_stat are declared and populated

 

      $event_stat[$ticket->event_id]++;

        $category_stat[$ticket->category_id]++;

 

Any ideas? Have I given enough information? Thank you so much for taking the time to look over this. I'm not going anywhere until this is resolved. So if you need more info please let me know. Thanks again.

 

Pat

Link to comment
Share on other sites

Still got errors..

 

By the way this is not my code, this from the PhpMyTicket CMS system.

 

So I added the declarations at the top of the class. Here's the entire code.

 

<?php
require_once("classes/ShopDB.php");
require_once("classes/Ticket.php");
require_once("classes/Seat.php");


class Order {

  var $places=array();
  var $event_stat=array();
  var $category_stat=array();
  var $order_user_id;
  var $sid;
  var $handling_id;
  var $organizer_id;

  function Order ($order_user_id,$sid,$handling_id,$organizer_id,$no_fee){

    if(!$order_user_id){return;}  

    $this->order_user_id=$order_user_id;
    $this->order_sid=$sid;
    $this->order_organizer_id=$organizer_id;
    $this->order_handling_id=$handling_id;
    
    require_once('classes/Handling.php');
    $hand=Handling::load($handling_id);
    $this->order_handling=&$hand;
  

  }

  function add_seat ($event_id,$category_id,$place_id,$price,$discount=null){
    //echo "$event_id,$category_id,$place_id,{$this->order_user_id},{$this->order_sid},$price,$discount";
    array_push($this->places,new Ticket($event_id,$category_id,$place_id,$this->order_user_id,$this->order_sid,$price,$discount));
  }
  
  function size (){
    return count($this->places);
  }
  
  function load ($order_id){
    global $_SHOP;
    
    $query="select * from `Order` 
    WHERE order_id = ".ShopDB::quote($order_id)." 
	and order_organizer_id={$_SHOP->organizer_id}";
    if($data=ShopDB::query_one_row($query)){
      $order=new Order(0,0,0,0,0);
      $order->_fill($data);
      return $order;
    }
  }

  function load_ext ($order_id){
    global $_SHOP;
    $query = "SELECT * FROM `Order`, User 
              WHERE order_id = ".ShopDB::quote($order_id)." 
						and order_user_id=user_id 
						and order_organizer_id={$_SHOP->organizer_id}";
      
    if($data=ShopDB::query_one_row($query)){
      $order=new Order(0,0,0,0,0);
      $order->_fill($data);
      return $order;
    }
      
  }
   
  function _fill ($data){
    foreach($data as $k=>$v){
      $this->$k=$v;
    }
  }




  function parzial (){
    $res=0;
    foreach($this->places as $seat){
      $res+=$seat->price;
    }
    return $res;
  }
  
  function save_full (){
    if(!ShopDB::begin()){return FALSE;}
    
    if(!$this->save()){ShopDB::rollback();return FALSE;}
      
    if(!ShopDB::commit()){ShopDB::rollback();return FALSE;}

    return $this->order_id;
  }
  
  function save () {
    

    if($this->order_id){
      return FALSE; //already saved
    }
    
    $parzial=$this->parzial();
    $fee=$this->order_handling->calculate_fee($parzial);
    $total=$parzial+$fee;

    $fee=number_format($fee, 2, '.', '');
    $total=number_format($total, 2, '.', '');
    
    $this->order_partial_price=$parzial;
    $this->order_total_price=$total;
    $this->order_fee=$fee;

    $query="INSERT INTO `Order` (
      order_user_id,
      order_session_id,
      order_tickets_nr,
      order_total_price,
      order_date,
      order_handling_id,
      order_status,
      order_fee,
      order_organizer_id
      ) VALUES (".
      ShopDB::quote($this->order_user_id).",".      
      ShopDB::quote($this->order_sid).",".      
      ShopDB::quote($this->size()).",".      
      ShopDB::quote($total).",".      
      "NOW(),".      
      ShopDB::quote($this->order_handling->handling_id).",".
      ShopDB::quote("ord").",".
      ShopDB::quote($fee).",
      {$this->order_organizer_id}  
      )";


    if(ShopDB::query($query)){
      $order_id=mysql_insert_id();
      $this->order_id=$order_id;
      
      foreach(array_keys($this->places) as $i){
        $ticket =& $this->places[$i];
$ticket->order_id($order_id);

if(!$ticket->save()){
  return FALSE;  
}

        $event_stat[$ticket->event_id]++;
        $category_stat[$ticket->category_id]++;

      }
      
      require_once('classes/Event_stat.php');
      require_once('classes/Category_stat.php');

     foreach($event_stat as $event_id=>$count){
       if(!Event_stat::dec($event_id,$count)){return FALSE;}
     }
      
     foreach($category_stat as $cat_id=>$count){
       if(!Category_stat::dec($cat_id,$count)){return FALSE;}
      }
     $this->set_status('ord',TRUE);
      return $order_id;
      
    }else{
       return FALSE;
    }
  }
  function getID () {
     return $this->order_id;
  }
  
  /* static functions of common use */

function order_delete_ticket ($order_id,$seat_id,$organizer_id,$user_id=0){

  if($user_id){
    $where_t="seat_user_id=$user_id";
    $where_o="order_user_id=$user_id";
  }else{
    $where_t="1";
    $where_o="1";
  }


  if(!ShopDB::begin()){
    echo "<div class=error>".cannot_begin_transaction."</div>";
    return FALSE;
  }

  $query="select * from Seat where seat_id='$seat_id' 
  and seat_order_id='$order_id' and
  seat_organizer_id=$organizer_id
  and $where_t FOR UPDATE";

  if(!$seat=ShopDB::query_one_row($query)){
    echo "<div class=error>".cannot_find_seat."</div>";
    ShopDB::rollback();
    return FALSE;
  }
  
  $query="select * from `Order` where order_id='$order_id' 
  and order_organizer_id=$organizer_id
  and $where_o 
  FOR UPDATE";

  if(!$order=ShopDB::query_one_row($query)){
    echo "<div class=error>".cannot_find_order."</div>";
    ShopDB::rollback();
    return FALSE;
  }
  
  if($order['order_tickets_nr']==1){
    ShopDB::rollback();
    return Order::order_delete($order_id,$organizer_id,$user_id);
  }


  $place=array('seat_id'=>$seat['seat_id'],
               'event_id'=>$seat['seat_event_id'],
               'category_id'=>$seat['seat_category_id'],
       'pmp_id'=>$seat['seat_pmp_id']);

  if(!Seat::cancel(array($place),$seat['seat_user_id'])){
    echo "<div class=error>".cannot_delete_ticket."(1)</div>";
    ShopDB::rollback();
    return FALSE;  
  }

  $query="select sum(seat_price) as total from Seat where seat_order_id='$order_id'";
  if(!$res=ShopDB::query_one_row($query)){
    echo "<div class=error>".cannot_delete_ticket."(2)</div>";
    ShopDB::rollback();
    return FALSE;  
  }
  $total=$res['total'];
  
  require_once('classes/Handling.php');
  if($hand=Handling::load($order['order_handling_id'])){
    
    $fee=$hand->calculate_fee($total);  
    $total+=$fee;
  }

  $query="update `Order` 
          set order_tickets_nr=(order_tickets_nr-1), 
      order_total_price=$total,
      order_fee=$fee
          where order_id='$order_id' and 
  $where_o
  LIMIT 1";
    
  if(!ShopDB::query($query) or mysql_affected_rows()!=1){
    echo "<div class=error>".cannot_delete_ticket."(3)</div>";
    ShopDB::rollback();
    return FALSE;
  }


  if(!ShopDB::commit()){
    echo "<div class=error>".cannot_delete_ticket."(4)</div>";
    ShopDB::rollback();
    return FALSE;
  }
  
  echo "<div class=success>".ticket_deleted."</div>";  
  return TRUE;
}
  
function order_delete ($order_id,$organizer_id,$user_id=0){
  global $_SHOP;
  if(!ShopDB::begin()){
    echo "<div class=error>".cannot_begin_transaction."</div>";
    return FALSE;
  }

  $where_t="seat_organizer_id='$organizer_id'";
  $where_o="order_organizer_id='$organizer_id'";
  
  
  if($user_id){
    $where_t.=" and seat_user_id=$user_id";
    $where_o.=" and order_user_id=$user_id";
  }
  

  $query="SELECT * FROM Seat WHERE seat_order_id='$order_id' and $where_t FOR UPDATE"; 
  if(!$res=ShopDB::query($query)){
    echo "<div class=error>".order_not_canceled."(1)</div>";
    ShopDB::rollback();
    return FALSE;
  }

  $query="select * from `Order` where order_id='$order_id' 
  and $where_o 
  FOR UPDATE";

  if(!$order=ShopDB::query_one_row($query)){
    echo "<div class=error>".cannot_find_order."</div>";
    ShopDB::rollback();
    return FALSE;
  }


  while($row=mysql_fetch_object($res)){
    $user_id=$row->seat_user_id;
    $places[]=array(
      'seat_id'=>$row->seat_id,
      'category_id'=>$row->seat_category_id,
      'event_id'=>$row->seat_event_id,
      'pmp_id'=>$row->seat_pmp_id);
  }   
  
  if(count($places)==0){
    echo "<div class=error>".order_not_canceled."(2)</div>";
    ShopDB::rollback();
    return FALSE;
  }
  
  if(!Seat::cancel($places,$user_id)){
    echo "<div class=error>".order_not_canceled."(3)</div>";
    ShopDB::rollback();
    return FALSE;
  }


  
  $query="UPDATE  `Order` set order_status='cancel' 
          where order_id='$order_id' and $where_o";

  if(!$res=ShopDB::query($query)){
    ShopDB::rollback();
    echo "<div class=error>".order_not_canceled."(4)</div>";
    return FALSE;
  }

  if(!ShopDB::commit()){
    echo "<div class=error>".order_not_canceled."(5)</div>";
    ShopDB::rollback();
    return FALSE;
  }else{
    //echo "<div class=success>".order_canceled."</div>";

    require_once('classes/Handling.php');
    if($this->order_handling=Handling::load($order['order_handling_id'])){
		$this->order_handling->on_order_delete($order_id);
	}

	return TRUE;
  }
} 


function set_payed ($order_id,$organizer_id, $user_id=0){
    
  $order=Order::load($order_id);
  $order->set_payment_status ('payed');
}
  
function order_reemit ($order_id, $organizer_id){

  if(!ShopDB::begin()){
    echo "<div class=error>".cannot_begin_transaction."</div>";
    return FALSE;    
  }

  $query="SELECT * FROM `Order` WHERE order_id='$order_id' and order_organizer_id='$organizer_id' FOR UPDATE";
  if(!$order_old=ShopDB::query_one_row($query)){
    echo "<div class=error>$order_id ".order_not_found."</div>";
    return;
  }
    
  if($order_old['order_status']=='cancel' or 
  $order_old['order_status']=='reemit'){
    echo "<div class=error>$order_id ".order_cannot_reemit."</div>";
    ShopDB::rollback();
    return;
  }
  
  
  $query="INSERT INTO `Order` (
  order_user_id, 
  order_tickets_nr,
  order_total_price,
  order_date,
  order_status,
  order_shipment_status,
  order_payment_status,
  order_handling_id,
  order_fee,
  order_organizer_id
  ) VALUES (
  '{$order_old['order_user_id']}',
  '{$order_old['order_tickets_nr']}',
  '{$order_old['order_total_price']}',
  NOW(),
  '{$order_old['order_status']}',
  '{$order_old['order_shipment_status']}',
  '{$order_old['order_payment_status']}',
  '{$order_old['order_handling_id']}',
  '{$order_old['order_fee']}',
  '{$organizer_id}'
  )";
  
  if(!ShopDB::query($query)){
    echo "<div class=error>$order_id ".order_cannot_reemit."</div>";
    ShopDB::rollback();
    return;
  }

  $new_id=mysql_insert_id();
  echo "<div class=success>".new_order_created.": $new_id</div>";

  $query="SELECT seat_id from Seat where seat_order_id='$order_id' and seat_organizer_id='$organizer_id' FOR UPDATE";
  if(!$res=ShopDB::query($query)){
    echo "<div class=error>$order_id ".order_cannot_reemit."</div>";
    ShopDB::rollback();
    return;
  }

  while($seat = mysql_fetch_array($res)){
    $code=Ticket::generate_code(;
    $query="UPDATE Seat set seat_order_id='$new_id',seat_code='$code' WHERE seat_id='{$seat['seat_id']}' and seat_organizer_id='$organizer_id'";
    if(!ShopDB::query($query)){
      echo "<div class=error>$order_id ".order_cannot_reemit."</div>";
      ShopDB::rollback();
      return;
    }
  }



  $query="UPDATE  `Order` set order_status='reemit',order_reemited_id='$new_id' 
          where order_id='$order_id' and order_organizer_id='$organizer_id'";
  if(!$res=ShopDB::query($query)){
    echo "<div class=error>$order_id ".order_cannot_reemit."</div>";
    ShopDB::rollback();
    return;
  }

  if(!ShopDB::commit()){
    echo "<div class=error>$order_id ".order_cannot_reemit."</div>";
    ShopDB::rollback();
    return;
  }

  echo "<div class=success>$order_id ".old_order_canceled."</div>";

  return $new_id;
} 



function set_status ($new_status,$dont_do_update=FALSE){
  return $this->_set_status('order_status',$new_status,$dont_do_update);  
}

function set_payment_status ($new_status,$dont_do_update=FALSE){
  return $this->_set_status('order_payment_status',$new_status,$dont_do_update);  
}
  
function set_shipment_status ($new_status,$dont_do_update=FALSE){
  return $this->_set_status('order_shipment_status',$new_status,$dont_do_update);  
}

function _set_status ($field,$new_status,$dont_do_update=FALSE){
  global $_SHOP;
  $old_status=$this->order_status;

  if(!$this->user_id){
    $query="select * from User where user_id='{$this->order_user_id}'";
    if($data=ShopDB::query_one_row($query)){
      $this->_fill($data);
    }
  }

  if($field=='order_payment_status' and $new_status=='payed' and $this->order_payment_id){
    $suppl = ", order_payment_id='{$this->order_payment_id}'";
  }

  $query="UPDATE `Order` set $field='$new_status' $suppl where order_id='{$this->order_id}' and order_organizer_id={$_SHOP->organizer_id}";
  if($dont_do_update or (ShopDB::query($query))){// and mysql_affected_rows()==1)){
    if(!$this->order_handling){
      require_once('classes/Handling.php');
      $this->order_handling=Handling::load($this->order_handling_id);
    }  
    $this->order_handling->handle($this,$new_status,$old_status,$field);
  }
}

function toTrash(){
  global $_SHOP;
  ShopDB::begin();

$query="select order_id, order_tickets_nr, count(seat_id) as count 
        from `Order`, Seat
				where seat_order_id=order_id and
				seat_status='trash' and
				order_organizer_id='{$_SHOP->organizer_id}'
				group by order_id
				FOR UPDATE";

if(!$res=ShopDB::query($query)){
  ShopDB::rollback();
	return FALSE;
}				

$count=0;

while($data=mysql_fetch_array($res)){
  if($data['order_tickets_nr']==$data['count']){
  		$count++;

      $query="update `Order` set order_status='trash' where order_id='".
		$data['order_id']."'";

		if(!ShopDB::query($query)){
		  ShopDB::rollback();
			return FALSE;
		}
	}
}

ShopDB::commit();
return $count;
}

function emptyTrash(){
  global $_SHOP;
  ShopDB::begin();

$query="delete `Order`
				from `Order` left join Seat on order_id=seat_order_id 
				where order_status='trash' and 
				order_organizer_id={$_SHOP->organizer_id} and
				seat_id is NULL";

if(!ShopDB::query($query)){
  ShopDB::rollback();
	return FALSE;
}				

ShopDB::commit();
return TRUE;
}

function purgeDeleted($order_handling_id){
  global $_SHOP;

if($order_handling_id>0){
  $handling_cond = "and order_handling_id='$order_handling_id'";
}
$query = "update `Order`
				set order_status='trash'  
				where order_status='cancel' and 
				order_organizer_id={$_SHOP->organizer_id} $handling_cond";

  ShopDB::query($query);
}

function purgeReemited($order_handling_id){
  global $_SHOP;

if($order_handling_id>0){
  $handling_cond = "and order_handling_id='$order_handling_id'";
}
$query = "update `Order`
				set order_status='trash' 
				where order_status='reemit' and 
				order_organizer_id={$_SHOP->organizer_id} $handling_cond";

  ShopDB::query($query);
}

function purge($order_handling_id=0){
Order::purgeDeleted($order_handling_id);
Order::purgeReemited($order_handling_id);
}

function delete_expired($handling_id, $expires_min){
global $_SHOP;
        require_once('classes/ShopDB.php');

$ttl=(int)$expires_min;
if($ttl<=15){return;}

ShopDB::begin();

  $query="SELECT order_id FROM `Order` WHERE 
order_handling_id='$handling_id' and
(now() - interval $ttl minute) > order_date and
order_shipment_status='none' and 
order_status='ord' and
order_payment_status='none' and 
order_payment_id is NULL";

//echo $query;
  if($res=ShopDB::query($query)){
  while($data=mysql_fetch_array($res)){
		$order_id=$data['order_id'];
		echo "<br>$order_id";
		Order::order_delete($order_id,$_SHOP->organizer_id);
	}
}


}

}
?>

EDITED BY WILDTEEN88: Please use code tags (


) when including code within posts

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.