 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
05-31-2009, 07:35 AM
|
#1 (permalink)
|
|
The Contributor
Join Date: May 2009
Posts: 53
Thanks: 2
|
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!
|
|
|
05-31-2009, 11:05 AM
|
#2 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
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.
__________________
|
|
|
|
05-31-2009, 02:06 PM
|
#3 (permalink)
|
|
The Gregarious
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
|
Quote:
Originally Posted by Tanax
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.
|
|
|
|
05-31-2009, 02:51 PM
|
#4 (permalink)
|
|
The Contributor
Join Date: Feb 2009
Posts: 64
Thanks: 1
|
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).
|
|
|
|
05-31-2009, 04:14 PM
|
#5 (permalink)
|
|
The Addict
Join Date: Jun 2008
Posts: 335
Thanks: 2
|
Objects should ONLY handle business logic whilst content should be kept to files designed to handle View logic.
|
|
|
|
05-31-2009, 07:52 PM
|
#6 (permalink)
|
|
The Contributor
Join Date: May 2009
Posts: 53
Thanks: 2
|
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>'; } ?>
|
|
|
05-31-2009, 08:00 PM
|
#7 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
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.
__________________
|
|
|
|
05-31-2009, 08:13 PM
|
#8 (permalink)
|
|
The Contributor
Join Date: May 2009
Posts: 53
Thanks: 2
|
Quote:
Originally Posted by Tanax
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
|
|
|
06-01-2009, 12:51 AM
|
#9 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
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.
|
|
|
|
06-01-2009, 09:08 AM
|
#10 (permalink)
|
|
The Contributor
Join Date: May 2009
Posts: 53
Thanks: 2
|
Can't wait Village Idiot. :D
|
|
|
06-01-2009, 02:13 PM
|
#11 (permalink)
|
|
The Contributor
Join Date: Feb 2009
Posts: 64
Thanks: 1
|
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 ;)
|
|
|
|
06-01-2009, 02:15 PM
|
#12 (permalink)
|
|
The Gregarious
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
|
Quote:
Originally Posted by Village Idiot
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!
|
|
|
|
06-04-2009, 07:18 PM
|
#13 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
This article is taking me longer than I originally thought it would take. I am finding it stuff very difficult to put into words.
|
|
|
|
06-05-2009, 08:47 PM
|
#14 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
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.
|
|
|
|
06-05-2009, 09:19 PM
|
#15 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
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.
|
|
|
06-05-2009, 09:23 PM
|
#16 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
Quote:
Originally Posted by Wildhoney
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.
|
|
|
|
06-06-2009, 10:03 AM
|
#17 (permalink)
|
|
The Acquainted
Join Date: Oct 2007
Posts: 170
Thanks: 18
|
We're all looking forward to your writings, Village Idiot!
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|