#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    Sydney, NSW, Australia
    Posts
    40
    Rep Power
    15
    Hi,

    I am writing a template function, and want to replace a string such as

    <*price*>

    With the value that price is a MySQL database. For example, I have a page that is

    page.php?cid=3

    ----

    <?php

    function parse_template($filename){
    # ---------------------------------------------------
    # Loads a basic static template, checks for dynamic content
    # and prints it on the screen.

    global $templatedir;

    chdir($templatedir);
    $fd = fopen ($filename, "r");
    $contents = fread ($fd, filesize ($filename));
    fclose ($fd);

    # Now I have to search for a string (*this*)
    print "$contents";
    }

    I am thinking along the lines of using str_replace and do something like

    str_replace("(*this*)","$this->this","whatever");

    # I can't remember the syntax for the str_replace :-)

    # $this-> will be the results from a mysql query.

    I don't want to have to specify all the things that need to be replaced though, I want to be able to add fields to the database and they are automatically searched for...

    Perhaps a MySQL query on all the fields in the table and then doing a foreach statement?

    Thanks in advance for any help!


    ------------------
    Michael Bray
    http://www.webhostarea.com
  2. #2
  3. No Profile Picture
    Apprentice Deity
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 1999
    Location
    Niagara Falls (On the wrong side of the gorge)
    Posts
    3,237
    Rep Power
    19
    That would be my solution. Assuming you use mysql_fetch_array() you could do something like:

    function parse_template($filename){
    # ---------------------------------------------------
    # Loads a basic static template, checks for dynamic content
    # and prints it on the screen.

    global $templatedir;
    global $data;

    chdir($templatedir);
    $fd = fopen ($filename, "r");
    $contents = fread ($fd, filesize ($filename));
    fclose ($fd);

    while (list($key,$value)=each($data))
    {
    str_replace("(*$key*)",$vlaue,$contents);
    }

    print "$contents";
    }

    $result=mysql_query(....);
    $data=mysql_fetch_array($result);
    parse_template('filename');
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    Sydney, NSW, Australia
    Posts
    40
    Rep Power
    15
    <?php

    function parse_template($filename){
    # ---------------------------------------------------
    # Loads a basic static template, checks for dynamic content
    # and prints it on the screen.

    global $templatedir;
    global $data;

    chdir($templatedir);
    $fd = fopen ($filename, "r");
    $contents = fread ($fd, filesize ($filename));
    fclose ($fd);

    while (list($key,$value)=each($data)){
    str_replace("$key",$value,$contents);
    }
    print "$contents";
    }

    ?>

    #====================================

    <?php

    // Filename: test.php

    # Required Files.
    # ---------------------------------------------------
    require("config.php");
    require("modules/MySQL.php");
    require("modules/SiteHTML.php");

    connect();
    $query = "select * from webhost where host_id = '7'";
    $result = mysql_query($query);
    $data = mysql_fetch_array($result);
    parse_template('test.inc');

    ?>

    #====================================

    test.inc =

    <b>HTML Template lib test</b><p>

    (*companyname*)<p>
    (*5*)

    #====================================

    companyname is a field that should turn up from that query. It doesn't work.

    Any advice? Thanks!

    ------------------
    Michael Bray
    http://www.webhostarea.com

Similar Threads

  1. Dynamic Content - Main Memory Access
    By ccrroossusa in forum ASP Programming
    Replies: 1
    Last Post: November 13th, 2003, 02:04 PM
  2. Dynamic Content - Main Memory Access
    By ccrroossusa in forum Java Help
    Replies: 0
    Last Post: November 13th, 2003, 11:53 AM
  3. could u help me out
    By ANGEL_STEALTH in forum HTML Programming
    Replies: 5
    Last Post: October 1st, 2003, 01:10 AM
  4. Please Look Down Into My Problem
    By ANGEL_STEALTH in forum HTML Programming
    Replies: 0
    Last Post: September 30th, 2003, 06:14 AM
  5. developping dynamic content undr GL6.01
    By cybermarios in forum Web Design Help
    Replies: 4
    Last Post: September 28th, 2003, 03:20 PM

IMN logo majestic logo threadwatch logo seochat tools logo