SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2004
    Location
    Telford, UK.
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Drop Down menu in php from Mysql - How?

    How do I make a drop down menu from data stored in a mysql database? Sorry if dumb question but I've no idea - can't find it in my books!

    Also, how do I get the selected item from the drop down menu into a string to forward to another webpage?

    This is my code so far (the drop down lists have the information stored in them at moment because I'm unsure how to get the menu from the database yet. I want the string to come from one of the two drop down lists.

    PHP Code:
     <?php
    session_start
    (); 
    if(
    $_SESSION['login'] != 'Logged') { 
    header("Location: index.php"); 


    // OPEN THE DATABASE CONNECTION
    $conn=mysql_connect ("localhost""user""password") or die ('I cannot connect to the database because: ' mysql_error());

    // PICK THE DATABASE TO USE
    mysql_select_db ("database",$conn); 
      
    // CHECK IF THE FORM IS SUBMITTED
      
    if(isset($_POST['submit_form']))


    // carry on.

          
    {
             
    $sql 'SELECT id, name, pin, pin2, station FROM clinician WHERE';
             
    $sql .= ' Clinician_pin = \'' $pin '\'';
             
    $sql .= ' OR ';
             
    $sql .= ' Clinician_pin = \'' $pin2 '\'';
             
    $sql .= ' OR ';
             
    $sql .= ' Clinician_name = \'' $name '\'';
          
    $login_check mysql_query($sql);
          
          if (
    false === $login_check)
          {
              
    $error_msg .= $sql ' failed due to ' mysql_error() . '<br />'
          }
          else
          {
            
    // get the rows from the database
            
    $login_rows mysql_num_rows($login_check);


             
    // there should be only 1 row in the database for this username / password combination
            
    if(== $login_rows)
            {
    // DEFINE SESSION VARIABLES
         
          
    $row mysql_fetch_assoc($login_check); 
          
    $_SESSION['clinician_id'] = $row['id'];
          
    $_SESSION['clinician_name'] = $row['name']; 
          
    $_SESSION['clinician_pin'] = $row['pin'];
          
    $_SESSION['clinician_station'] = $row['station'];


    // REDIRECT
              
    header('Location: clinician_checks2.php');
              exit;


    ?>

    <html>
    <body bgcolor="#99CCFF">
    <p align="center"><font size="7" face="Arial Narrow">WMAS - </font><i><b><font size="7" face="Papyrus">mk</font></b></i><font size="7" face="Arial Narrow">
    PORTAL</font></p>
    <p align="center"><font size="6">Wolverhampton</font></p>

    <p align="center"><font size="4" face="Arial">PRF Checks - by Clinician</font></p>
    <p align="center"><font face="Arial">Select Clinician&nbsp;</font></p>

    <form name="submit_data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
      <p align="center">Select by PIN:&nbsp; <select size="1" name="Clinician_pin">
        <option>TW00818</option>
        <option>PA13831</option>
      </select>&nbsp; <input type="hidden" name="submit_form">
      <input type="submit" name="submit" value="submit" label="submit">

      </p>

    </form>

    <form name="submit_data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
      <p align="center">Select by Name: <select size="1" name="Clinician_name">
        <option>Kilvert, Matt</option>
      </select>&nbsp; <input type="hidden" name="submit_form">
      <input type="submit" name="submit" value="submit" label="submit">
      </p>
    </form>

    </body>

    </html>
    This code gives me a
    Parse error: parse error, unexpected $ in /home/kilvert/public_html/pictures/WMAS/clinician_checks.php on line 87

  2. #2
    SitePoint Guru babyboy808's Avatar
    Join Date
    Nov 2004
    Location
    dublin
    Posts
    602
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    loop the data out of the db, once you have the variables, insert them into the dropdown...

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2004
    Location
    Telford, UK.
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure exactly how to do that but had a go and this is what i get?

    PHP Code:
    <?php
    session_start
    (); 
    if(
    $_SESSION['login'] != 'Logged') { 
    header("Location: index.php"); 


    // OPEN THE DATABASE CONNECTION
    $conn=mysql_connect ("localhost""username""password") or die ('I cannot connect to the database because: ' mysql_error());

    // PICK THE DATABASE TO USE
    mysql_select_db ("database",$conn); 
      
    // CHECK IF THE FORM IS SUBMITTED
      
    if(isset($_POST['submit_form']))


            {
    // DEFINE SESSION VARIABLES
         
          
    $row mysql_fetch_assoc($login_check); 
          
    $_SESSION['clinician_id'] = $row['id'];
          
    $_SESSION['clinician_name'] = $row['name']; 
          
    $_SESSION['clinician_pin'] = $row['pin'];
          
    $_SESSION['clinician_station'] = $row['station'];


    // REDIRECT
              
    header('Location: clinician_checks2.php');
              exit;


    ?>

    <html>
    <body bgcolor="#99CCFF">
    <p align="center"><font size="7" face="Arial Narrow">WMAS - </font><i><b><font size="7" face="Papyrus">mk</font></b></i><font size="7" face="Arial Narrow">
    PORTAL</font></p>
    <p align="center"><font size="6">Wolverhampton</font></p>

    <p align="center"><font size="4" face="Arial">PRF Checks - by Clinician</font></p>
    <p align="center"><font face="Arial">Select Clinician&nbsp;</font></p>

    <form name="submit_data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
      <p align="center">Select by PIN:&nbsp; <select size="1" name="Clinician_pin">

    // LOOP DATA FROM DB  
      <?
    $i
    =0;
    while (
    $i $num) {


    $clinician_pin=mysql_result($result,$i,"pin");
    ?>
        <option>$clinician_pin</option>
    <?
    $i
    ++;
    ?>
      </select>&nbsp; <input type="hidden" name="submit_form">
      <input type="submit" name="submit" value="submit" label="submit">

      </p>

    </form>

    </body>

    </html>
    But this gives me
    Parse error: parse error, unexpected $ in /home/kilvert/public_html/pictures/WMAS/TESTclinician_checks.php on line 68
    But as far as I know i only have 67 lines!!! Any ideas where the PARSE error is?

  4. #4
    SitePoint Guru
    Join Date
    Aug 2003
    Location
    CT
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When ever you have a parse error after your code, it means you forgot to close something like a } or a ;

    That said, I would re-arrange your code. This html/php mixing stuff needs to stop :P


    thorw this:

    Code:
      <?
    $i=0;
    while ($i < $num) {
    
    
    $clinician_pin=mysql_result($result,$i,"pin");
    ?>
        <option>$clinician_pin</option>
    <?
    $i++;
    } ?>

    back up top with the rest of your php, and simple make your option sets a variable. (and it helps if you set a value)


    Code:
      <?
    $i=0;
    
    $myoptions = "";
    
    
    $clinician_pin=mysql_result($result,$i,"pin");
    
    while ($i < $num) {
     $myoptions .= "<option value=\".$clinician_pin."\">". $clinician_pin ."</option>\r\n";
    
    $i++;
    } ?>
    and then:

    Code:
    <select>
    <?php echo $myoptions; ?>
    </select>
    in your markup. it makes things so much easier to clean up and look at, and remember, php parses on the server long before there's even any html to spit out. Ideally, you want ALL php functions to be done before you even spit out the very first <html> opener tag.
    My Blog: SkeyMedia.com

  5. #5
    SitePoint Zealot
    Join Date
    Dec 2004
    Location
    Telford, UK.
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey thanks briansol - I'll remember to keep all the php together - thanks for that tip!!

    What's a "markup" ?? Sorry!

    I've tried to change things around and now getting this - which has really confused me now!!

    Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /home/kilvert/public_html/pictures/WMAS/TESTclinician_checks.php on line 40

    Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/kilvert/public_html/pictures/WMAS/TESTclinician_checks.php on line 40
    This is my new script (for what it's worth!!) ha ha.

    PHP Code:
     <?php
    session_start
    (); 
    if(
    $_SESSION['login'] != 'Logged') { 
    header("Location: index.php"); 


    // OPEN THE DATABASE CONNECTION
    $conn=mysql_connect ("localhost""user""password") or die ('I cannot connect to the database because: ' mysql_error());

    // PICK THE DATABASE TO USE
    mysql_select_db ("db",$conn); 
      
    // CHECK IF THE FORM IS SUBMITTED
      
    if(isset($_POST['submit_form']))


            {
    // DEFINE SESSION VARIABLES
         
          
    $row mysql_fetch_assoc($login_check); 
          
    $_SESSION['clinician_id'] = $row['id'];
          
    $_SESSION['clinician_name'] = $row['name']; 
          
    $_SESSION['clinician_pin'] = $row['pin'];
          
    $_SESSION['clinician_station'] = $row['station'];


    // REDIRECT
              
    header('Location: clinician_checks2.php');
              exit;


    $i=0;

    $myoptions "";


    $clinician_pin=mysql_result($result,$i,"pin");

    while (
    $i $num) {
     
    $myoptions .= "<option value=\".$clinician_pin."\">"$clinician_pin ."</option>\r\n";

    $i++;


    ?>

    <html>
    <body bgcolor="#99CCFF">
    <p align="center"><font size="7" face="Arial Narrow">WMAS - </font><i><b><font size="7" face="Papyrus">mk</font></b></i><font size="7" face="Arial Narrow">
    PORTAL</font></p>
    <p align="center"><font size="6">Wolverhampton</font></p>

    <p align="center"><font size="4" face="Arial">PRF Checks - by Clinician</font></p>
    <p align="center"><font face="Arial">Select Clinician&nbsp;</font></p>

    <form name="submit_data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
      <p align="center">Select by PIN:&nbsp; <select size="1" name="Clinician_pin">

    <select>
    <?php echo $myoptions?>
    </select>

     
      </select>&nbsp; <input type="hidden" name="submit_form">
      <input type="submit" name="submit" value="submit" label="submit">

      </p>

    </form>

    </body>

    </html>

    Is it something to do with this line? maybe the speech marks?!?!

    PHP Code:
      $myoptions .= "<option value=\".$clinician_pin."\">"$clinician_pin ."</option>\r\n"

  6. #6
    SitePoint Zealot
    Join Date
    Dec 2004
    Location
    Telford, UK.
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ha ha!! I'm learning quickly!!

    I removed one of the speech marks and it was better - although now I'm getting this....

    Parse error: parse error, unexpected $ in /home/kilvert/public_html/pictures/WMAS/TESTclinician_checks.php on line 74
    This is revised script (but no line 74!)

    PHP Code:
     <?php
    session_start
    (); 
    if(
    $_SESSION['login'] != 'Logged') { 
    header("Location: index.php"); 


    // OPEN THE DATABASE CONNECTION
    $conn=mysql_connect ("localhost""user""password") or die ('I cannot connect to the database because: ' mysql_error());

    // PICK THE DATABASE TO USE
    mysql_select_db ("db",$conn); 
      
    // CHECK IF THE FORM IS SUBMITTED
      
    if(isset($_POST['submit_form']))


            {
    // DEFINE SESSION VARIABLES
         
          
    $row mysql_fetch_assoc($login_check); 
          
    $_SESSION['clinician_id'] = $row['id'];
          
    $_SESSION['clinician_name'] = $row['name']; 
          
    $_SESSION['clinician_pin'] = $row['pin'];
          
    $_SESSION['clinician_station'] = $row['station'];


    // REDIRECT
              
    header('Location: clinician_checks2.php');
              exit;


    $i=0;

    $myoptions "";


    $clinician_pin=mysql_result($result,$i,"pin");

    while (
    $i $num) {
     
    $myoptions .= "<option value=\".$clinician_pin.\">"$clinician_pin ."</option>\r\n";

    $i++;


    ?>

    <html>
    <body bgcolor="#99CCFF">
    <p align="center"><font size="7" face="Arial Narrow">WMAS - </font><i><b><font size="7" face="Papyrus">mk</font></b></i><font size="7" face="Arial Narrow">
    PORTAL</font></p>
    <p align="center"><font size="6">Wolverhampton</font></p>

    <p align="center"><font size="4" face="Arial">PRF Checks - by Clinician</font></p>
    <p align="center"><font face="Arial">Select Clinician&nbsp;</font></p>

    <form name="submit_data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
      <p align="center">Select by PIN:&nbsp; <select size="1" name="Clinician_pin">

    <select>
    <?php echo $myoptions?>
    </select>

     
      </select>&nbsp; <input type="hidden" name="submit_form">
      <input type="submit" name="submit" value="submit" label="submit">

      </p>

    </form>

    </body>

    </html>

  7. #7
    SitePoint Zealot
    Join Date
    Dec 2004
    Location
    Telford, UK.
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, sort of getting there but really can't get the list right. Can someone have a look at this and see what is going wrong please?

    It shows the right amount of options (records from database) but it only shows the first data record that many times!

    ie There are 3 records in the table and the first record is TW818 - so TW818 is shown 3 times instead of the desired TW818, TW819 etc

    PHP Code:
    <?php
    session_start
    (); 
    if(
    $_SESSION['login'] != 'Logged') { 
    header("Location: index.php"); 
    }

    // OPEN THE DATABASE CONNECTION
    $conn=mysql_connect ("localhost""user""password") or die ('I cannot connect to the database because: ' mysql_error());

    // PICK THE DATABASE TO USE
    mysql_select_db ("database",$conn); 

    // SELECT DATA FROM DATABASE
    $query="SELECT * FROM clinician";
    $result=mysql_query($query);

    $num=mysql_numrows($result);

    mysql_close();

    $i=0;

    $myoptions "";


    $clinician_pin=mysql_result($result,$i,"pin");

    while (
    $i $num) {
     
    $myoptions .= "<option value=\".$clinician_pin.\">"$clinician_pin ."</option>\r\n";

    $i++;
    }    







    ?>

    <html>
    <body bgcolor="#99CCFF">
    <p align="center"><font size="7" face="Arial Narrow">WMAS - </font><i><b><font size="7" face="Papyrus">mk</font></b></i><font size="7" face="Arial Narrow">
    PORTAL</font></p>
    <p align="center"><font size="6">Wolverhampton</font></p>

    <p align="center"><font size="4" face="Arial">PRF Checks - by Clinician</font></p>
    <p align="center"><font face="Arial">Select Clinician&nbsp;</font></p>

    <form name="submit_data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
      <p align="center">Select by PIN:&nbsp; 
      
      <select size="1" name="Clinician_pin"><?php echo $myoptions?></select>&nbsp; 
      <input type="hidden" name="submit_form">
      <input type="submit" name="submit" value="submit" label="submit">

      </p>

    </form>




    <form name="submit_data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
      <p align="center">Select by Name: <select size="1" name="Clinician_name">
        <option><?PHP echo $dropname ?></option>
      </select>&nbsp; <input type="hidden" name="submit_form">
      <input type="submit" name="submit" value="submit" label="submit">
      </p>
    </form>

    </body>

    </html>

  8. #8
    SitePoint Member Jion72's Avatar
    Join Date
    Apr 2006
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This line

    $clinician_pin=mysql_result($result,$i,"pin");

    has to be inside the

    while ($i < $num) {

    }
    as then the $i variable gets a different value each time, if not it is reading $i always
    as 0

    I hope it works for you now

  9. #9
    SitePoint Zealot
    Join Date
    Dec 2004
    Location
    Telford, UK.
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perfect - thanks loads!!

  10. #10
    SitePoint Zealot
    Join Date
    Dec 2004
    Location
    Telford, UK.
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Problem now is what value is the form sending? And to where?

    I want the select item from the drop down menu to be a string in the re-directed webpage.

    But the redirected webpage isn't picking it up?

    PHP Code:
     <?php
    session_start
    (); 
    if(
    $_SESSION['login'] != 'Logged') { 
    header("Location: index.php"); 

    ?>

    <html>
    <body bgcolor="#99CCFF">
    <p align="center"><font size="7" face="Arial Narrow">WMAS - </font><i><b><font size="7" face="Papyrus">mk</font></b></i><font size="7" face="Arial Narrow">
    PORTAL</font></p>
    <p align="center"><font size="6">Wolverhampton</font></p>

    <p align="center"><font size="4">PRF Checks - by Clinician</font></p>
    <p align="center"><font size="3"><?php echo $_SESSION['name'?></font></p>


    <p align="center">How do you want the data reported?</p>
    <p align="center">&nbsp;</p>

    <ul>
      <li><p align="center" style="line-height: 150%"><a href="checks_output1.php">View ALL
        Checks</a></li>
      <li><p align="center" style="line-height: 150%"><a href="checks_output2.php">View Checks
        between Specific Dates</a></li>
      <li><p align="center" style="line-height: 150%"><a href="checks_output3">View ALL
        Checks with &lt;90%</a></li>
    </ul>

    <p align="left">&nbsp;</p>
    <p align="center">&nbsp;</p>
    <p align="center">&nbsp;</p>


    </body>

    </html>


Bookmarks

Posting Permissions

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