...

View Full Version : Where do I process this?



LearningCoder
11-06-2012, 05:54 AM
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.

Custard7A
11-06-2012, 10:49 AM
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?

LearningCoder
11-06-2012, 11:12 AM
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.

tangoforce
11-06-2012, 01:54 PM
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?


That's right.

Which part? :D 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? :confused: :cool:

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.

LearningCoder
11-06-2012, 02:33 PM
Well I thought my reply after that explained the type of setup I have, or did I miss something? :p

Regards,

LC.

Custard7A
11-06-2012, 05:44 PM
I believe I understand now. You could do something like this at the top of your primary index page:




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:




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.

LearningCoder
11-06-2012, 06:47 PM
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.

LearningCoder
11-06-2012, 10:14 PM
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:


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
$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.

LearningCoder
11-07-2012, 12:12 AM
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
$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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum