开发者

How to create html page like a template file

As we all know facebook is a very good php based application. I was wondering how they have created pages for user profile.

As we know the pages look the same for every us开发者_如何学Cer and only the content inside the page will be changing depending on the user.

So how can we create something similar using php. How can we have a html page as a template and call it based on the user login.

I want some ideas to get started as I am building something similar to this.


You really don't need a template system for this. Just pass different data to it.

<?
$user = mysql_query("SELECT * FROM users WHERE id='62' LIMIT 1");
$u = mysql_fetch_array($user);
?>

Welcome, <?=$u['username']?>.<br><br>

Thanks for visiting our site.  How is your mother, <?=$u['mother_name']?>?<br><br>

Username: <input type="text" name="username" value="<?=$u['username']?>" />

Obviously this could use some tweaking, more checks, an actual form tag..etc etc etc, but it should give a good idea of how you can write a dynamic page without needing a template-type tag-replacement system.


Welcome to the world of dynamic web design,

Assuming you don't want to look into a CMS:

Essentially you are going to create a webpage but instead of content, you will have php tags that most likely request and echo database content, the content is usually determined by a get variable (in Facebook's case that variable is the user id).

More specifically you would use a more complex version of the following code at the top of your page:

<?php
 $givenId = $_GET["uid"]; //or whatever your get variable is
if(ctype_digit($givenId)){ //or whatever validation you want to use to prevent sql injection
        $givenId = intval($givenId);
        mysql_connect("localhost", "USERNAME", "PASSWORD");
        mysql_select_db("YOUR_DB");
        $oisthere = mysql_query("SELECT * FROM TABLE WHERE id=$givenId");
        mysql_close();
        if(mysql_num_rows($oisthere)!=0){
            $sqlholder = mysql_fetch_row($oisthere);
        }
        else{
            //return error
        }
    }
?>

Then in your page you could have references to <?php echo($sqlholder[1]); ?>, where the fist column holds their username or status or other important data.

However the fact that you're asking the question tells me that you don't know much about php, mysql, and dynamic webdesign in general. Because of that I'm going to recommend you don't start building your site until you have read up and mastered the concepts. As a starting place I suggest: w3schools.


You need to use some tags, and at output, you should be replace this tags with specific content. A good example is <p>{text}</p> when {text} is a tag.


It's not easy to do from scratch. I'd suggest starting out by looking at some content management systems (CMSes) such as Joomla!, Drupal, PostNuke, etc. If you really want to do a complete custom job, then I'd look into some frameworks that are geared towards templates, such as Smarty, Django, or CodeIgniter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