php classes?????????

freecrm

New Member
Messages
629
Reaction score
0
Points
0
I am about to embark on a fairly complex modular specification capture system, revolving around a simple parent table (with ID's) and numerous component child-tables, linking back to the main parent table.

The form entry (insert) pages will also have field dependancies. i.e., if one drop-down menu option is selected, a number of fields become available. If a different menu option is selected, a different set of menu options becomes available. (This alone creates problems without AJAX)

I have been advised that the best way to achieve this is using OOL or classes, but after starting to read a little about them, I cannot get my head around how they can be applied to this process.

Is anyone able to explain - in simple english - what a class is and how it might be applied to this process?

Class Confused() {
function PrintConfusion() {
echo "what the hell?";
}
$weird<=Confused();
echo $weird;
}
....arrrghhh!
 

natsuki

New Member
Messages
112
Reaction score
0
Points
0
A class is like an object with properties and functions (called methods). You use classes to group things like

class Rectangle
{
public $length;
public $width;

public function set_size($l, $w)
{
blah
}
public function area()
..blah
}

so you can make Rectangle objects then you have them easily accessible and organized

sorry haven't done programming for a long time ^^;
i dunno how it can be applied though ^^;
 
Last edited:

vol7ron

New Member
Messages
434
Reaction score
0
Points
0
I have been advised that the best way to achieve this is using OOL or classes
The correct term is OOP - object oriented programming.

Is anyone able to explain - in simple english - what a class is and how it might be applied to this process?

Class Confused() {
function PrintConfusion() {
echo "what the hell?";
}
$weird<=Confused();
echo $weird;
}

Your syntax is already wrong - Class Confused(){...} should not have any parentheses, it should be Class Confused {...}.

A class is simply a user-defined data type. Instead of saying a variable is a number or a string, you can define it as something more abstract. It can be a number, a string, or even run functions. You essentially are creating an object with lots of properties.

So if you want to store facts about your users using your webpage, you might want to create a class to store their name, age, etc:

Code:
Class User{
   public $name;
   public $age;
   public $race;
   public $gender;
}

That's the framework, now to create a "User" variable you would say something like:
Code:
$usernumber1 = new User();
$usernumber2 = new User();

And you could set the names that belong to that users doing this:
Code:
$usernumber1->name = "vol7ron";
$usernumber1->gender = "male";
$usernumber1->gender = "25";

$usernumber2->name = "freecrm";
$usernumber2->gender = "male";
$usernumber2->gender = "15";

You see?

This gets more cool when you embed functions into the class:
Code:
Class User{
   public $name;
   public $age;
   public $race;
   public $gender;
   

  public function toOneHundred()
  {
     return (100-$this->age);
  }
}

So using our above example the outputs would be 75 and 85, respectively:
Code:
echo  $usernumber1->toOneHundred();  
echo  $usernumber2->toOneHundred();


Hope this isn't too complicated.
 
Last edited:

tttony

Member
Messages
147
Reaction score
0
Points
16
PHP:
<?php
class myClass
{

	public $public_var;
	private $private_var = "This var can't no be access";
	
	public function myClass($par1, $par2)
	{
		$this->public_var = "Init function myClass(" .$par1. "," .$par2. ")";
	}
	
	public function pFunction1($par1, $par2)
	{
		$this->public_var = "function pFunction1();";
		return ($par1 + $par2);
	}
	public function pFunction2($par1, $par2)
	{
		$this->public_var = "function pFunction2();";
		return ($par1 - $par2);
	}

	function __destruct()
	{
		echo $this->public_var = "__destruct() myClass()";
	}
}

$c = new myClass("parameter1", "parameter2");

echo "public_var: " . $c->public_var . "<br>";  // Init function myClass(parameter1,parameter2)
echo "pFunction1: " . $c->pFunction1(3,5) ."<br>"; // 5 + 3 = 8
echo "public_var: " . $c->public_var . "<br>";  // function pFunction1();
echo "pFunction2: " . $c->pFunction2(18,3) ."<br>"; // 18 - 3 = 15
echo "public_var: " . $c->public_var ."<br>"; // function pFunction2();
//echo "private_var: " . $c->private_var ."<br>"; // Show this error: Fatal error: Cannot access private property myClass::$private_var in
$c = null; // call __destruct()

?>

[Best PHP Class Tutorial on the Net]
 

freecrm

New Member
Messages
629
Reaction score
0
Points
0
tttony - thanks for the input but it explains little!

natsuki - its the explanation of the "blah"'s I need as well!

vol7ron - this is starting to make sense, but I am still failing to see the advantage over functions or includes, other than it creates multiple instances of the same variable.... like usernumber 1,2,3 etc.

Surely, if you want to set out variables and then carry out "methods" on them, you could do this in a function within a loop anyway.

From what I've seen, you still have to define each variable, so how does it save time???.....

***thinks.... why am I having such a problem understanding this?***

OK.. lets try a practical solution.

If I have a master table(spec): say specid(int,10, autoincrement), specificationname(varchar,20), vehicletypeid(int,10)

child table (trailer): trailerid(int,10, autoincrement), specid(int,10), options(varchar,50)

child table (chassis): chassisid(int,10, autoincrement), specid(int,10), options (varchar, 50)

That gives you a basid understanding of the table structure, although this will become more modular and much more complex.

The forms (as discussed above) will have exclusivity - i.e. options will only appear if one option is chosen.

So the idea is that if you select (in this example) in the form, a vehicle type of trailer, a number of other dynamic options appear pertaining to the child trailer table (and subtables).

In this way, the forms will become a list of modules, loading depending on the options that are picked. Rather than creating a form and system for each type of vehicle (that are very different), the one page will act as a template and only load in the modules according to what is chosen.

Do I take it that each module would be a "class"?
 

tttony

Member
Messages
147
Reaction score
0
Points
16
Ok I think I understood...


The table spec has connection with table trailer and table chassis, then when you choose ie:

spec = specA
-- trailer = trailerA
-- chassis = chassisA

spec = specB
-- trailer = trailerB
-- chassis = chassisB

.....

Here I think you have a confussion because ona thing is the form behavior(HTML) and other thing is the PHP behavior

In this example you need three Select Controls:

1. Spec [name=spec]
2. Trailer [name=trailer]
3. Chassis [name=chassis]

When is selected a determinated Spec List then automatically(with AJAX or Javascript) the Trailer List and Chassis List is changed.

Now when you want to save data doesnt matter what you have selected on each Select List because the:

PHP:
 $_POST['spec'] = DATA_VARIABLE_DEPEND_THE SELECT
 $_POST['trailer'] = DATA_VARIABLE_DEPEND_THE SELECT
 $_POST['chassis'] = DATA_VARIABLE_DEPEND_THE SELECT

// SQL STATEMENTS

Only change the data but is always the same parameters

I dont know if you ubnderstand me what I mean... its difficult to explain...
 
Last edited:

vol7ron

New Member
Messages
434
Reaction score
0
Points
0
I don't think you need classes to accomplish this. Classes are more conceptual then necessary for what you're doing, if I understand correctly. You are trying to dynamically select characteristics based on the composition of a vehicle.

If I understand what you're saying, you're wanting to limit what fields are shown in a drop down of a form, based on vehicle type? This is completely different from the purpose of a Class. Classes are used basically as groupings or multiple variable types and functions, very similar to your tables. In fact your tables are a type of class. Think about your queries, they're in the form "schema.tablename.fieldname", which is just like saying schema->tablename->fieldname, or whatever. Though, in your table each field is either a numeric or character variable, you don't have embedded functions.

For what you're doing all you need is a few different SELECT statements to load your form and you should be good to go.
 

freecrm

New Member
Messages
629
Reaction score
0
Points
0
Now that to me makes sense.

Although this may get more complicated the further down this road I go.

I'm not doing this for the freecrm site. its for my work site and we manufacture commercial vehicles.

As you can guess, there are many types of commercial vehicles and the majority of ours are bespoke.

As a result, one table could have literally thousands of columns and the form to create an insert would be massive. Even broken down into pages, this would be unmanageable.

Both of you I think have understood my problem.

vol7ron - I'm not limiting the choices in drop-down menus, I'm limiting whether the drop-down appears at all.

As a test, I had started by creating one drop-down (single choice) with a refresh onchange. This posted value is then stored to session and I can then add an

if($_SESSION['valuefromdropdown'] == "trailer"){show fields relating to trailer choice} elseif($_SESSION['valuefromdropdown'] =="chassis"{show fields relating to chassis} etc. etc.

As I'm storing to session, I can then re-enter that value back into the original field.

Each sub-form element may then have additional sub-sub forms etc. etc.

This is complex because I'm continually refreshing the page to get the posted values in order to show the rest of the elements. (Someone suggested AJAX but I'm no wizz at JS, let alone AJAX.)

The thing that is bugging me as this project progresses is the number of if-else statements and whether there is a more efficient way of achieving the required result.

The end result being - an insert into all parent and child tables, capturing all data in shown fields...

Still no need for classes???
 

vol7ron

New Member
Messages
434
Reaction score
0
Points
0
Whether it's rows in a drop down or many dropdowns themselves, it doesn't matter. Give a better example of your tables and it'd be easier to show how to do this. I can't show you right now, because I'm trying to find something out for work as well, but I can when I get home.
 

natsuki

New Member
Messages
112
Reaction score
0
Points
0
if you have lots of options then you can use switch-case instead of if-else so you don't need to write if ($_SESSION['valuefromdropdown'] == blah) all the time

you use classes to group things, in this case the table is a class but more of a struct since there's no functions and the properties are public

you can create a class for each module or you can go the procedural way but will usually end up having lots of global functions that do different things depending on the parameters, if you have classes instead then you can have the same functions that do diffrent things for different classes (much like a namespace)

if you have massive data then putting them in classes than in variables and arrays would make it easier to organize and manipulate them, you can have a class create a form depending on the option (blah..)

I use classes in C++ for all things having similar characteristics or usage and procedural for all miscellaneous and general-purpose stuffs. (not doing programming lately)
 

vol7ron

New Member
Messages
434
Reaction score
0
Points
0
I use classes in C++ for all things having similar characteristics or usage and procedural for all miscellaneous and general-purpose stuffs. (not doing programming lately)

Yes and classes are understandable in conditions where inheritance and morphism is key, but I don't think it is needed here. Classes are good for object oriented node lists such as the DOM hierarchy, but all he needs is SQL to retrieve the data and relationships and Javascript/PHP/Perl (any script) to build the form.

Classes might actually be a waste of memory here.
 

freecrm

New Member
Messages
629
Reaction score
0
Points
0
Thanks for your opinions guys.

I'll leave this thread open for a bit until I've done some simple exersizes
 

vol7ron

New Member
Messages
434
Reaction score
0
Points
0
again, we've already discussed what a class is, which I think he has a better understanding.

the isssue is what does he want to do? he gave a little explanation, but with a little more example data, we can return a better example of how to accomplish it w/o the user of classes/structs, just tables and functions
 
Top