SitePoint Sponsor

User Tag List

Page 3 of 3 FirstFirst 123
Results 51 to 57 of 57
  1. #51
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    # Submit the userid to the invoice object if it exists in the database then add to it.
    # This is invoice number: 2546;
    $invoice = new Invoice$_GET['userid'], '2546' );

    # Output the result.
    print $invoice->build(); 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  2. #52
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    PHP Code:
    # Submit the userid to the invoice object if it exists in the database then add to it.
    # This is invoice number: 2546;
    $invoice = new Invoice$_GET['userid'], '2546' );

    # Output the result.
    print $invoice->build(); 
    Exactly. Inside that invoice class, there would be code that uses many more specialized objects.

    @OP You have gone from being curious about how we achieve things in OOP to arrogant in just a few posts. We don't code whole applications here. That isn't the purpose of the forum. If a new coder comes here expecting to see an entire application, we ask that they find a book or tutorial and come back if they get stuck. We all volunteer to answer questions here, and most of us do this for a living.

    No it is not expensive to code an OOP app, it is actually cheaper than procedural, because if you have been coding for any decent length of time, much of the code that you would use, has already been written. If not by you then someone else.

    In order to do any of what you describe in a procedural fashion, you would have to write virtually everything from scratch, every time you wanted to do it. It's pointless to take the time to write structures that you have written over and over for multiple projects again, rather than encapsulating them in a class and re-using that code each time.

    OOP seeks to emulate real life. Everything around you is an object or is composed of many objects. Each of those objects have properties (width, height, color) and some have things that they do (methods). An engine for instance, is an object, made up of many smaller specialized objects. All of those objects work together to do what an engine does. That engine is part of a vehicle. Some vehicles have wings, some have wheels, others have pontoons.

    Now apply this to a computer problem. For database access (to get the invoice record) you would use a database abstraction class(s). There are many pre-written classes for that purpose. I have my own that is part of a custom built framework. The project hasn't even started yet, and I already have some of the code.

    Now we want to fill the page with data. We have a controller class which handles the request from the browser with help from a router class that interprets the URL and can then pass on to the controller the proper information about what resources to access. Both classes have also already been written. What code to access, what resources are needed, all wrapped up in a neat URL format. No need to write branching logic to control program flow, it's all there in the URL.

    The proper code is called, data is retrieved and a template page is populated with globally accessible variables that can be placed anywhere in the page. The only code that needs to be written, is the code that specifically asks for a record from the invoices table and then formats that data as needed along with a few specialized controller actions. That is a comparatively small part of the total project. Everything else was already written before the project started.

    Now that is the web, and the only arguably procedural part of that is possibly the template. In desktop OOP programming, interfaces are done with an API, and are not (excluding new systems that use document based GUI description languages) document based as the web is. Interface elements in that case are objects that use an API to construct an interface using the operating system's graphics system. Unless you want something highly customized. Those objects are also already written for you.

    Even a small project can benefit from OOP in fact I have rarely done one that couldn't at least benefit from a pre-written piece of code to avoid repeating myself. My time is valuable and having to write email code that I have written 50 times before would seem pointless. I simply include an email class with a special validation and spam filtering sub class, code a few lines and I'm completely done with the mail portion of a safe secure contact form.

    As far as OOP in PHP goes. PHP only recently, gained a significant amount of OOP features. Therefore, the bulk of tutorials and books describe procedural code. However, a reasonably competent programmer, that has a good grasp of OOP concepts can easily convert a procedural example of an operation, to an OOP based one, where appropriate.

    Frankly this is more explanation than you should ever have to get here.
    Visit my blog
    PHP && Life
    for technology articles and musings.

  3. #53
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nice list... I am aware my code needs organizational improvements like the ones listed, I just don't know how to go about certain things.

    The main thing for me right now is seperating the layers... especially the design layer and the actual PHP code, I find it nearly impossible to not have a big mess of PHP code inside of my HTML. Its something I'd love to read about different strategies of fixing

  4. #54
    SitePoint Zealot
    Join Date
    Jul 2006
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re Roth

    especially the design layer and the actual PHP code, I find it nearly impossible to not have a big mess of PHP code inside of my HTML
    Basically, this is all I have been after. Take a complex lookup I can now do using procedual code, where do you put the html to show returned data on a form in OOp php code? I have played with some OOP code, but do not understand how it all works together.

    Think back when you guys first learned php, you probably got results using an sql statement then used echos to display that in a table. Like:

    Code:
    <?php
    // Connecting, selecting database
    $link = mysql_connect('host', 'rooty', 'sammy123')
    or die('Could not connect: ' . mysql_error());
    echo 'Connected successfully';
    mysql_select_db('whateverdb') or die('Could not select database');
    
    // Performing SQL query
    $t1 = $_POST['t1'];
    
    $query = "SELECT * FROM tblLitDtlAuthor";
    $query = $query . " where Author like '$t1&#37;'";
    
    $result = mysql_query($query) or die('Query failed: ' . mysql_error());
    
    // Printing results in HTML
    if (mysql_num_rows($result) > 0) { 
    // yes 
    // print them one after another 
    echo "<table cellpadding=2 border=1>"; 
    echo "<tr>"; 
    echo "<td>"."id"."</td>"; 
    echo "<td>" ."name"."</td>"; 
    echo "</tr>"; 
    
    // echo "<table>"; 
    while ($row = mysql_fetch_array($result)) {
    //while($row = mysql_fetch_row($result)) { 
    echo "<tr>"; 
    echo "<td>".$row[0]."</td>"; 
    echo "<td>" . $row[1]."</td>"; 
    echo "</tr>"; 
    } 
    echo "</table>"; 
    } 
    else { 
    // no 
    // print status message 
    echo "No rows found!"; 
    } 
    
    
    // Free resultset
    mysql_free_result($result);
    
    // Closing connection
    mysql_close($link);
    ?>
    Ok, I understand how to connect to a database and call up results using OOP, there are examples of this all over. But where and how to display them in a table (or form if editing a record) without mixing in html is where I lack the know-how.

    Now look at this:
    This is just a code fragment where the end of each row has view and edit links. All of this has to be able to go somewhere. Where in OOP do yo display the actual results to the user?

    Code:
    if (empty($row[6])) {
    	echo "<td>&nbsp;</td>";
    	}
    else {
    	echo "<td>$row[6]</td>";
    	}
    if (empty($row[7])) {
    	echo "<td>&nbsp;</td>";
    	}
    else {
    	$tpic = $row[7];
    	echo "<td><a href=\"upload/$tpic\" target=\"_blank\"><img width=\"80\" border=\"0\" src = \"upload/$tpic\">$tpic</a></td>";
    
    	// echo "<td><a href=\"upload/$tpic\" target=\"_blank\">$tpic</a></td>";
    	
    	
    	// echo "<td>$row[7]</td>";
    	}
    // $tmppetid = $row['petid'];
    echo "<td><a href=\"edit2.php?petid=$tmppetid&page=$page&t1=$t1\"> EDIT </a></td>";
    echo "<td><a href=\"view2.php?petid=$tmppetid&page=$page&t1=$t1\"> VIEW </a></td>";
    Meanwhile I am going to look at bamboo invoice, and thanks for the link.
    Last edited by jim9; Feb 11, 2008 at 10:04. Reason: spell

  5. #55
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Who said you cannot return HTML from an object? If the object's purpose is to return HTML in your example building a table then that is perfectly fine.

    One just doesn't print from inside an object.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  6. #56
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Rough needs to be refined
    PHP Code:
    class Html_Table
    {
        protected 
    $input;
        protected 
    $output;

        public function 
    __construct $input )
        {
            
    $this->input $input;
            
    $this->_process($input);
        }

        public function 
    __toString ()
        {
            return 
    $this->output;
        }

        protected function 
    _process $data )
        {
            
    $columns array_keys$data[0] );
            
    $r '<table>%s%s</table>';

            
    $cols '<tr>';
            foreach ( 
    $columns as $col) {
                
    $cols .= '<th>' $col '</th>';
            }
            
    $cols .= '</tr>';

            
    $rows '';
            foreach ( 
    $data as $row ) {
                
    $rows .= '<tr><td>' join$row'</td><td>' ) . '</td></tr>';
            }

            
    $this->output sprintf$r$cols$rows );
        }

    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #57
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jim9 View Post
    Ok, I understand how to connect to a database and call up results using OOP, there are examples of this all over. But where and how to display them in a table (or form if editing a record) without mixing in html is where I lack the know-how..
    Normally for general presentation markup you would simply have a controller method dynamically include your HTML and drop simple values into the markup with PHP. However for tabular data (and for other dynamic markup), I use a class whose sole purpose is to generate a table containing the data from an array, usually derived from a SQL query. I can use it's render method to save the markup string to a variable , which can then be echoed into the rest of the markup.

    As I said, the web is document based, so even the top notch OOP frameworks eventually drop data into mixed PHP/HTML pages. The difference is, that it is just data. Any processing or retrieval has already been done. The most complex construct you would have in your markup is a foreach loop.

    You would never have SQL queries and calls to mysql functions or any other functions in the presentation, mostly echos and a few if's and loops.

    In desktop OOP, through an API, it would all be done programmatically with no markup at all, just objects.
    Visit my blog
    PHP && Life
    for technology articles and musings.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •