Jump to content

Bad sql query - If, else problem


schoumi26

Recommended Posts

Hello,

 

I use prestashop 1.5.6.2

 

I created a new page that allows my clients to see the history of contest in which they participated.

 

ContestController.php

 

<?php

class ContestControllerCore extends FrontController
{
public $php_self = 'contest';
 
    public          $id_contest;
    public          $id_customer;
    public          $date;
    public          $desc;
public          $typetr;
public          $statut;
 
public function initContent()
{
  parent::initContent();
 
  $this->setTemplate(_PS_THEME_DIR_.'contest.tpl');
}
 
public static $definition = array(
  'table' => 'contest',
  'primary' => 'id_contest',
  'fields' => array(
    'id_customer' => array(
                'type' => ObjectModel::TYPE_INT,
                'validate' => 'isUnsignedInt',
                'required' => true,
            ),
            'date' => array(
                'type' => ObjectModel::TYPE_STRING,
            ),
'typetr' => array(
                'type' => ObjectModel::TYPE_STRING,
            ),
'statut' => array(
                'type' => ObjectModel::TYPE_STRING,
            ),
            'desc' => array(
                'type' => ObjectModel::TYPE_STRING,
                'required' => true,
            )
  ),
);
 
public function displaycontent() {
 
$requete = Db::getInstance()->executeS('SELECT * FROM `'._DB_PREFIX_.'contest` WHERE `id_customer` = \''.pSQL($context->customer->id).'\'')or die(mysql_error()); 
$this->context->smarty->assign('requete',$requete);
$this->setTemplate(_PS_THEME_DIR_.'contest.tpl');
 
}
}

 

Contest.tpl

 

<table id="order-list" class="std">

        <thead>
            <tr>
                <th class="first_item">{l s='ID'}</th>
<th class="columnDate">{l s='Date'}</th>
<th class="item">{l s='Type'}</th>
<th class="item">{l s='Desc'}</th>
                <th class="item">{l s='Statut'}</th>            
            </tr>
        </thead>
        <tbody>
        {foreach from=$requete item=trans name=myLoop}
            <tr>
                <td class="history">{$trans->id_contest}</td>
<td class="history_date bold">{$trans->date}</td>
<td class="history">{$trans->typetr}</td>
                <td class="history">{$trans->desc}</td>
                <td class="history">{$trans->statut}</td>      
            </tr>
        {/foreach}
        </tbody>
    </table>

 

No results are returned to contest.tpl while there has results in my table. 
 
I have the following errors with the debug mode: 
 
Notice: Undefined index: requete in 
Notice: Trying to get property of non-object in 
 
Thank you for your help
Edited by schoumi26 (see edit history)
Link to comment
Share on other sites

When I add {$requete| print_r}  on my page contest.tpl, it shows me the results of my query. 

 

But in my <table>, it returns me in every fields 

 

Notice: Trying to get property of non-object in /home/jeconomi/public_html/cache/smarty/compile/40/49/81/404981feda6ea707bc1411f4e9583c35ac063fa9.file.contest.tpl.php on line 71 

 

I do not understand my error 

 

Thank you for your help

Link to comment
Share on other sites

When there is a result, it appears perfectly. 
 
When the query returns no results, nothing is displayed. 
 
However, I put an if condition. If a result is returned, it is displayed. Else, "You do not have a contest" is displayed. 
 
I do have a problem with the condition. 
 
Thank you for your help

 

contest.tpl

 

{if $requete && count($requete)} 

    <table id="order-list" class="std">
        <thead>
            <tr>
                <th class="first_item">{l s='ID'}</th>
<th class="columnDate">{l s='Date'}</th>
<th class="item">{l s='Type'}</th>
<th class="item">{l s='Desc'}</th>
                <th class="item">{l s='Statut'}</th>            
            </tr>
        </thead>
        <tbody>
        {foreach from=$requete item=trans name=myLoop}
            <tr>
                <td class="history_method">{$trans['id_contest']}</td>
<td class="history_date bold">{$trans['date']}</td>
<td class="history_link bold">{$trans['typetr']}</td>
                <td class="history_method">{$trans['desc']}</td>
                <td class="history_state">{$trans['statut']}</td>      
            </tr>
        {/foreach}
 
        </tbody>
    </table>
    <div id="block-order-detail" class="hidden"> </div>
    {else}
        <p class="warning">{l s='You do not have a contest'}</p>
     {/if}
Link to comment
Share on other sites

I just try {$requete| var_dump} and he returned me this results in the database.

 

 

 

array(1) { [0]=> array(6) { ["id_contest"]=> string(2) "25" ["id_customer"]=> string(1) "7" ["date"]=> string(10) "21/04/2014" ["desc"]=> string(5) "20.00" ["typetr"]=> string(12) "Contest" ["statut"]=> string(36) "Lost" } }

 

Thank you very much 

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