[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • Last Modified:

If a dynamic select box has no data, default to empty...?

I have a select box that is filled with dynamic data (code below).  If there is no data in the record, I need the box to be empty...right now it has the first record in the record source.

 <select name="raceFastestLap" id="raceFastestLap">
          <?php
do {  
?>
          <option value="<?php echo $row_alldrivers['racerID']?>"<?php if (!(strcmp($row_alldrivers['racerID'], $row_Recordset1['racerID']))) {echo "selected=\"selected\"";} ?>><?php echo $row_alldrivers['racerNickName']?></option>
          <?php
} while ($row_alldrivers = mysql_fetch_assoc($alldrivers));
  $rows = mysql_num_rows($alldrivers);
  if($rows > 0) {
      mysql_data_seek($alldrivers, 0);
        $row_alldrivers = mysql_fetch_assoc($alldrivers);
  }
?>
        </select>
0
Kevin Smith
Asked:
Kevin Smith
  • 3
  • 3
1 Solution
 
K VDatabase ConsultantCommented:
I think before looping only you can manually put the blank entry!
    <option value="" ....
0
 
nasirbestCommented:
use "while" loop instead of "do"

and additionally on empty record-set if you want to show something more meaning full rather then empty select box like "No record found" etc.. then enclose this loop into if statement. like
<select name="raceFastestLap" id="raceFastestLap">
<?php 
$rows = mysql_num_rows($alldrivers);

if ($rows > 0) {
    while ($row_alldrivers = mysql_fetch_assoc($alldrivers)) { 
?>
    <option value="<?php echo $row_alldrivers['racerID']?>"<?php if (!(strcmp($row_alldrivers['racerID'], $row_Recordset1['racerID']))) {echo "selected=\"selected\"";} ?>><?php echo $row_alldrivers['racerNickName']?></option>
<?php 
    } 
} else {
?>
    <option value="">No record found</option>
<?php
}

if($rows > 0) {
    mysql_data_seek($alldrivers, 0);
    $row_alldrivers = mysql_fetch_assoc($alldrivers);
}
?>
</select>

Open in new window

0
 
Kevin SmithAuthor Commented:
The code above seems to just be removing the first record from the dynamic list...?
0
[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

 
nasirbestCommented:
there may be a row "before" code (which you have placed in question) like

mysql_fetch_assoc($alldrivers);

remove it, it is not needed if you are using my code. there must be not such line "before" of my code.
0
 
Kevin SmithAuthor Commented:
removed it from the code, still doesn't work.  is there a way to leave it where it's at and remove it from your code?  the code that does that delivers data to other parts of the page.
0
 
nasirbestCommented:
OK, leave this that line.

and add this one before while loop

    mysql_data_seek($alldrivers, 0);


like
<select name="raceFastestLap" id="raceFastestLap">
<?php 
$rows = mysql_num_rows($alldrivers);

if ($rows > 0) {
    mysql_data_seek($alldrivers, 0);
    while ($row_alldrivers = mysql_fetch_assoc($alldrivers)) { 
?>
    <option value="<?php echo $row_alldrivers['racerID']?>"<?php if (!(strcmp($row_alldrivers['racerID'], $row_Recordset1['racerID']))) {echo "selected=\"selected\"";} ?>><?php echo $row_alldrivers['racerNickName']?></option>
<?php 
    } 
} else {
?>
    <option value="">No record found</option>
<?php
}

if($rows > 0) {
    mysql_data_seek($alldrivers, 0);
    $row_alldrivers = mysql_fetch_assoc($alldrivers);
}
?>
</select>

Open in new window

0
 
Kevin SmithAuthor Commented:
Still having issues with this, but went in another direction anyway.  Thanks!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now