tags:

views:

98

answers:

2

Hello all,
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  

My goal is to check if the current song if not an encore. If not I want to check and see if it has the same date as the previous song. If not, I want to add what I have in '$encore' to the response and reset '$encore'. My problem is that '$previousDate' (which is initialized to an empty string) is always the same as '$song_date'

can anyone see why?

+2  A: 

Please add some rows of the table to help troubleshoot... But one guess would be, possibly $row['date'] is mistyped and is always evaluating to the empty string. You could try:

  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. Add some debugging info. Where you have echo "tagged at " . $row['name']; also echo $song_date so you can see what the dates are which are not functioning as you expect.

If you post more details (like actual table data, or the output of the code) I can review and revise my answer.

Josh
A: 

I'm sorry. The code works fine. The error was in what I was expecting from my data. Lesson learned. Thanks...

danwoods