www.webdeveloper.com
Results 1 to 5 of 5

Thread: Use date picker to create $variable for query

Hybrid View

  1. #1
    Join Date
    May 2012
    Posts
    11

    Exclamation Use date picker to create $variable for query

    Hi All,

    I'm hoping I can get some help with my code. I have been working on it for quite some time and I have gotten just about everything working.

    What the page currently does:
    #1 Queries my database to get a list of options for my drop-down menu
    #2 On selecting an option from the generated drop-down, the page reloads and queries my database, returning only users that meet the shifts from my drop-down.

    What I still what it to do:
    The very first thing I would like my form to do is to have a datepicker (already have one working on the page). When selecting a date, the date is pushed into the first query Point #1. This query will the select only options (shifts) that are available on that date.
    Once the shift is selected the query in Point #2 must only select users from the second table who are scheduled for that date, and the selected shift.

    Below is my complete code. The code below functions perfectly as describe in under the "What the page currently does" section.

    PHP Code:
    My page is attached
    Attached Files Attached Files

  2. #2
    Join Date
    Feb 2014
    Location
    Canada
    Posts
    155
    You haven't shown your date-picker so assuming it's a date form field, which would look something like:

    HTML Code:
    <form action = "script.php" method = "get">
         <input type = "date" name = "datePicker" />
         <input type = "submit" value = "Get shifts" />
    </form>
    Your PHP would be:

    PHP Code:
    $date "";
    if(isset(
    $_GET['datePicker'])) {
         
    $date $_GET['datePicker'];
    } else {
         
    $date "";
    }
    // sanitize $date if users can freely enter it
    $sql "... WHERE DATE(start) = $date ... 
    Also, in the <body> (which you have 2 of), does the page not load properly when you plainly write the HTML without being in php echo statements? If not, you can remove them as it makes it easier to read and debug for later use.

  3. #3
    Join Date
    May 2012
    Posts
    11
    Quote Originally Posted by Error404 View Post
    You haven't shown your date-picker so assuming it's a date form field, which would look something like:

    HTML Code:
    <form action = "script.php" method = "get">
         <input type = "date" name = "datePicker" />
         <input type = "submit" value = "Get shifts" />
    </form>
    Your PHP would be:

    PHP Code:
    $date "";
    if(isset(
    $_GET['datePicker'])) {
         
    $date $_GET['datePicker'];
    } else {
         
    $date "";
    }
    // sanitize $date if users can freely enter it
    $sql "... WHERE DATE(start) = $date ... 
    Also, in the <body> (which you have 2 of), does the page not load properly when you plainly write the HTML without being in php echo statements? If not, you can remove them as it makes it easier to read and debug for later use.
    Thanks I'm gonna plug this in and see how it works. One question though. One selecting the drop-down menu my page reloads as it queries the database. will this not effect the date selected? I just want to be sure.

    The page does load better without the echoing, I just thought the echoing was the better way to do it.

  4. #4
    Join Date
    May 2012
    Posts
    11
    Oh yes sorry I forgot to mention, yes it is a regular datepicker. I'm using the Zebra datepicker.

    And where in my page do I add the code you gave me?

    thanks.

  5. #5
    Join Date
    May 2012
    Posts
    11

    Unhappy

    Getting these two errors when plugging the code in:

    Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\page1.php on line 20

    Fatal error: Call to a member function fetchAll() on a non-object in C:\xampp\htdocs\page1.php on line 121
    Line 20 refers to this function:
    Code:
    function createOptions($optionList, $selectedValue)
    {
        $options = '';
        foreach ($optionList as $option)
        {
            $selected = ($option['value']==$selectedValue) ? ' selected="selected"' : '';
            $options .= "<option value='{$option['value']}'{$selected}>{$option['label']}</option>\n";
        }
        return $options;
    }
    
    function db_result_to_array($result)
    {
        for ($count=0; $row = $result->fetch_assoc(); $count++) {
            $res_array[$count]=$row;
        }
        return $res_array;
    }
    LINE 121 refers to the second query:
    Code:
        $sql = "
                                                    SELECT 
                                                                                    DISTINCT(agent)	
                                                    ,                               agent_id 
                                                    FROM 
                                                                                    schedule  
                                                    WHERE 
                                                                                    DATE(start) = $date
                                                    AND        						shift = $shift
                                                    AND                        		agent_id != 0												
                                                    ORDER BY 
                                                                                    agent ASC";
        $query = $db -> query ($sql);
        $result = $query -> fetchAll (PDO::FETCH_ASSOC);

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles