Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 08-20-2007, 04:50 AM php & .tpl page
Novice Talker

Posts: 9
Trades: 0
I have a index.php page with all the php etc in top level then I have a template folder with 2 files called master.tpl & index.tpl
I also have an include folder which has a file called "rand_prods.php"
this file has my code for the random products as below:
Code:
    <div id="rightcolumn">
  <?php foreach ($products as $item): ?>
   <div class="rightbox_wrapper">
    <div class="rightbox">   
     <a href="<?=$item['url']; ?>">
     <img border="0" src="<?=$item['image_url']; ?>" width="100" title="<?=$item['name']; ?>" class="product_image" alt="" /></a>
     <div class="product_wrapper">
      <a href="<?=$item['url']; ?>"><h4><?=$item['name']; ?></h4></a>
      <p><?=$item['description']; ?>... <a href="ajmp.php?id=<?=$item['id']; ?>">On sale for <?=$item['price']; ?> Buy Now!</a></p>
     </div>
    </div>
   </div>
   <?php endforeach; ?>   
   <hr />
  </div>
Now if I link that include file from the index.tpl page it works i.e.
<? include ("includes/rand_prods.php"); ?>
but I want to instead link it from the master.tpl page but when I do that I get this error:
Quote:
Warning: Invalid argument supplied for foreach() in c:\wamp\www\mywebsite\includes\rand_prods.php on line 2
even if I just place this code directly in my master.tpl page without using an include file then it still has this error...
the strange thing is that there is a similar file linked from the master.tpl page that works so why doesnt the "rand_prods.php" page work?
__________________
New site to
Please login or register to view this content. Registration is FREE
- please help kick start my new website -
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
sitesbuysell is offline
Reply With Quote
View Public Profile Visit sitesbuysell's homepage!
 
 
Register now for full access!
Old 08-20-2007, 05:06 AM Re: php & .tpl page
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
My first guess is that $products don't exists or exists but isn't an array in this page, so it cannot be iterated by foreach.
try to add :
PHP Code:
<?php
echo"<pre>";
var_dump($products);
echo
"</pre>";
die();
?>
before your foreach. It will dump the content and type of $products, if it exists.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 08-20-2007, 06:23 AM Re: php & .tpl page
Novice Talker

Posts: 9
Trades: 0
I did that and now it just shows the word "NULL"
__________________
New site to
Please login or register to view this content. Registration is FREE
- please help kick start my new website -
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
sitesbuysell is offline
Reply With Quote
View Public Profile Visit sitesbuysell's homepage!
 
Old 08-20-2007, 06:36 AM Re: php & .tpl page
Novice Talker

Posts: 9
Trades: 0
I also wrote about this issue here so if you want to see more code then it is there and save posting it again...
__________________
New site to
Please login or register to view this content. Registration is FREE
- please help kick start my new website -
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
sitesbuysell is offline
Reply With Quote
View Public Profile Visit sitesbuysell's homepage!
 
Old 08-20-2007, 07:28 AM Re: php & .tpl page
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
I have no account on dp, and don't want to make another one.
The word NULL means that you have the variable declared, but it's value is bull, not an array, so the foreach cannot iterate through it.

So, for finding where your problem is, go step by step.
First, try to dump $products after the db retrieval:
Code:
$query = "SELECT image_url, description FROM `products` ORDER BY rand() LIMIT 4;";
database_inquiry($query, $rows);
$products = $rows;
Then, as you re-populate it later, try after the update:
PHP Code:
$i=0;
foreach (
$products as $item)
   {
   
$item['price'] = '$'.number_format($item['price'],2,'.',',');
   
$products[$i] = $item;
   
$i++;
   } 
I don't see any flagrant errors in your script. Just as a remark, you blindly set $i as the array key. Why not use the extended syntax of foreach, which allow you the get both index and value ?
PHP Code:
foreach ($products as $key=>$item){
   
$item['price'] = '$'.number_format($item['price'],2,'.',',');
   
$products[$key] = $item;
   } 
But your best way to find out what's happen is to deplace the var_dump() block you put after my first post, and see where the content of $products disapears, or if it ever get filled from the db. Maybe database_inquiry() isn't working in that page...
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 08-20-2007, 07:41 AM Re: php & .tpl page
Novice Talker

