Rechercher : dans
Par :

[PHP] If dans While

Dernière réponse le 14 jan 2009 à 18:30:27 Kalamya, le 4 oct 2005 à 20:36:21 
 Signaler ce message aux modérateurs

Bonjour à tous,

J'ai un p'tit problème, je souhaite mettre un if dans un while mais ce dernier me parasite mon code.

-------------------------------------------
<select size="1" name="menu" style="font-family: Verdana; font-size: 8pt" tabindex="6">
<option selected value="0">- Tri par clients -</option>
<?php

$req = mysql_query("SELECT*FROM repertoire WHERE categorie='Client'");
$res = mysql_numrows($req); // on compte le nombre de résultats
$i=0; // on initialise notre compteur "$i" à Zéro
WHILE($res!=$i) // chaque fois que "$res" est dif. de "$i", donc qu'il y a un enreg.
{
$yclient = mysql_result($req,$i,"societe");
$zcaract_variable=strlen("$yclient");

if ($zcaract_variable>13) {
$zclient = substr("$yclient", 0, 13);
$zsuite=".."; }
else {
$zsuite="";}

echo "<option value=\"commandes/tri/tri.php?mode=$yclient\">$zclient $zsuite..</option>"; // on affiche les résultats

$i++; // on ajoute un au compteur et on retourne à WHILE
}

echo"</select>";

?>
-------------------------------------------
Il me duplique le premier nom de clients de la liste et ne m'affiche plus les autres. Si je retire le if... ça marche. J'ai donc du mal bidouillé mon code lol.

Quelqu'un peut m'aider ?

Merci d'avance !!!

Meilleures réponses pour « [PHP] If dans While » dans :
PHP - Structures conditionnelles Voir Qu'est-ce qu'une structure conditionnelle? On appelle structure conditionnelle les instructions qui permettent de tester si une condition est vraie ou non, c'est-à-dire si la valeur de son expression vaut 0 ou 1 (le PHP associe le mot clé true à 1...
[PHP] Les ressemblances à ne pas confondre VoirIntroduction Concaténation ou paramètres ? Guillemets/Apostrophe Structure require/include isset vs empty htmlentities ou htmlspecialchars ? Nombre de signes égal Introduction Cette astuce présente plusieurs fonction ou astuces en...
[Shell] Tester une variable numérique VoirTester une variable numérique    Préambule Dans un environnement "shell", les variables sont, par défaut, de type "chaîne de caractères". De ce fait il n'est pas possible de déclarer une variable de type "entier" (enfin, ceci n'est pas tout à...
Javascript - Les structures conditionnelles VoirQu'est-ce qu'une structure conditionnelle? On appelle structure conditonnelle les instructions qui permettent de tester si une condition est vraie ou non, ce qui permet notamment de donner de l'interactivité à vos scripts. L'instruction...
PHP - Les fichiers VoirLa gestion des fichiers avec PHP Avec PHP, la création ou la lecture de fichiers est, une fois de plus, assez simple. Il existe une multitude de fonctions dédiées à l'utilisation des fichiers. La communication entre le script PHP et le fichier...
PHP - Récupération de données VoirPHP rend très simple la récupération de données envoyées par l'intermédiaire de formulaires HTML. Création d'un formulaire Grâce à la balise FORM du langage HTML, il est très simple de créer des formulaires comprenant : des champs de saisie des...

1

wiwimagique, le 4 oct 2005 à 21:35:20

C'est simple, ton $zclient n'est affecté qu'une fois, et ce exclusivement dans le block if
Il suffit que la condition ne soit plus réalisée pour que $zclient ne soit plus mis à jour.


Dans sa voiture rouge et jaune

Répondre à wiwimagique

2

Kalamya, le 4 oct 2005 à 22:15:50

Comment je dois faire alors ? :o/

Répondre à Kalamya

3

wiwimagique, le 4 oct 2005 à 23:30:44

