Jump to content

Help with dropdown list constructon script


Darkmatter5

Recommended Posts

I have 2 relevant tables for this code.

 

genres

genre_idgenre

1UNKNOWN

2Action

3Role-Play

 

game_genres

game_idgenre_id

12

22

33

 

Now I have 2 instances where I use the genre dropdown list.  As a list for searching or a list for forms.

 

Here's the function call I use when I'm needing a search list.

<?php $vein->list_genres('list',$_GET['g']); ?>

 

Here's the function call I use when I'm needing a form list.

<?php $vein->list_genres(form,''); ?> if the list is on the add form

<?php $vein->list_genres(form,$_GET['i']); ?> if the list is on the edit form

 

Now here's my code:

<?php
  function list_genres($type,$default) {
    include 'library/config.inc.php';
    $conn=mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
    mysql_select_db($dbname);

    $query=mysql_query("SELECT * FROM genres ORDER BY genre ASC") or die(mysql_error());
    echo "<select name='g' method='post' class='text_boxes'>";
    if($type=="list") { echo "<option value='all'>All Games</option>"; }
    while($row=mysql_fetch_array($query)) {
      $r1=$row['genre_id'];
      $r2=$row['genre'];
      switch($type) {
        case "list":
          if($default==$r1) { echo "<option value='$r1' selected='selected'>$r2</option>"; }
          else { echo "<option value='$r1'>$r2</option>"; }
          break;
        case "form":
          if(empty($default)) { $default=1; }
          else {
            $Qgenre_id=mysql_query("SELECT genre_id FROM game_genres WHERE game_id='$default'") or die(mysql_error());
            $Dgenre_id=mysql_fetch_array($Qgenre_id);
            $default=$Dgenre_id['genre_id'];
          }
          if($default==$r1) { echo "<option value='$r1' selected='selected'>$r2</option>"; }
          else { echo "<option value='$r1'>$r2</option>"; }
          break;
      }
    }
    echo "</select>";

    mysql_close($conn);
  }
?>

 

So the desired end result should be

1. On a search list the dropdown should be a a list of all the genres with "All Games" listed first and if $_GET['g'] is set then select that genre.

2. On the add form the dropdown should be a list of all the genres and default to "UNKOWN".

3. On the edit form the dropdown should be a list of all the genres and default to the specified games genre.

 

The list portion of the switch seems to be working fine, but the form stuff keeps defaulting to another genre besides the games genre or the default UNKNOWN.

 

Clear as mud?  Thanks in advance!!

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.