开发者

Select dropdown with two Sql query

I have some difficulty with my 'select'

In my select/menu, I display all the options come from a table (table_db_email) in my database

In this table (tb_code_prmtn11_email) I have two field : fld_email_id fld_name_email

It works here is a code

<select name="email_adress_menu" id="email_adress_menu"  class="valid"  onchange="submit()">
<?php
  echo "<option selected=\"selected\" value=''>Choose your name</option>"; 
  $req_email_adress_menu =   "SELECT DISTINCT id_email, fld_name_email, fld_adresse_email FROM $table_db_email ORDER BY fld_name_email ";
  $rep_email_adress_menu =  mysql_query($req_email_adress_menu, $cnx) or die( mysql_error() ) ;

  while($show_email_adress_menu = mysql_fetch_assoc($rep_email_adress_menu)) {
    echo '<option value="'.$show_email_adress_menu['id_email'].'"';
    //if($primes==$show_email_adress_menu['fld_name_email']){echo " selected";} //display to select an option!!!!!!!!!!!!!!!!
    echo '>'.$show_email_adress_menu['fld_name_email'].'  - '.$show_email_adress_menu['fld_adresse_email'].'</option>';
  }
?>
</select>

I have some other information coming from another table : tb_code_prmtn11 (containing information about people)

I have a few fields :

  • id_resultat
  • fld_name
  • fld_email_id; ( FOREIGN KEY (fld_email_id) REFERENCES tb_code_prmtn11_email (id_email) ON DELETE NO ACTION ON UPDATE CASCADE;)

I want to put this menu on another Web page and this select must display all of the options as below( with) BY SELECTING THE OPTION THAT MATCHES THE INFORMATION FOUND IN THE SECOND TABLE : tb_code_prmtn11

How can I do this ?

SELECT td.id_resultat,td.fld_email_id,email.fld_name_email 
  FROM $table_db td
  JOIN $table_db_email email ON td.fld_email_id = email.id_email 
 WHERE td.id_resultat = $id 

This code works also but I don't know how I can include this second query in my menu...


Your code works... I see my select : it works... It display all of the option BUT Without selected option.

So second query works :(It's on line 42)

I get the following code in FireFox :

27   <form id="form-fan" method="post" action="edit.php">

    <option value="tomo">TOTO MONO  - toto.mono@test.com</option>
<option value="kito">KIKI TOTO - kiki.toto@test.com</option>

So I want display option selected with others options but I can't... My menu works but any options are selected, however, in reality, there is an option that is selected because in my database, there is one in each

I tried my first query (line 18 in your/my code)

SELECT tbl.id_resultat,tbl.fld_email_id,email.fld_nom_email,email.id_email
                                   FROM tb_code_prmtn11 tbl 

                                   INNER JOIN tb_code_prmtn11_email email
                                   ON tbl.fld_email_id = email.id_email 

                                   WHERE tbl.id_resultat='27'

*$table_db tbl* => tb_code_prmtn11

*$table_db_email* -> tb_code_prmtn11_email

and it's work... **id_resultat : 27

fld_email_id:kito

fld_nom_email:KIKI TOTO

id_email:kito**

So how can I display the correct option selected with others option in my select ? How I can get the following code in FireFox : 27

    <option value="tomo">TOTO MONO  - toto.mono@test.com</option>
<option value="kito"   selected>KIKI TOTO - kiki.toto@test.com</option>

I checked my variable Id : it's ok... Here is your new code with my changes to adapt to my page :

    <?php

include"../bd_db/connection.php";

include"../bd_db/selection.php";

    $id=$_GET['id'];
     echo $id;

if (isset($_POST) && !empty($_POST))
{
    if (array_key_exists('ajaxId', $_POST))
    {
         if ((int)$_POST['ajaxId'] <= 0)
         {
             throw new Exception("go to hell");
         }
     $query = "SELECT td.id_resultat as id, email.fld_name_email AS name FROM " . $table_db . " td INNER JOIN " . $table_db_email . " email ON td.fld_email_id = email.id_email WHERE td.id_email = " . $_POST['ajaxId']; //!!!!! line 18


     $result = mysql_query($query, $cnx);

     if (mysql_num_rows($result) > 0)
     {
          $data = array();
          while ($row=mysql_fetch_array($result))
          {
               $data[$row["resultat"]] = $row["name"];
          }

          echo json_encode($data);
          exit();
     }
}

if (array_key_exists('select-big-fan', $_POST))
{
    // you've found a big fan selected
}
}
$query = "SELECT DISTINCT id_email AS id, fld_nom_email AS name, fld_adresse_email AS email FROM " . $table_db_email . " ORDER BY name"; //second query works :(line 42)

$result = mysql_query($query, $cnx);

?>
<?php if (mysql_num_rows($result) > 0) : ?>
   <form id="form-fan" method="post" action="<?php echo $_SERVER["PHP_SELF"] ?>">
   <select name="select-fan" id="select-fan">
   <?php while ($row=mysql_fetch_array($result)) : ?>
      <option value="<?php echo $row["id"] ?>"><?php echo $row["name"] ?> - <?php echo $row["email"] ?></option>
   <?php endwhile ?>
   </select>
   </form>
   <script type="text/javascript">
       jQuery(document).ready(function(){
           jQuery("#select-fan").change(function(){
               var fanId = jQuery(this).val();
               jQuery.ajax(function(){
                  url: "<?php echo $_SERVER["PHP_SELF"] ?>",
                  data: {ajaxId: fanId},
                  success: function(data){
                       selectBigFan = jQuery('<select>', {name: 'select-bigfan', id: 'select-bigfan'});
                       jQuery.each(data, function(bigFanId, bigFanText){
                            optionBigFan = jQuery('<option>' {value: bigFanId, text: bigFanText});
                            optionBigFan.appendTo(selectBigFan);
                       });
                       selectBigFan.insertAfter(jQuery('#select-fan'));
                       jQuery('<input>', {type: 'submit', value: 'send'});
                  }
               });
           });
       });

   </script>
<?php else : ?>
   <p>Yup, there's nothing here, do u have beer ?</p>
   <?php 
endif 
?>

Have a nice day


Here is your code with my changes to adapt to my page ...

    <?php

include"../bd_db/connection.php";

include"../bd_db/selection.php";
$id=$_GET['id'];
     echo $id;

if (isset($_POST) && !empty($_POST))
{
    if (array_key_exists('ajaxId', $_POST))
    {
         if ((int)$_POST['ajaxId'] <= 0)
         {
             throw new Exception("go to hell");
         }

         $query = "SELECT td.id_resultat as id, email.fld_nom_email AS name FROM " . $table_db . " td INNER JOIN " . $table_db_email email ON td.fld_email_id = email.id_email WHERE td.fld_email_id = " . $_POST['ajaxId']"; //!!!!! la ligne 19






         $result = mysql_query($query, $cnx);

     开发者_如何转开发    if (mysql_num_rows($result) > 0)
         {
              $data = array();
              while ($row=mysql_fetch_array($result))
              {
                   $data[$row["resultat"]] = $row["name"];
              }

              echo json_encode($data);
              exit();
         }
    }

    if (array_key_exists('select-big-fan', $_POST))
    {
        // you've found a big fan selected
    }
}

$query = "SELECT DISTINCT id_email AS id, fld_nom_email AS name, fld_adresse_email AS email FROM " . $table_db_email . " ORDER BY name";

$result = mysql_query($query, $cnx);

?>
<?php if (mysql_num_rows($result) > 0) : ?>
   <form id="form-fan" method="post" action="<?php echo $_SERVER["PHP_SELF"] ?>">
   <select name="select-fan" id="select-fan">
   <?php while ($row=mysql_fetch_array($result)) : ?>
      <option value="<?php echo $row["id"] ?>"><?php echo $row["name"] ?> - <?php echo $row["email"] ?></option>
   <?php endwhile ?>
   </select>
   </form>
   <script type="text/javascript">
       jQuery(document).ready(function(){
           jQuery("#select-fan").change(function(){
               var fanId = jQuery(this).val();
               jQuery.ajax(function(){
                  url: "<?php echo $_SERVER["PHP_SELF"] ?>",
                  data: {ajaxId: fanId},
                  success: function(data){
                       selectBigFan = jQuery('<select>', {name: 'select-bigfan', id: 'select-bigfan'});
                       jQuery.each(data, function(bigFanId, bigFanText){
                            optionBigFan = jQuery('<option>' {value: bigFanId, text: bigFanText});
                            optionBigFan.appendTo(selectBigFan);
                       });
                       selectBigFan.insertAfter(jQuery('#select-fan'));
                       jQuery('<input>', {type: 'submit', value: 'send'});
                  }
               });
           });
       });
});

   </script>
<?php else : ?>
   <p>Yup, there's nothing here, do u have beer ?</p>
<? php 
endif 
?>

I have an error message that comes from the line 18

Parse error: syntax error, unexpected T_STRING in C:\inetpub\wwwroot\byrd\apps_code_promotion\verification\edit.php on line 18

So I tried also with following line but I have same error message :

    $query = "SELECT td.id_resultat as id, email.fld_nom_email AS name FROM " . $table_db . " td INNER JOIN " . $table_db_email email ON td.fld_email_id = email.id_email WHERE td.fld_email_id = " . $_POST['ajaxId']"; //!!!!! la ligne 19


$query = "SELECT td.id_resultat as id, email.fld_nom_email AS name FROM " . $table_db . " td INNER JOIN " . $table_db_email email ON td.fld_email_id = email.id_email WHERE td.fld_email_id = . $_POST['ajaxId']; //!!!!! la ligne 19

why second option must be selected ? Because we have two screen (file) and one dropdown menu : On first screen, I display all information on user :

<?php

            $req=  " select tbl.id_resultat,schl.fld_school,tbl.fld_note,email.fld_nom_email,email.fld_adresse_email 

            FROM $table_db tbl 


            INNER JOIN $table_db_school schl
            ON tbl.fld_school_id = schl.fld_id_school

            INNER JOIN $table_db_email email
            ON tbl.fld_email_id = email.id_email";
            $rep =  mysql_query($req, $cnx) or die( mysql_error() ) ;



            while($row=mysql_fetch_row($rep)){
                $id_resultat=$row[0];
                $fld_school=$row[1];
                $fld_note=$row[2];
                $fld_nom_email=$row[3];
                $fld_adresse_email=$row[4];


                echo "<tr ><td>$id_resultat</td><td>
$fld_shool</td><td>
$fld_note</td><td>
$fld_nom_email</td><td>
$fld_adresse_email</td><td>
<a href=\"edit.php?id=$id_resultat\">to verify</a></td>
                </tr>"

?>

On the second screen, (when an user click on the link ''to verify'') he can see selected option in dropdown menu and if it's necesary, he can chose an other option (email...)

So how can I display the correct option selected with others option in my select ?


you can use ajax with jQuery and some js effects

EDIT :

(your_page.php)
<?php

...

if (isset($_POST) && !empty($_POST))
{
    if (array_key_exists('ajaxId', $_POST))
    {
         if ((int)$_POST['ajaxId'] <= 0)
         {
             throw new Exception("go to hell");
         }

         $query = "SELECT td.id_resultat as id, email.fld_name_email AS name FROM " . $table_db . " td INNER JOIN " . $table_db_email email ON td.fld_email_id = email.id_email WHERE td.id_email = " . $_POST['ajaxId'];
         $result = mysql_query($query, $cnx);

         if (mysql_num_rows($result) > 0)
         {
              $data = array();
              while ($row=mysql_fetch_array($result))
              {
                   $data[$row["resultat"]] = $row["name"];
              }

              echo json_encode($data);
              exit();
         }
    }

    if (array_key_exists('select-big-fan', $_POST))
    {
        // you've found a big fan selected
    }
}

$query = "SELECT DISTINCT id_email AS id, fld_name_email AS name, fld_adresse_email AS email FROM " . $table_db_email . " ORDER BY name";
$result = mysql_query($query, $cnx);

?>
<?php if (mysql_num_rows($result) > 0) : ?>
   <form id="form-fan" method="post" action="<?php echo $_SERVER["PHP_SELF"] ?>">
   <select name="select-fan" id="select-fan">
   <?php while ($row=mysql_fetch_array($result)) : ?>
      <option value="<?php echo $row["id"] ?>"><?php echo $row["name"] ?> - <?php echo $row["email"] ?></option>
   <?php endwhile ?>
   </select>
   </form>
   <script type="text/javascript">
       jQuery(document).ready(function(){
           jQuery("#select-fan").change(function(){
               var fanId = jQuery(this).val();
               jQuery.ajax(function(){
                  url: "<?php echo $_SERVER["PHP_SELF"] ?>",
                  data: {ajaxId: fanId},
                  success: function(data){
                       selectBigFan = jQuery('<select>', {name: 'select-bigfan', id: 'select-bigfan'});
                       jQuery.each(data, function(bigFanId, bigFanText){
                            optionBigFan = jQuery('<option>' {value: bigFanId, text: bigFanText});
                            optionBigFan.appendTo(selectBigFan);
                       });
                       selectBigFan.insertAfter(jQuery('#select-fan'));
                       jQuery('<input>', {type: 'submit', value: 'send'});
                  }
               });
           });
       });
});

   </script>
<?php else : ?>
   <p>Yup, there's nothing here, do u have beer ?</p>
<?php endif ?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