TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-31-2009, 07:35 AM   #1 (permalink)
The Contributor
 
Sirupsen's Avatar
 
Join Date: May 2009
Posts: 53
Thanks: 2
Sirupsen is on a distinguished road
Default HTML Inside the class?

Hello! I have some trouble figuring out if I should make the HTML inside of my class, of if stuff like that is done outside of the class? For some reason the code looks a lot better when you do it inside the classes, but of course they become heavy too. What do you guys do? And what would you recommend me to do? :o Thanks!
Send a message via AIM to Sirupsen Send a message via MSN to Sirupsen Send a message via Yahoo to Sirupsen Send a message via Skype™ to Sirupsen
Sirupsen is offline  
Reply With Quote
Old 05-31-2009, 11:05 AM   #2 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

I recomend only using the class for handling the data. Then you have the HTML seperate(outside of the class), and feed the HTML with the data returned from the class.
__________________
Tanax is offline  
Reply With Quote
Old 05-31-2009, 02:06 PM   #3 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Tanax View Post
I recomend only using the class for handling the data. Then you have the HTML seperate(outside of the class), and feed the HTML with the data returned from the class.
Agreed. I think that's that is confusing about the power of OOP. It can really do almost anything you need it to do so where does one stop? If you think of it from an MVC point of view, you would keep the class for data usage only, while you keep the HTML/design separate.

I hope that makes sense.
allworknoplay is offline  
Reply With Quote
Old 05-31-2009, 02:51 PM   #4 (permalink)
The Contributor
 
Sakakuchi's Avatar
 
Join Date: Feb 2009
Posts: 64
Thanks: 1
Sakakuchi is on a distinguished road
Default

Definetely keep it out. Just had a project some weeks ago, where all of the pagecontent was created in a class. It was such a mess, and it was almost impossible to maintain. It also kills your eyes when you need to find a certain point in that code-mess (No syntax-highlighting).
Sakakuchi is offline  
Reply With Quote
Old 05-31-2009, 04:14 PM   #5 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default

Objects should ONLY handle business logic whilst content should be kept to files designed to handle View logic.
Enfernikus is offline  
Reply With Quote
Old 05-31-2009, 07:52 PM   #6 (permalink)
The Contributor
 
Sirupsen's Avatar
 
Join Date: May 2009
Posts: 53
Thanks: 2
Sirupsen is on a distinguished road
Default

So this looks good then..?

PHP Code:
        <?php
             
include_once("_class/database.php");
             include_once(
"_class/news.php");

            
$DB = new DB;
            
$NEWS = new news;
            
$DB->setup_db('localhost','root','','db');

          if (
$_POST['title'])
            
$NEWS->admin_write($_POST);

            if (
$_GET ['post'] == 1) {
              echo 
'<form method="post" action="index.php">';
              echo 
'<h1>Post something!</h1>';
              echo 
'<input type="text" name="title"><br>';
              echo 
'<input type="text" name="content"></textarea><br>';
              echo 
'<input type="submit" name="submit">';
              echo 
'</form>';
             } else {
                
$sql "SELECT * FROM slimplCMS_posts";
                
                foreach (
$DB->select($sql) AS $row) {
                    echo
                    
"<div id='post_".$row['id']."'>
                    <p>
                        "
.$row['title']."
                    </p><p>
                        "
.$row['bodytext']."
                    </p>
                        "
.$row['created']."
                    </p></div><br>"
;
                }
                echo 
'<a href="?post=1" title="Write an entry!" />Write an entry!</a>';
            }
        
?>
Send a message via AIM to Sirupsen Send a message via MSN to Sirupsen Send a message via Yahoo to Sirupsen Send a message via Skype™ to Sirupsen
Sirupsen is offline  
Reply With Quote
Old 05-31-2009, 08:00 PM   #7 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Well yes.
Ideally I wouldn't use PHP to parse out layout HTML(such as forms and stuff).

It's better to use some sort of template system and use regular HTML(with PHP conditionals).

But that is good. At least you're not using HTML inside your class.
__________________
Tanax is offline  
Reply With Quote
Old 05-31-2009, 08:13 PM   #8 (permalink)
The Contributor
 
Sirupsen's Avatar
 
Join Date: May 2009
Posts: 53
Thanks: 2
Sirupsen is on a distinguished road
Big Grin

Quote:
Originally Posted by Tanax View Post
Well yes.
Ideally I wouldn't use PHP to parse out layout HTML(such as forms and stuff).

It's better to use some sort of template system and use regular HTML(with PHP conditionals).

But that is good. At least you're not using HTML inside your class.
Alright, thanks! Now I know a bit more on how to think OOP, this all made me a bit confused. :)
If anything got anything to add, just shoot! :D
Send a message via AIM to Sirupsen Send a message via MSN to Sirupsen Send a message via Yahoo to Sirupsen Send a message via Skype™ to Sirupsen
Sirupsen is offline  
Reply With Quote
Old 06-01-2009, 12:51 AM   #9 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

You've struck me with inspiration to write another article. I will be writing about basic program design, your issue will certainly fall within the usage of classes and other logic areas.

I've never been able to properly convey good program design and object uses to someone who doesn't already know it, so this may take a few days to work out.
__________________

Village Idiot is offline  
Reply With Quote
Old 06-01-2009, 09:08 AM   #10 (permalink)
The Contributor
 
Sirupsen's Avatar
 
Join Date: May 2009
Posts: 53
Thanks: 2
Sirupsen is on a distinguished road
Default

Can't wait Village Idiot. :D
Send a message via AIM to Sirupsen Send a message via MSN to Sirupsen Send a message via Yahoo to Sirupsen Send a message via Skype™ to Sirupsen
Sirupsen is offline  
Reply With Quote
Old 06-01-2009, 02:13 PM   #11 (permalink)
The Contributor
 
Sakakuchi's Avatar
 
Join Date: Feb 2009
Posts: 64
Thanks: 1
Sakakuchi is on a distinguished road
Default

I personally preffer most of the time something like this(depending wheter there is more html or php):


PHP Code:
<?php
include_once("_class/database.php");
include_once(
"_class/news.php");

$DB = new DB;
$NEWS = new news;
$DB->setup_db('localhost','root','','db');

if (
$_POST['title'])
   
$NEWS->admin_write($_POST);

if (
$_GET ['post'] == 1) { ?>
    <form method="post" action="index.php">
    <h1>Post something!</h1>
    <input type="text" name="title"><br>
    <input type="text" name="content"></textarea><br>
    <input type="submit" name="submit">
</form>
<?php
} else { 
     
$sql "SELECT * FROM slimplCMS_posts";
                
    foreach (
$DB->select($sql) AS $row) {?>
        <div id="post_"<?=$row['id']?>">
        <p><?=$row['title']?></p>
        <p><?=$row['bodytext']?></p>
        <p><?=$row['created']?></p>
        </div><br>";
<?php ?>

<a href="?post=1" title="Write an entry!" />Write an entry!</a>
<?php ?>
Now, you can't see the advantage since the webeditor now has no syntax-highlighting on html, but when opening in eclipse or any similar IDE - it should show you also syntaxhighlighting on the html part. But as I said - I think i'ts not always the best way to do it - it depends on how much html do you have in comparison to the php code you have ;)
Sakakuchi is offline  
Reply With Quote
Old 06-01-2009, 02:15 PM   #12 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Village Idiot View Post
You've struck me with inspiration to write another article. I will be writing about basic program design, your issue will certainly fall within the usage of classes and other logic areas.

I've never been able to properly convey good program design and object uses to someone who doesn't already know it, so this may take a few days to work out.
Please do! I can't wait!
allworknoplay is offline  
Reply With Quote
Old 06-04-2009, 07:18 PM   #13 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

This article is taking me longer than I originally thought it would take. I am finding it stuff very difficult to put into words.
__________________

Village Idiot is offline  
Reply With Quote
Old 06-05-2009, 08:47 PM   #14 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

I'm beginning to think it might be better to film it as a lecture, what I'm writing just doesn't seem to be getting it though. So we might looking at a lot longer than a few days, this would take weeks. I'm wrote out an incomplete powerpoint to go with it and it is 25 slides already. This could be over an hour of me talking. Doing this would be harder, but probably be the best tutorial I've done.
__________________

Village Idiot is offline  
Reply With Quote
Old 06-05-2009, 09:19 PM   #15 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

You know how to create an atmosphere of anticipation !
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 06-05-2009, 09:23 PM   #16 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
You know how to create an atmosphere of anticipation !
Accidentally, yes. I got my head into more than I first realized. I don't even know how I am going to get this stuff filmed. I still hope to get this completed, but don't expect anything soon.
__________________

Village Idiot is offline  
Reply With Quote
Old 06-06-2009, 10:03 AM   #17 (permalink)
The Acquainted
 
Join Date: Oct 2007
Posts: 170
Thanks: 18
maZtah is an unknown quantity at this point
Default

We're all looking forward to your writings, Village Idiot!
maZtah is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
base classes..... allworknoplay Absolute Beginners 16 05-10-2009 08:09 PM
[Tutorial] Basic tutorial about class basics Tanax Absolute Beginners 14 07-24-2008 01:37 PM
PHP5 Classes A to Z Part 1 quantumkangaroo Advanced PHP Programming 11 04-01-2008 04:21 AM
Embeding PHP inside HTML documents Orc General 8 01-18-2008 08:10 PM
Class inside a class(?) Morishani Advanced PHP Programming 12 11-28-2007 10:47 PM


All times are GMT. The time now is 08:45 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design