Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    922
    Thanks
    76
    Thanked 29 Times in 29 Posts

    Question Where do I process this?

    Hi, I am using a template system where I load different files into one main index.php page.

    I have a 'products.htm' template page where I am going to be displaying information from a database, display images etc.

    I am going to write a script which will retrieve all the files and display them to the user.

    I'm not sure where I need to be processing this. Do I write it inside my index.php page or would I write it in the template page?

    My initial guess is to write it inside the template file so it can process what to display and then display it in the file. I'd rather be able to keep it all separate and just output the data from an included file.

    What's the best way to go about it and where would I place the script?

    Kind regards,

    LC.

  • #2
    Regular Coder Custard7A's Avatar
    Join Date
    Jul 2010
    Location
    Australia
    Posts
    286
    Thanks
    32
    Thanked 33 Times in 33 Posts
    I don't quite understand how your "template system" works. Are you talking about dynamically serving the public pages (E.G: /index.php?page=products ), or just managing and including your sever-side scripts?

  • #3
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    922
    Thanks
    76
    Thanked 29 Times in 29 Posts
    That's right. I have one index.php which includes a back end php script (at the very top) which deals with the templates, loading them in through anchor whose href attribute is just like you have shown above /index.php?page=products. I then include the template system within the body of my index.php page.

    Because this script I am going to write will only relate to that one template page and will not be needed in any of the others, I am not sure where I need to write it.

    Kind regards,

    LC.
    Last edited by LearningCoder; 11-06-2012 at 10:15 AM.

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,311
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    Quote Originally Posted by Custard7A View Post
    I don't quite understand how your "template system" works. Are you talking about dynamically serving the public pages (E.G: /index.php?page=products ), or just managing and including your sever-side scripts?
    Quote Originally Posted by LearningCoder View Post
    That's right.
    Which part? He gave you an OR question there which was a "is it this OR is it this?" type answer expected - you replied with "That's right.". Which part is it thats right?

    As for the original question, I always keep PHP out of template files (ok some of my early 'experiments' have it mixed in but I after I got the hang of it that stopped). My template files only have html in them and tags which I replace and thats it. Everything else I do within the php file.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #5
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    922
    Thanks
    76
    Thanked 29 Times in 29 Posts
    Well I thought my reply after that explained the type of setup I have, or did I miss something?

    Regards,

    LC.

  • #6
    Regular Coder Custard7A's Avatar
    Join Date
    Jul 2010
    Location
    Australia
    Posts
    286
    Thanks
    32
    Thanked 33 Times in 33 Posts
    I believe I understand now. You could do something like this at the top of your primary index page:

    PHP Code:

    if( $GET['page'] == "products") { require "products_script.php"; } 
    Naturally that has to come before the page itself is included for you to be able to use it right. This is pretty much the same as putting the require at the top of products.htm, for most purposes.
    However, you might end up doing this for more pages. You would probably be best to include them in the primary index page, for the sake of accessing them as a group. Might be like this:

    PHP Code:

        
    if( $GET['page'] == "products") { require "products_script.php"; }

    elseif( 
    $GET['page'] == "userform") { require "userform_script.php"; }

    elseif( 
    $GET['page'] == "messages") { require "messages_script.php"; }

     
    // .. A switch statement would be better. 
    It would probably be better for logical semantics as well, if you care about that. Letting the primary index page put together the puzzle, so to speak.

    P.S. You probably wouldn't want to do this if you start including a lot of scripts. I'm looking into PHP autoloading myself as a more efficient method of including resources.

  • Users who have thanked Custard7A for this post:

    LearningCoder (11-06-2012)

  • #7
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    922
    Thanks
    76
    Thanked 29 Times in 29 Posts
    Thank you.

    It does seem logical now to put it in my index.php page as that is the 'base' for all my content. I wasn't sure about accessing the template as the file structure differs from my usual setups, where I put absolutely everything (apart from css,js,images) into the main site directory. Thinking about it I can just use scandir() and dirname()

    Thanks for the help, much appreciated!

    Kind regards,

    LC.

  • #8
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    922
    Thanks
    76
    Thanked 29 Times in 29 Posts
    Just writing my product retrieval script (very basic at the moment, just trying to get functionality between retrieving and displaying).....

    Currently having an issue with saving the results to an array so that I can use it in my main index.php page and subsequently change the order, limits etc.

    I am able to save the results to my array but it is returning duplicate values for every field.

    For example, this is my output:
    Code:
    Array
    (
        [0] => Array
            (
                [0] => 1
                [productID] => 1
                [1] => bench.jpg
                [product_image] => bench.jpg
                [2] => Bench
                [product_name] => Bench
                [3] => Bench hand crafted. Treated and preserved using quality timber treatment. Made from quality pine.
                [product_details] => Bench hand crafted. Treated and preserved using quality timber treatment. Made from quality pine.
                [4] => 149.99
                [product_price] => 149.99
            )
    
        [1] => Array
            (
                [0] => 2
                [productID] => 2
                [1] => table.jpg
                [product_image] => table.jpg
                [2] => Table
                [product_name] => Table
                [3] => This table is ideal for sitting outside and accomodates all people as not only can the height of the seat be adjusted, but also removed completely.
                [product_details] => This table is ideal for sitting outside and accomodates all people as not only can the height of the seat be adjusted, but also removed completely.
                [4] => 249.99
                [product_price] => 249.99
    This is my script:
    PHP Code:
    <?php
    $connection 
    mysql_connect("localhost","root","") or die("Error connecting to server.");

    $database mysql_select_db("gardenable",$connection) or die("Error selecting the database.");

    $query mysql_query("SELECT * FROM products");

    $count 0;
    $product_array = array();

    while (
    $row mysql_fetch_array($query)) {
          
       
    $product_array[] = $row;
       
    $count++;
    }
    echo 
    "<pre>";
    print_r($product_array);
    echo 
    "</pre>";
    ?>
    I also tried imploding the $row variable before saving it into my array, which works and creates just the one array rather than a multi-dim array but it still gives me multiple values. I thought about using the array_flip() then array_unique() but they proved to be the wrong type of functions as they didn't return the desired results.

    Does anyone know how I can get around this? I think it's something probably quite simple but I've tried to do many different things with no results.

    Not sure if I need to create a loop which will delete and index keys of the arrays which are saved in the main first array? I'd really like to know why it is giving me duplicates...

    P.S - The data is just mock data, no real prices or descriptions.

    Appreciate any help

    Kind regards,

    LC.
    Last edited by LearningCoder; 11-06-2012 at 09:19 PM.

  • #9
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    922
    Thanks
    76
    Thanked 29 Times in 29 Posts
    Ok so I played around with my code and managed to get it to save the data into an array.

    Only problem is that it's not displaying my images, just a 'broken img icon' which leads me to think it's something to do with the paths..

    I'll carry on with it, anyway it looks like so:
    PHP Code:
    <?php
    $connection 
    mysql_connect("localhost","root","") or die("Error connecting to server.");

    $database mysql_select_db("gardenable",$connection) or die("Error selecting the database.");

    $query mysql_query("SELECT * FROM products");

    $count 0;
    $product_array = array();

    while (
    $row mysql_fetch_array($query)) {
          
       
    $product_array[$count] = array(
                           
    "productID" => $row['productID'],
                           
    "productImg" => "<img src='{$row['product_image']}' border='0' />",
                           
    "productName" => $row['product_name'],
                           
    "productDetails" => $row['product_details'],
                           
    "productPrice" => "&pound;".$row['product_price']
                        );
       
    $count++;
    }
    echo 
    "<pre>";
    print_r($product_array);
    echo 
    "</pre>";
    ?>
    Edit: Got it working! It was a problem with the file paths. They were not relative to my index.php page, where the script is 'required'.

    Regards,

    LC.
    Last edited by LearningCoder; 11-06-2012 at 11:31 PM.


  •  

    Posting Permissions

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