SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Jun 2000
    Location
    Netherlands
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,

    I have a searchform where i want to be able to search for persons who are
    married and display the selected records in a table. That's going o.k.
    But if i don't check the checkbox, there has to be a 'zero' value so that
    all the records in the database will be returned. What am i doing wrong (or
    what am i missing) in the following code to produce such a form?
    Thanks in advance,

    Joep
    ------------------------------------------------
    <html>
    <body>
    <?
    $db = mysql_connect("localhost", "username", "password");
    mysql_select_db("database",$db);

    if ($submit)
    for($i=0;$i<count($married);$i++)
    {
    $sql = "SELECT * FROM employees WHERE married = '$married'";
    $result = mysql_query($sql);

    echo "<table border=1>\n";
    echo
    "<tr><td>Name</td><td>Adres</td><td>Children</td><td>Income</td></tr>\n";
    while ($myrow = mysql_fetch_array($result)) {
    printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n",
    $myrow[Name], $myrow[Adres], $myrow[Children], $myrow[Income]);
    }}

    ?>

    <P>
    <form method="post" action="<? echo $PHP_SELF?>">
    Married? <input type=checkbox name="married" value=y> Yes <br>
    <input type="Submit" name="submit" value="Search">
    </form>
    </body>
    </html>


  2. #2
    SitePoint Zealot
    Join Date
    Nov 1999
    Location
    Manhattan!
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When I do checkboxes in php I give them the value=1. Then I insert the value into tinyint field with default=null.

    When I get the data from the table it is a simple (assume field name for the checkbox is called married):

    echo "<checkbox name='married' value='1'";
    if($data['married']) echo "checked>";
    else echo ">";

    Christian


    ------------------
    Axie automatically performs free, customizable searches for new
    websites and delivers the links to members by personalized email.
    http://www.axie.com/

  3. #3
    SitePoint Member
    Join Date
    Jun 2000
    Location
    Netherlands
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,

    Thanks for the reply. On your way (change the ENUM <which i had to generate yes / no> to a tynint i do get other outpout than i had before. Now i get the records with value=1 (if checked) and the value=0 (if unchecked) out of my database. But what i can't figure out:
    Lets say, i've got 4 records (people) in the database. Two of them are married. The searchresults i want are as following: when i check the box there are coming out 2 records (because 2 people are married) If the box is unchecked, i want to have all the people shown. (4 people). Is that possible??
    Why do i have to use a tynint? (I prefer the ENUM with the values 'yes' 'no', is that possible?)
    Thanks in advance,

    Joep van Schaik

  4. #4
    SitePoint Zealot
    Join Date
    Nov 1999
    Location
    Manhattan!
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use tinyint, because php uses 1 as Boolean True and 0 as Boolean False.

    Why not just do the following (the checkbox is called married and value=1):

    if($married)
    $sql = "SELECT * FROM my_table WHERE married=1";
    else
    $sql = "SELECT * FROM my_table";

    $result = mysql_query($sql);

    while($data = mysql_fetch_array($result)) {
    echo "Name = ". $data['name'] ."<br>";
    echo "Status = ";
    if($data['married'])
    echo "married<br>";
    else
    echo "single<br>";

    ...
    ...
    ...
    }



    ------------------
    Axie automatically performs free, customizable searches for new
    websites and delivers the links to members by personalized email.
    http://www.axie.com/

  5. #5
    SitePoint Member
    Join Date
    Jun 2000
    Location
    Netherlands
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,

    I'm thinking and trying but i can't work it out. Your given example works fine but only with one checkbox (?). I am trying to get the following: let's say i want to see a person who is 'married','non smoking' and has its 'driverslicence'. (3x checkbox with values 'yes'/'no')
    If one of the checkboxe is checked it only has to filter the table on the selected value. (so ignore the other two). When two boxes are selected, it has to filter the table with two values (like 'yes' and 'yes') but ignore the third one. Can you follow me?
    I've searched almost all (?) the tutorials on the web but can't find anything familiair with this. Maybe you have valuable links for me?
    Thanks in advance,

    Joep van Schaik
    The Netherlands


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
  •