Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parse error - unexpected T_STRING 1

Status
Not open for further replies.

EvilAsh

Technical User
Oct 22, 2005
56
GB
Please help me!

I have migrated a shopping cart script from my test environment (PHP 4) to a live server (PHP 5).

When I open the site I get the following message:

Code:
Parse error: syntax error, unexpected T_STRING in /home/xxx/public_html/test/shoppingcart.php on line 1

Now I have been through every line of code & checked all the {}, " and ; are correctly in place but the error persists.

Please can anyone help?

The code is below:

Code:
<?php

class cart 

{ //open cart class



  var $username = "xxx"; 
  var $password = "xxx"; 
  var $database = "xxx"; 
  var $hostname = "localhost"; 
  var $inv_table = "inventory"; 
  var $cart_table = "shopping";  


  
  
  
	function cart($cart_id) 
	{ //open cart id
	   $this->dblink = mysql_connect($this->hostname, $this->username, $this->password); 
	   mysql_select_db($this->database, $this->dblink); 
	   $this->cart_id = $cart_id; 
	} //close cart id
	
	
	
	
	
//add & update (via add to basket) functions
	function add_item($product, $category, $price, $quantity, $colour) 
		{ //open cart func
		
		$qty = $this->check_item($product, $colour); 
	
		if($qty == 0) 
		
		{ //open qty 0
		
			$query = "INSERT INTO ".$this->cart_table . " (session, product, category, price, quantity, colour) VALUES ('".$this->cart_id."', '$product', '$category', '$price', '$quantity', '$colour') "; 
			mysql_query($query, $this->dblink);
			//print $query;
			
		} //close qty 0
		
		else 
		
		{ //open qty <1
		
			$quantity += $qty;
					$query = "UPDATE ".$this->cart_table . " SET quantity='$quantity' WHERE session='".$this->cart_id."' AND 
					product='$product'and colour='$colour' "; 
			mysql_query($query, $this->dblink); 
			print $query;

		} //close qty <1
		
		return true; 
	}  //close cart func
	
	
	
	
//view quantitys	
	function check_item($product, $colour) 
	
	{ //open check items func
	
		$query = "SELECT quantity FROM ".$this->cart_table. " WHERE session='".$this->cart_id."' AND product='$product' and colour='$colour'"; 
		$result = mysql_query($query, $this->dblink); 
	
		if(!$result) 
		{ //open result arg
			return 0; 
		} //close result func
	
		$numRows = mysql_num_rows($result); 
	
		if($numRows != 0) 
		{ //open not 0 arg
			$row = mysql_fetch_object($result); 
			return $row->quantity; 
		} //close not 0 arg
	
	  }  //close check items func
	
	
	
	
//delete item function	
	function delete_item($product, $colour) 
	{ //open query
		$query = "DELETE FROM ".$this->cart_table." WHERE session='" . $this->cart_id."' AND product='$product'and colour='$colour'"; 
		mysql_query($query, $this->dblink); 
	 } //close query
	
	
//empty basket
	function clear_cart() 
	{ //open query
		$query = "DELETE FROM ".$this->cart_table." WHERE session='".$this->cart_id."' "; 
		mysql_query($query, $this->dblink); 
		
	 }  //close query
	
	
//update quantity	
	function modify_quantity($product,$quantity,$colour) 
	{ //open query
	
		if($quantity <= 0)  return $this->delete_item($product);
	
		$query = "UPDATE ".$this->cart_table. " SET quantity='$quantity' WHERE session='".$this->cart_id."'AND product='$product'and 
		colour='$colour'"; 
		mysql_query($query, $this->dblink); 
		
		
	
		return true; 
	 }  //close query
	
	
	function get_contents() 
	{//open function
		
		//get stuff in the cart and return in indexed array
		$q1 = "SELECT * FROM ". $this->cart_table. " WHERE session='".$this->cart_id."'";
		$incart_r = mysql_query($q1,$this->dblink) or die(mysql_error());
		
        $numrows=mysql_num_rows($incart_r);
        
		if ($numrows == 0)  
		
		{ $contents==0;} //open & close argument
		
		else
		
		{//open argument
	
		$contents = array();
		
		while($incart = mysql_fetch_array($incart_r))
		
		{ //open query
			//get the item's price first
			$q2 = "SELECT price FROM ". $this->cart_table. " WHERE product='".$incart["product"]."' ";
			$price_row = mysql_query($q2) or die(mysql_error());
			$price = mysql_fetch_row($price_row);
			

			
			//build array of info
			$item = array($incart['product'],$incart['category'],$incart['quantity'],$incart['price'],$incart['colour']);
			array_push($contents,$item);
		
		} //close query
		
		} //close argument
		return $contents;
	
	} //close function
	
	 
	 
	 
	 
	function cart_subtotal() 
	
	{//open function
	   $contents = $this->get_contents();
	   $total = 0;
	   for($i=0; $i < count($contents); $i++)
	   { //open arg
			$total = $total + ($contents[$i][3]* $contents[$i][2]);
	   } //close arg
	   $total = "£".number_format($total, 2, '.', '');
		   return $total;
	   
	}//close function
	
	
	
	
	
	function cart_total() 
	
	{//open function
	   $contents = $this->get_contents();
	   	   if ($contents==0)
	   { //open arg
	   //no items
	   $total = "£0.00";
		   return $total;
	   }//close arg
	   else
	   { //open arg
	   //items
	   $total = 0;
	   
	   for($i=0; $i < count($contents); $i++)
	   {//open calc
			$total = $total + (($contents[$i][3]* $contents[$i][2]));
			
	   }//close calc
	   //postage argument - change $stdpost if postage to be added
	   $stdpost="0.00";
	   $total = $total + $stdpost;
	   $total = "£".number_format($total, 2, '.', '');
		   return $total;
	   }//close arg
	} //close function
	
	
	
	
		function post_total() 
		{//open func
	   $contents = $this->get_contents();
	   $total = 0;
	   for($i=0; $i < count($contents); $i++)
	   {//open
			$total = $total + ($contents[$i][4]* $contents[$i][2]);
	   }//close
	   $total = "£".number_format($total, 2, '.', '');
		   return $total;
	   
	}//close func
	
	
	

	function quant_items() 
	{ //open func
	   $contents = $this->get_contents();
	   $q = 0;
	   for($i=0; $i < count($contents); $i++)
	   {//open
			$q = $q + $contents[$i][2];
	   }//close
	   return $q;
	 } //close func

	 
	 
	 
 		function single_total() 
		{//open func
	   $contents = $this->get_contents();
	   $total = 0;
	   for($i=0; $i < count($contents); $i++)
	   {//open
			$total = $total + ($contents[$i][4]* $contents[$i][2]);
	   }//close
	   $total = "£".number_format($total, 2, '.', '');
		   return $total;
	   
	}//close func

	
	
	
} //close class

?>
 
there will be something wrong in the script that includes/requires this script.
 
Thanks for the reply.

Having stayed up all night, testing the class object and the "parent" file, line by line, I uploaded a simple class file:

Code:
<?php
class SimpleClass
{
  
    test $var = 'a default value';

}
?>

To my suprise, when I called the file direct from the server, the same error message appeared!

Before I speak to the hosts, I would mention that their hosting has PHP 4.4.7. Could anyone advise:
- Does this version need Class objects to be activated (in the .ini)?
- Has it a history of Class issues?

Many thanks.
 
Classes in php4 are not at all implemented the same way as in php 5!

From the documentation:

In PHP 4, only constant initializers for var variables are allowed. To initialize variables with non-constant values, you need an initialization function which is called automatically when an object is being constructed from the class. Such a function is called a constructor.

Code:
<?php
class SimpleClass
{
  
   var test  = 'a default value';

}
?>
 
Thanks elck

I have tried the amended code but with no change to teh outcome :(


To amend what I said earlier, the development environment is PHP 4.1.1 and live is 4.4.7 so should they not be (nearly) the same?
 
neither will work. and you won't be getting the same error in each case.

if you are going for explicit assignment, you should instantiate class properties like this

Code:
class foo{

  //for php5
  public $bar;
  
  //for php4
  var $bar;
}

the keywords here are var, public, private and static. var is used for php4. var is also retained in php5 for backwards compatibility.


 
Fixed it!!!

After much hair pulling etc I discovered that the server was parsing the comments and also (seemingly) the whitespace!

I believe that the text editor I was using was the culprit as I simply opened the file in Dreamweaver, saved it and reuploaded it to the server.

Voila! Works perfectly!

Many thanks!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top