Free Web Hosting Forum
Go Back   Free Web Hosting Forum > Website Building > Web Programming
Reload this Page cleanest way to seperate HTML code in PHP
Reply
 
Thread Tools Display Modes
(#11 (permalink))
Old
Senior Member
weishun is on a distinguished road
 
Posts: 223
Join Date: Feb 2009
Default 03-07-2009, 01:10 PM

Quote:
Originally Posted by Megalith View Post
very well i will paraphrase using the initial example...
You, still, seem to be missing the point; let me try an example.

1) you have 100 pages
2) you want "hello world" and "goodbye world", which we'll call the "html", on each page

Regardless of how you paraphrase it, both your suggestion and the "echo" method require the "hello world" and "goodbye world" to be entered on each page. Neither is a "clean" nor "efficient" method of either "storing" the "chunks" of "html" nor of outputting them.

Quote:
...the echo function is not quick so why use it in this example?
I didn't use it nor was it used in an example. This was how the original poster displayed "html". Your suggestion is quicker but it's, also, irrelevant as it doesn't the answer the question that was asked. You aren't "storing" the "chunks"; you're entering them as required and you're not "outputting" the html; you're including it. Again, neither is an answer to the question and neither is "clean" or "efficient".

I'm sorry if you still don't understand but I don't know how to make it any clearer.

