#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    3
    Rep Power
    0

    if/then statements with php


    I'm having trouble trying to use an IF/THEN statement in my php code to query our ldap database based on the selected radio button & text entered into a textbox.

    For example,
    I have a textbox and 4 radio buttons. the 4 radio buttons are 'Department', 'Login', 'Last Name', 'Full Name'

    How do i construct an IF/THEN statement to tell the code which query to use, based on the selected radio button?

    Here is my code (which the query currently does not work):
    PHP Code:
    <html>
    <head>
    </head>

    <body>
    <form action="phonebook2.php" method="POST" target="_parent">
    <div align="center"> 
    <p><img src="phone.gif" width="650" height="62"></p>
    <hr width="90%">
    <p><font size="4"><strong>Faculty, Staff, & Students</strong></font></p>
    <p> 
    <input type="text" name="input" length="30">
    <br>
    <br>
    <input type="radio" name="choice" value="eiuedudepartment">Department 
    <br>
    <input type="radio" name="choice" value="uid">Login 
    <br>
    <input type="radio" name="choice" value="sn">Last Name&nbsp;
    <br>
    <input type="radio" name="choice" value="cn">Full Name&nbsp;
    </p>
    <p>
    <input type="submit" name="submit" value="Search"></p>
    </p>
    <hr width="90%">
    <p align="left"> 

    <?php

    // LDAP variables
    $ldaphost "??.??.?.??";

    // specify the LDAP server to connect to
    $ldapconn ldap_connect($ldaphost) or die("Could not connect to server");

    // bind to the LDAP server specified above 
    $ldapdn 'uid=stutech,ou=people,dc=eiu,dc=edu';
    $ldappass 'test2day';

    // bind to the LDAP server specified above 
    $ldapbind ldap_bind($ldapconn$ldapdn$ldappass) or die("Could not bind to server");

    // create the search string
    $ldapquery "(|(eiuedudepartment=" $_POST['eiuedudepartment'] . ")(uid=" $_POST['uid'] . ")(sn=" .
    $_POST['sn'] . ")(cn=" $_POST['cn'] . "))";

    // start searching
    // specify both the start location and the search criteria
    // in this case, start at the top and return all entries 
    $result ldap_search($ldapconn,"ou=people,dc=eiu,dc=edu"$ldapquery) or die("Error in search
    query"
    );

    // print number of entries found
    echo "Number of entries found: " ldap_count_entries($ldapconn$result) .
    "<p>";

    // get entry data as array
    $info ldap_get_entries($ldapconn$result);

    // iterate over array and print data for each entry
    for ($i=0$i<$info["count"]; $i++)
    {
    echo 
    "name: "$info[$i]["cn"][0] ."<br>";
    echo 
    "curriculum: "$info[$i]["eiueducurriculum"][0] ."<br>";
    echo 
    "office phone: "$info[$i]["eiueduofficephone"][0] ."<br>";
    echo 
    "office location: "$info[$i]["eiueduofficeaddress"][0] ."<br>";
    echo 
    "department: "$info[$i]["eiuedudepartment"][0] ."<br>";
    echo 
    "type: "$info[$i]["eiuedutype"][0] ."<br>";
    echo 
    "www: "$info[$i]["eiueduuserurl"][0] ."<br>";
    echo 
    "user id: "$info[$i]["uid"][0] ."<br>";
    echo 
    "e-mail: "$info[$i]["maillocaladdress"][0] ."<p>"; }

    // all done? clean up
    ldap_close($ldapconn);

    ?>
    </p>
    </div>
    </form>

    </body>
    </html>
    any suggestions? thanks in advance....
    Last edited by Viper_SB; October 18th, 2004 at 07:20 PM. Reason: Added Code tags
  2. #2
  3. Psycho Canadian
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Jan 2001
    Location
    Canada
    Posts
    4,846
    Rep Power
    635
    A switch statement should work nicely, remember that you need to retive $_POST['choice'] as that is the radio button.

    PHP Code:
    switch ($_POST['choice'])
    {
        case 
    'eiuedudepartment':
            
    $ldapquery "(eiuedudepartment=*)";
            break;
        case 
    'uid':
            
    $ldapquery "(uid=*)";
            break;
        case 
    'sn':
            
    $ldapquery "(sn=*)";
            break;
        case 
    'cn':
            
    $ldapquery "(cn=*)";
            break;

    If you're every unsure of what variables are being passed or how you can use this on the posted to page.
    PHP Code:
    print '<pre>';
    print_r($_POST); 
    It'll give you a full list of the $_POST variable.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    3
    Rep Power
    0

    if/then statements with php


    I put the code into my php, however, when I select a radio button, and enter text (according the radio button), the query retrieves every entry in the ldap directory, not just those entries based on the queries....

    any suggestions?

    here is my code again:
    PHP Code:
    <html>
    <head>
    </head>

    <body>
    <form action="phonebook2.php" method="POST" target="_parent">
      <div align="center"> 
      <p><img src="phone.gif" width="650" height="62"></p>
      <hr width="90%">
      <p><font size="4"><strong>Faculty, Staff, &amp; Students</strong></font></p>
        <p> 
          [b]<input type="text" name="input" length="30">
          <br>
          <br>
          <input type="radio" name="choice" value="eiuedudepartment">Department&nbsp
          <br>
          <input type="radio" name="choice" value="uid">Login&nbsp
          <br>
          <input type="radio" name="choice" value="sn">Last Name&nbsp;
          <br>
          <input type="radio" name="choice" value="cn">Full Name&nbsp;
        [/b]</p>
        <p>
          <input type="submit" name="submit" value="Search"></p>
          </p>
        <hr width="90%">
        <p align="left"> 
        
    <?php

    // LDAP variables
    $ldaphost "??.??.?.??";

    // specify the LDAP server to connect to
    $ldapconn ldap_connect($ldaphost) or die("Could not connect to server");

    // bind to the LDAP server specified above 
    $ldapdn 'uid=stutech,ou=people,dc=eiu,dc=edu';
    $ldappass 'test2day';

    // bind to the LDAP server specified above 
    $ldapbind ldap_bind($ldapconn$ldapdn$ldappass) or die("Could not bind to server");

    // create the search string
    [b]switch ($_POST['choice']) 

        case 
    'eiuedudepartment'
            
    $ldapquery "(eiuedudepartment=*)"
            break; 
        case 
    'uid'
            
    $ldapquery "(uid=*)"
            break; 
        case 
    'sn'
            
    $ldapquery "(sn=*)"
            break; 
        case 
    'cn'
            
    $ldapquery "(cn=*)"
            break; 

    [/
    b]

    // start searching
    // specify both the start location and the search criteria
    // in this case, start at the top and return all entries 

    $result ldap_search($ldapconn,"ou=people,dc=eiu,dc=edu"$ldapquery) or die("Error in search
    query"
    );

    // print number of entries found
    echo "Number of entries found: " ldap_count_entries($ldapconn$result) .
    "<p>";

    // get entry data as array
    $info ldap_get_entries($ldapconn$result);

    // iterate over array and print data for each entry
    for ($i=0$i<$info["count"]; $i++)
    {
        echo 
    "name: "$info[$i]["cn"][0] ."<br>";
        echo 
    "curriculum: "$info[$i]["eiueducurriculum"][0] ."<br>";
        echo 
    "office phone: "$info[$i]["eiueduofficephone"][0] ."<br>";
        echo 
    "office location: "$info[$i]["eiueduofficeaddress"][0] ."<br>";
        echo 
    "department: "$info[$i]["eiuedudepartment"][0] ."<br>";
        echo 
    "type: "$info[$i]["eiuedutype"][0] ."<br>";
        echo 
    "www: "$info[$i]["eiueduuserurl"][0] ."<br>";
        echo 
    "user id: "$info[$i]["uid"][0] ."<br>";
        echo 
    "e-mail: "$info[$i]["maillocaladdress"][0] ."<p>"; }

    // all done? clean up
    ldap_close($ldapconn);

    ?>
    </p>
    </div>
    </form>

    </body>
    </html>
    Last edited by Viper_SB; October 19th, 2004 at 11:43 AM. Reason: Added php tags
  6. #4
  7. Psycho Canadian
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Jan 2001
    Location
    Canada
    Posts
    4,846
    Rep Power
    635
    You need to change the queries to however you want them to be, mine were just for show I don't know how your server is setup. Also print out $_POST['choice'] and see what's in it.

    Please remember to use the [php] [/php] tags around your code, that way it's MUCH easier to see at a glance what might be wrong.

IMN logo majestic logo threadwatch logo seochat tools logo