Neden sadece bir kez bu PHP fonksiyon çağrısı çalışır?

6 Cevap php

I've wrote a simple array, and I would use it to print an html list option set, with a selected element. My problem starts if I try to print multiple lists in my page, because only the first list is printed correctly, why?

<?php


$units = array (
'0' => 'Units',
'kJ' => 'Kilojoule: kJ',
'g' => 'Grams: g',
'mg' => 'Milligrams: mg',
'mcg' => 'Micrograms: mcg, µg');

function unit_select_option ($attributes, $code = "") {
    global $units;
    $html = "<select title=\"Kilojoule: kJ;&#13;Grammi: g;&#13;Milligrammi: mg;&#13;Microgrammi: mcg, µg;\" $attributes>\r";

    while (list($key, $name) = each($units)) {
        if ($key == "0") {
            $html .= "  <option title=\"$name\" value='$key'>$name</option>\r";
        } else if ($key == $code) {
            $html .= "  <option title=\"$name\" selected=\"selected\" value='$key'>$key</option>\r"; 
        } else {
            $html .= "  <option title=\"$name\" value='$key'>$key</option>\r";
        }
    }
    $html.= "</select>\r";
    return $html;
}

print unit_select_option ('class="units_select"', "g");
print unit_select_option ('class="units_select"', "mg");
print unit_select_option ('class="units_select"', "mcg");
?>

kod garip bir şey olmamalı ama sayfa herhangi bir hata döndürmez çünkü ben sorunu bulamadım.

html code:
<select title="Kilojoule: kJ;&#13;Grammi: g;&#13;Milligrammi: mg;&#13;Microgrammi: mcg, µg;" class="units_select">
    <option title="Unit&agrave;" value='0'>Unit&agrave;</option>
    <option title="Kilojoule: kJ" value='kJ'>kJ</option>
    <option title="Grammi: g" selected="selected" value='g'>g</option>
    <option title="Milligrammi: mg" value='mg'>mg</option>
    <option title="Microgrammi: mcg, µg" value='mcg'>mcg</option>
</select>
<select title="Kilojoule: kJ;&#13;Grammi: g;&#13;Milligrammi: mg;&#13;Microgrammi: mcg, µg;" class="units_select">
</select>
<select title="Kilojoule: kJ;&#13;Grammi: g;&#13;Milligrammi: mg;&#13;Microgrammi: mcg, µg;" class="units_select">
</select>

6 Cevap

Dan each() :

Return the current key and value pair from an array and advance the array cursor.

After each() has executed, the array cursor will be left on the next element of the array, or past the last element if it hits the end of the array. You have to use reset() if you want to traverse the array again using each.

Yani:

function unit_select_option ($attributes, $code = "") {
  global $units;
  $html = "<select title=\"Kilojoule: kJ;&#13;Grammi: g;&#13;Milligrammi: mg;&#13;Microgrammi: mcg, µg;\" $attributes>\r";
  reset($units);
  while (list($key, $name) = each($units)) {
    if ($key == "0") {
      $html .= "  <option title=\"$name\" value='$key'>$name</option>\r";
    } else if ($key == $code) {
      $html .= "  <option title=\"$name\" selected=\"selected\" value='$key'>$key</option>\r"; 
    } else {
      $html .= "  <option title=\"$name\" value='$key'>$key</option>\r";
    }
  }
  $html.= "</select>\r";
  return $html;
}

Eğer aynı dizide kullandığı başka bir şey aramak döngü içinde ise anlam onun evresel olmayan, bu sizin dış çağrı etkileyebilir edeceğiz çünkü ben each() önlemek eğilimindedir. Iyi değil. Sadece bir foreach döngü kullanarak daha iyi olma eğilimindedir:

function unit_select_option ($attributes, $code = "") {
  global $units;
  $html = "<select title=\"Kilojoule: kJ;&#13;Grammi: g;&#13;Milligrammi: mg;&#13;Microgrammi: mcg, µg;\" $attributes>\r";
  foreach ($units as $key => $name) {
    if ($key == "0") {
      $html .= "  <option title=\"$name\" value='$key'>$name</option>\r";
    } else if ($key == $code) {
      $html .= "  <option title=\"$name\" selected=\"selected\" value='$key'>$key</option>\r"; 
    } else {
      $html .= "  <option title=\"$name\" value='$key'>$key</option>\r";
    }
  }
  $html.= "</select>\r";
  return $html;
}

ve tüm bu sorunları önlemek.

each() iç dizi imleci ilerletir. $ Birimleri global bir değişken olduğundan, unit_select_option sizin ilk çağrı () $ birimlerinin sonuna imleci ilerler, ve orada bir sonraki aramalar için kalır.

Sen sonunda reset($units); kullanarak diziyi sarmak gerekir unit_select_option().

PHP Belgeleri: reset

Sen dizi resetle olmalıdır: kullanım reset ()

Ama neden bir foreach döngüsü kullanmak değil mi?

foreach($units as $key => $name){ ... }

Ve o kötülük, küresel kullanmayın. Işlev gövdesi içinde, statik olarak $ birimler diziyi bildirmek.

Sen ise blok önce veya sonra reset sizin diziye gerekir.

Diğer cevaplar zaten soru çözmüş olmalıdır.

Ben PHP sadece yazabilirsiniz foreach construct, so instead of the while döngü olduğunu eklemek istiyorum

foreach ($unit as $key => $name) {
  ...
}

Eğer kullanıyorsanız, reset() gerek yoktur foreach.

Eğer global dizi sıfırlama değildi çünkü Tamam, diğerleri söylediler sorun oldu.

Ancak, küresel kullanmak yerine unit_select_option içine her zaman onu geçmek için değil cazip olurdu. (Diziler ve nesneler PHP'nin son sürümlerinde referans geçti, bunu yapmak için hiçbir neden ve genellikle iyi programlama uygulama olarak kabul edilmektedir edilmektedir.)

İkincisi, while döngüsü bazı garip şeyler yapıyoruz - Ben bir foreach yineleyici gibi, bu durumda daha mantıklı olacağını düşündüm olurdu:

foreach($units as $key => $value)

Sadece bir fikir. :-)