2010-02-16 168 views
0

我有一个问题,让我难住,也许你可以帮忙。我有以下的php: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 

我的目标是检查当前的歌曲,如果不是一个encore。如果不是,我想检查它是否与上一首歌有相同的日期。如果不是,我想将'$ encore'中的内容添加到响应中并重置'$ encore'。我的问题是'$ lastDate'(初始化为空字符串)始终与'$ song_date'相同

任何人都可以看到为什么?

+1

Wbere确实$ song_date从哪里来?你能展示完整的循环吗? –

+1

不,循环看起来o.k.对我来说。我看不出为什么previousDate不正确。 –

回答

0

对不起。代码工作正常。错误在于我期望的数据。学过的知识。谢谢...

2

请添加表格的一些行以帮助排除故障......但有一种猜测是,可能是$row['date']错误类型,并且总是评估为空字符串。你可以尝试:

  1. 变化$previousDate = "";$previousDate = false;,改变: if($previousDate != $song_date){if($previousDate !== $song_date){ - 这将导致if语句不再执行,即使$ song_date是一个空字符串,至少在第一次运行...

  2. 添加一些调试信息。如果你有echo "tagged at " . $row['name'];也回声$song_date,所以你可以看到哪些日期哪些不是如你所期望的那样运作。

如果您发布更多信息(如实际表格数据或代码输出),我可以查看和修改我的答案。