+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: php classes?????????

  1. #1
    freecrm's Avatar
    freecrm is offline x10 Elder freecrm is an unknown quantity at this point
    Join Date
    May 2008
    Location
    UK
    Posts
    629

    php classes?????????

    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!

  2. #2
    natsuki's Avatar
    natsuki is offline x10 Sophmore natsuki is an unknown quantity at this point
    Join Date
    Sep 2008
    Posts
    112

    Re: php classes?????????

    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 by natsuki; 12-16-2008 at 08:43 AM.

  3. #3
    vol7ron's Avatar
    vol7ron is offline x10 Lieutenant vol7ron is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    DC
    Posts
    434

    Re: php classes?????????

    Quote Originally Posted by freecrm View Post
    I have been advised that the best way to achieve this is using OOL or classes
    The correct term is OOP - object oriented programming.

    Quote Originally Posted by freecrm View Post
    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 by vol7ron; 12-16-2008 at 09:23 AM.
    If you find my post useful please add to my reputation by clicking the +Rep button
    You may also use the Donate link to donate credits - this is appreciated too Thanks to those whom have donated so far!


  4. #4
    tttony's Avatar
    tttony is offline x10 Sophmore tttony is an unknown quantity at this point
    Join Date
    Jan 2008
    Location
    ~ 10°10'27.10''' N 67°59'59'.59''' O elev. 457m
    Posts
    147

    Re: php classes?????????

    PHP Code:

    <?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]
    PHP & MySQL Web Developer
    Free Domain co.cc

  5. #5
    freecrm's Avatar
    freecrm is offline x10 Elder freecrm is an unknown quantity at this point
    Join Date
    May 2008
    Location
    UK
    Posts
    629

    Re: php classes?????????

    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"?

  6. #6
    tttony's Avatar
    tttony is offline x10 Sophmore tttony is an unknown quantity at this point
    Join Date
    Jan 2008
    Location
    ~ 10°10'27.10''' N 67°59'59'.59''' O elev. 457m
    Posts
    147

    Re: php classes?????????

    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 Code:
     $_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 by tttony; 12-16-2008 at 12:19 PM. Reason: MORE CODE xD
    PHP & MySQL Web Developer
    Free Domain co.cc

  7. #7
    vol7ron's Avatar
    vol7ron is offline x10 Lieutenant vol7ron is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    DC
    Posts
    434

    Re: php classes?????????

    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.
    If you find my post useful please add to my reputation by clicking the +Rep button
    You may also use the Donate link to donate credits - this is appreciated too Thanks to those whom have donated so far!


  8. #8
    freecrm's Avatar
    freecrm is offline x10 Elder freecrm is an unknown quantity at this point
    Join Date
    May 2008
    Location
    UK
    Posts
    629

    Re: php classes?????????

    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???

  9. #9
    vol7ron's Avatar
    vol7ron is offline x10 Lieutenant vol7ron is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    DC
    Posts
    434

    Re: php classes?????????

    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.
    If you find my post useful please add to my reputation by clicking the +Rep button
    You may also use the Donate link to donate credits - this is appreciated too Thanks to those whom have donated so far!


  10. #10
    natsuki's Avatar
    natsuki is offline x10 Sophmore natsuki is an unknown quantity at this point
    Join Date
    Sep 2008
    Posts
    112

    Re: php classes?????????

    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)

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Ever Been Suspended For Using PHP?
    By dragoneye_xp in forum Off Topic
    Replies: 26
    Last Post: 08-16-2009, 07:17 PM
  2. [PHP] Variables in PHP
    By Bryon in forum Tutorials
    Replies: 15
    Last Post: 01-29-2009, 09:46 AM
  3. currently have an application pending php
    By biomasti in forum Free Hosting
    Replies: 1
    Last Post: 09-03-2008, 01:58 PM
  4. php errors galore
    By DMG Online in forum Scripts & 3rd Party Apps
    Replies: 9
    Last Post: 05-17-2008, 06:23 AM
  5. Pregunta sobre las Configuraciones PHP
    By Trevelin in forum Soporte
    Replies: 11
    Last Post: 04-28-2008, 01:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers