Page 1 sur 2
Boucle ? ou pas Boucle ?
Posté : 04 févr. 2006, 13:11
par Dom!
Bonjour,
Je réalise un tableau avec des cases à cocher. Je souhaite cocher ces case si la valeur est dans ma base, pour cela je fais ceci :
<?
$test=$_GET['numero_contrat'];
$sqldid = "select id, num_contrat, garanties from garanties where num_contrat = '".$test."'";
// $nbsql += 1;
if ($resselectdid = mysql_query($sqldid)) {
$selectdid = mysql_fetch_array($resselectdid);
}
echo 'La valeur $test = ['.$test.'] et $selectdid["garanties"] = '.$selectdid['garanties'].'<br />';
?>
Puis je dis à ma case de ce cocher si la valeur est dans ma base. Mon probleme c'est qu'avec ce script je recupere la derniere valeur de garanties.... comment récuperer toutes les valeurs en chaine ? j'ai regarde vos tutoriaux sur les chaines et boucle mais je n'arrive a rien ! pourriez vous m'aider ? merci !
Posté : 04 févr. 2006, 13:22
par Cyrano
Essaye comme ceci:
<?php
$test = $_GET['numero_contrat'];
$sqldid = "select id, num_contrat, garanties from garanties where num_contrat = '". $test ."'";
// $nbsql += 1;
if ($resselectdid = mysql_query($sqldid))
{
while(false !== ($selectdid = mysql_fetch_array($resselectdid)))
{
echo "La valeur \$test = [". $test ."] et \$selectdid[\"garanties\"] = ". $selectdid['garanties'] ."<br />\n";
}
}
?>
Posté : 04 févr. 2006, 13:26
par Dom!
C'est tout simplement parfait

merci
Posté : 04 févr. 2006, 14:32
par Dom!
Je réouvre ce post car je mets ce code pour cocher ma case mais ca ne fonctionne pas ! je ne comprend pas pourquoi ! merci pour votre aide.
<td width="27"></span><input name="id_garanties[]" type="checkbox" class="champ" id="id_garanties[]" value="9" <? if ($selectdid["garanties"] == 9) print "checked"; ?> ></td>
Posté : 04 févr. 2006, 15:07
par albat
Ceci fonctionne-t-il mieux ?
<td width="27"><input name="id_garanties[]" type="checkbox" class="champ" id="id_garanties[]" value="9" <?php echo ($selectdid['garanties']==9) ? checked="checked" : ""); ?> /></td>
<td width="27"></span> : écriture illicite
<? : à remplacer par <?php
"checked" : à remplacer par checked="checked"
Posté : 04 févr. 2006, 16:09
par Tictac
je comprend meme pas que tu aies pas un message d'erreur , tu as oublié de mettre les {} de ton if
<td width="27"></span><input name="id_garanties[]" type="checkbox" class="champ" id="id_garanties[]" value="9" <? if ($selectdid["garanties"] == 9) print "checked"; ?> ></td>
<!--devient -->
<td width="27"></span><input name="id_garanties[]" type="checkbox" class="champ" id="id_garanties[]" value="9" <? if ($selectdid["garanties"] == 9) {echo "checked"}; ?> ></td>
le mieux est d'utiliser l'opérateur ternaire comme l'a fait
albat
++
Posté : 04 févr. 2006, 17:14
par albat
Lorsque le resultat d'un test if se limite à une instruction, les accolades sont facultatives
Cependant, il est préférable d'en mettre en toutes circonstances.
Dans le cas qui nous préoccupe, leur absence ne déclenche pas d'erreur.
Posté : 05 févr. 2006, 01:03
par Dom!
J'ai une erreur :
Parse error: parse error, unexpected '=' in /var/www/html/Intranet/garanties.php on line 356
J'ai pourtant fait comme vous m'aviez di....
Posté : 05 févr. 2006, 01:09
par Truc
Tu sais qu'il y a eu plusieurs propositions et chacune contient un = et il n'y a pas 356 lignes visibles ici.
Comment veux tu qu'on sache d'où le problème peut venir ?

Posté : 05 févr. 2006, 01:12
par Dom!
Oups c'est vrai pardon pardon
<td width="27"><input name="id_garanties[]" type="checkbox" class="champ" id="id_garanties[]" value="3" <?php echo ($selectdid['garanties']==3) ? checked="checked" : ""); ?> /></td>
C'est cette ligne

[/php]
Posté : 05 févr. 2006, 01:22
par Truc
essaie avec:
<td width="27"><input name="id_garanties[]" type="checkbox" class="champ" id="id_garanties[]" value="3" <?php $checked = ($selectdid['garanties']==3) ? "checked='checked'" : ""; echo $checked;?> /></td>
une parenthèse de trop
Posté : 05 févr. 2006, 14:29
par Dom!
BOnjour à tous !
Quelqun pourrait il me dire pourquoi mes cases ne se coche pas quand garanties est dans ma base ??
<?php
$test = $_GET['numero_contrat'];
$sqldid = "select id, num_contrat, garanties from garanties where num_contrat = '". $test ."'";
// $nbsql += 1;
if ($resselectdid = mysql_query($sqldid))
{
while($selectdid = mysql_fetch_array($resselectdid))
{
echo "La valeur \$test = [". $test ."] et \$selectdid[\"garanties\"] = ". $selectdid['garanties'] ."<br />\n";
}
}
?>
<td><input name="id_garanties[]" type="checkbox" id="id_garanties[]" value="3" <?php if ($selectdid["garanties"] == 3) {echo "checked=\"checked\"";} ?> ></td>
<td class="win_blue_text">RESPONSABILITE CIVILE</td>
<td></td>
</tr>
<tr>
<td width="27"><input name="id_garanties[]" type="checkbox" id="id_garanties[]" value="9" <?php if ($selectdid["garanties"] == 9) {echo "checked=\"checked\"";} ?> ></td>
<td class="win_blue_text">DEFENSE / RECOURS</td>
<td width="100"><label></label>
</label>
<label>
</label></td>
</tr>
<tr>
<td width="27"><input name="id_garanties[]" type="checkbox" id="id_garanties[]" value='2' <?php $checked = ($selectdid['garanties']==2) ? "checked='checked'" : ""; echo $checked;?> /></td>
<td class="win_blue_text">ASSISTANCE</td>
<td><select name="franchises[2]" id="franchises[2]">
<option value="00 Kms">00 Kms</option>
<option value="30 Kms">30 Kms</option>
<option value="50 Kms">50 Kms</option>
</select></td>
</table>
Merci pour votre aide
Posté : 05 févr. 2006, 14:47
par Dom!
Bon j'ai revu mon code ! le probleme c'est que j'ai toutes mes cases à cocher en triple

je ne comprend pas pourquoi ?
<p class="win_blue_text">Veuillez selectionner les garanties pour le contrat numéro <?php echo $_GET['numero_contrat']; ?>
<form action="" method=post name="form1" id="form1">
<table wid>
<table width="411" border="0" align="center" class="win_blue">
<tr>
<table width="411" border="0" align="center" class="win_blue">
<tr>
<?php
$test = $_GET['numero_contrat'];
$sqldid = "select id, num_contrat, garanties from garanties where num_contrat = '". $test ."'";
// $nbsql += 1;
if ($resselectdid = mysql_query($sqldid))
{
while($selectdid = mysql_fetch_array($resselectdid))
{
echo "La valeur \$test = [". $test ."] et \$selectdid[\"garanties\"] = ". $selectdid['garanties'] ."<br />\n";
?>
<td><input name="id_garanties[]" type="checkbox" id="id_garanties[]" value="3" <?php if ($selectdid["garanties"] == 3) {echo "checked=\"checked\"";} ?> ></td>
<td class="win_blue_text">RESPONSABILITE CIVILE</td>
<td></td>
</tr>
<tr>
<td width="27"><input name="id_garanties[]" type="checkbox" id="id_garanties[]" value="9" <?php if ($selectdid["garanties"] == 9) {echo "checked=\"checked\"";} ?> ></td>
<td class="win_blue_text">DEFENSE / RECOURS</td>
<td width="100"><label></label>
</label>
<label>
</label></td>
</tr>
<tr>
<td width="27"><input name="id_garanties[]" type="checkbox" id="id_garanties[]" value="2" <?php if ($selectdid["garanties"] == 2) {echo "checked=\"checked\"";} ?> ></td>
<td class="win_blue_text">ASSISTANCE</td>
<td><select name="franchises[2]" id="franchises[2]">
<option value="00 Kms">00 Kms</option>
<option value="30 Kms">30 Kms</option>
<option value="50 Kms">50 Kms</option>
</select></td>
</tr>
<tr>
<td width="27"><input name="id_garanties[]" type="checkbox" id="id_garanties[]" value="10" <?php if ($selectdid["garanties"] == 10) {echo "checked=\"checked\"";} ?> ></td>
<td class="win_blue_text">ASSURANCE DU CONDUCTEUR</td>
<td><label></label></td>
</tr>
<tr>
<td width="27"><input name="id_garanties[]" type="checkbox" id="id_garanties[]" value="7" <?php if ($selectdid["garanties"] == 7) {echo "checked=\"checked\"";} ?> ></td>
<td class="win_blue_text">INCENDIE
<label></label></td>
<td><input name='franchises[7]' type=text class="win_blue_text" id="franchises[7]" value="0" size="8">
<span class="win_blue_text Style1">€uro </span></td>
</tr>
<tr>
<td width="27"><input name="id_garanties[]" type="checkbox" id="id_garanties[]" value="6" <?php if ($selectdid["garanties"] == 6) {echo "checked=\"checked\"";} ?> ></td>
<td class="win_blue_text">VOL / VANDALISME</td>
<td><input name='franchises[6]' type=text class="win_blue_text" id="franchises[6]" value="0" size="8">
<span class="win_blue_text Style1">€uro</span></td>
</tr>
<tr>
<td width="27"><input name="id_garanties[]" type="checkbox" id="id_garanties[]" value="8" <?php if ($selectdid["garanties"] == 8) {echo "checked=\"checked\"";} ?> ></td>
<td class="win_blue_text">BRIS DE GLACE</td>
<td><input name='franchises[8]' type=text class="win_blue_text" id="franchises[8]" value="0" size="8">
<span class="win_blue_text Style1">€uro</span></td>
</tr>
<tr>
<td width="27"><input name="id_garanties[]" type="checkbox" id="id_garanties[]" value="5" <?php if ($selectdid["garanties"] == 5) {echo "checked=\"checked\"";} ?> ></td>
<td class="win_blue_text">DOMMAGES COLLISION</td>
<td><span class="win_blue_text Style1">
<input name='franchises[5]' type=text class="win_blue_text" id="franchises[5]" value="0" size="8">
€uro</span></td>
</tr>
<tr>
<td width="27"><input name="id_garanties[]" type="checkbox" id="id_garanties[]" value="4" <?php if ($selectdid["garanties"] == 4) {echo "checked=\"checked\"";} ?> ></td>
<td class="win_blue_text">DOMMAGES TOUS ACCIDENTS</td>
<td><input name='franchises[4]' type=text class="win_blue_text" id="franchises[4]" value="0" size="8">
<span class="win_blue_text Style1">€uro</span></td>
</tr>
<tr>
<td width="27"><input name="id_garanties[]" type="checkbox" id="id_garanties[]" value="11" <?php if ($selectdid["garanties"] == 11) {echo "checked=\"checked\"";} ?> ></td>
<td class="win_blue_text">ACCESSOIRES</td>
<td><input name='franchises[11]' type=text class="win_blue_text" id="franchises[11]" value="0" size="8">
<span class="win_blue_text Style1">€uro</span></td>
</tr>
<tr>
<td width="27"><input name="id_garanties[]" type="checkbox" id="id_garanties[]" value="12" <?php if ($selectdid["garanties"] == 12) {echo "checked=\"checked\"";} ?> ></td>
<td class="win_blue_text">EFFETS / OBJETS PERSONNELS</td>
<td><input name='franchises[12]' type=text class="win_blue_text" id="franchises[12]" value="0" size="8">
<span class="win_blue_text Style1">€uro</span></td>
</tr>
<tr>
<td width="27"><input name="id_garanties[]" type="checkbox" id="id_garanties[]" value="17" <?php if ($selectdid["garanties"] == 17) {echo "checked=\"checked\"";} ?> ></td>
<td class="win_blue_text">APPAREIL RADIO </td>
<td><input name='franchises[17]' type=text class="win_blue_text" id="franchises[17]" value="0" size="8">
<span class="win_blue_text Style1">€uro</span></td>
</tr>
<tr>
<td width="27"><input name="id_garanties[]" type="checkbox" id="id_garanties[]" value="18" <?php if ($selectdid["garanties"] == 18) {echo "checked=\"checked\"";} ?> ></td>
<td class="win_blue_text">CREDIT BAIL</td>
<td> </td>
</tr>
<tr>
<td width="27"><input name="id_garanties[]" type="checkbox" id="id_garanties[]" value="13" <?php if ($selectdid["garanties"] == 13) {echo "checked=\"checked\"";} ?> ></td>
<td class="win_blue_text">PROTECTION JURIDIQUE</td>
<?php
}
}
?>
<td> </td>
</tr>
<tr>
<td colspan="3"><label></label>
<label>
<input name="numero" type="hidden" id="numero" value="<?php echo $_GET['numclient']; ?>">
<input name="numero_contrat" type="hidden" id="numero_contrat" value="<?php echo $_GET['numero_contrat']; ?>">
</label></td>
</tr>
<tr>
<td colspan="3"><div align="center">
<input name='valider' type=submit class="win_blue_titre" value='Valider les garanties'>
</div></td>
</tr>
</table>
<tr>
<p>
<a href="javascript:history.go(-1);"><-- Retour</a></p>
</form>
Aidez moi s'il vous plaiiiiiit merciiiiiiiiii

Posté : 05 févr. 2006, 18:32
par Truc
que contient $selectdid['garanties'] ? un entier ?
ensuite si tu as toutes les cases en triple c'est parce que tu boucles sur le résultat de la requete.
Etant donné que tu effectue un choix sur le numéro de contrat (d'apres la requete) tu dois avoir plusieurs contrats avec le même numéro

Posté : 05 févr. 2006, 18:35
par Dom!
$selectdid['garanties'] contient un entier
Ensuite oui j'a plusieur numero de contrat . Mais je ne trouve toujours sia spas comment faire en sorte à régler le soucis.... Ou est ce qu'il faut que je boucle ?
merci