SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    E-business guru Eirik's Avatar
    Join Date
    Nov 2000
    Location
    Oslo, Norway
    Posts
    413
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    A hassle with hard-coded content

    Hi,

    As I finish off my first database-driven site, I would like to, once and for all, understand the concept of hard-coded content.

    Similar to sitepoint.com, I run a web development site at netmaking.com consisting of mainly articles, but also tools, a newsletter and all the other stuff that goes along with it. The article-content I've put into a database, but since there's no common layout in the design of the other pages (some have forms, some have graphics and so on) I've hard-coded the content of these pages (or at least I belive that is the correct term) into the various PHP pages.

    However, what is so frustrating about doing this is that I have to put a quote in front of and at the end of each line. Also, I have to escape all the quotes in the HTML code. This sort of defeats the purpose of seperating the content and the code, so I'm thinking there must be a better way to do this, but how?

    Ideas are greatly appreciated !
    Sincerely,

    Eirik Johansen
    Netmaking AS

  2. #2
    ********* Addict
    Join Date
    Apr 2000
    Location
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Templates are the solution. PHPLib [http://phplib.netuse.de/] is quite widely used, and their templates class is good. Of course, you can also develop your own simple template system, which is not too hard to do.

    I suggest you to store something like "template_set_id" along with the articles and categories, and when you retreive the templates, grab it with a template_set_id which matches the one you took from the articles or categories table.

    Regards,

    Pedro

  3. #3
    Serial Publisher silver trophy aspen's Avatar
    Join Date
    Aug 1999
    Location
    East Lansing, MI USA
    Posts
    12,939
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you tried using PHP's addslashes function?

    Before you enter it into the db do this

    $content = addslashes($content);

    That will escape all the necessary characters for inserting into the db.
    Chris Beasley - I publish content and ecommerce sites.
    Featured Article: Free Comprehensive SEO Guide
    My Guide to Building a Successful Website
    My Blog|My Webmaster Forums

  4. #4
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Today I've got a few questions relating to PHP which I hope someone can help me with.

    1. With a timestamp in a mysql table, is it automatically added or do I have to add it via PHP code? If so, then what is it?

    2. How can I get the date in the format of: 1st of January, 2001 from a timestamp?

    3. With article ratings, should I create a separate record in the database for each record, or should I have one record which is constantly updated?

    4. Even though I already have a CMS, I'm upgrading it at the moment to make it more efficient and I was wondering if I should just let the records for the article have HTML code in them or should I create tags like [b] and [i], [img], etc which are then replaced when the article is requested.

    Thanks for your help,
    Peter

  5. #5
    SitePoint Zealot Overgrow's Avatar
    Join Date
    Jan 2001
    Location
    Your basement
    Posts
    192
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1) I don't use the date/time mySQL field. I find it's easier to use an int(11) field and store it with PHP code. Inside of your insert you do a VALUES('".time()."',

    2) Use the date() command and your 11-character timestamp from #1. The syntax for that format would be (assuming row[timestamp] is your fetched value)

    $dateString = date("jS",$row[timestamp]) . " of " . date("F, Y",$row[timestamp]);

    The first date command does the 1 and st, then it concates it with the " of " and the second date command does the January, 2001 part.

    3) Personally I would only allow logged in members to rate things that way you don't get lots of junk data. Therefore in order to check if someone has rated a certain article, you should store each rating as a separate entry.

    4) I do str_replaces for my own CMS.. I found it was the easiest and fastest way for me.
    Overgrow the Government: The Edge Forums
    presented by Overgrow Marijuana Magazine

    *warning: adult content

  6. #6
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just like to say that I'm sorry for accidentally posting the message in this thread. I didn't realise.

    Opps.

    -Peter

  7. #7
    Serial Publisher silver trophy aspen's Avatar
    Join Date
    Aug 1999
    Location
    East Lansing, MI USA
    Posts
    12,939
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can also use the SQL scalar function CURTIME

    insert into table set blah blah blah, time_stamp = CURTIME(), blah blah

    Popular Scalar Functions:
    • CONVERT(value, datatype)
    • CONCAT(string1, string2)
    • LCASE(string)
    • LENGTH(string)
    • LTRIM(string)
    • LEFT(string, count)
    • RIGHT(string, count)
    • RTRIM(string)
    • UCASE(string)
    • CURDATE()
    • CURTIME()
    • DAYOFMONTH(date)
    • MONTH(date)
    • YEAR(date)
    • MOD(int1, int2)
    Chris Beasley - I publish content and ecommerce sites.
    Featured Article: Free Comprehensive SEO Guide
    My Guide to Building a Successful Website
    My Blog|My Webmaster Forums

  8. #8
    E-business guru Eirik's Avatar
    Join Date
    Nov 2000
    Location
    Oslo, Norway
    Posts
    413
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ...but back to my thread... :)

    Hi again,

    I'm already using templates, so that's not the problem. Also, I don't think the addslashes function would work. Here's an extract of one of my hard-coded pages so you know what I'm talking about.

    PHP Code:
    <?php

    $title 
    "netmaking: Contact us!";
    $keywords "";
    $description "";
    $content =
    "<SPAN class=\"heading\">Contact us !</SPAN>" .
    "<P>" .
    "<SPAN class=\"normaltext\">Have a question? Some critisism? A few kind words? A proposal? What ever it is, use this form to contact us. We will get back to you ASAP. " .
    "<P>";

    include(
    "/usr/www/netmakin/template/templateinclude.inc");

    ?>
    As you see I have to put a quote in front of and at the end of each line, and I also have to escape all quotes within the code for the code to be included in the variable.

    Does this make anyone go "AHA!" ?

    Thanks in advance !
    Sincerely,

    Eirik Johansen
    Netmaking AS

  9. #9
    SitePoint Zealot
    Join Date
    May 2000
    Posts
    150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Eirik

    Sorry, I'm no expert in PHP, but couldn't you place the HTML in non-interpolated strings?

    $content = '<html></html>';

    In perl you could do this:

    $content = <<HTML;

    <html><font face="Arial"></font></html>

    HTML

    not sure about PHP though :-(

    I'd prefer using templates, where the HTML and the PHP code are seperate (which is way better anyway IMHO).

    petesmc:

    1. A timestamp in mysql is automatically updated whenever you update that particular row (INSERT/UPDATE).

    2. See the DATE_FORMAT function for format specifiers. Something like this:

    SELECT DATE_FORMAT(date_added, '%W %D %M %Y') FROM table

    3. Storing it in a seperate "rating" table would be better, esp. if you want to let the user add comments too.

    4. I reckon it would be way better to store it as plain HTML (and thus save processing time). BTW, why use vB tags in the first place? Why not use plain (selected) HTML?

    Regards,
    Arpith

  10. #10
    E-business guru Eirik's Avatar
    Join Date
    Nov 2000
    Location
    Oslo, Norway
    Posts
    413
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Arpith,

    Sorry, I'm no expert in PHP, but couldn't you place the HTML in non-interpolated strings?
    I could, but then I would have to escape all the single quotes instead of the double ones, so the problem would really be the same.

    In perl you could do this:

    $content = <<HTML;
    Yes, I know, and that's exactly the sort of thing I'm looking to do. Sadly, that Perl syntax doesn't work with PHP (just tried it out). But thanks anyway.

    Any other suggestions? Or am I just being lazy here not wanting to escape all the quotes?
    Sincerely,

    Eirik Johansen
    Netmaking AS

  11. #11
    SitePoint Zealot
    Join Date
    May 2000
    Posts
    150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, if you are using this for HTML, you would probably be using more of double quotes rather than single quotes. So, this could help reduce the number of escape sequences needed. Likewise, in SQL queries, I tend to use more of single quotes in the string rather than double quotes.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •