php code in .tpl not working

Discussion in 'PHP' started by save, Aug 20, 2007.

  1. #1
    I have problem with this piece of php...

    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:

    				<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>
    Code (markup):
    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:

    I also get the same error if I just don't even use an include file and place the code directly in the master.tpl page.

    the strange thing is that their is a similar file linked from the master.tpl page that works so why doesnt the "rand_prods.php" page work?
     
    save, Aug 20, 2007 IP
  2. dalton

    dalton Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    try changing:

    <?php foreach ($products as $item): ?>
    PHP:
    to

    <?php foreach ($products as $item) { ?>
    PHP:
     
    dalton, Aug 20, 2007 IP
  3. save

    save Active Member

    Messages:
    1,047
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    80
    #3
    that did something but now I get this error:

    c:\wamp\www\mywebsite\includes\rand_prods.php on line 13

    line 13 = <?php endforeach; ?>

    when I look at the source it says this:

    <b>Parse error</b>: parse error, unexpected T_ENDFOREACH in <b>c:\wamp\www\mywebsite\includes\rand_prods.php</b> on line <b>13</b><br />
     
    save, Aug 20, 2007 IP
  4. dalton

    dalton Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    change:

    to

     
    dalton, Aug 20, 2007 IP
  5. save

    save Active Member

    Messages:
    1,047
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    80
    #5
    I changed it to <?php } ?> but now I get this error again on line 2 and I use <?php foreach ($products as $item) { ?> like you showed:

    Warning: Invalid argument supplied for foreach() in c:\wamp\www\mywebsite\includes\rand_prods.php on line 2
     
    save, Aug 20, 2007 IP
  6. dalton

    dalton Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    looking into as we speak :)
     
    dalton, Aug 20, 2007 IP
  7. save

    save Active Member

    Messages:
    1,047
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    80
    #7
    ok thanks.
     
    save, Aug 20, 2007 IP
  8. dalton

    dalton Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    can you supply the full code to master.tpl?
     
    dalton, Aug 20, 2007 IP
  9. dalton

    dalton Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    So far I think it's an array issue. Or coulde be a typo in <?php foreach ($products as $item): ?> ...possibily $product in stead of $products or $item instead of $items
     
    dalton, Aug 20, 2007 IP
  10. save

    save Active Member

    Messages:
    1,047
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    80
    #10
    I could show the master.tpl page but I doubt it would help because all it is is html css etc, no php, all that is done on the index.php page in the top level. the index.php page is quite large so I will post some parts here:

    <?php
    
    require_once('xml2array.php');
    require_once('config.php');
    require_once('common.php');
    
    $rss_id = urldecode(custom_get('rss_id'));
    // Get affiliate list
    
    $query = "SELECT * from datafeeds;";
    database_inquiry($query, $rows);
    $affiliates = $rows;
    
    // get values from database
    
    $query = "SELECT * from `misc` WHERE id = '1';";
    database_inquiry($query, $rows);
    $featured = $rows[0]['featured'];
    
    // get products based on featured settings
    
    $product_names = explode("\n",$featured);
    
    $in_clause = "IN (";
    foreach ($product_names as $item)
       {
       $item = addslashes(str_replace("\r",'',$item));
       $in_clause .= "'$item',";
       }
    $in_clause = substr($in_clause,0,strlen($in_clause)-1);
    $in_clause .= ")";
    
    $query = "SELECT image_url, description FROM `products` ORDER BY rand() LIMIT 4;";
    database_inquiry($query, $rows);
    $products = $rows;
    
    // generate category menu
    
    $menu = "<li><a href=\"index.php\">Home</a></li>\n";
     
    foreach($rows as $row)
       {
       $name = urlencode($row['category_name']);
       $menu .= "<li><a href=\"?cat_id=$row[category_id]&name=$name\">$row[category_name]</a></li>\n";
       }
    
    
    // process rss feed (optional)
    
    $rss_menu = '';
    $rss = '';
    $count = 0;
    $rest = $cfg['rss_feed'];
    $format = 'RSS/CHANNEL/ITEM';
    if ($rest != '')
       {
       convert_xml($rest, "process_rss", $format, $cfg['curl']);
       }
         
       
    
    // format price
    $i=0;
    foreach ($products as $item)
       {
       $item['price'] = '$'.number_format($item['price'],2,'.',',');
       $products[$i] = $item;
       $i++;
       }
    
    
    // Generate inner template
    
    $tpl = & new Template();
    $tpl->set('products',$products);
    $tpl->set('cat_name',$cat_name);
    $tpl->set('results',$results);
    $tpl->set('rss',$rss);
    $tpl->set('Xserver',$cfg['Xserver']);
    
    $main_content = $tpl->fetch('templates/index.tpl');
    
    
    // Generate outer template
    
    $tpl2 = & new Template();
    $tpl2->set('main_content',$main_content);
    $tpl2->set('affiliates',$affiliates);
    
    
    $tpl2->set('menu',$menu);
    $tpl2->set('rss_menu',$rss_menu);
    
    $tpl2->set('title',$title);
    $tpl2->set('keywords',$title);
    $tpl2->set('description',$title);
    $tpl2->set('site_name',$cfg['site_name']);
    $tpl2->set('site_slogan',$cfg['site_slogan']);
    $tpl2->set('menuitem1',$cfg['menuitem1']);
    $tpl2->set('menuitem2',$cfg['menuitem2']);
    $tpl2->set('menuitem3',$cfg['menuitem3']);
    $tpl2->set('menuitem4',$cfg['menuitem4']);
    
    $tpl2->set('GoogleAnalytics',$cfg['GoogleAnalytics']);
    $tpl2->set('domain',$cfg['domain']);
    $tpl2->set('h1',$cfg['h1']);
    $tpl2->set('h2',$cfg['h2']);
    $tpl2->set('keyword1',$cfg['keyword1']);
    $tpl2->set('keyword2',$cfg['keyword2']);
    $tpl2->set('intro',$cfg['intro']);
    $tpl2->set('phrase1',$cfg['phrase1']);
    $tpl2->set('phrase2',$cfg['phrase2']);
    
    $tpl2->set('featuredprod',$cfg['featuredprod']);
    
    $tpl2->set('MetaTitle',$cfg['MetaTitle']);
    $tpl2->set('MetaDescription',$cfg['MetaDescription']);
    $tpl2->set('MetaKeywords',$cfg['MetaKeywords']);
    
    $x = $tpl2->fetch('templates/master.tpl');
    
    // Generate html page
    
    echo $x;
    Code (markup):
    the rest is not relevant, it is just funtions for RSS feeds etc...
     
    save, Aug 20, 2007 IP
  11. save

    save Active Member

    Messages:
    1,047
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    80
    #11
    what the heck.. here is the part of the master.tpl page


    <? include ("includes/vitals.php"); ?>
    
    <body>
    	<!-- Start Header -->
    	  <? include ("includes/header.php"); ?>
    	<!-- Start Main Content -->
    	<div id="main" class="container">
    		<!-- left column (products and features) -->
    		 <? include ("includes/leftmenu.php"); ?>  		
    		<!-- main content area -->
    		<div id="center">
    			<div class="article_wrapper">
    				<h2>Weekly News Updates</h2>
    			 <?=$main_content; ?> 
    			</div>
    			<div class="article_wrapper">
    				<h2>heading</h2>
    				<p>Lorem ipsumu.</p>
    			</div>
    			<hr />
    		</div>
    		<!-- product sales boxes -->
    		
    		
    		 <? include ("includes/rand_prods.php"); ?>
    		
    
    	</div>
    Code (markup):
     
    save, Aug 20, 2007 IP
  12. dalton

    dalton Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    what array did you assign to $products?

    i may have missed it as i'm getting ready for work :p
     
    dalton, Aug 20, 2007 IP
  13. save

    save Active Member

    Messages:
    1,047
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    80
    #13
    that is posted above,, it is after this: // get products based on featured settings

    note: it is probably something simple as I'm not an expert at php, just still learning :)

    I guess it must be something to do with the order in the index.php page at including the master.tpl page & the index.tpl page since when I include the "rand_prods.php" file on the index.tpl file it works fine but when I include it in the master.tpl file it won't work. So I guess it is not the code on any page other than the index.php page? Of course I can't include it in the index.tpl as it doesn't display in the correct html layout then but I did that just to test it.
     
    save, Aug 20, 2007 IP
  14. dalton

    dalton Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    After reading the code for some reason I think you may need to try this:

    <?php foreach ($product_names as $item): ?>
    PHP:
    Don't worry, I'm fairly new to php myself and jump on every opporunity to learn.
     
    dalton, Aug 20, 2007 IP
  15. save

    save Active Member

    Messages:
    1,047
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    80
    #15
    that never worked but I have been talking on another thread & making some progress there - link here
     
    save, Aug 20, 2007 IP
  16. dalton

    dalton Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    might want to try sitepoint.com forums as well
     
    dalton, Aug 20, 2007 IP