PHP变量赋值

PHP变量赋值,php,binding,Php,Binding,我有一个问题一直困扰着我,也许你能帮助我。我有以下几点: 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($respons

我有一个问题一直困扰着我,也许你能帮助我。我有以下几点:

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  
我的目标是检查当前歌曲是否为安可。如果没有,我想检查一下,看看它是否与前一首歌的日期相同。如果没有,我想将“$encore”中的内容添加到响应中,并重置“$encore”。我的问题是“$previousDate”(初始化为空字符串)始终与“$song_date”相同


有人知道原因吗?

请在表格中添加一些行以帮助排除故障。。。但有一种猜测是,
$row['date']
可能输入错误,并且总是计算为空字符串。你可以试试:

  • 更改<代码>$previousDate=“”至
    $previousDate=false和更改:
    
    if($previousDate!=$song_date){
    if($previousDate!==$song_date){
    ——这将导致if语句不再执行,即使$song_date是空字符串,至少在第一次运行时是如此

  • 添加一些调试信息。在“$row['name']”处标记了“
    echo”;
    也标记了echo
    $song\u date
    ,这样您就可以看到哪些日期没有按预期运行


  • 如果发布更多详细信息(如实际表数据或代码输出)我可以查看并修改我的答案。

    对不起,代码运行正常。错误在我从数据中预期的范围内。经验教训。谢谢…

    b$song\u date来自哪里?你能显示完整的循环吗?不,循环在我看来没问题。我不明白为什么以前的日期不正确。