Last edited by weishun; 03-07-2009 at 01:13 PM. Reason: typos
Reply With Quote
Sponsored Links
(#12 (permalink))
Old
Junior Member
Megalith is on a distinguished road
 
Posts: 14
Join Date: Feb 2009
Default 03-10-2009, 11:53 AM

The best solution to many problems is not always the one which looks best. in terms of efficiency however if you are using an include then you are calling on additional modules which is not efficient, it may look neater than an example which has all the code in the same module but it is not more efficient. If i had to output 100 pages each containing hello world, goodbye world and some other text which was not the same on each page then i would consider using mysql for it and put the differing html in a database and call it as needed using say a pageID to reference the text needed, in that case indeed you would need to use an echo call as it's easier (although probably possible without using it).

The original example however is not as complicated and seems to be a function which displays a table every time a function is called. The question on efficiency and cleanliness of code like many programming problems should be solved on an individual basis, there is no efficient solution for every problem.
Reply With Quote
(#13 (permalink))
Old
Senior Member
weishun is on a distinguished road
 
Posts: 223
Join Date: Feb 2009
Default 03-10-2009, 01:16 PM

Most of your post is just a rehash of stuff you've, already, said, still doesn't address the issues of "storing the chunks" or "outputting" and you still seem to be confusing "outputting" with "displaying" as well as ignoring "storing".

I'm curious about this bit, though:
Quote:
i would consider using mysql for it and put the differing html in a database and call it as needed using say a pageID to reference the text needed, in that case indeed you would need to use an echo call as it's easier (although probably possible without using it).
Using a MySQL db occurred to me but I wouldn't unless there were a significant number of "chunks" and, given the problems people seem to be having with their MySQL servers, not on 000webhost, in any case. If I were to do something along this line, on 000webhost, I'd use a flat file.

What I'm interested in is: "call it as needed using say a pageID to reference the text needed, in that case indeed you would need to use an echo call as it's easier". By "pageID", I'm assuming you mean a unique identifier, assigned to each "chunk", in the database. Is that correct?

I believe I know what you mean by "use an echo call" but would appreciate an example to confirm my understanding's correct

"Easier" than what?
Reply With Quote
(#14 (permalink))
Old
Junior Member
Megalith is on a distinguished road
 
Posts: 14
Join Date: Feb 2009
Default 03-13-2009, 02:15 AM

yes indeed i mean a unique identifier, a simple paired list with the html stored as a chunk in the database, i did an adventure game ages ago using this method with each unique ID being used as a page number, the routine would have some options (what do you wanna do?) clicking the link would bring up the relevant page and display it, worked well for that application.

The problem, as i am lead to believe, with the echo function is it is not an efficient piece of code (due to the nature of what it is doing i.e. it parses the code and outputs it as html, in many cases there is no need to parse the code (like in the initial example given) and it can be output as html directly). If writing a clumsy looking script without the echo function being used wherever possible means the code runs faster then in my book that makes it an efficient solution (in terms of performance at least).

When i said easier in relation to the MySQL example, say i have the following snippet (i assume the database is connected and that the query has returned a single line of data into an array called $result, the contents of the array for simplicity would be 'Content' and would be accessed using 'PageID' i.e the page contents are determined from the unique pageID identifier)

PHP Code:
echo $result['Content']; 
To do the same in html is not so easy, i'd say impossible although i'm sure someone can provide a method to do this without calling an echo function.

If instead of 100's of pages there where 20 i would use the html direct within the file and use a session or post variable to determine which snippet to use. So as i said each problem has it's own efficient solution, the initial example given is an overly simplistic one which would be a case of calling the table function and the fixed html being generated for it. I believe in that example the best solution is to not use the echo function but instead to use the HTML directly outside the php compiler as per my earlier post.

swings and roundabouts...
Reply With Quote
(#15 (permalink))
Old
Junior Member
Megalith is on a distinguished road
 
Posts: 14
Join Date: Feb 2009
Default 03-13-2009, 02:31 AM

Another solution that occurs to me given 100's of unique pages would be to name the files in a manner where they can be easily constructed i.e. page1.htm, page2.htm ... page999.htm then you can use something like this (assuming that the relevent page needed is in a variable called $page):-
PHP Code:
header("Location: page" $page ".htm"); 
this would only work however if no other content was on the page already. I believe the header function is faster than the include function?
Reply With Quote
(#16 (permalink))
Old
Senior Member
weishun is on a distinguished road
 
Posts: 223
Join Date: Feb 2009
Default 03-13-2009, 06:09 PM

Thanks for the reply which confirms my understanding of what you, initially, posted.

As I said, I don't think using a db is a good idea, unless there are a huge number of these "chunks". Also, with the problems users are experiencing with their MySQL servers, I don't think it'd be a good idea here, in any case.

I've kinda gotten into this and have an idea I want to try, as soon as I have an hour or so. If it works, I'll post the code as I think it'd be the basis for a pretty good, lightweight, basic CMS; especially, for people on 000wh or others who don't have MySQL.
Reply With Quote
(#17 (permalink))
Old
Senior Member
JohnMark is on a distinguished road
 
Posts: 272
Join Date: Mar 2009
Default 03-20-2009, 04:46 PM

I don't see why anyone would want to store anything but content in Mysql. I just don't see the purpose. Unless you have a seperate template file for every single page/post on your site, this would really be almost useless and defeat the whole purpose of the database.

Now if you wanted something like a rotating ad script, that could be something to store into a database, as it is constantly changing, and each page could access this Mysql data wherever it is called from the page.

Just my thoughts,
Cheers,
Reply With Quote
(#18 (permalink))
Old
Member
johnc is on a distinguished road
 
Posts: 47
Join Date: Jan 2009
Default php require - 03-20-2009, 10:25 PM

I break my code into chunks that make logical sense for my site.

Each code segment is included using a php 'require' statement. The reason for doing this is that even if only a single code segment is not loaded the whole page should be considered invalid.

The first segment is for the DOCTYPE, then meta data.

After that I build my site from other code segments that contain the header and footer information, left and right columns and main content. I think you get the idea.

I don't know if this is technically a template but that is how I think of it. Notice in the following example of my, only slightly edited, home page there is ZERO content. The content is in the code segments.

John

------------------------

<?php require ("php/docType.php"); ?>

<head>

<?php require ("php/metaData.php"); ?>

<title>Key Largo Software</title>

</head>

<body id="home">

<div id="mainContainer">
<div id="header">
<!-- Start Header Content -->

<?php require ("php/header.php"); ?>

<!-- End Header Content -->
</div> <!-- <div id="header"> -->

<div id="content">
<!-- Start Main (Middle) Content -->

<?php require ("content.php"); ?>

<!-- End Main (Middle) Content -->

</div> <!-- <div id="content"> -->

<div id="left">
<!-- Start Left Column (Menu) Content -->

<?php require ("left.php"); ?>

<!-- End Left Column (Menu) Content -->

</div> <!-- <div id="left"> -->

<div id="right">
<!-- Start Right Column (Menu) Content -->

<?php require ("right.php"); ?>

<!-- End Right Column (Menu) Content -->
</div> <!-- <div id="right"> -->

</div><!-- <div id="mainContainer"> -->

<div id="footer">
<!-- Start Footer Content -->

<?php require ("php/footer.php"); ?>

<!-- End Footer Content -->

<!--
<?php require ("php/hitCounterCode.php"); ?>
-->

</div> <!-- <div id="footer"> -->

</body>

</html>
Reply With Quote
(#19 (permalink))
Old
Senior Member
JohnMark is on a distinguished road
 
Posts: 272
Join Date: Mar 2009
Default 03-20-2009, 11:06 PM

No this is cool. If you take a CMS like Wordpress it breaks everything down to 'header', 'footer', 'content' etc. This is usually an ideal way to go

Take care.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.5.2
vBulletin Skin developed by: vBStyles.com