-2

I am brand new to PHP, and am creating a page with job postings separated by category. I would like for a message "Sorry, there are currently no openings in this division." to display under the category if there are no jobs in the database that match that category. Each row in the table will have some category assigned, so the code I have written checks for the value and puts the associated information in the div. IE - retail jobs go into Div id="retail". Wholesale jobs go into div id="wholesale"

I have written the following code but it doesn't work.

There are jobs in the table with the jobcat Retail, but the Sorry message appears 3 times underneath the first record returned, outside of a div with the class "job-details"

There are no jobs in the next category in the table, and with this code the "Sorry" message repeats 5 times.

Can anyone help me achieve what I am trying to do please? I have not found any similar questions where an echo is repeating like this but I may not be using the right search terms.

The code below repeats 4 times on my page, with different categories for jobcat.

         <div id="retail"><h2>Retail</h2>
           <?php do { 
                 if ($row_info['jobcat'] == 'Retail'){
                     echo "<div class='job-post'>"; 
                     echo "<p><span class='header'>Position: </span>".$row_info['jobtitle'] . "</p>";
                     echo "<p><span class='header'>Location: </span>".$row_info['joblocation'] . "</p>";
                     echo "<p class='view-job'>View Details</p> <div class='job-details'>".nl2br($row_info['jobdesc']) ." <a href='job-application.php?id=".$row_info['id']."'>Apply</a></div>"; 
                     echo "</div>";
                    }  else{
                        echo "<p>Sorry, there are currently no openings in this division.</p>";
                        }
                  } while ($row_info = mysql_fetch_assoc($info));
                     mysql_data_seek($info, 0);
                    $row_info = mysql_fetch_assoc($info);
                    ?>
         </div>

I tried modifying based on the answer below to check for the value rather than empty, and this isn't working either - surely its poor implementation on my part but wanted to show anyway. With this code, the first category pulls in the correct data, but the Sorry message shows for all other categories, even if there is a match in the table:

           <?php 
               if ($row_info['jobcat'] == 'Retail')
               {
                  while ($row_info = mysql_fetch_assoc($info)) {
                     echo "<div class='job-post'>"; 
                                     echo "<p><span class='header'>Position: </span>".$row_info['jobtitle'] . "</p>";
                                     echo "<p><span class='header'>Location: </span>".$row_info['joblocation'] . "</p>";
                                     echo "<p class='view-job'>View Details</p> <div class='job-details'>".nl2br($row_info['jobdesc']) ."<a href='job-application.php?id=".$row_info['id']."'>Apply</a></div>"; 
                                     echo "</div>";
                  }

               }
               else
               {
                  echo "Sorry, there are no open positions at this time.";
               }
               mysql_data_seek($info, 0);
                $row_info = mysql_fetch_assoc($info);
            ?>
4

3 回答 3

1

You need to not use a do/while loop for one, and secondly, your error message display for every iteration of the loop. Check the data first, and then loop through it if it's not empty

<?php 
   if (!empty($info))
   {
      while ($row_info = mysql_fetch_assoc($info)) {
         // display data
      }

   }
   else
   {
      // error message here
   }
?>

You should also use mysqli_, not mysql_ functions because they are deprecated. I also strongly suggest getting an introduction to programming PHP book.

于 2013-06-11T18:41:11.127 回答
0

Set a flag if the error condition arises, and then output an error after the loop (not inside it).

<?php
    $flag = false;

    do {
        if ($row_info['jobcat'] == 'Wholesale') {
            echo "<div class='job-post'>"; 
            echo "<p><span class='header'>Position: </span>".$row_info['jobtitle'] . "</p>";
            echo "<p><span class='header'>Location: </span>".$row_info['joblocation'] . "</p>";
            echo "<p class='view-job'>View Details</p> <div class='job-details'>".nl2br($row_info['jobdesc']) ."<a href='job-application.php?id=".$row_info['id']."'>Apply</a></div>"; 
            echo "</div>";
        } else{
            $flag = true;
        }
    } while ($row_info = mysql_fetch_assoc($info)); 

    mysql_data_seek($info, 0);
    $row_info = mysql_fetch_assoc($info);

    if ($flag) {
        echo "<p>Sorry, there are currently no openings in this division.</p>"
    }
?>
于 2013-06-11T18:42:07.277 回答
0
#assume there are no openings until we know otherwise
$openings_available = false;

while ($row_info = mysql_fetch_assoc($info)) {
    if ($row_info['jobcat'] == 'Wholesale') {
        #echo statements here

        $openings_available = true;    #now we know there's at least one opening
    }
}

if (!$openings_available) {
    echo "<p>Sorry, there are currently no openings in this division.</p>";
}

Or, if you don't want to re-set $openings_available every time you have a jobcat, you could do this:

#assume there ARE openings until we encounter a row without a 'jobcat'
$openings_available = true;

while ($row_info = mysql_fetch_assoc($info)) {
    if ($row_info['jobcat'] == 'Wholesale') {
        #echo statements here
    } else {
        $openings_available = false;
    }
}

if (!$openings_available) {
    echo "<p>Sorry, there are currently no openings in this division.</p>";
}

I find this harder to understand, though.

于 2013-06-11T20:04:07.800 回答