Forum Moderators: coopster & phranque

Message Too Old, No Replies

Calling "includes" from mySQL

to sit nicely into a div

         

brotherhood of LAN

1:11 am on Jul 2, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hello folks, here's the prob (for me anyway!).....

<?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

brotherhood of LAN

1:27 am on Jul 2, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I suppose one way of getting round this is using multiple connections perhaps....not sure about them either actually.

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 :)

ergophobe

2:09 am on Jul 2, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Okay, maybe I need to reread your post again, but I'm not certain whether you want to

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

brotherhood of LAN

2:21 am on Jul 2, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hey ergo...2 was closer to the mark i guess....here's some background to what I want done...illustrated by the above code.

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

SmallTime

3:23 am on Jul 2, 2002 (gmt 0)

10+ Year Member



I am probably more in the dark on php than you, have been getting started with it on a test server. Here is what I would try, if I understand what you want to do:
In your "nav.inc" file, in same directory, consists of -
<?php
$nav1 = "stuff you want";
echo $nav1;
?>

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.

jatar_k

3:53 am on Jul 2, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



BOL,

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.

brotherhood of LAN

4:05 am on Jul 2, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Well thanks for the help so far, hats off to the both of you.

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

jatar_k

4:12 am on Jul 2, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



glad you sorted it BOL,

I'll try to be more help next time.;)

ergophobe

6:24 pm on Jul 2, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



BOL,

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.

ergophobe

6:55 pm on Jul 2, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



One other rather OT thing. I don't think this comment remotely addresses your question, but it may help in the long run, especially if people follow up on it with criticism. Though I'm sure "efficiency freaks" will quibble with doing this with an interpreted language, I generate HTML in three ways

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

brotherhood of LAN

9:18 am on Jul 3, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks for following up ergo. I hear what you are saying, and appreciate your concern with not needing stuff in a db.

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 ;)

ergophobe

5:59 pm on Jul 3, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




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