Ben réfléchis. Imagine qu'aucun nom de société ne dépasse 13 caractères.
dans ce cas, il n'y aura jamais de $zclient ....




<select size="1" name="menu" style="font-family: Verdana; font-size: 8pt" tabindex="6">
<option selected value="0">- Tri par clients -</option>
<?php

$req = mysql_query("SELECT societe FROM repertoire WHERE categorie='Client'");
WHILE($res = mysql_fetch_array($req)) 
{
$yclient = $res["societe"];
$zcaract_variable=strlen("$yclient");

$zclient = substr("$yclient", 0, 13);

if ($zcaract_variable>13) {
$zsuite="..";
} else {
$zsuite="";
}

echo "<option value=\"commandes/tri/tri.php?mode=$yclient\">$zclient $zsuite..</option>"; // on affiche les résultats

}
?>
</select>



J'ai légèrement modifié ton code.

Dans sa voiture rouge et jaune

Répondre à wiwimagique

4

Kalamya, le 5 oct 2005 à 14:56:29

Merci beaucoup !!!!!!!
Ca marche super !!
Merci encore pour ta patience :o)

Répondre à Kalamya

5

 lucas, le 14 jan 2009 à 18:30:27

Bonjour je souhaite profiter de ton experience en php pour resoudre ce perti souci avec while if echo
cars je comprends pas grande chose en php merci d'avance lucas

ps. desolee pour le francais je suis bresilien

voila c'est un boite qu'affiche la categorie parent en têtê et les sous-categories en bas avec une image plus le prix e le mon de produit

j'arrive juste a affiche la categorie parent et les sous categories)

voici mon code

<?php

