Page 1 of 1

Way to display pages?

Posted: Thu Mar 27, 2008 9:40 pm
by Sequalit
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


need some help with my application, ive gotten to a certain point where I realized my current way of doing things is.... way too time consuming..

I'll show you what I got, please offer any suggestions or changes, or a better system of doing this.

I hate HTML, and use WYSIWYG editors to design the site, and what i need to do is be able to copy the HTML code and paste the relavant php code to display the correct stuff... any help would be appreciated!

Yep very basic stuff on just how to display something simple, like a news page! lol...

Question 1: can you read in an html file into a variable that has %s wherever i would want to insert php code and then ahve all the php code inserted afterwords then run it?

Question 2: is can you "include_once" a php file that displays the main content but store the html code in a variable so it can be displayed with the rest of the code, take a look:

or is this the best way of doing it!

index.php

Code: Select all

 
<?php
    define("ANTIHACKER", null);
    include_once('database.php');//simple, mysql class to run queries and stuff...
    include_once('page.php');
    include_once('class_lib.php');//holds session class does session related stuff
    $mysql = new MySQL();//"localhost", "smilies", "secret1", "cowofdoom");
    $session = new Session($mysql);
    $_SESSION['user'] = '1';
    
    $moduleArray=array(
        'home' => 'news.php',
        'news' => 'news.php',
        'viewtransactions' => 'transactions.php'
        );
    $titleArray=array(
        'index.php' => 'Home',
        'news.php' => 'News',
        'transactions.php' => 'Transactions'
        );
        
    if(!isset($_REQUEST['module_MAIN']) || !isset($moduleArray[$_REQUEST['module_MAIN']])){
        $module = $moduleArray['home'];
        echo "IF";
    }else{
        $module = $moduleArray[$_REQUEST['module_MAIN']];
        echo "ELSE<br>".$_REQUEST['module_MAIN']."<br>";
    }
    $title = $titleArray[$module];
    $module = "Modules/".$module;
    
    $page = new Page($title, date('Y'), "Smilies of Doom!", $mysql);
    $page->addContent($module);
    $page->display();
    $mysql->close();
?>
 
page.php

Code: Select all

 
<?php
    
class Page {    
    private $page;
    private $title;
    private $copyright;
    private $year;
    private $footerAdded;
    private $counter;
    private $main;
    private $mysql;
    
    function Page($title, $year, $copyright, $mysql){
        $this->page = '';
        $this->main = '';
        $this->title = $title;
        $this->year = $year;
        $this->copyright = $copyright;
        $this->footerAdded = false;
        $this->mysql = $mysql;
        $this->addHeader();
    }
    function get(){
        if(!$this->footerAdded)
            $this->addFooter();
        return $this->page;
    }
    function addContent($content){
        //$this->page .= $content;
        $this->main = $content;
        
        include_once $this->main;
        $pageData = new PageData($this->mysql);
        $this->page .= $pageData->get();
    }
    private function addHeader(){
        $this->page = <<<EOT
            <html>
            <head>
            <title> $this->title </title>
            </head>
            <body>  
EOT;
    }
    private function addFooter(){
        $this->page .= <<<EOT
            <div align='center'>&copy; $this->year $this->copyright</div>
            </body>
            </html> 
EOT;
    }   
    function display(){
        if(!$this->footerAdded)
            $this->addFooter();
        echo $this->page;
    }
}
?>
 
news.php -basic example of the format im using currently

Code: Select all

 
<?php 
class PageData{
    private $mysql;
    
    function PageData($mysql){
        $this->mysql = $mysql;
    }
    function get(){
        $data = <<<EOF
<div id="body">
<a href="index.php?module_MAIN=viewtransactions">View Transactions</a><br />
<a href="file:///home/thadeus/">notes</a>
</div>      
EOF;
        return $data;
    }
}
?>
 
 
transactions.php

Code: Select all

 
<?php 
class PageData{
    private $mysql;
    
    function PageData($mysql){
        $this->mysql = $mysql;
    }
    function get(){
        if($_REQUEST['module_MAIN'] == 'viewtransactions'){
            $data = "<div id='body'>" .
                    "<a href='index.php?'>Home</a><br />";
            
            $sql = "".
"SELECT userID, date, transactions.name, amt, transactionTypes.transType, transactionCategories.transCat".
" FROM transactions, transactionTypes, transactionCategories".
" WHERE transactions.typeID = transactionTypes.typeID".
" AND transactions.catID = transactionCategories.catID".
" AND userID = ".$_SESSION['user']."".
" LIMIT 0 , 30";
            $transactions = $this->mysql-queryGetResults($sql);
            $transaction = $transactions->fetch();
            
            do{
                if($transaction['transType'] == 'Income')
                    $type = true;
                else
                    $type = false;
 
yeah this is where it gets really tricky....
transactions.php cont....

Code: Select all

 
                $data .= "".
"<table style='text-align: left; width: 100%;' border='1'".
" cellpadding='2' cellspacing='2'>".
"  <tbody>".
"    <tr>".
"      <td colspan='3' rowspan='1''>Deposits</td>".
"      <td colspan='3' rowspan='1'>Withdrawals</td>".
"    </tr>".
"    <tr>".
"      <td>Date</td>".
"      <td>Name</td>".
"      <td>Amount</td>".
"      <td>Date</td>".
"      <td>Name</td>".
"      <td>Amount</td>".
"    </tr>".
"    <tr>".
"      <td>";if($type){echo $transaction['date'];}."</td>".
"      <td>".if($type){echo $transaction['name'];}."</td>".
"      <td>".if($type){echo $transaction['amt'];}."</td>".
"      <td>".if(!$type){echo $transaction['date'];}."</td>".
"      <td>".if(!$type){echo $transaction['name'];}."</td>".
"      <td>".if(!$type){echo $transaction['amt'];}."</td>".
"    </tr>".
"  </tbody>".
"</table>".
"";
            }while($transaction = $results->fetch());
            $data .= "</div>";
        }
        
        if($_REQUEST['module_MAIN'] == 'edittransactions'){
            $data = <<<EOF
<div id="body">
<a href="index.php?module_MAIN=viewtransactions">View Transactions</a><br />
<a href="file:///home/thadeus/">notes</a>
</div>      
EOF;    
        }
        return $data;
    }
}
?>
 
 
sorry for all the code, i don't expect you to go over all of it, but its there if you want it.
might be easier telling me how you do it instead of trying to 'fix' my system (which would work, just gets time consuming to post html into strings manually)


Thank you for your time!


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: Way to display pages?

Posted: Thu Mar 27, 2008 10:00 pm
by Christopher
You should use a template library. A lot of people around here say good things about TemplateLite.

Re: Way to display pages?

Posted: Fri Mar 28, 2008 5:49 pm
by Sequalit
I tried Smarty, and I just hated it because I ended up having to code things twice which is stupid.

I will try TemplateLite though, thank you for the recommendation.

Re: Way to display pages?

Posted: Fri Mar 28, 2008 6:54 pm
by Christopher
Why did you have to code things twice?

Re: Way to display pages?

Posted: Sat Mar 29, 2008 5:21 pm
by Sequalit
I don't remember, its been a few years since I have done any web development and have forgotten most of it.

I remember you had to code loops in PHP AND in Smarty as well... got kinda annoying.

Re: Way to display pages?

Posted: Thu Apr 03, 2008 9:08 pm
by s.dot
TemplateLite is very similar to smarty. I highly recommend it!
You shouldn't have to code things twice. :)

Re: Way to display pages?

Posted: Fri Apr 04, 2008 3:25 am
by anto91
i would suggest on using the zend framework using the layout component

Re: Way to display pages?

Posted: Fri Apr 04, 2008 8:13 pm
by Sequalit
Zend costs money no?

I ended up using PHP as my template system :) Plus I don't have to rely on third party code ;)

In another thread, located here: http://www.phpfreaks.com/forums/index.p ... 065.0.html

Re: Way to display pages?

Posted: Fri Apr 04, 2008 8:23 pm
by Christopher
The Zend Framework is free (MIT license). If you don't want to use "third party code" then you will end up (like this thread) having to solve every little problem you have.