我有一个让我难过的问题,也许你可以提供帮助。我有以下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”。我的问题是'$ previousDate'(初始化为空字符串)始终与'$ song_date'相同
谁能明白为什么?
答案 0 :(得分:2)
请添加表格的某些行以帮助排除故障...但是有人猜测,可能$row['date']
输入错误,并且总是在评估空字符串。你可以尝试:
将$previousDate = "";
更改为$previousDate = false;
并更改:
if($previousDate != $song_date){
到if($previousDate !== $song_date){
- 这会导致if语句不再执行,即使$ song_date是一个空字符串,至少在第一次运行时...
添加一些调试信息。您echo "tagged at " . $row['name'];
的位置也会回显$song_date
,以便您可以看到日期不符合预期的日期。
如果您发布更多详细信息(如实际表格数据或代码输出),我可以查看并修改我的答案。
答案 1 :(得分:0)
对不起代码工作正常。错误是我对数据的期望。学过的知识。感谢...