Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2002
    Posts
    42

    simple logic problem

    can anybody suggest why this bit of script does not work? the idea is if there is nothing in the database i echo a response to the document to say that; ie ' there are currently no voids at as ash-coop' as indicated in the the script. it is a simple logic probem but to my mind the logic is right here, yet it does not work.
    PHP Code:
      $query "SELECT * FROM voids;";
      
    $result mysql_query($query) or die("could not execute query");
      echo 
    "<table width='95%'><tr><th align='center'>Place</th><th align='center'>Number of rooms</th><th align='center'>Room number</th><th align='center'>Date available</th></tr><tr><td colspan='5'><hr noshade color='black'></td></tr>";
      while (
    $row mysql_fetch_array($result)) {
        
    extract($row);
        if (
    $row == 0) {
            echo 
    "<tr><td colspan='5' align='center'>There are currently no voids at Ash Co-op.</td></tr>";
          } else {
            echo 
    "<tr><td align='center'>$property</td><td align='center'>$type</td><td align='center'>$roomnum</td><td align='center'>$day $month $year</td></tr><tr><td colspan= '5'><hr color='black'></td></tr>";
          }
        }
        echo 
    "</table>"
    i would greatly appreciate the views of a clearer minded logician or someone who knows more about php than me.
    cheers and thanks

  2. #2
    Join Date
    Mar 2002
    Location
    Denmark
    Posts
    684
    while ($row = mysql_fetch_array($result))

    well if the table is empty, the while loop will not be executed so you need to restructure the logic so that you can print : "There are currently no voids at Ash Co-op." if its empty.....

    try checking if no result was returned then output or loop through the data...
    ~Chris

  3. #3
    Join Date
    Jul 2004
    Location
    N29° 22.725' X W95° 6.452'
    Posts
    343
    i would write it as:

    if(mysql_num_rows($result) == 0) {
    echo no records
    } else {
    while ($row = mysql_fetch_array($result)) {
    extract($row);
    your stuff here
    }
    }


    Because there are no records in your table the while loop is not processing, therefor your line "There are currently no voids at Ash Co-op" is not being echoed. You are saying: while there are records do my ifelse statement but there are no records so it's not processing the ifelse.

    Nchris already stated this but I was giving you an example.

  4. #4
    Join Date
    Mar 2002
    Posts
    42
    thanks, i think i was doing something like that last night before bed although it still wasn't working before i fell unconscious. i think my vision was going by then. i'll have another look tonight. it definetly makes more sense though. thanks again.

  5. #5
    Join Date
    Mar 2002
    Posts
    42

    thanks

    thanks for your help all.
    this was the solution i was working on which didn't work last night due to belgian lager. thanks again

    PHP Code:
      if ($row mysql_fetch_array($result)) {
        
    extract($row);
        echo 
    "<tr><td align='center'>$property</td><td align='center'>$type</td><td align='center'>$roomnum</td><td align='center'>$day $month $year</td></tr><tr><td colspan= '5'><hr color='black'></td></tr>";
      }
      else {
        echo 
    "<tr><td colspan='5' align='center'>There are currently no voids at Ash co-op</td></tr></table>";
      }
      echo 
    "</table>"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •