0

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
  • 1
    Wbere does $song_date come from? Can you show the full loop? Commented Feb 16, 2010 at 18:26
  • 1
    Nope, the loop looks o.k. to me. I can't see why previousDate wouldn't be correct. Commented Feb 16, 2010 at 18:40

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.