Jump to content

PHP and mysql problem


mackin

Recommended Posts

HI - I am dynamically populating a drop down form list from the table 'hotels', and the 'est_town' column. It is supposed to only list each town once, but several towns appear multiple times. Am I using the DISTINCT command correctly and if so, what can i do to stop multiple appearances of towns?

 

 

$query_hotels_select = "SELECT DISTINCT est_town FROM hotels ORDER BY est_town ASC";
$hotels_select = mysql_query($query_hotels_select, $contractors) or die(mysql_error());
$row_hotels_select = mysql_fetch_assoc($hotels_select);
$totalRows_hotels_select = mysql_num_rows($hotels_select);

 

FORM BIT

 

 <select name="digs" id="digs">
<?php
do {  
?>
<option value="<?php echo $row_hotels_select['est_town']?>"<?php if (!(strcmp($row_hotels_select['est_town'], $row_hotels_select['est_town']))) {echo "selected=\"selected\"";} ?>><?php echo ucwords(strtolower($row_hotels_select['est_town']))?></option>
           <?php 
} while ($row_hotels_select = mysql_fetch_assoc($hotels_select));
  $rows = mysql_num_rows($hotels_select);
  if($rows > 0) {
      mysql_data_seek($hotels_select, 0);
  $row_hotels_select = mysql_fetch_assoc($hotels_select);
  }
?>
           
        </select>

Link to comment
Share on other sites

while ($row_hotels_select = mysql_fetch_assoc($hotels_select));
  $rows = mysql_num_rows($hotels_select);
  if($rows > 0) {
      mysql_data_seek($hotels_select, 0);
  $row_hotels_select = mysql_fetch_assoc($hotels_select);
  }

 

this is your problem, you are using mysql_data_seek statically inside of a loop, this is a no no as it will consistently reset the result pointer to the first row. Typically, this function is used to return the current row pointer and display it. Really, all that is needed is this:

 

if($hotels_select)
{
   while ($row_hotels_select = mysql_fetch_assoc($hotels_select))
   {
     $rows = mysql_num_rows($hotels_select);
     ?>
       <option value="<?php echo $row_hotels_select['est_town']?>"<?php if (!(strcmp($row_hotels_select['est_town'], $row_hotels_select['est_town']))) {echo "selected=\"selected\"";} ?>><?php echo ucwords(strtolower($row_hotels_select['est_town']))?></option> 
     <?php
   }
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.