$query = ("select c.categories_id, cd.categories_name, c.parent_id, c.categories_image from " .

TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.categories_id = cd.categories_id

order by sort_order, cd.categories_name limit 30");

$categories_query = tep_db_query($query);

while ($categories = tep_db_fetch_array($categories_query)){

if ($categories['categories_id'] == 22) {

$cPath_new = tep_get_path($categories['categories_id']);

text_subcategories = '';

$subcategories_query = tep_db_query($query);

while ($subcategories = tep_db_fetch_array($subcategories_query)) {

if ($subcategories['parent_id'] == $categories['categories_id'])
{

$cPath_new_sub = "cPath=" . $categories['categories_id'] . "_" . $subcategories['categories_id'];

$text_subcategories .= ' <font color="#fe9900">  •  </font> <a href="' . tep_href_link(FILENAME_DEFAULT, $cPath_new_sub, 'NONSSL') . '">' . $subcategories['categories_name'] . '</a>

<hr color="#CCCCCC" align=left width="175" size="1">' . " ";

}
}

echo '
<table cellspacing=0 cellpadding=0 bgcolor="#FFFFFF">

<tr><td height=8></td></tr>

<tr><td background=images/header/menu.gif width=390 height=30>

<table cellspacing=0 cellpadding=0 align=left>

<a class="main12" href="' . tep_href_link(FILENAME_DEFAULT, $cPath_new, 'NONSSL') . '">  <b>' . $categories['categories_name'] . '</b></a>

</td></tr>

</table>

</td></tr>

<tr><td class=bg5a align=center>

<table background=images/header/bas_box_cat.gif cellspacing=0 cellpadding=0 align=left width=390 border=2>

<tr><td bgcolor=#ffffff height=0 colspan=2></td></tr>

<tr><td align=left class="smallText" width="280" valign="top" height=175>' . $text_subcategories . '<font color=fe9900>  » </font><a class=main13 href="' . tep_href_link(FILENAME_DEFAULT, $cPath_new, 'NONSSL') . '">voir la liste complête</a>';

$new_products_query = tep_db_query("select p.products_id, p.products_image, p.products_quantity, p.products_tax_class_id, p.products_price, manufacturers_id, p2c.categories_id, p2c.products_id from " . TABLE_PRODUCTS . " p, products_description pd, products_to_categories p2c where p2c.products_id = pd.products_id AND p.products_id = pd.products_id and p.products_status = '1' and (p2c.categories_id = 103 or p2c.categories_id = 49 or p2c.categories_id = 50 or p2c.categories_id = 85 or p2c.categories_id = 51 or p2c.categories_id = 102 or p2c.categories_id = 104) order by rand() desc limit 1");

while ($new_products = tep_db_fetch_array($new_products_query)) {

$new_products['products_name'] = tep_get_products_name($new_products['products_id']);

$manufacturer_query = tep_db_query("select manufacturers_name from " . TABLE_MANUFACTURERS . " where manufacturers_id = ". $new_products['manufacturers_id']);

$manufacturer = tep_db_fetch_array($manufacturer_query);

$product_query = tep_db_query("select products_description from " . TABLE_PRODUCTS_DESCRIPTION . " where products_id = '" . (int)$new_products['products_id'] . "' and language_id = '" . (int)1 . "'");

$product = tep_db_fetch_array($product_query);

$new_products['products_description'] = $product['products_description'];

// if (MODULE_ORDER_TOTAL_ECOTAX_STATUS=='true' && !$new_products['ecotax_rates_id']=="0"){

// $new_products_ecotax=tep_get_ecotax_description($new_products['ecotax_rates_id']);

// $ecotax_rates_id=($new_products['ecotax_rates_id']!=$new_products_ecotax[0]['ecotax_id_other']&&$new_products_ecotax[0]['ecotax_id_other']!="" && $new_products_ecotax[0]['ecotax_id_other']!="0")?$new_products_ecotax[0]['ecotax_id_other']:$new_products['ecotax_rates_id'];}
//fin modif by paddybl for ecotax</P> <P> tep_db_query("update " . TABLE_PRODUCTS_DESCRIPTION . " set products_viewed = products_viewed+1 where products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "' and language_id = '" . (int)$languages_id . "'");</P> <P> if ($new_price = tep_get_products_special_price($product_info['products_id'])) {
//debut modif by paddybl for ecotax

$products_price = '<s style="color:#777777; font-size:12px">' . $currencies->display_price($new_products['products_price'], tep_get_tax_rate($new_products['products_tax_class_id']),$ecotax_rates_id) . '</s> <span class="productSpecialPrice">' . $currencies->display_price($new_price, tep_get_tax_rate($new_products['products_tax_class_id']),$ecotax_rates_id) . '</span>';

{

$products_price = $currencies->display_price($new_products['products_price'], tep_get_tax_rate($new_products['products_tax_class_id']),$ecotax_rates_id);

}

echo '

</td><td width=176 valign=top align=center>

<table cellspacing=0 cellpadding=0 align=center width=300 border=3>

<tr><td height=35 valign=top colspan=2> <a class=tx13 href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $new_products['products_id']) . '">'. $manufacturer['manufacturers_name'] . ' ' . $new_products['products_name'] . '</a>

</td></tr>

<tr><td colspan=2><img src=images/header/m42.gif width=170 height=1></td></tr>

<tr><td height=3 colspan=2></td></tr>

<tr><td valign=top align=center colspan=2>

<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $new_products['products_id']) . '">' . tep_image(DIR_WS_IMAGES . $new_products['products_image'], $new_products['products_name'], SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT, ' class=br') . '</a>' . $new_products['products_name2'] . '</td>

</tr>

<tr><td height=3 colspan=2></td></tr>

<tr><td align=center width=176 class=tx6>'.$products_price.'<img src=images/header/01.gif width=77 height=16></td></tr>

</table>

</td>

'; }echo '</tr>


</table>

</td></tr>

<tr><td align=center width=176 class=tx6>'.$products_price.'</td><td>

</table>';

}
}
?>

Répondre à lucas