Posts: 9
Trades: 0
ok thanks for this, i will have my dinner now and take a look at all this soon....
__________________
New site to
Please login or register to view this content. Registration is FREE
- please help kick start my new website -
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
sitesbuysell is offline
Reply With Quote
View Public Profile Visit sitesbuysell's homepage!
 
Old 08-20-2007, 09:15 AM Re: php & .tpl page
Novice Talker

Posts: 9
Trades: 0
I was a little confused on where & how to run these code samples you showed....

what I did do though which might help to track the problems is I cut the code from the index.php page and placed it in the file "rand_prods.php" above the other code, then I placedyour debug script below that code I cut & pasted and this is the results:

Code:
array(4) {  [0]=>  array(2) {    ["image_url"]=>    string(59) "/images/items/large/l32673.jpg"    ["description"]=>    string(200) "Area Rugs Bring Color, Style and Comfort to Your Home Decor From our popular Kharma Collection, these quality floor coverings offer traditional designs in a sophisticated palette of colors to compleme"  }  [1]=>  array(2) {    ["image_url"]=>    string(59) "/images/items/large/l35339.jpg"    ["description"]=>    string(198) "(description of product here...)"  }  [2]=>  array(2) {    ["image_url"]=>    string(59) "/images/items/large/l35632.jpg"    ["description"]=>    string(198) "(description of product here...)"  }  [3]=>  array(2) {    ["image_url"]=>    string(59) "/images/items/large/l34775.jpg"    ["description"]=>    string(67) "(description of product here...)"  }}
If I comment out the debug code then it seems to work altough the layout is not right but should be able to be fixed... does this help to see where the problem is?
__________________
New site to
Please login or register to view this content. Registration is FREE
- please help kick start my new website -
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
sitesbuysell is offline
Reply With Quote
View Public Profile Visit sitesbuysell's homepage!
 
Old 08-20-2007, 09:25 AM Re: php & .tpl page
Novice Talker

Posts: 9
Trades: 0
so I can fix the layout, it was not related but I would like to still be able to call the code in the index page as I have to also have it there as other functions rely on it so I would have to use it twice if I also have it in the "rand_prods.php" page which is not very efficient.
__________________
New site to
Please login or register to view this content. Registration is FREE
- please help kick start my new website -
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
sitesbuysell is offline
Reply With Quote
View Public Profile Visit sitesbuysell's homepage!
 
Old 08-20-2007, 10:59 AM Re: php & .tpl page
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
This debug snippet does nothing else than showing you the content of the variable $products at a certain emplacement in your code.

If the place where you placed it showed that it was an array of 4 values, then it says that the array was correctly populated at this place of the script.

Try to move it further down, until you found the place where it's deleted or replaced by a empty/null value.

To call it from other pages, simply isolate the data manipulation in a single file, totally outside everything else, and simply do an require("file.php"); to import it into your file.
It's the simplest way.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 08-20-2007, 11:37 AM Re: php & .tpl page
Novice Talker

Posts: 9
Trades: 0
Quote:
Originally Posted by tripy View Post
To call it from other pages, simply isolate the data manipulation in a single file, totally outside everything else, and simply do an require("file.php"); to import it into your file.
It's the simplest way.
****, Im lost there, how would I do this exactly, i'm not following you here?
__________________
New site to
Please login or register to view this content. Registration is FREE
- please help kick start my new website -
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
sitesbuysell is offline
Reply With Quote
View Public Profile Visit sitesbuysell's homepage!
 
Old 08-20-2007, 10:37 PM Re: php & .tpl page
Novice Talker

Posts: 9
Trades: 0
To be specific, it is just this part I'm not sure what you mean? I know how to use "Require" but I don't know what you mean with the rest

Quote:
Originally Posted by tripy View Post
To call it from other pages, simply isolate the data manipulation in a single file, totally outside everything else
__________________
New site to
Please login or register to view this content. Registration is FREE
- please help kick start my new website -
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
sitesbuysell is offline
Reply With Quote
View Public Profile Visit sitesbuysell's homepage!
 
Old 08-21-2007, 03:28 AM Re: php & .tpl page
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Sorry, was too busy yesterday to come online.
I just looked again at your files, and they already are already separated...

Re-reading the previous posts, I don't know why I talked you about this last part.
I guess I have mixed several threads in my mind.
It may be wise that I start sleeping more than 4 hours a night....
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to php & .tpl page
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.35226 seconds with 12 queries