Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2006
    Posts
    3

    Unanswered: Linked or chained selectors

    Hello All,

    I am trying to do something in php I'm not sure I can do.
    I want a user to select an item from a mysql query displayed as a dropdown in a form and then populate a variable with data from a second mysql db table field captured in the same query. The query works. Both fields from the mysql db are returned. There are around 40 different selections that could possibly be made, each with it's own 2nd field data.

    The problem I am having is that only the first row of data in field2 is ever being selected after field1 is selected.

    PHP Code:

    <?php require_once('Connections/itdb3.php'); ?>
    <?php
    mysql_select_db
    ($database_itdb3$itdb3);
    $query_Recordset1 "SELECT probsel.Problem_id, probsel.Support_id FROM probsel ORDER BY probsel.Problem_id";
    $Recordset1 mysql_query($query_Recordset1$itdb3) or die(mysql_error());
    $row_Recordset1 mysql_fetch_assoc($Recordset1);
    $totalRows_Recordset1 mysql_num_rows($Recordset1);

    $dbHost "localhost";
    $dbName "itdb3";
    $dbUser "root";
    $dbPass "root";
    $connection mysql_connect($dbHost$dbUser$dbPass);
    if (!
    $connection) {
        exit(
    '<p>Unable to connect to the database.</p>');
    }
    mysql_select_db($dbName$connection);
    if (!@
    mysql_select_db($dbName)) {
        exit(
    '<p>Unable to locate the Table</p>');
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>2Select</title>
    <link href="main.css" rel="stylesheet" type="text/css" />
    </head>

    <body>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form1" >
      <table border="1" bgcolor="#D6DFEF">
        <tr>
          <td>Problem</td>
          <td>Support</td>
        </tr>
        <tr>
          <td>
            <select class="style4" size="1" name="problem" <?php echo $row_Recordset1['Problem_id']; ?>>
              <option selected="selected">Select the Problem</option>
              <?php
    do {  
    ?>
              <option value="<?php echo $row_Recordset1['Problem_id']?>"<?php if (!(strcmp($row_Recordset1['Problem_id'], $row_Recordset1['Problem_id']))) {echo "selected=\"selected\"";} ?>><?php echo $row_Recordset1['Problem_id']?></option>
              <?php
    } while ($row_Recordset1 mysql_fetch_assoc($Recordset1));
      
    $rows mysql_num_rows($Recordset1);
      if(
    $rows 0) {
          
    mysql_data_seek($Recordset10);
          
    $row_Recordset1 mysql_fetch_assoc($Recordset1);
      }
    ?>
          </select></td>
          <td><input class="style4" name="support" type="text" value="<?php echo $row_Recordset1['Support_id']; ?>" />
          </td>
        </tr>
    </table>  
    </form>
    </body>
    </html>
    <?php
    mysql_free_result
    ($Recordset1);
    ?>
    Any help is appreciated.
    Last edited by garry3d; 04-19-06 at 09:37.

  2. #2
    Join Date
    Apr 2005
    Location
    Baltimore, MD
    Posts
    297
    Quote Originally Posted by garry3d
    Hello All,
    The problem I am having is that only the first row of data in field2 is ever being selected after field1 is selected.
    Could you be a little bit more specific? I don't see anything named field1 or field2 and I only see one select tag...?


    Also, next time use php tags
    PHP Code:
    <?php require_once('Connections/itdb3.php'); ?>
    <?php
    mysql_select_db
    ($database_itdb3$itdb3);
    $query_Recordset1 "SELECT probsel.Problem_id, probsel.Support_id FROM probsel ORDER BY probsel.Problem_id";
    $Recordset1 mysql_query($query_Recordset1$itdb3) or die(mysql_error());
    $row_Recordset1 mysql_fetch_assoc($Recordset1);
    $totalRows_Recordset1 mysql_num_rows($Recordset1);

    $dbHost "localhost";
    $dbName "itdb3";
    $dbUser "root";
    $dbPass "root";
    $connection mysql_connect($dbHost$dbUser$dbPass);
    if (!
    $connection) {
    exit(
    '<p>Unable to connect to the database.</p>');
    }
    mysql_select_db($dbName$connection);
    if (!@
    mysql_select_db($dbName)) {
    exit(
    '<p>Unable to locate the Table</p>');
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>2Select</title>
    <link href="main.css" rel="stylesheet" type="text/css" />
    </head>

    <body>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form1" >
    <table border="1" bgcolor="#D6DFEF">
    <tr>
    <td>Problem</td>
    <td>Support</td>
    </tr>
    <tr>
    <td>
    <select class="style4" size="1" name="problem" <?php echo $row_Recordset1['Problem_id']; ?>>
    <option selected="selected">Select the Problem</option>
    <?php
    do {
    ?>
    <option value="<?php echo $row_Recordset1['Problem_id']?>"<?php if (!(strcmp($row_Recordset1['Problem_id'], $row_Recordset1['Problem_id']))) {echo "selected=\"selected\"";} ?>><?php echo $row_Recordset1['Problem_id']?></option>
    <?php
    } while ($row_Recordset1 mysql_fetch_assoc($Recordset1));
    $rows mysql_num_rows($Recordset1);
    if(
    $rows 0) {
    mysql_data_seek($Recordset10);
    $row_Recordset1 mysql_fetch_assoc($Recordset1);
    }
    ?>
    </select></td>
    <td><input class="style4" name="support" type="text" value="<?php echo $row_Recordset1['Support_id']; ?>" />
    </td>
    </tr>
    </table>
    </form>
    </body>
    </html>
    <?php
    mysql_free_result
    ($Recordset1);
    ?>
    Give a man a fish and you feed him for a day. Teach a man to fish and you lose a steady customer.
    -----------
    Eclectic Web Development (under heavy construction)

  3. #3
    Join Date
    Apr 2006
    Posts
    3
    Sorry for the confusion-
    field1 is actually Problem_id and field2 is Support_id.
    You are correct, there is only 1 select tag. When the user selects an item from this select tag, i want to populate a variable with the data from the Support_id field in the db.

    Table being queried contains 3 fields:
    field 1 = record_id
    field 2 = Problem_id
    field 3 = Support_id

    Each Problem_id has a corresponding Support_id assigned to it in the table.
    The query selects Problem_id and Support_id from the table.

    In my code, I am putting the Support_id into a form field which I really want to replace with a variable. I was thinking to solve it first with a field displaying the Support_id and converting it to a variable after I got it working. I don't want the user to see this (Support_id) in the form.

    I hope this answers your question.

  4. #4
    Join Date
    Apr 2005
    Location
    Baltimore, MD
    Posts
    297
    In my code, I am putting the Support_id into a form field which I really want to replace with a variable. I was thinking to solve it first with a field displaying the Support_id and converting it to a variable after I got it working. I don't want the user to see this (Support_id) in the form.
    What exactly do you mean by variable? You want to store the selected Problem_id's corresponding Support_id in a php variable? Or do you want to display the Support_id somehwere on the page when a Problem_id is selected?

    To handle form variables in php, use $_GET or $_POST. To have something happen when you choose an option from a select, use the onchange attribute.

    HTH
    Give a man a fish and you feed him for a day. Teach a man to fish and you lose a steady customer.
    -----------
    Eclectic Web Development (under heavy construction)

  5. #5
    Join Date
    Apr 2006
    Posts
    3

    Response 2

    My original intent was to have the Support_id data as a php variable.

    I could live with having it displayed in the form field.

  6. #6
    Join Date
    Apr 2005
    Location
    Baltimore, MD
    Posts
    297
    When you submit your form, have your php code get the value of the selected problem and then use that value to run a query and find all of the support id's associated with it.

    PHP Code:
    //more code...
    $prob_id $_POST['problem'];
    $query_Recordset1 "SELECT probsel.Problem_id, probsel.Support_id FROM probsel WHERE probsel.Problem_id = $prob_id ORDER BY probsel.Problem_id";
    //more code... 
    Does that help?
    Give a man a fish and you feed him for a day. Teach a man to fish and you lose a steady customer.
    -----------
    Eclectic Web Development (under heavy construction)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •