Welcome! Log In Create A New Profile

Advanced

Classes

Posted by TiyaniN 
Announcements Last Post
Announcement myUnisa availability 21 to 24 March 2019 03/25/2019 07:26PM
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Classes
March 27, 2013 01:08PM
Language: PHP
<html> <head> <title> Creating an object </title> </head>   <body> <h2>Creating an object</h2> <?php class Person { var $name;   function set_name($data) { $this->name = $data; }   function get_name() { return $this->name; }   $ralph = new Person; $ralph->set_name("Ralph");   echo "The name of your friend is ", $ralph->get_name(),"."; }   ?> </body> </html>

I'm getting an error when I run this script from the book, can anyone help. Page 250 - 251
avatar Re: Classes
March 27, 2013 01:51PM
Hi, What error code/message are you getting? And on what line do you get the error message?
Just from a quick look,
1) I see there in the "echo " statement there is a comma. Replace your comma's with FULL STOPS (.)
2) When you are creating a new object, there must be two brackets. E.g.
Language: PHP
$ralph = new Person();

Regards, Jared
Re: Classes
March 27, 2013 05:55PM
Your last curly bracket is in the wrong place.
It should be after the last function, before you create the object.
Re: Classes
March 28, 2013 02:13PM
Thank you, I rectified the error.
Re: Classes
April 14, 2013 02:01PM
Hi all,

Is anyone using the __autoload function to include/require classes from seperate page?

I can't get the function to work for me so i'm resulting to normal include statement which is ok for this project since i only
have 1 class page but in futur i will want to use __autoload function if i have more than 1 class pages.

The class Car is on a page called classes.php & being used in OOP_properties.php
Here is my code:
Language: PHP
function __autoload($class_name) { require $class_name . '.php'; }   $ix35 = new Car; $ix35->set_Model("Hyundai"); $ix35->set_Colour("Bronze");
Error code:
Warning: require(Car.php) [function.require]: failed to open stream: No such file or directory in C:\Program Files (x86)\EasyPHP-5.3.3\www\OOP_properties.php on line 33
Fatal error: require() [function.require]: Failed opening required 'Car.php' (include_path='.;C:\php5\pear') in C:\Program Files (x86)\EasyPHP-5.3.3\www\OOP_properties.php on line 33

Why is it trying to open Car.php & not look for the Car class on any .php pages?
avatar Re: Classes
April 19, 2013 09:56AM
Hi, @ 46470107
To answer your first question, NO I'm not using the __autoload function. Shouldn't all our code be on one page?
Re: Classes
April 21, 2013 05:33PM
I am failing to display data on the list boxes, what might be the problem?

Here is my sample code and error messages:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\php4\oop5_select.php on line 109

and the code is:

Language: PHP
public function Fetch($model, $colour){ // line 106 $query=mysql_query("SELECT * FROM ".$set_table_name." ORDER BY ".$order); // line 107 $result = array(); // line 108 while ($record = mysql_fetch_array($query)) { // line 109 $result[] = $record; // line 110 } // line 111 return $result; // line 112 } // line 113
avatar
Mac
Re: Classes
April 22, 2013 08:28AM
Your SQL query is failing for some reason.

Add an error check

Language: PHP
if (!$result) { // add this check. die('Invalid query: ' . mysql_error());
Re: Classes
April 22, 2013 10:27AM
Hi @45352380,

I think you can choose to have your classes on the page it's being used or on a seperate page.

I have my classes on a seperate page classes.php, then on all other pages just have the "include" statement.

But the book have an function __autoload as i show above, that in the book it says it will look for all classes in any .php pages and auto include them.
This function is not working for me...

if you have some spare time try it out, I don't have the book with me but it was in chap 7 or 8...
Re: Classes
April 24, 2013 08:51PM
Hi TiyaniN

it seems that your $order and $set_table_name variables are not defined
Re: Classes
May 05, 2013 10:41PM
Hi Mac

Im having the same problems as TiyaniN, where the list box does not populate the data.
Where do I insert the error check?

Thanks
avatar
Mac
Re: Classes
May 06, 2013 08:03AM
After the sql query

Language: PHP
$query=...... $result=mysql_num_rows($query); if (!$result) { die('Invalid query: ' . mysql_error()); }
Re: Classes
May 06, 2013 09:12PM
Hi Mac

When i run the error check this is the error msg I get:

Invalid query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY' at line 1


Language: PHP
$query=mysql_query("SELECT * FROM ".$set_table_name." ORDER BY ".$order);

Please help.

Thanks
avatar
Mac
Re: Classes
May 07, 2013 08:17AM
As it says, you have an error in that query near ORDER BY.... and it is there about (it is never precise, but it points to the end of the line

I am not going to say where - see if you can find it. Make sure you have everything required in terms of quotes, full-tops etc.
Re: Classes
May 07, 2013 08:46AM
Hi Candice,

Also check this line of code jus above the code you posted:
Language: PHP
public function Fetch(……, ……){


Tip make 100% sure about parameters...
Sorry, only registered users may post in this forum.

Click here to login