Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Displaying search results with php mysql javascript

Options
  • 03-04-2009 3:54pm
    #1
    Registered Users Posts: 13,746 ✭✭✭✭


    Hi, I will explain this the best I can, I want to display the data as follows,
    I search dt205 year 2 calculus/analysis in our drop downs
    I want it to display

    results:
    dt205 year 2 calculus/analysis semester 2 2006/2007 and Link.

    So far I cant get it to display anything like above, tried a few ways but no joy.

    below are the tables I am using and the code which propmts the user to pick a course code, year and subject using dropdowns.
    dropdowns are dependant eachother.
    ie selection of course year depends on course code picked etc

    The database is exampapers,

    the tables are:

    elements

    course_code course_year subject sitting year link
    DT205 2 Calculus/Analysis Semester 2 2006/2007Click here
    DT205 3 Classical Mechanics Semester 1 2006/2007Click here
    DT205 3 Classical Mechanics Semester 1 2007/2008Click here
    DT205 4 Integral Equations Semester 2 2006/2007Click here
    DT205 4 Differential Equations Semester 1 2006/2007Click here
    DT205 4 Differential Equations Semester 1 2007/2008Click here
    DT205 1 Computer Architecture Semester 1 2008/2009

    link will be with the rest of the headings, jus not enough space :D

    coursecodetable


    courseid course_code
    1 dt205
    2 dt008



    courseyeartable

    yearid courseid course_year
    1 1 1
    2 1 2
    3 1 3
    4 1 4
    5 2 1
    6 2 2
    7 2 3


    The code is:

    <?php

    $programme = $progyear = $subjectname = $examyear = null; //declare vars

    $conn= mysql_connect("localhost", "root", "pwrd");
    $db = mysql_select_db('exampapers',$conn);

    if(isset($_GET["programme"]) && is_numeric($_GET["programme"]))
    {
    $programme = $_GET["programme"];
    }

    if(isset($_GET["progyear"]) && is_numeric($_GET["progyear"]))
    {
    $progyear = $_GET["progyear"];
    }

    if(isset($_GET["subjectname"]) && is_numeric($_GET["subjectname"]))
    {
    $subjectname = $_GET["subjectname"];
    }

    if(isset($_GET["examyear"]) && is_numeric($_GET["examyear"]))
    {
    $examyear = $_GET["examyear"];
    }

    ?>

    <script language="JavaScript">

    function autoSubmit()
    {
    var formObject = document.forms;
    formObject.submit();
    }

    </script>

    <form name="theForm" method="get">



    <select name="programme" onChange="autoSubmit();">
    <option value="null"></option>
    <option value="1" <?php if($programme == 1) echo " selected"; ?>>DT205</option>
    <option value="2" <?php if($programme == 2) echo " selected"; ?>>DT008</option>
    </select>

    <br><br>



    <?php

    if($programme != null && is_numeric($programme))
    {

    ?>

    <select name="progyear" onChange="autoSubmit();">
    <option value="null"></option>

    <?php

    //POPULATE DROP DOWN MENU WITH course_year FROM A GIVEN course_code

    $sql = "SELECT DISTINCT yearid, course_year FROM courseyeartable WHERE courseid = $programme";
    $progyears = mysql_query($sql, $conn);

    while($row = mysql_fetch_array($progyears))
    {
    echo ("<option value=\"$row[yearid]\" " . ($progyear == $row["yearid"] ? " selected" : "") . ">$row[course_year]</option>");
    }

    ?>

    </select>

    <?php

    }

    ?>

    <br><br>

    <?php

    if($progyear != null && is_numeric($progyear) && $programme != null)
    {

    ?>

    <select name="subjectname" onChange="autoSubmit();">
    <option value="null"></option>

    <?php

    //POPULATE DROP DOWN MENU WITH subjects FROM A GIVEN course_code, course_year

    $sql = "SELECT DISTINCT subject from elements WHERE course_year = $progyear ";
    $subjects = mysql_query($sql,$conn);

    while($row = mysql_fetch_array($subjects))
    {
    echo ("<option value=\"$row[subject]\" " . ($subjects == $row["subject"] ? " selected" : "") . ">$row[subject]</option>");
    }

    ?>

    </select>

    <?php

    }

    ?>


    </form>


Comments

  • Registered Users Posts: 13,746 ✭✭✭✭Misticles


    anyone know about this or yas all too lazy to read it :D


  • Registered Users Posts: 26,579 ✭✭✭✭Creamy Goodness


    us programmers like to take our weekends off :P but since i'm doing my FYP i don't get weekend breaks :P.

    without looking too much into your code, you should always echo out the $sql variable right before you execute it and then run that query on the mysql command line to make sure it returns the results that you expect, also make sure your $GET variables are being assigned properly.


  • Users Awaiting Email Confirmation Posts: 351 ✭✭ron_darrell


    At first glance, allowing that you want to do it this way which I'm not sure is the best way to achieve the results you're looking for, you have problems with the <form> tag and with your javascript (there may also be errors in your php (beyond mixing html and php which is not advised))

    First change document.forms('theForm') to document.getElementById('theForm') (which is a terrible name for your form btw) - avoid browser specific code and try and design your javascript using the DOM methodology where possible.

    Secondly, your form has a method but no action. I think what you're trying to do is get the page to perform your action (which is fine - just not the way you're trying to do it).

    This smells of "homework/copy and paste from google" to me. Try planning out what you want your code to do before you write/copy and paste it. Less bug testing that way (plus you'll understand what's happening).

    Regards,
    RD


  • Registered Users Posts: 13,746 ✭✭✭✭Misticles


    I take offence to that suggested copy nd paste.
    thats take hours of toil for a non epxerienced person to do!!

    its not homework either- personal project.

    thanks though :D


Advertisement