Posez votre question Signaler

[PHP] If dans While [Résolu]

Kalamya 47Messages postés 2 mars 2005Date d'inscription - Dernière réponse le 14 janv. 2009 à 18:30
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 !!!
Lire la suite 

[PHP] If dans While »

5 réponses
Réponse
+0
moins plus
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.


Ajouter un commentaire
Réponse
+0
moins plus
Comment je dois faire alors ? :o/
wiwimagique - 4 oct. 2005 à 23:30
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.

Kalamya - 5 oct. 2005 à 14:56
Merci beaucoup !!!!!!!
Ca marche super !!
Merci encore pour ta patience :o)
Ajouter un commentaire
Réponse
+0
moins plus
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>';

}
}
?>
Ajouter un commentaire
Ce document intitulé « [PHP] If dans While » issu de CommentCaMarche (www.commentcamarche.net) est mis à disposition sous les termes de la licence Creative Commons. Vous pouvez copier, modifier des copies de cette page, dans les conditions fixées par la licence, tant que cette note apparaît clairement.
Dossier à la une
5 extensions si vous voulez revenir à l'ancien Facebook