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
https://forums.phpfreaks.com/topic/256183-php-and-mysql-problem/
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
   }
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.