I am total newbie and need help on Mysql search in PhP

Discussion in 'PHP' started by hitesh123, Aug 21, 2007.

  1. #1
    someone sugested this forum to get solution
    and I am trying :)

    I am total newbie and need some expert help

    We have a forum (Php) for our school cadets and want to know how to make available in search

    cadets in age range.
    in our forum database Mysql there is no feild of age. but it out puts age of cadet through a code
    which calculates cadet's age from db feilds cadet_birthday,
    cadet_birthmonth and cadet_birthyear

    and in profile it says
    cadet
    name: lalit
    age : 16
    gendre : male
    class: 8th

    can you tell me how to make a mysql search code for searching cadets for a particular range like e.g.

    search cadets

    gender=emale
    age= 10 to 15

    php code :
    <?php
    $max_results = 10;
    $from = (($page * $max_results) - $max_results);

    $get_results = mysql_query("SELECT * FROM cadetforum_users WHERE cadet_gender='".$_GET['gender']."' AND cadet_class='".$_GET['class']."' AND cadet_rank='".$_GET['rank']."' AND cadet_section='".$_GET['section']."' AND active_status='".$_GET['online']."' AND profile_picture='".$_GET['picture']."' LIMIT $from, $max_results");

    ?>


    Regards,

    hitesh
     
    hitesh123, Aug 21, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    What format is the birthday stored in? UNIX time stamp? If you're unsure, can you post an example of a database entry?
     
    nico_swd, Aug 22, 2007 IP
  3. ssanders82

    ssanders82 Peon

    Messages:
    77
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I think he means birthday, birthmonth, and birthyear are separate integer fields in the database. Is this correct?

    Have a look at mysql's date functions:

    dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html
     
    ssanders82, Aug 23, 2007 IP
  4. jakomo

    jakomo Well-Known Member

    Messages:
    4,262
    Likes Received:
    82
    Best Answers:
    0
    Trophy Points:
    138
    #4
    jakomo, Aug 23, 2007 IP
  5. hitesh123

    hitesh123 Guest

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    yes its like that i viewed db inphpmy admin
    it is like

    id
    cadet_fname
    cadet_lname
    cadet_gender
    cadet_birthday
    cadet_birthmonth
    cadet_birthyear
    cadet_class
    cadet_section

    but on "cadet_age" entry

    but in cadets profile output it shows cadets age
    like I said earlier.

    what we need is to search cadets on gender and age base.
    and list on (separate pages) boy cadets and girl cadets
    like section wise
    e.g.

    domainname/forum/cade_boys.php
    BOY CADETS
    +-----+---------+---------+--------+----------+----------+
    id gender name age class section
    +-----+---------+---------+--------+----------+----------+
    1 male rohit 12 8 A
    +-----+---------+---------+--------+----------+----------+
    2 male kamal 14 9 C
    +-----+---------+---------+--------+----------+----------+

    domainname/forum/cade_girls.php

    GIRL CADETS
    +-----+---------+---------+--------+----------+----------+
    id gender name age class section
    +-----+---------+---------+--------+----------+----------+
    5 female sonia 12 8 A
    +-----+---------+---------+--------+----------+----------+
    12 female Neetal 13 9 C
    +-----+---------+---------+--------+----------+----------+

    a descreptive help will be appreciated.

    One more thing I was checking my email's inbox
    for any reply to or notification on this topic but it
    was not mailed to me. how can i set it in this forum that if anybody
    answers this querry and that answer or atleast a message should be sent to my email address to check this topic.
     
    hitesh123, Aug 28, 2007 IP
  6. Patrick_M

    Patrick_M Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Hi,

    I'm still not sure I totally understand how the age thing is working in your
    database, from this post, it looks as though you're saying the ages/years are
    already added in the database like: cadet_birthyear = 1977 as apposed to using
    a date or timestamp.

    Anyway, here's how I would start working with what you're looking for:

    
    <?php
    $q = "SELECT * FROM YourDBTable WHERE cadet_gender = '$genderVar' AND cadet_birthyear BETWEEN '$startyear' AND '$endyear'";
    $r = mysql_query($q) OR DIE (mysql_error());
    
    while($a = mysql_fetch_array($r)){
      echo $a[cadet_gender]." ".$a[cadet_birthyear];  //etc,etc..
    }
    ?>
    
    Code (markup):
    That should get you started.

    You can setup your HTML table (if you're going to use one) within your
    while loop and have it built dynamically.

    You can also go further using the "BETWEEN" like:

     BETWEEN '$startyear' AND '$endyear' AND month BETWEEN '$monthbegin' AND '$monthend'
    Code (markup):
    Hope this helps,

    Patrick
     
    Patrick_M, Aug 28, 2007 IP
  7. hitesh123

    hitesh123 Guest

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    here is the html form

    [CODE]<form name="cadetsearch" action="search.php" method=post>
    gender<br><input type=text name=gender value="" size=20> <br>
    class<br><select name=class value="">
    <option value="Not specified">Select</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
    <option value="11">11</option>
    <option value="12">12</option></select>
    <br>Rank<br><select name=rank value="">
    <option value="Not specified">Select</option>
    <option value="sargent">sargent</option>
    <option value="senior">senior</option>
    <option value="junior">junior</option></select>
    <br>
    
    <br>Age From<br><select name=agef value="">
    <option value="Not specified">Select</option>
    <option value="10">10</option>
    <option value="11">11</option>
    <option value="12">12</option>
    <option value="13">13</option>
    <option value="14">14</option>
    <option value="15">15</option>
    <option value="16">16</option>
    <option value="17">17</option>
    <option value="18">18</option>
    </select>
    <br>Age To<br><select name=aget value="">
    <option value="Not specified">Select</option>
    <option value="10">10</option>
    <option value="11">11</option>
    <option value="12">12</option>
    <option value="13">13</option>
    <option value="14">14</option>
    <option value="15">15</option>
    <option value="16">16</option>
    <option value="17">17</option>
    <option value="18">18</option>
    </select><br>
    <input type=submit name=submit value="SUBMIT" size=20>
    </form>[/CODE]
    HTML:
    I figured the script is calculating a relation with current date to the
    cadet_birthyear, I think.

    but how to implement above form to search varialble
    age from "$agef" to age to "$aget"
    from MySQL DB
     
    hitesh123, Aug 30, 2007 IP
  8. hitesh123

    hitesh123 Guest

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Plz someone help me in this,
    I an newbie and its difficult for me but I don't think its that
    difficult for an expert Php Mysql programmer.

    if script can out put age of cadet in relation to year of birth then
    a script can categorise cadests in search based on
    the age and gender too. But I am not finding any solution to that
    by myself.
     
    hitesh123, Sep 2, 2007 IP