|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
topolofi Smarty Rookie
Joined: 16 Jun 2006 Posts: 27
|
Posted: Fri Mar 02, 2007 6:29 pm Post subject: Reconnaître le type de champ d'une variable |
|
|
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 |
|
topolofi Smarty Rookie
Joined: 16 Jun 2006 Posts: 27
|
Posted: Sat Mar 03, 2007 10:03 am Post subject: |
|
|
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 |
|
topolofi Smarty Rookie
Joined: 16 Jun 2006 Posts: 27
|
Posted: Sat Mar 03, 2007 10:10 am Post subject: |
|
|
Autre possibilité, mais à mon avis moins sur
Code: | {if is_numeric($line)} |
On peut utiliser toute fonction PHP dans un {if}. |
|
Back to top |
|
|
|
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
|