<?php include("a/head.inc");
$db = mysql_connect("localhost", "user", "password");
mysql_select_db("mydb",$db);
$result = mysql_query("SELECT `pageid`,`title`,`description`,`keywords` FROM `page` WHERE 1 AND `pageid` LIKE 'index.php'",$db);
while ($myrow = mysql_fetch_row($result)) { printf("%s%s%s", $myrow[1], $myrow[2], $myrow[3]);}
include("a/top.inc");
$result = mysql_query("SELECT `pageid`,`d1`,`d2`,`d3` FROM `page` WHERE 1 AND `pageid` LIKE 'index.php'",$db);
while ($myrow = mysql_fetch_row($result)) { printf("<div id=\"L\">%s</div><div id=\"C\">%s</div><div id=\"R\">%s</div>", $myrow[1], $myrow[2], $myrow[3]);}
include("a/bottom.inc");
?>
Basically I want to be able to edit the site anywhere, anytime sorta thing, and using a basic template for the edited information to fit within.
As you can see, I'm using the db to call the title and meta tags for a particular page, and then second time round, call the various divs that are used on the site.
The thing is, within each div, I "hope to" use "includes" of information. i.e. one page may contain an include for main navigation, and within the same div, more options are called on to the page....
How exactly would I do this? I tried <?php include("wanttoincludethis.inc"); ?> in one of the fields of the db but this does not work.
Anyone in here have the magical solution? :)
I'm not too knowledgable in PHP (is that obvious)....would love suggestions here. I think I sorta understand why the php include doesnt work when called from the database......though not sure of the ......
Richard
Essentially, apart from including "includes" there will also be pages using the above template where more db results are produced (from a different table)
so any coding tips / magical answers are twice as much appreciated :)
1. have file names in your DB and then include those files
2. have include statements in, say, memo fields in your DB and you want them to actually get interpreted as php, rather than just printing out.
If #1, then just get the var and include it.
If #2, you will need to use eval so that the text gets evaluated as PHP.
[php.net...]
Does this help?
Tom
1. run the site 100% for php and css.
2. the template of the site is called using the above code (i.e. mandatory html, css style sheet, opening tags, title etc)
3. this bit >>>> printf("<div id=\"L\">%s</div><div id=\"C\">%s</div><div id=\"R\">%s</div>", $myrow[1], $myrow[2], $myrow[3]);} is intended to call 3 divs which have their own style.
I want to "include" navigation structures.....but the amount of info to be included will be bespoke to each page...i.e. I want to just "include" these repetitive navigation bars for each section
So essentially..yes to "text gets evaluated as PHP"......I'm reading that thing now thought it looks lengthy and probably past my comprehension at the mo....
As also mentioned....in the <div id="c"> section......some pages may include further database results bespoke to that page...i.e. a links directory.
So basically I want the left and right divs to contain includes of navigation and repetitive info....and call the main content from another db.
Phew! I'm just trying to do it in the most efficient (and correct way) possible..so suggestions welcome
and in your php page:
<?php
include ("nav1.inc");
?>
So you have separate, fixed .inc files for all included items, and call variable stuff from the db.
I read your post about 4 times and am still not sure what exactly the dilemma is.
What I understand is, depending on the page, you want to include varying amounts of information which you would like to pull from a DB.
I would start with
require_once $DOCUMENT_ROOT . "/a/head.inc";
and for the footer
require_once $DOCUMENT_ROOT . "/a/bottom.inc";
for the body stuff, I would include a file that did a select based on what page it was included on so it selected the info for that specific page (I have code for this at work which I can post in the morning). I have no idea if this comes even close to answering your question.
>><?php include("wanttoincludethis.inc"); ?>
this part confused me. If you were selecting the file name to include on a specific page. I don't think this works. I would store the inc file's name in the db and then select it and echo it as such,
$pagequery = "select incfile from table where pageid='presentpage' and field='content'";
$pq = mysql_query($pagequery);
$row = mysql_fetch_array($pq);
$incfile = $row["incfile"];
require_once $DOCUMENT_ROOT . "$incfile";
in the included file I would just code the div's etc straight up not include all that in the DB. But I may not be fully understanding what is going on. Have a file called 'index.inc' and 'content1.inc' and then just call the appropriate file for the appropriate page.
The reason the explanation may not be clear is because Im not all that clear about PHP, so I can't "talk fluently" about it ;)
I have sorted the prob anyway....its just my lack of understanding in PHP (or the flexibility of changing the code).
The ideal situation from the code made is that each new page made would call a bespoke set amount of includes for navigation as well as database produced content.
I'm just going to re-arrange the layout of the output, because all divs are absolutely positioned. The idea is that each page/folder will have its own properties in regards to what is included, and any unique content to that page can simply be typed in.
I'm sure one day I'll be able to clearly reference a PHP prob for you all to help me out with ;) ... but for now it seems the prob is over
Ask yourself this for each page element: do I really want this in the database?
For example, you can edit a file anytime anywhere just as easily as a DB using a control panel (read it into a text area on a form, rename the old file so you can have a "restore" function, write the new file). It should be transparent to the user of the control panel how the data is accessed, stored and so forth. This is the fundamental idea of data abstraction. Ideally, a user should *never* need to know about how the data is stored. The Structure and Interpretation of Computer Programs by Abelson and Sussman is the greatest for thinking through some of that stuff.
Anyway, I digress.... Perhaps the best way to proceed would be to tell us
1. What you want the final result to look like (i.e. post the HTML code you want to actually get generated)
2. Tell us the source of each code element (i.e. DB column name or filename or whatever)
I think that might get things working more smoothly.
1. Templates. This is good for overall layout and so forth. The big stuff. There is little PHP in a template except for variable substitutions (that is all the work of figuring out *what* will get laid out is done, the template just echoes strings).
2. Static pages that get included. Generally the HTML in these files is limited to minimal text formatting. If it's more complex than that, I use...
3. Function calls. You can go as far as you want, but here's a simple example. Instead of
blah blah <a mailto="some-email-address"> link text</a>
You might have
blah blah <?php echo make_email("some-email-address", "link text"); ?> blah
Why would you ever do this? Let's say that you decide to protect the email addresses on your site by, for example, encoding the @ sign or using javascript to output the email so that people who post messages on your site don't get scraped and spammed. Now you change the internals of the function and it cascades through the whole site. Similarly, you can have functions like "make_nav_links($page_id)" that will query the database and return links based on which page the user is visiting.
Yes, this slows things down marginally (benchmark it and you'll see that it still goes pretty fast unless the libraries get huge), but it helps you keep track of what you're doing.
So to go back to your original code, I suggest - and I suspect that someone will say "That's the stupidest thing I've ever seen" so be warned - the following.
<?php include("a/head.inc");
$db = mysql_connect("localhost", "user", "password");
mysql_select_db("mydb",$db);
You should have a top include file that gets included before any HTML output and that does a lot of processing beforehand. This becomes important if you want to add header info, but for now I think it's enough to know that the more you do before you output so much as a blank line, the better. In this top file, you should invoke you DB, not in your template file. If you wish to add a layer of database abstraction so you can change databases, this should be a function call like
select_db($db_name, $db_type, $etc, $etc)
$result = mysql_query("SELECT `pageid`,`title`,`description`,`keywords` FROM `page` WHERE 1 AND `pageid` LIKE 'index.php'",$db);
while ($myrow = mysql_fetch_row($result)) { printf("%s%s%s", $myrow[1], $myrow[2], $myrow[3]);}
include("a/top.inc");
Again, I like the processing to be done before I get to the template, so it would look more like:
thispage.php
$col_titles = get_col_titles($page_id);
template.php
show_titles($col_titles)
include("a/top.inc")
Also, I tend to use a lot of constants that are declared in my "top.php"
So I would have
include(SERVER_ROOT . FILE_TOP);
or something like that. It means you can change servers/directory structure and so on by just changing the constants file. Especially easy if you are synching a local development version with a live version. You just have an
include("local/development.php");
statement. Since constants can't be redeclared you can override your live settings and then change nothing and have it run live - as long as it doesn't find the local settings file, it will use the default settings.
I hope that makes some sense, even if it doesn't answer your question!
Tom
I'm partly experimenting and partly implementing a back end where some other people (not so technical minded as people around here) will be typing content. They just need to type in the content.....and the template (and PHP) puts it all in the right place. Essentially this is what I'm doing, and making sure that those non-technical people don't see anything technical :)
I'll check out your post later and try to form a reply.......but I think your intuition is right and you are giving me a better and more understandable use of PHP (I'm new to it).......cheers ;)
making sure that those non-technical people don't see anything technical
This is the essence of abstraction, both functional and data abstraction. How you implement it is up to you, and the simplest implementation is probably best. The key thing is that there is an abstraction layer so that you can have the interface remain the same (user always sees the same thing and has no clue how it's done), while the backend changes alogorithms, coding language, platforms, whatever.
I can't recommend enough getting a copy of Abelson and Sussman that I mentioned earlier and working through the problems. This is such a fundamental book that there are forums virutally devoted to discussing it. It has nothing to do with PHP (their examples are all in Scheme), but nothing has helped me so much with PHP.
Check out
[www-mitpress.mit.edu...]
I just found out that they now have the *entire* book online. You can see the table of contents at
[mitpress.mit.edu...]
Tom