Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Reconnaître le type de champ d'une variable

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Language: French
View previous topic :: View next topic  
Author Message
topolofi
Smarty Rookie


Joined: 16 Jun 2006
Posts: 27

PostPosted: Fri Mar 02, 2007 6:29 pm    Post subject: Reconnaître le type de champ d'une variable Reply with quote

Bonsoir,

J'ai besoin de connaître dans mon formulaire le type de champ issu de ma requête.

Exemple, je voudrais faire quelque chose du genre :
Code:
{if $line=champ numérique}
                  <td style="text-align:right">{$line}{$line}</td>
               {else}
                  <td>{$line}</td>
               {/if}


Est-ce possible avec Smarty ?

Merci d'avance
Back to top
View user's profile Send private message
topolofi
Smarty Rookie


Joined: 16 Jun 2006
Posts: 27

PostPosted: Sat Mar 03, 2007 10:03 am    Post subject: Reply with quote

Quleques visite et pas de réponse.

Ne poouvant attendre, voici commen j'ai fait :

Code:
$result = mysql_query($requete['sql_requete']) or exit(mysql_error());
$numrow=mysql_num_rows($result);

// Constitution de l'entête du tableau
$nomsChamps = array();
$typesChamps = array();
for ($i = 0; $i < mysql_num_fields($result); $i++)
{
    array_push($nomsChamps, mysql_field_name($result, $i));
    array_push($typesChamps, mysql_field_type($result, $i));
}
$smarty->assign("nomsChamps", $nomsChamps);
$smarty->assign("typesChamps", $typesChamps);


Je récupère donc le nom des champs et leur type dans mon script php

Puis dans mon formulaire html

Code:
<table>
         <tr>
            {foreach from=$nomsChamps item=nomChamp}
            <th>{$nomChamp}</th>
            {/foreach}
         </tr>
         {foreach from=$lignes item=ligne}
         {counter start=0 skip=1 assign=compteur}
         <tr>
            {foreach from=$ligne item=line}
               {if $typesChamps[$compteur]=="decimal" or $typesChamps[$compteur]=="real"}
                  <td style="text-align:right">{$line}</td>
               {else}
                  <td>{$line}</td>
               {/if}
               {counter}
            {/foreach}
         </tr>
         {/foreach}
      </table>


J'écris d'abord le nom de mes champs, puis pour chaque ligne, je fais un test du type de champ pour pouvoir avoir l'affichage souhaité.

Merci de m'avoir lu

A bientôt
Back to top
View user's profile Send private message
topolofi
Smarty Rookie


Joined: 16 Jun 2006
Posts: 27

PostPosted: Sat Mar 03, 2007 10:10 am    Post subject: Reply with quote

Autre possibilité, mais à mon avis moins sur

Code:
{if is_numeric($line)}


On peut utiliser toute fonction PHP dans un {if}.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Language: French All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP