Jump to content

Controller Foreach -> Template


Recommended Posts

Hi Everyone,

 

I've got a controller and template nearly done (special thanks to razaro!) but I'm having trouble with the last piece of it.

 

My controller and template are below. My goal is for the user to submit a wishlist, and the controller looks up the items and matches it to the database, which the template displays the results of. (Preferably how product-list.tpl displays products, but I'm not really that picky at the moment)

 

The form works. The controller gets the list. It's cleaned up, and it hits the foreach statement. At that point, the template only shows the last item in the wishlist. That's where I need some help :)

 

Yes, I'm sure there's a cleaner way to get this done. But I need it working right now.

 

 

Any help would be appreciated!

 

Controller -

<?php
//
// Eft Import Controller
// v12.13.2011
//
class EftImportControllerCore extends FrontController
{
public $php_self = 'eftimport.php';
public function preProcess()
{
parent::preProcess();
// If the submit button was clicked
if (Tools::isSubmit('submit_eft'))
  {
  $eftdata = Tools::htmlentitiesUTF8(Tools::getValue('eftdata'));

  // Cleanup the array and get it ready for the DB queries
  // Connect to Database

  // Cycle through the array and lookup each item
  foreach ($eft_data as $item)
 {
 // Query 1 - get product id & name
 $query = "SELECT l.id_product, l.name FROM ps_product_lang l WHERE name = '$item'";

 $id_result = mysql_query($query) or die(mysql_errno().": ".mysql_error()."<BR>");

 $id = @mysql_result ($id_result, $i, "id_product"); // Product ID
 $name = @mysql_result ($id_result, $i, "l.name");   // Product Name

 // Query 2 - Grab the item price & Image ID (graphics in the shop are "id_product.id_image.jpg" format)
 $query = "SELECT ps_product.price, ps_image.id_image
 FROM ps_product INNER JOIN ps_image ON ps_product.id_product=ps_image.id_image
 WHERE ps_product.id_product = '$id'";

 $result = mysql_query($query) or die(mysql_errno().": ".mysql_error()."<BR>");

 $price = @mysql_result ($result, $i, "p.price");  // Dirty Product Price
 $price = number_format($price, 2, '.', ',');	// Clean Product Price
 $id_img = @mysql_result ($result, $i, "i.id_image");// Image ID


// Smarty stuff
 $results = array('id'=>$id, 'name'=>$name, 'price'=>$price, 'id_img'=>$id_img, 'addtocart'=>$addtocart);
 self::$smarty->assign('results', $results);
  }
 }
  else{
self::$smarty->assign('eftdata', '');
  }
}
public function displayContent()
  {
  parent::displayContent();
  self::$smarty->display(_PS_THEME_DIR_.'eftimport.tpl');
  }
}
?>

 

 

Template -

{*
* EFT Import Template
* v12.13.2011
*}
{capture name=path}{l s='EFT Import'}{/capture}
{include file="$tpl_dir./breadcrumb.tpl"}
<div class="pl_category">
<h1>{l s='EFT Import'}</h1>
<div class="pl_ct">
<br />
{if isset($results)}
<h4>{l s='EFT Import Results'}</h4>
 <br>
<div >
{foreach from=$results key=k item=result}
       <span id={$k} >{$result}</span>
{/foreach}
</div>

{else}
** Display Form **
{/if}
</div>
</div>

Link to comment
Share on other sites

Some of the issue I seen right away:

$eftdata = Tools::htmlentitiesUTF8(Tools::getValue('eftdata'));

Will flat any values in eftdata (on purpose? and it goes to a foreach ? )

$eft_data?? where this var is coming from ($eftdata <> $eft_data)

You query the database without using the Db object. You do not really have a connection.

Your cleanup?!? does nothing and is SQL injection prone.

This to mention the obvious.

 

Best,

(d)oekia

Link to comment
Share on other sites

doekia,

 

Thanks for the Skype conversation, the education on Prestashop's functions, and for cleaning up my mess!

 

 

To everyone else -

 

If your looking for a developer that's friendly and knowledgeable, checkout doekia's services.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...