Page 1 of 1

how to get values from dropdown list

Posted: Thu Dec 24, 2009 1:45 pm
by spirala
hi, I hope someone will be kind and help me with my problem. Here is the table I need for mu problem

Code: Select all

CREATE TABLE `download` (
  `download_ID` int(3) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Primary',
  `date_download` date NOT NULL,
  `popularity` mediumint(9) DEFAULT NULL,
  `file_ID` int(3) unsigned NOT NULL,
  PRIMARY KEY (`download_ID`),
  KEY `FK_download` (`file_ID`),
  CONSTRAINT `FK_download` FOREIGN KEY (`file_ID`) REFERENCES `file` (`fileID`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I need to select a date, or only one month from the year, and to check for that date if there are any downloads and to display the number of downloads.
Searchin the net I combined some code and made the dropdown list for the years, month and days. Here it is:

Code: Select all

function DateSelector($inName, $useDate=0)
{
    /* create array so we can name months */
    $monthName = array(1=> "January", "February", "March",
            "April", "May", "June", "July", "August",
            "September", "October", "November", "December");
    
    /* if date invalid or not supplied, use current time */
    if($useDate == 0)
    {
        $useDate = Time();
    }
    
    /* make month selector */
    echo "<SELECT NAME=" . $inName . "Month>\n";
    for($currentMonth = 0; $currentMonth <= 12; $currentMonth++)
    {
        echo "<OPTION VALUE=\"";
        echo intval($currentMonth);
        echo "\"";
        if(intval(date( "m", $useDate))==$currentMonth)
        {
            echo " SELECTED";
        }
        echo ">" . $monthName[$currentMonth] . "\n";
    }
    echo "</SELECT>";
    
    /* make day selector */
    echo "<SELECT NAME=" . $inName . "Day>\n";
    for($currentDay=""; $currentDay <= 31; $currentDay++)
    {
        echo "<OPTION VALUE=\"$currentDay\"";
        if(intval(date( "d", $useDate))==$currentDay)
        {
            echo " SELECTED";
        }
        echo ">$currentDay\n";
    }
    echo "</SELECT>";
    
    /* make year selector */
    echo "<SELECT NAME=" . $inName . "Year>\n";
    $startYear = date( "Y", $useDate);
    for($currentYear = $startYear - 5; $currentYear <= $startYear+5;$currentYear++)
    {
        echo "<OPTION VALUE=\"$currentYear\"";
        if(date( "Y", $useDate)==$currentYear)
        {
            echo " SELECTED";
        }
        echo ">$currentYear\n";
    }
    
}
Thanks in advance

Re: how to get values from dropdown list

Posted: Thu Dec 24, 2009 2:14 pm
by manohoo
The answer is on the MySQL query:

by day:

Code: Select all

 
SELECT date_download, count(*)
FROM download
GROUP BY date_download
 
by month:

Code: Select all

 
SELECT concat(YEAR(date_download),MONTH(date_download)), count(*)
FROM download
GROUP BY concat(YEAR(date_download),MONTH(date_download))
 
... ans so forth

Re: how to get values from dropdown list

Posted: Thu Dec 24, 2009 3:15 pm
by spirala
thanks for the queries, but i also need some help with the php code and getting the selected values from the dropdown menu. Can you please help me?

Re: how to get values from dropdown list

Posted: Thu Dec 24, 2009 4:02 pm
by califdon
If you are asking how to recover the values selected by the user in those dropdowns, the function that you showed outputs 3 selector dropdowns, but we have to assume that you are building a form, of which these selectors will be a part, so the answer is that the script that handles this will be named in the form element's action= attribute (and its method must be specified as POST). In that script, you will be looking for $_POST variables with names equal to the names assigned by your function, that is, whatever value is in $inName followed by Month, Day, or Year.

Re: how to get values from dropdown list

Posted: Thu Dec 24, 2009 4:26 pm
by spirala
Is that suppose to be like this?

<form method="post" action="<?php echo $PHP_SELF;?>">

how to collect $_POST from a multiple select???

Re: how to get values from dropdown list

Posted: Thu Dec 24, 2009 5:34 pm
by califdon
spirala wrote:Is that suppose to be like this?

<form method="post" action="<?php echo $PHP_SELF;?>">
Yes, although you actually don't need the php code, if you just say action="", it will default to the same page.
how to collect $_POST from a multiple select???
If you mean that you have 3 select form elements, OK, but "multiple select" is a term that is used to mean the ability to simultaneously select more than one selection from the same list, which would require a whole different set of code. But I doubt that's what you actually meant.

Do you see, in your function code, where it creates each selector, like:

Code: Select all

echo "<SELECT NAME=" . $inName . "Month>\n";
It is assigning a NAME to that selector. The value selected by the user will be contained in the POST variable with that name. Now the issue here is that you are assigning a variable name, for some reason that I can't guess. So I can't tell you what name to use in your $_POST array because I don't know what value $inName will have. If this is all because you just copied somebody's code, you probably should remove that and use some literal value, such as NAME='MyMonth', and 'MyDay' and 'MyYear'. If you did that, then the selected values would be found in $_POST['MyMonth'], etc.