PHP değişken atama

2 Cevap php


I have a question that has me stumped, maybe you can help. I have the following php:

if(mysql_num_rows($result) > 0){
  //if so set up the xml
  $dom = new DOMDocument();
  $response = $dom->createElement('response');
  $encore = $dom->createElement('encoreSongs');
  $dom->appendChild($response);
  $previousDate = "";
  //if yes cycle through all results, checking their artist

  while($row = mysql_fetch_array($result)){
    //check if the current songs artist is in the artist array
    $song_name = $row['name'];
    $song_artist = $row['artist'];
    $song_date = $row['date'];
    $song_city = $row['city'];
    $song_state = $row['state'];
    $song_location = $song_city . ', ' . $song_state;
    $song_id = $row['unique_song_id'];
    $song_segue = $row['part_of_a_sugue'];

    $song_info = $dom->createElement('song');

    $idElement = $dom->createElement('song_id');
    $idText = $dom->createTextNode($song_id);
    $idElement->appendChild($idText);
    $song_info->appendChild($idElement);

    $nameElement = $dom->createElement('song_name');
    $nameText = $dom->createTextNode($song_name);
    $nameElement->appendChild($nameText);
    $song_info->appendChild($nameElement);

    $artistElement = $dom->createElement('song_artist');
    $artistText = $dom->createTextNode($song_artist);
    $artistElement->appendChild($artistText);
    $song_info->appendChild($artistElement);

    $dateElement = $dom->createElement('song_date');
    $dateText = $dom->createTextNode($song_date);
    $dateElement->appendChild($dateText);
    $song_info->appendChild($dateElement);

    $locationElement = $dom->createElement('song_location');
    $locationText = $dom->createTextNode($song_location);
    $locationElement->appendChild($locationText);
    $song_info->appendChild($locationElement);

    $segueElement = $dom->createElement('song_segue');
    $segueText = $dom->createTextNode($song_segue);
    $segueElement->appendChild($segueText);
    $song_info->appendChild($segueElement);

    //if the song is part of an encore, save it for later
    if($row['setOrEncore'] == 'encore'){
      $encore->appendChild($song_info);
    }

    else{
      /*if the previous song was an encore from another show and this song is a set,
      assume that the previous group of encores went with the previous show
      and append the encores before this new song/show */
      if($previousDate != $song_date){
        echo "tagged at " . $row['name'];
        //affix encore songs to 'response'
        $encore_songs = $encore->getElementsByTagName('song');
        foreach($encore_songs as $a){
          $response->appendChild($a);
        }
        //reset encore variable
        $encore = $dom->createElement('encoreSongs');  
      }
      //attach new song
      $response->appendChild($song_info);
    }

    $previousDate = $row['date'];
  }//end while  

Amacım mevcut şarkıyı değilse bir tekrarını olmadığını kontrol etmektir. Değilse Ben kontrol ve önceki şarkı olarak aynı tarih olup olmadığını görmek istiyorum. Eğer değilse, ben yanıt '$ encore' ne var ekleyebilir ve '$ encore' sıfırlamak istiyor. Benim sorunum (boş bir dize başlatıldı) '$ previousDate' her zaman '$ song_date' olarak aynı olmasıdır

neden herkes görebilir?

2 Cevap

Özür dilerim. Kod çalışıyor. Hata benim verilerinden bekliyordum ne oldu. Ders öğrendim. Teşekkürler ...

Gidermek için tablonun bazı satır eklemek ... Ama bir tahminim muhtemelen $row['date'] mistyped ve her zaman boş dize değerlendirmek duyarız. Eğer deneyebilirsiniz:

  1. Change $previousDate = ""; to $previousDate = false; and also change: if($previousDate != $song_date){ to if($previousDate !== $song_date){ -- This will cause the if statement to no longer execute even if $song_date is an empty string, at least on the first run...

  2. Bazı hata ayıklama bilgisi ekle. Eğer echo "tagged at " . $row['name']; de $song_date echo böylece tarihleri ​​beklediğiniz gibi çalışmıyor hangi görebilirsiniz var burada.

Eğer (gerçek tablo veri veya kod çıkışı gibi) daha fazla ayrıntı sonrası eğer ben gözden ve benim cevap revize edebilir.