What architecture should I use for writing my firs

2019-04-16 07:51发布

I just want to build my first dynamic Website. I want to use PHP, MYSQL, AJAX, HTML, CSS

I have some beginner Questions:

  • Should the Header and Navigation Bar excluded in a header.php and print out with echo?

  • Should the design tags in the echo in php (like: <a>1 Test test</a>) or only return the the data

  • Is there a good example for making dynamic websites?

My main problem is that i don't know how to make a clear structure. Where to make the right design (print out in the php ?)

9条回答
Viruses.
2楼-- · 2019-04-16 08:29

Well, to answer all your questions at once. The only technology you need is template.

Template is a typical PHP script, however, consists mostly of pure HTML, with some PHP code only to display dynamically generated data.

Create a main site template contains both header and navigation bar and footer everything else excluding actual page content.

Then create separate pages ("sections" of your site: news.php, links.php, etc.) But make every page of 2 parts: getting data part and displaying data part.
While getting data, not a single character should be printed out.
If some errors occurred, display an error page.
Once you get all your data with no errors - it's time to include a main template.

A typical script may look like

<?
//include our settings, connect to database etc.
include dirname($_SERVER['DOCUMENT_ROOT']).'/cfg/settings.php';
//getting required data
$DATA=dbgetarr("SELECT * FROM links");
// setting title for using in the main template
$pagetitle = "Links to friend sites";
//etc
//set page template filename
$tpl = "links.tpl.php";
//and then finally call a template:
include "main.tpl.php";
?>

where main.tpl.php is your main site template, including common parts, like header, footer, menu etc:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My site. <?=$pagetitle?></title>
</head>
<body>
<div id="page">
<? include $tpl ?>
</div>
</body>
</html>

and links.tpl.php is the actual page template:

<h2><?=$pagetitle?></h2>
<ul>
<? foreach($DATA as $row): ?>
<li><a href="<?=$row['link']?>" target="_blank"><?=$row['name']?></a></li>
<? endforeach ?>
<ul>

Eventually you may come to more complex designs, like with front controller one, but for the first site this one is both easy and powerful.

查看更多
爷、活的狠高调
3楼-- · 2019-04-16 08:32

If it is really your first website, I'd actually recommend using nothing in terms of frameworks. This buys you some time to get comfortable with HTML/CSS, SQL and PHP, without overloading you with higher-level principles such as MVC (model/view/controller) and others. I'm mostly concerned that starting with a framework right away makes the learning curve to steep, and skips over things such as getting comfortable with the programming language you'll be using.

You'll eventually make a mess with way, but this will only make you appreciate the frameworks more; you can then make the transition to using a framework such as CodeIgniter, Symfony or CakePHP (or others, because there's a whole bunch more).

Other frameworks that I really like working with are Play! for Java, and Rails for Ruby. Since you stated you're a beginner, you might consider these as well.

查看更多
趁早两清
4楼-- · 2019-04-16 08:36
  • Yes, I do recommend you include header and navbar scripts from another file.. where you can maintain them independently. I think that is pretty important.
  • I recommend you echo/print html from php, rather than insert php into html (easier when doing things like parsing $_GET/$_POST etc)

I recommend that you create a template, and another script which has functions that print (or echo) the html tags, header, footer, title bar, navbar etc. Just include the script with the functions and all your pages can have the following structure:

<?php
  include 'html_display_functions.php';

  /* put lines here to parse $_GET and $_POST, session_start()/$_SESSION,etc
     if needed */

  print_html_pre_content();
  print '<p>Hello, world! or other content.</p>';
  print_html_post_content();
?>

I have found this to be pretty clean, and it is easy to add functionality when you get to messing around with $_GET, $_POST and $_SESSION, etc.

查看更多
登录 后发表回答