SitePoint Sponsor

User Tag List

Results 1 to 16 of 16

Thread: Template problem

  1. #1
    SitePoint Enthusiast Virakor's Avatar
    Join Date
    Mar 2003
    Location
    Kentucky
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Template problem

    I tried to do template for my website but they won't show up. Here is my template calls:

    PHP Code:
    eval("\$index = \"".gettemplate('main_index')."\";"); 
    And here is my gettemplate function
    PHP Code:
    function gettemplate($templatename) {
    //Get template from database
    $template mysql_query("SELECT template FROM template WHERE title = '".addslashes($templatename)."'") or
            die (
    mysql_error());
    return 
    $template ;

    Is there something wrong? There are no errors it just shows nothing.

    Thanks

  2. #2
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    return str_replace('"''\\"'$template); 
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  3. #3
    SitePoint Enthusiast Virakor's Avatar
    Join Date
    Mar 2003
    Location
    Kentucky
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Even if I change what you told me I'm still not getting anything. It just shows blank and when I look at the source it just gives me the opening and closing tags for the html document. My quess would be that we never really queryed the database since all I have is the query stored in a variable. But still I don't think that is the problem because at least something work so up. Anymore suggestions?

  4. #4
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i'm an idiot for not seeing this!

    you need to fetch the info, [img]images/smilies/tongue.gif[/img].
    PHP Code:
    $template_body mysql_fetch_array($template);
    return 
    str_replace("\\'""'"addslashes($template_body['template'])); 
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  5. #5
    SitePoint Enthusiast Virakor's Avatar
    Join Date
    Mar 2003
    Location
    Kentucky
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmm something is still wrong and I can't see it. I have made the changes above yet still it does not seem to work. Here is what I have:

    My gettemplate function:
    PHP Code:
    /////////////////// Start Gettemplate //////////////////
    function gettemplate($templatename) {
    //Get template from database
    $template mysql_query("SELECT template FROM template WHERE title = '".addslashes($templatename)."'") or
            die (
    mysql_error());
    $template_body mysql_fetch_array($template);
    return 
    str_replace("\\'""'"addslashes($template_body['template']));

    my call for the gettemplate function:
    PHP Code:
    eval("\$header = \"".gettemplate('main_head')."\";"); 

  6. #6
    SitePoint Enthusiast Virakor's Avatar
    Join Date
    Mar 2003
    Location
    Kentucky
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok i just tried using this function:
    PHP Code:
    function gettemplate($templatename,$slashes=1) {
      
    //Get template from database
      
    $template mysql_query("SELECT template FROM template WHERE title = '".addslashes($templatename)."'") or
            die (
    mysql_error());
      return 
    $slashes str_replace("\\'""'"addslashes($template)) : $template;

    and it also didn't work I guess the problem is within where I call it from. I'm going to guess that the call is made to the function ok and then the function excutes ok but instead of returning the function it returns nothing. I don't think it is something wrong with my query because it world return an error if it didn't find what I was looking for right? Could someone please help?

  7. #7
    SitePoint Enthusiast Virakor's Avatar
    Join Date
    Mar 2003
    Location
    Kentucky
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry for the triple post but I'm trying to help people lead to my problem as best as possiable. I just ran this:
    PHP Code:
    <?php

    mysql_connect
    ('mysql.iglou.com''patrickmcdaniel''sticks')
            or die(
    "Could not connect: " mysql_error());
    mysql_select_db('patrickmcdaniel')
            or die(
    "Could not select db: " mysql_error());

    $test mysql_query ("SELECT template FROM template WHERE template_id = 1") or
        die (
    mysql_error());
    $template mysql_fetch_array($test);
    echo 
    $template['template'];
    ?>
    I ran it to insure that infact the templates where there and it ran ok and returned just like I wanted it. So it has to be my function which is currently this:

    PHP Code:
    /////////////////// Start Gettemplate //////////////////
    function gettemplate($templatename) {
    //Get template from database
    $template mysql_query("SELECT template FROM template WHERE title = '".addslashes($templatename)."'" ) or
            die (
    mysql_error());
    $template_body mysql_fetch_array($template);
    return 
    str_replace("\\'""'"addslashes($template_body['template']));

    Could someone please help me to figuare this out it is a real brain twister.

  8. #8
    Non-Member Icheb's Avatar
    Join Date
    Mar 2003
    Location
    Germany
    Posts
    1,474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You do realize that one query per template is resource intensive and pointless?

    Do something like this outside of your gettemplate() calls:

    PHP Code:
    $templatesused="'template1','template2'";
    $templatequeries=db_query("SELECT templatename,template FROM asctemplates WHERE templatename IN ($templatesused)");
    while(
    $templatequery=mysql_fetch_array($templatequeries)) {
       
    $templates["$templatequery[templatename]"]=$templatequery['template'];
    }
    unset(
    $templatequery,$templatesused);
    mysql_free_result($templatequeries); 
    And use this as your gettemplate() function:

    PHP Code:
    function gettemplate($templatename) { 
       global 
    $templates;
      if(!empty(
    $templates["$templatename"])) {
         
    $template=$templates["$templatename"];
      }
      else {
         
    $gettemplates=db_query("SELECT template FROM asctemplates WHERE templatename='$templatename'");
       
    $gettemplate=mysql_fetch_array($gettemplates);
       
    $template=$gettemplate['template'];
      } 
      return 
    addslashes($template);


  9. #9
    SitePoint Enthusiast Virakor's Avatar
    Join Date
    Mar 2003
    Location
    Kentucky
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So when I called the different templates with this what would it look like? And would I place that template cache at the beginning of each page?

  10. #10
    Non-Member Icheb's Avatar
    Join Date
    Mar 2003
    Location
    Germany
    Posts
    1,474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just do something like

    PHP Code:
    $template1=gettemplate('template1'); 
    If you want to eval it you have to do

    PHP Code:
    eval("\$template1=\""gettemplate('template1') ."\";"); 
    The query which selects every template has to be done before the calls to gettemplate, where you put them is at your discretion. Either put it on every page or in a file which you reference from everywhere. You can also put $templatesused in every file and the query in the referenced page, which is actually recommended.

  11. #11
    SitePoint Enthusiast Virakor's Avatar
    Join Date
    Mar 2003
    Location
    Kentucky
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok so I will do something like this:

    PHP Code:
    $templeatsunder="template1, template2"
    include ("global.php"
    And in my global has that has the very frist thing right after I connect to the database? And from you eval() I should be able to leave my template calls alone just add the cache and the template function?

  12. #12
    Non-Member Icheb's Avatar
    Join Date
    Mar 2003
    Location
    Germany
    Posts
    1,474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just try it exactly as I told you and post if you have problems.

  13. #13
    SitePoint Enthusiast Virakor's Avatar
    Join Date
    Mar 2003
    Location
    Kentucky
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok frist problem is that you used db_query which is not a function so hopefully changing it to mysql_query won't hurt anything

  14. #14
    SitePoint Enthusiast Virakor's Avatar
    Join Date
    Mar 2003
    Location
    Kentucky
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok after I corrected the query to match my database it still doesn't work for some reason it doesn't want to show up I will break down the complete files that way everyone knows. And Sorry for the long post

    here is the index.php file that should call the templeates:

    PHP Code:
    <?php
    //set title
    $title "PatrickMcDaniel.net - Made of 1% content, 99% \"Miscellaneous\"";

    $templatesused="'main_head','main_index','news_header','news_main','news_footer','main_footer'";

    //get the global file
    include ("global.php");

    // Get date
    $today date("F j, Y");

    //This will select topics from the topics table listing in descending order by the topic ID(tid)
    $sql "SELECT * FROM $topics_table WHERE forum_id='$newsid' order by tid desc";
    $sql_topic mysql_query($sql) or print mysql_error();

    // Get header template
    eval("\$head = \"".gettemplate('main_head')."\";");
    // Get main index template
    eval("\$index = \"".gettemplate('main_index')."\";");

    //Get news header
    eval("\$newheader = \"".gettemplate('news_header')."\";");
        
    //Creates the while loop
    while($topic mysql_fetch_array($sql_topic))
    {
    $news_sql mysql_query("select * from $posts_table WHERE topic_id='$topic[tid]' order by pid asc");
    $post=mysql_fetch_array($news_sql);


    $date date ("D, j M Y h:i A" ,$post["post_date"]);

    //Get news body
    eval("\$news = \"".gettemplate('news_main')."\";");
    }
    //get news footer
    eval("\$newfooter = \"".gettemplate('news_footer')."\";");

    //Get page footer
    eval("\$footer = \"".gettemplate('main_footer')."\";");
    ?>
    Here is the global file :
    PHP Code:
    <?php
    // Get the config file
    include ("admin/config.php");

    // Get the templates cached
    $templatequeries=mysql_query("SELECT title,template FROM template WHERE title IN ($templatesused)" ) or
            die (
    mysql_error());
    while(
    $templatequery=mysql_fetch_array($templatequeries)) {
       
    $templates["$templatequery[templatename]"]=$templatequery['template'];
    }
    unset(
    $templatequery,$templatesused);
    mysql_free_result($templatequeries); 

    // Get the functions file
    include ("admin/functions.php");

    ?>
    Here is the functions file:
    PHP Code:
    <?php

    /////////////////// Start Gettemplate //////////////////
    function gettemplate($templatename) {
       global 
    $templates;
      if(!empty(
    $templates["$templatename"])) {
         
    $template=$templates["$templatename"];
      }
      else {
         
    $gettemplates=mysql_query("SELECT title FROM template WHERE title='$templatename'" ) or
                 die (
    mysql_error());
       
    $gettemplate=mysql_fetch_array($gettemplates);
       
    $template=$gettemplate['template'];
      }
      return 
    addslashes($template);
    }

    ?>
    And here is the config file:
    PHP Code:
    <?php
    /////////////////////////////////////////
    //                CONFIG FILE                
    /////////////////////////////////////////

    //Database variables
    $server "";        //MySQL Server
    $username "";        //Database Username
    $password "";        //Database Password
    $database "";        //Database Name

    //IBF information
    $topics_table "ibf_topics";    //Name of the topics table
    $posts_table "ibf_posts";        //Name of the posts table
    $newsid "10";                    //News forums ID

    // Connect to the database
    mysql_connect($server$username$password)
            or die(
    "Could not connect: " mysql_error());
    mysql_select_db($database)
            or die(
    "Could not select db: " mysql_error());
    ?>
    And if you want to see what happens go to www.patrickmcdaniel.net/index.php I can not for the life of me figuare out what is wrong it looks like good code and I know the templates are there and I get no errors so I have no clue.

  15. #15
    Non-Member Icheb's Avatar
    Join Date
    Mar 2003
    Location
    Germany
    Posts
    1,474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's because you don't output them. You need another template where you have $head etc. and then you have to echo that template's variable.

    For an example

    eval("\$page=\"". gettemplate('page') ."\";" );
    echo $page;


    And this query makes no sense, I think you know why:

    PHP Code:
    SELECT title FROM template WHERE title='$templatename' 

  16. #16
    SitePoint Enthusiast Virakor's Avatar
    Join Date
    Mar 2003
    Location
    Kentucky
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes I saw it as well but I did change a few more thanks and now atleast I get eval errors instead of nothing. I have advanced!

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
  •