__construct() problem ??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
minos_mks
Forum Commoner
Posts: 69
Joined: Thu Feb 04, 2010 1:58 am

__construct() problem ??

Post by minos_mks »

Can anybody help to fix that problem ????

Code: Select all

class DatabaseObject {
public $item_code;
	public $price	;
	public $sqr;
	public $type	;	 
	public $n_bed;
	public $n_bath;
	public $activation;
	public $disc;
	public $note;
	public $datetime;
	public $has_pic	;
	public $state;
	public $address;
	public $city;
	public $zipcode;
	public $fname;
	public $lname;
	public $email;
	public $phone;
	public $hphone;
	public $Country;
	public $year ;
	public $mileage;
function __construct() 
{
	if ( $table_name == 1 )
	{  		echo "home seq</br>";   
		  // protected static $table_name ;
			protected static $db_fields = array('item_code', 'price','fname','lname','email', 'sqr', 'type','n_bed',
											'n_bath','phone','hphone','activation', 'disc','note','datetime','has_pic','address',
											'state','city','Country','zipcode','user_code','year');
	}
	if ( $table_name == 2 )
	{       protected static $table_name ="";
			echo "auto seq</br>";
			protected static $db_fields = array('item_code', 'price','fname','lname','email', 'mileage', 'type',
										'phone','hphone','activation', 'disc','note','datetime','has_pic','address',
										'state','city','Country','zipcode','user_code','year');
	}
 }

it's giving me that error:

( ! ) Parse error: syntax error, unexpected T_PROTECTED in C:\wamp\www\1111\includes\database_object.php on line 35

line 35 is :
protected static $db_fields = array('item_code', 'price','fname','lname','email', 'sqr', 'type','n_bed',
'n_bath','phone','hphone','activation', 'disc','note','datetime','has_pic','address',
'state','city','Country','zipcode','user_code','year');
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: __construct() problem ??

Post by social_experiment »

Code: Select all

<?php
protected static $db_fields = array('item_code', 'price','fname','lname','email', 'sqr', 'type','n_bed',
                                                                                        'n_bath','phone','hphone','activation', 'disc','note','datetime','has_pic','address',
                                                                                        'state','city','Country','zipcode','user_code','year');
?>
you have to declare this at the top of the class not inside the methods and when calling it use $this->db_fields
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
minos_mks
Forum Commoner
Posts: 69
Joined: Thu Feb 04, 2010 1:58 am

Re: __construct() problem ??

Post by minos_mks »

but i have more than one $db_fields for different DB tables. i want to use each one depend on the entry form page . that's why i am cheeking the $table_name !!!!!
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: __construct() problem ??

Post by social_experiment »

you can declare the variables as you do, you just can't use the keywords private, protected, public in front of the variable when you do declare them inside a method (a function in the class)

a different approach might be this

Code: Select all

<?php
    // top of the class
    protected $_db_fields;

 if ($tablename == 2) {
     $this->_db_fields = array('field', 'field1');
 }
 // repeat...
?>
then you can use $this->_db_fields elsewhere in the class when you want to access the contents of the property
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
minos_mks
Forum Commoner
Posts: 69
Joined: Thu Feb 04, 2010 1:58 am

Re: __construct() problem ??

Post by minos_mks »

Thank you So much
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: __construct() problem ??

Post by Celauran »

Rather than having a bunch of conditionals, wouldn't it make more sense to just inspect the table?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: __construct() problem ??

Post by AbraCadaver »

Celauran wrote:Rather than having a bunch of conditionals, wouldn't it make more sense to just inspect the table?
Yes, or maybe extend DatabaseObject with an Auto class and a Home class?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
minos_mks
Forum Commoner
Posts: 69
Joined: Thu Feb 04, 2010 1:58 am

Re: __construct() problem ??

Post by minos_mks »

okay , first of all thank you guys for your help but i still didn't get that done yet ,

so i have 3 files: database_object.php , add.php , add_ad_result.php

first: add_ad_result.php

Code: Select all

<?php require_once("/includes/initialize.php"); ?>
<?php 	include ("layouts/header.php"); 
		//require_once(LIB_PATH.DS."phpMailer".DS."language".DS."phpmailer.lang-en.php");
		//require_once(LIB_PATH.DS."phpMailer".DS."class.phpmailer.php");
		//require_once(LIB_PATH.DS."phpMailer".DS."class.smtp.php"); 
?>


        <div class="art-content-layout">
    <div class="art-content-layout-row">
	 <div id="accordion">
	 <?php
		
if(isset($_POST['submit'])) 
{echo "form fileds submited</br></br>";
	$table_name= $_GET["table_name"];
	$disc = trim($_POST['disc']);
	$type = trim($_POST['type']);
	$address = trim($_POST['address']);
	$fname = trim($_POST['fname']);
	$lname = trim($_POST['lname']);
	$email = trim($_POST['email']);
	$email2 = trim($_POST['email2']);
	$phone = trim($_POST['phone']);
	$hphone = trim($_POST['hphone']);
	if(isset($_POST['sqr']))       {$sqr = $_POST['sqr'];}else {$sqr = 0;}
	$Price = trim($_POST['Price']);
	$note = trim($_POST['note']);
	$city = trim($_POST['city']);
	$state = trim($_POST['state']);
	$Country = trim($_POST['Country']);
	$n_bed = trim($_POST['n_bed']);
	$n_bath = trim($_POST['n_bath']);
	$datetime = trim($_POST['datetime']);
	$user_code = trim($_POST['user_code']);
	if(isset($_POST['mileage']))       {$mileage = $_POST['mileage'];}else {$mileage = 0;}
	if(isset($_POST['year']))       {$mileage = $_POST['year'];}else {$year = 0;}
	$zipcode = "n/a";
	$activation = "n/a";
	
if ($table_name == "ad_d_apart") 
 {
	$new_ad = ad::MakeObjectApart($table_name,$lname,$fname,$Price,$sqr,$type,$email,$hphone,$phone,
	$n_bed,$n_bath,$activation,$disc,$note,$datetime,$zipcode,$state,$address,$Country,$city,$user_code);
}
if ($table_name == "ad_d_cars") 
 {
	$new_ad = ad::MakeObjectcars($table_name,$lname,$fname,$Price,$mileage,$type,$email,$hphone,$phone,
	$year ,$activation,$disc,$note,$datetime,$zipcode,$state,$address,$Country,$city,$user_code);
}	  echo $tablename = $table_name ;
if ($table_name == "ad_d_jobs") 
 {
	$new_ad = ad::MakeObjectjobs($table_name,$lname,$fname,$Price,$sqr,$type,$email,$hphone,$phone,
	$n_bed,$n_bath,$activation,$disc,$note,$datetime,$zipcode,$state,$address,$Country,$city,$user_code);
}
if ($table_name == "ad_d_donation") 
 {
	$new_ad = ad::MakeObjectdonation($table_name,$lname,$fname,$Price,$sqr,$type,$email,$hphone,$phone,
	$n_bed,$n_bath,$activation,$disc,$note,$datetime,$zipcode,$state,$address,$Country,$city,$user_code);
}	  

if($new_ad && $new_ad->create($tablename)) 
	  {
			// comment saved
			// No message needed; seeing the comment is proof enough.
			
			// Send email
			//$new_ad->try_to_send_notification();
			echo "object created and saved";
	    // Important!  You could just let the page render from here. 
	    // But then if the page is reloaded, the form will try 
			// to resubmit the comment. So redirect instead:
	    //redirect_to("photo.php?id={$photo->id}");
	
		} else
		{
			// Failed
	  echo  $message = "There was an error that prevented the comment from being saved.";
		}
		} 
else 
{ 
		 $table_name= $_GET["table_name"];
	  $disc = "";
	  $address = "";
	  $fname = "";
	  $lname = "";
	  $email = "";
	  $email2 = "";
	  $phone = "";
	  $hphone = "";
	  $sqr = "";
	  $Price = "";
	  $note = "";
	  $city = "";
	  $state = "";
	  $Country = "";
	  $n_bed = "";
	  $n_bath = "";
	  $type = "";
	  $datetime = "";
	  $user_code = "";
	
} 
 
	?>

	<h1>Post Another AD</h1>
	<h1>Go To Housing Ads</h1>
	

 </div>
 <?php include ("layouts/footer.php"); ?>

----------------------------------------------------------

second file add.php

Code: Select all

<?php

// If it's going to need the database, then it's 
// probably smart to require it before we start.
require_once(LIB_PATH.DS.'database.php');

class ad extends DatabaseObject {

    protected static $table_name ="ad_d_cars";
    //protected static $table_name2 ="ad_d_apart";
protected static $db_fields ;

	  public $item_code;
	public $price	;
	public $sqr;
	public $type	;	 
	public $n_bed;
	public $n_bath;
	public $activation;
	public $disc;
	public $note;
	public $datetime;
	public $has_pic	;
	public $state;
	public $address;
	public $city;
	public $zipcode;
	public $fname;
	public $lname;
	public $email;
	public $phone;
	public $mileage ;
	public $hphone;
	public $year;
	public $Country;
	
	public $user_code = 1;
	
	//private $temp_path;
	//protected $upload_dir="images";
	
 	public static function MakeObjectApart ($table_name,$lname,$fname,$Price,$sqr,$type,$email,$hphone,$phone,
	$n_bed,$n_bath,$activation,$disc,$note,$datetime,$zipcode,$state,$address,$Country,$city,$user_code) 
	{global $db_fields ;
		 $db_fields = array('item_code', 'price','fname','lname','email', 'sqr', 'type','n_bed',
                                    'n_bath','phone','hphone','activation', 'disc','note','datetime','has_pic','address',
									'state','city','Country','zipcode','user_code');	
	
			 print_r ($db_fields);
			$ad = new ad();
	    
	    //$ad->created = strftime("%Y-%m-%d %H:%M:%S", time());
	    $ad->table_name = $table_name;
	    $ad->lname = $lname;
	    $ad->fname = $fname;
	    $ad->email = $email;
	    $ad->hphone = $hphone;
	    $ad->phone = $phone;
	    $ad->price = $Price;
	    $ad->sqr = $sqr;
	    $ad->type = $type;
	    $ad->n_bed = $n_bed;
	    $ad->n_bath = $n_bath;
	    $ad->activation = $activation;
	    $ad->disc = $disc;
	    $ad->note = $note;
	    $ad->address = $address;
	    $ad->zipcode = $zipcode;
	    $ad->datetime = $datetime;
	    $ad->city = $city;
	    $ad->user_code = $user_code;
	    $ad->state = $state;
	    $ad->Country = $Country;
		//echo "object created";
	    return $ad;
		
	}

 	public static function MakeObjectcars($table_name,$lname,$fname,$Price,$mileage,$type,$email,$hphone,$phone,
	$year ,$activation,$disc,$note,$datetime,$zipcode,$state,$address,$Country,$city,$user_code) 
	{
	
	$db_fields = array('item_code', 'price','fname','lname','email', 'sqr', 'type',
                                    'phone','hphone','activation', 'disc','note','datetime','has_pic','address',
									'state','city','Country','zipcode','user_code','year','mileage');
		print_r ($db_fields);	$ad = new ad();
	    //$ad->created = strftime("%Y-%m-%d %H:%M:%S", time());
	    $ad->table_name = $table_name;
	    $ad->lname = $lname;
	    $ad->fname = $fname;
	    $ad->email = $email;
	    $ad->hphone = $hphone;
	    $ad->phone = $phone;
	    $ad->price = $Price;
	    $ad->mileage = $mileage;
	    $ad->type = $type;
	    $ad->year = $year;
	    $ad->n_bath = $n_bath;
	    $ad->activation = $activation;
	    $ad->disc = $disc;
	    $ad->note = $note;
	    $ad->address = $address;
	    $ad->zipcode = $zipcode;
	    $ad->datetime = $datetime;
	    $ad->city = $city;
	    $ad->user_code = $user_code;
	    $ad->state = $state;
	    $ad->Country = $Country;
		//echo "object created";
	    return $ad;	
	}	
	function getjobtype($id)
	{
	$sql = "SELECT * FROM jobs_type" ;
	$sql .= " where id=$id LIMIT 1";
	$jobtypes = DatabaseObject::find_by_sql($sql);
	foreach($jobtypes as $jobtype):
	     $jobname =   $jobtype->type;
		 	endforeach; 
			return $jobname;
	}
	
function delete_ad ($cat_type,$ad_code)						///    To Add Ad
 {
    global $connection; 
    $tablename = select_table($cat_type);     
    $pic_tablename =  select_pic_tablename($cat_type); 
    
   $query1 = "   DELETE FROM $pic_tablename
          WHERE item_code=$ad_code ";
    echo $query1;
$delete_ad_pic = mysql_query($query1,$connection);
            confirm_query($delete_ad_pic);
            
    $query2 = "   DELETE FROM $tablename
              WHERE item_code=$ad_code ";
      echo $query2;  
    $delete_ad = mysql_query($query2,$connection);
                confirm_query($delete_ad);             
           return $delete_ad;
 }	
  function select_ad_update ($user_code,$tablenumb)                 // to review the resent ads for one user
{
 global $connection; 
//echo $tablenumb ;
$tablename = select_table($tablenumb);
            $query = " select $tablename.disc , $tablename.item_code ";
            $query .= " from $tablename ,ads_deitals_items ";  
            $query .= " where activation = 1 and ads_deitals_items.item_code = $tablename.item_code and ads_deitals_items.user_name = \"$user_code\"";
            $query .= " ORDER BY $tablename.datetime DESC";
             
            echo $query;
            $select_ad_update_apart = mysql_query($query,$connection);
            return $select_ad_update_apart;
} 
 function user_ads ($tablename,$user_code)                   ///    to view all the ads for the same user
{ global $connection;
$query = " SELECT * ";
$query .= " from $tablename , ads_deitals_items "; 
if  ($tablename=="ad_d_jobs")
            {
            $query .= " ,jobs_type";
            } 
if  ($tablename=="ad_d_furnisher")
            {
            $query .= " ,furnisher_categ";
            } 
if  ($tablename=="ad_d_needs")
            {
            $query .= " ,need_categ";
            } 
if  ($tablename=="ad_d_donation")
            {
            $query .= " ,donation_categ";
            }               
$query .= " where activation = 1 and ads_deitals_items.item_code = $tablename.item_code and ads_deitals_items.user_name = \"$user_code\"";
 if  ($tablename=="ad_d_jobs")
            {
            $query .= " and ad_d_jobs.job_type = jobs_type.jobs_type_code  ";
            }
 if  ($tablename=="ad_d_furnisher")
            {
            $query .= " and ad_d_furnisher.item_categ_code = furnisher_categ.furnisher_categ_code  ";
            }
 if  ($tablename=="ad_d_needs")
            {
            $query .= " and ad_d_needs.need_categ_code = need_categ.need_categ_code  ";                  
            }
 if  ($tablename=="ad_d_donation")
            {
            $query .= " and ad_d_donation.donation_categ_code = donation_categ.donation_categ_code  ";
            }           
 $query .= " ORDER BY ads_deitals_items.datetime DESC";
 
          //  echo "$query.<BR><BR>";
            $user_ads = mysql_query($query,$connection);
            confirm_query($user_ads); 
            return $user_ads;
}
 function resent_ads ($tablename,$per_categ)                 // to review the resent ads
{ global $connection;
            $query = " SELECT * ";
            $query .= " from $tablename ";  
            $query .= " where activation = 1 ";
            $query .= " ORDER BY datetime DESC";
            $query .= " LIMIT  $per_categ ";
             if  ($tablename=="ad_d_jobs")
            {
            $query = " SELECT * ";
            $query .= " from $tablename ,jobs_type  ";
            $query .= " where activation = 1 and ad_d_jobs.job_type = jobs_type.jobs_type_code  ";
            $query .= " ORDER BY datetime DESC";
            $query .= " LIMIT  $per_categ ";   
                }
            echo $query;
			//$result = mysql_query($query, $database->connection);
            $resent_ads = mysql_query($query,$connection);
            confirm_query($resent_ads); 
            return $resent_ads;
}
function select_ads_note ($tablename,$ad_code)               // select add rec
{ global $connection;
            $query = "SELECT * ";
			$query .= "from $tablename ";
			$query .= "where activation = 1 , item_code = $ad_code";
			echo $query;
			$result_ads = mysql_query($query,$connection);
			confirm_query($result_ads_note);
			return $result_ads_note;
}          

 function repost_ad ($tablenumb,$ad_code)
 {
    global $connection;
    $tablename = select_table($tablenumb);  
   $query = "   UPDATE ads_deitals_items , $tablename SET 
 ads_deitals_items.datetime = NOW(),$tablename.datetime = NOW()
WHERE ads_deitals_items.item_code=$ad_code and $tablename.item_code=$ad_code ";
 echo    $query;
$update_ad = mysql_query($query,$connection);
            confirm_query($update_ad); 
            return $update_ad; 
 }
 function select_ads ($tablename,$per_page,$start,$order,$tablenumb)
{ global $connection;
            $query = " SELECT * ";
			$query .= " from $tablename ";
			$query .= " where activation = 1 ";
             $query .= " order by $order DESC  ";
			$query .= " ORDER BY datetime DESC";
            $query .= " LIMIT $start , $per_page ";
			echo $query;
			$result_ads = mysql_query($query,$connection);
			confirm_query($result_ads); 
			return $result_ads;
}
function select_not_ad ($tablename,$item_code)
{ global $connection;
            $query = "SELECT * ";
			$query .= "from $tablename ";
			$query .= "where item_code = $item_code  ";
			//echo $query;
			$result_not_ad = mysql_query($query,$connection);
			confirm_query($result_not_ad);
			return ($result_not_ad);
}
function get_not_ad($tablenumb,$item_code)
{	
		  $tablename =  select_table($tablenumb);
		  $not_ad_data= ad::select_not_ad ($tablename,$item_code);
		  //echo $tablename;
		//$data = select_ads ($tablename);
	 
		return ($not_ad_data);  
		}
                                    ///////////////    get the search resaults /////////////
function get_ads_search ($cat_type,$per_page,$start,$order,$search_or_no,$disc,$has_pic,$only_titel,$min_price,$max_price,$region,$state,$zipcode,$radius,
                         $min_year,$max_year,$model,$min_mileage,$max_mileage,$shiping,$apart_type,$n_bed,$max_Foot,$min_Foot,$furn_categ,$doun_categ,
						 $rating,$exp_year,$jobs_code,$UTI_categ)
{ 
  
$tablename = select_table($cat_type);
     //   echo "</br>stat".$city."</br>";
	//	  echo "</br>city".$city."</br>";
	 $region=city::select_city_by_id($region);
	 $state=city::select_state_by_id($state);
	 $state=ucfirst(strtolower($state));        // upper case 
$zipcodes_list = get_zip_by_citys($region,$state);
while ($ad = mysql_fetch_array($zipcodes_list)) 
			{           
					  $zipcodes=$ad['zip_code']  ;
					 // echo $zipcodes ;
			}
//$string = var_export($zipcodes, true);                   //print array as string

                           
						 
$data = ad::select_ads_search ($tablename,$per_page,$start,$order,$search_or_no,$disc,$has_pic,$only_titel,$min_price,$max_price,$region,$state,$zipcode,$radius,
                           $min_year,$max_year,$model,$min_mileage,$max_mileage,$shiping,$apart_type,$n_bed,$max_Foot,$min_Foot,$furn_categ,$doun_categ,
						   $rating,$exp_year,$jobs_code,$UTI_categ,$zipcodes);     // the user search 
          
        return ($data);  
}											 // ads search		
function select_ads_search ($tablename,$per_page,$start,$order,$search_or_no,$disc,$has_pic,$only_titel,$min_price,$max_price,$region,$state,$zipcode,$radius,
                           $min_year,$max_year,$model,$min_mileage,$max_mileage,$shiping,$apart_type,$n_bed,$max_Foot,$min_Foot,$furn_categ,$doun_categ,
						   $rating,$exp_year,$jobs_code,$UTI_categ,$zipcodes)
{ global $connection;       
      	
			//	 echo "</br>region:".$region."</br>";  
            //  echo $zipcodes[0];
            $query = " SELECT * ";
            $query .= " from $tablename ";
            $query .= " where activation = 1";

             if ($disc) 
             {
              $query .= " and disc LIKE  '%$disc%'" ;   
             }
               if ($apart_type <> 0) 
             {
              $query .= " and type =  $apart_type" ;   
             }
             if ($doun_categ <> 0) 
             {
              $query .= " and donation_categ_code =  $doun_categ" ;   
             }
             if ($UTI_categ <> 0) 
             {
              $query .= " and need_categ_code =  $UTI_categ" ;   
             }
			 if ($furn_categ<>0) // furnither                                               
             {
              $query .= " and item_categ_code =  $furn_categ" ;   
             }
             if ($has_pic == 1 )
              {
                 $query .= " and has_pic = 1" ;
                // echo "</br>$query</br>"; 
              } 
               if ($jobs_code <> 0) 
             {
              $query .= " and job_type =  $jobs_code" ;   
             }
              if ($rating)                                          
              {
			 // echo "insed rating";
                 $query .= " and rating = \"$rating\"" ;
                // echo "</br>$query</br>"; 
              } 
              if ($exp_year <> 0)
              {
                 $query .= " and years_exprin = $exp_year" ;
                // echo "</br>$query</br>"; 
              } 
 if ($min_price && $max_price)                        
                 {    //echo "min_price:$min_price</br>" ;
                      $query .= " and price BETWEEN $min_price AND $max_price" ;
                      //echo "</br>$query</br>";
               }
             if ($min_price && !$max_price ) 
                  {
                     $query .= " and price >= $min_price" ;
                      //   echo "</br>$query</br>";
                  }
             if (!$min_price && $max_price ) 
                  {
                     $query .= " and price <= $max_price" ;
                       //  echo "</br>$query</br>";
                  }
               if ($shiping <> 0)
              {
                 $query .= " and shiping_police = $shiping" ;
                // echo "</br>$query</br>"; 
              }   
              
                  				
             if ($model) 
             {
                 if ($model<>0){$query .= " and c_model = $model" ;    // if it's  = 0 that's mean that he need all the models
                        // echo "</br>$query</br>";
                 }
             }                                                  
             //////////////
             if ($min_mileage && $max_mileage)                         
                 {    
                      $query .= " and mileage BETWEEN $min_mileage AND $max_mileage" ;
                    //  echo "</br>$query</br>";
               }
             if ($min_mileage && !$max_mileage ) 
                  {
                     $query .= " and mileage >= $min_mileage" ;
                      //   echo "</br>$query</br>";
                  }
             if (!$min_mileage && $max_mileage ) 
                  {
                     $query .= " and mileage <= $max_mileage" ;
                      //   echo "</br>$query</br>";
                  }
                  //////
                    if ($min_year && $max_year)                         
                 {    
                      $query .= " and year BETWEEN $min_year AND $max_year" ;
                     // echo "</br>$query</br>";
               }
             if ($min_year && !$max_year ) 
                  {
                     $query .= " and year >= $min_year" ;
                       //  echo "</br>$query</br>";
                  }
             if (!$min_year && $max_year ) 
                  {
                     $query .= " and year <= $max_year" ;
                     //    echo "</br>$query</br>";
                  }
             if ($n_bed && $n_bed <> "Any" ) 
                  {
                     $query .= " and n_bed = $n_bed" ;
                     //    echo "</br>$query</br>";
                  }
                  if ($min_Foot && $max_Foot)                         
                 {    
                      $query .= " and sqr BETWEEN $min_Foot AND $max_Foot" ;
                     // echo "</br>$query</br>";
               }
             if ($min_Foot && !$max_Foot ) 
                  {
                     $query .= " and sqr >= $min_Foot" ;
                       //  echo "</br>$query</br>";
                  }
             if (!$min_Foot && $max_Foot ) 
                  {
                     $query .= " and sqr <= $max_Foot" ;
                     //    echo "</br>$query</br>";
                  }
				  
           
		
             if ($region && $state ) 
                 { 
				
				// echo "66666666";
          //  echo "radius not 0.</br>";
              //   $zipcodes = mysql_fetch_array($zipcodes) ;    
                    $z = new zipcode_class;

//echo"</br>radius:".$radius."</br>";
                    $zips = $z->get_zips_in_range($zipcodes,$radius, _ZIPS_SORT_BY_DISTANCE_ASC, true); 
                if ($zips === false) echo ' Error: '.$z->last_error;
                    else {
                        $zip_list = $zipcodes  ;
                        //echo "the current zipcode: ".$zip_list."</br>";
                       foreach ($zips as $key => $value) 
                       {
                          //echo "Zip code <b>$key</b> is <b>$value</b> miles away from <b>$zipcodes</b>.<br />";    // to print each zipcode and how many maile deffrence with the main zipcode
                          $zip_list .=  "," ; 
                          $zip_list .=  $key ;
                           
                       }
                       $query .= " and zipcode IN($zip_list)" ;
                      // global $zips;
                      //  echo "number: $zip_list";
                       // One thing you may want to do with this is create SQL from it. For example, 
                       // iterate through the array to create SQL that is something like:
                       // WHERE zip_code IN ('93001 93002 93004')
                       // and then use that condition in your query to find all pizza joints or 
                       // whatever you're using it for. Make sense? Hope so.
                       
                     //  echo "<br /><i>get_zips_in_range() executed in <b>".$z->last_time."</b> seconds.</i><br />";
                    }   //  $query .= " and city = \"$city\"" ;    // if it's  = 0 that's mean that he need all the models
                        //  echo "</br>$query</br>";
              } 
			  
        $query .= " order by $order DESC  ";
             
           // $query .= " ORDER BY datetime DESC";
            $query .= " LIMIT $start , $per_page ";
           echo $query;
            $result_ads = mysql_query($query,$connection);
            confirm_query($result_ads);
 
$num_rows = mysql_num_rows($result_ads);
         if ($num_rows == 0)
         {
             die("Tray to use a differant Conditions for the search");
         }
         else  
//echo "$num_rows Rows\n";
            return $result_ads;
}
function get_ads($tablenumb,$per_page,$start,$order,$search_or_no)
{	
		  $tablename = select_table($tablenumb);
		  //echo $tablename;
          if ($search_or_no == 1 )
          { 
              $data = select_ads_search ($cat_type,$per_page,$start,$order,$search_or_no,$disc,$has_pic,$only_titel,$min_price,$max_price,$city,$state,$radius,$zipcodes,
$min_year,$max_year,$model,$min_mileage,$max_mileage,
$apart_type,$n_bed,$max_Foot,$min_Foot,$furn_categ,$doun_categ);     // the user search 
          }  
          else
		$data = select_ads ($tablename,$per_page,$start,$order,$tablenumb);         // the user see all the ads
		return ($data);  
}
function select_cars_models ()           // use the table name and put it in the select statment and then call confirm function
{ global $connection;
            $query = " SELECT * ";
            $query .= " from cars_marke ";
            $query .= " ORDER BY cars_model_name ASC";
            //echo $query;
            $cars_models = mysql_query($query,$connection);
            confirm_query($cars_models); 
            return $cars_models;
}
function select_cars_model_note ($c_model)
{ global $connection;
            $query = " SELECT cars_model_name ";
            $query .= " from cars_marke ";
            $query .= " where cars_marke_code = $c_model  ";
             
            //echo $query;
            $cars_models = mysql_query($query,$connection);
            confirm_query($cars_models); 
            return $cars_models;
}
function select_jobs_types ()           // use the table name and put it in the select statment and then call confirm function
{ global $connection;
            $query = " SELECT * ";
            $query .= " from jobs_type ";
            $query .= " ORDER BY jobs_type, jobs_name ASC";
            //echo $query;
            $jobs_types = mysql_query($query,$connection);
            confirm_query($jobs_types); 
            return $jobs_typs;
}
function select_jobs_types_note ($job_type)		// use the table name and put it in the select statment and then call confirm function
{ global $connection;
            $query = " SELECT jobs_name , jobs_type ";
            $query .= " from jobs_type ";
            $query .= " where jobs_type_code = $job_type  ";
            //echo $query;
            $job_type = mysql_query($query,$connection);
            confirm_query($job_type); 
            return $job_type;
}
function select_furnisher_categ_note ($furnisher_categ)
{ global $connection;
            $query = " SELECT  furnisher_categ_name ";
            $query .= " from furnisher_categ ";
            $query .= " where furnisher_categ_code = $furnisher_categ  ";
            //echo $query;
            $furnisher_type = mysql_query($query,$connection);
            confirm_query($furnisher_type); 
            return $furnisher_type;
}
function select_dounation_categ_note ($dounation_categ)		// use the table name and put it in the select statment and then call confirm function
{ global $connection;
            $query = " SELECT  donation_categ_name ";
            $query .= " from donation_categ ";
            $query .= " where donation_categ_code = $dounation_categ  ";
            //echo $query;
            $dounation_type = mysql_query($query,$connection);
            confirm_query($dounation_type); 
            return $dounation_type;
}
function select_apart_types_note ($apart_types)    // use the table name and put it in the select statment and then call confirm function
{ global $connection;
            $query = " SELECT apart_type_code , apart_type_name ";
            $query .= " from apart_type  ";
            $query .= " where apart_type_code = $apart_types  ";
            //echo $query;
            $apart_types = mysql_query($query,$connection);
            confirm_query($apart_types); 
            return $apart_types;
}
function select_furn_categ ()
{ global $connection;
            $query = " SELECT * ";
            $query .= " from furnisher_categ ";
            $query .= " ORDER BY furnisher_main_categ,furnisher_categ_name ASC";
            //echo $query;
            $select_categs = mysql_query($query,$connection);
            confirm_query($select_categs); 
            return $select_categs;
}
function select_donation_categ ()
{ global $connection;
            $query = " SELECT * ";
            $query .= " from donation_categ ";
            $query .= " ORDER BY donation_main_categ , donation_categ_name  ASC";
            //echo $query;
            $select_categs = mysql_query($query,$connection);
            confirm_query($select_categs); 
            return $select_categs;
}
function select_need_categ ()
{ global $connection;
            $query = " SELECT * ";
            $query .= " from need_categ ";
            $query .= " ORDER BY need_categ_name ASC";
            //echo $query;
            $need_categs = mysql_query($query,$connection);
            confirm_query($need_categs); 
            return $need_categs;
}
function select_housing_types ()
{ global $connection;
            $query = " SELECT * ";
            $query .= " from apart_type ";
            $query .= " ORDER BY apart_type_name ASC";
            //echo $query;
            $housing_types = mysql_query($query,$connection);
            //confirm_query($housing_types); 
            return $housing_types;
}

}
?>
----------------------------------------------------------
third
database_object.php

Code: Select all

<?php
// If it's going to need the database, then it's 
// probably smart to require it before we start.
require_once(LIB_PATH.DS.'database.php');

class DatabaseObject {
 protected static $tablename ="";
protected static $db_fields ;
 public $item_code;
	public $price	;
	public $sqr;
	public $type	;	 
	public $n_bed;
	public $n_bath;
	public $activation;
	public $disc;
	public $note;
	public $datetime;
	public $has_pic	;
	public $state;
	public $address;
	public $city;
	public $zipcode;
	public $fname;
	public $lname;
	public $email;
	public $phone;
	public $hphone;
	public $Country;
	public $year ;
	public $mileage ;
	public function save($tablename) {
	  // A new record won't have an id yet.
	  return isset($this->id) ? $this->update() : $this->create($tablename);
	}
	
	public function create($tablename) {
		global $database;
		// Don't forget your SQL syntax and good habits:
		// - INSERT INTO table (key, key) VALUES ('value', 'value')
		// - single-quotes around all values
		// - escape all values to prevent SQL injection
		$attributes = $this->sanitized_attributes();
	  $sql = "INSERT INTO $tablename (";
		$sql .= join(", ", array_keys($attributes));
	  $sql .= ") VALUES ('";
		$sql .= join("', '", array_values($attributes));
		$sql .= "')";
	  if($database->query($sql)) {
	    $this->id = $database->insert_id();
	    return true;
	  } else {
	    return false;
	  }
	}

	public function update($tablename) {
	  global $database;
		// Don't forget your SQL syntax and good habits:
		// - UPDATE table SET key='value', key='value' WHERE condition
		// - single-quotes around all values
		// - escape all values to prevent SQL injection
		$attributes = $this->sanitized_attributes();
		$attribute_pairs = array();
		foreach($attributes as $key => $value) {
		  $attribute_pairs[] = "{$key}='{$value}'";
		}
		$sql = "UPDATE ".static::$table_name." SET ";
		$sql .= join(", ", $attribute_pairs);
		$sql .= " WHERE id=". $database->escape_value($this->id);
	  $database->query($sql);
	  return ($database->affected_rows() == 1) ? true : false;
	}

	public function delete($tablename) {
		global $database;
		// Don't forget your SQL syntax and good habits:
		// - DELETE FROM table WHERE condition LIMIT 1
		// - escape all values to prevent SQL injection
		// - use LIMIT 1
	  $sql = "DELETE FROM $table_name";
	  $sql .= " WHERE id=". $database->escape_value($this->id);
	  $sql .= " LIMIT 1";
	  $database->query($sql);
	  return ($database->affected_rows() == 1) ? true : false;
	
		// NB: After deleting, the instance of User still 
		// exists, even though the database entry does not.
		// This can be useful, as in:
		//   echo $user->first_name . " was deleted";
		// but, for example, we can't call $user->update() 
		// after calling $user->delete().
	}
	public function try_to_send_notification() {
		$mail = new PHPMailer();

		$mail->IsSMTP();
		$mail->Host     = "smtp.gmail.com";
		$mail->Port     = 25;
		$mail->SMTPAuth = false;
		$mail->Username = "minamarcos@gmail.com";
		$mail->Password = "minos101";

		$mail->FromName = "Photo Gallery";
		$mail->From     = "";
		$mail->AddAddress("minamarcos@gmail.com", "Photo Gallery Admin");
		$mail->Subject  = "New Photo Gallery Comment";
    $datetime = datetime_to_text($this->datetime);
		$mail->Body     =<<<EMAILBODY

A new comment has been received in the Photo Gallery.

  At {$datetime}, {$this->disc} wrote:

{$this->note}

EMAILBODY;

		$result = $mail->Send();
		return $result;
		
}	

	function find_all($find_all) 
	{  global $cat_type ;
	
	     
		return static::find_by_sql("SELECT * FROM ".$cat_type);
  }
  
  public static function find_by_id($id=0) {
    $result_array = static::find_by_sql("SELECT * FROM ".$tablename." WHERE id={$id} LIMIT 1");
		return !empty($result_array) ? array_shift($result_array) : false;
  }
  
  public static function find_by_sql($sql="") {
    global $database;
 //  echo $sql;
    $result_set = $database->query($sql);
    $object_array = array();
    while ($row = $database->fetch_array($result_set)) {
      $object_array[] = static::instantiate($row);
    }
    return $object_array;
  }

	public static function count_all() {
	  global $database;global $cat_type ;
	echo  $sql = "SELECT COUNT(*) FROM ".$cat_type;
    $result_set = $database->query($sql);
	  $row = $database->fetch_array($result_set);
    return array_shift($row);
	}

	private static function instantiate($record) {
		// Could check that $record exists and is an array
		$class_name = get_called_class() ;	
    $object = new $class_name;

		// Simple, long-form approach:
		// $object->id 				= $record['id'];
		// $object->username 	= $record['username'];
		// $object->password 	= $record['password'];
		// $object->first_name = $record['first_name'];
		// $object->last_name 	= $record['last_name'];
		
		// More dynamic, short-form approach:
		foreach($record as $attribute=>$value){
		  if($object->has_attribute($attribute)) {
		    $object->$attribute = $value;
		  }
		}
		return $object;
	}
	
	private function has_attribute($attribute) {
	  // We don't care about the value, we just want to know if the key exists
	  // Will return true or false
	  return array_key_exists($attribute, $this->attributes());
	}

	protected function attributes() { 
		// return an array of attribute names and their values
	  $attributes = array();
	  foreach(static::$db_fields as $field) {
	    if(property_exists($this, $field)) {
	      $attributes[$field] = $this->$field;
	    }
	  }
	  return $attributes;
	}
	
	protected function sanitized_attributes() {
	  global $database;
	  $clean_attributes = array();
	  // sanitize the values before submitting
	  // Note: does not alter the actual value of each attribute
	  foreach($this->attributes() as $key => $value){
	    $clean_attributes[$key] = $database->escape_value($value);
	  }
	  return $clean_attributes;
	}
	

}
?>
Last edited by minos_mks on Wed Jul 24, 2013 9:11 pm, edited 1 time in total.
minos_mks
Forum Commoner
Posts: 69
Joined: Thu Feb 04, 2010 1:58 am

Re: __construct() problem ??

Post by minos_mks »

and it's giving me the error
Warning: Invalid argument supplied for foreach() in C:\wamp\www\1111\includes\database_object.php on line 185

Code: Select all

 foreach(static::$db_fields as $field) {
and my php is 5.3.13
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: __construct() problem ??

Post by social_experiment »

Code: Select all

<?php
 self::$db_fields as $field
?>
to access the property inside the class you should use the self keyword
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: __construct() problem ??

Post by requinix »

...unless you want to access a static property of a child class, then you need static::. Using self:: will only look at the current class regardless of inheritance.

$db_fields is null. You defined it

Code: Select all

protected static $db_fields ;
but you don't give it a value. The foreach is complaining because null isn't an array.

You do try to give it a value

Code: Select all

$db_fields = array('item_code', 'price','fname','lname','email', 'sqr', 'type',
    'phone','hphone','activation', 'disc','note','datetime','has_pic','address',
    'state','city','Country','zipcode','user_code','year','mileage');
but that's only a local variable in the MakeObjectcars method.


Give it a value inside the definition:

Code: Select all

protected static $db_fields = array('item_code', 'price','fname','lname','email', 'sqr', 'type',
    'phone','hphone','activation', 'disc','note','datetime','has_pic','address',
    'state','city','Country','zipcode','user_code','year','mileage');
(and remove that one line from MakeObjectcars)
minos_mks
Forum Commoner
Posts: 69
Joined: Thu Feb 04, 2010 1:58 am

Re: __construct() problem ??

Post by minos_mks »

the problem that $db_fields wouldn't has the same array of value every time. it depends on what kind of ad (Cars, jobs , homes) is previewing or saving in to the DB. that's why i an doing the declaration in the MakeObjectcars function , MakeObjectcars function and MakeObjectcars function in add_ad_result.php file depends on the table name saving the data to or previewing from

and that's why i am using foreach(static::$db_fields as $field) cause i want to get the value not from DatabaseObject .php but from add.php !!!!!!!! :banghead: :banghead:
Last edited by minos_mks on Wed Jul 24, 2013 9:20 pm, edited 1 time in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: __construct() problem ??

Post by requinix »

Then a static variable is not appropriate. Use an instance variable or method.

Actually don't. Your entire approach does not make sense: you're using this DatabaseObject subclass to handle database model work for the ad subclass... which is not a database model. No, it's not a database model because it gets its data from different database tables. It's a business model.

Create different subclass for each table of ad data. These are real database models. They correspond directly to individual database tables and extend from the DatabaseObject class. They use the static $db_fields variable and don't try to change table names.
Then rewrite your existing ad class so that it uses those database models to do its work. It does not interact with the database directly but rather uses whatever database model class is appropriate: the one for ad_d_apart, or ad_d_cars, or whatever.
To do that part it uses an interface. Create an interface which covers all the methods the business model class needs to call on the database model classes. Looks like getters and possibly setters for things like the item_code, price, address information, etc, as well as methods to create and delete ads. Then make sure your database model classes implement it and your business model class only uses those methods available. Since there's additional data specific to each database model you can take the path of using arrays to deal with other non-standard pieces of data not available through the interface.
minos_mks
Forum Commoner
Posts: 69
Joined: Thu Feb 04, 2010 1:58 am

Re: __construct() problem ??

Post by minos_mks »

that it sounds great i will do that , thank you sooooo mush requinix
Post Reply