Jump to content


Photo

How do i check for dates in an sql database?


  • Please log in to reply
11 replies to this topic

#1 scallywag05

scallywag05
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 15 April 2006 - 12:56 PM

Hi,
I have set up a form which displays months of the year as checkbox values and i want to when the user clicks on, for example january, to run an sql query that checks for entries on that date, but my array is set up as $month(jan, feb.....) and my sql database has date entries as 0000-00-00. How can i check depending on what the user clicks if there are entries for this month?
Thanks

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,016 posts

Posted 15 April 2006 - 01:24 PM

$curr_month = date('Y-m');

$sql = "SELECT COUNT(*) FROM mytable WHERE DATE_FORMAT(datecol, '%Y-%m') = '$curr_month'";
$res = mysql_query($sql);

if (mysql_result($res, 0) == 0) {

       echo "No records this month";
}

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 scallywag05

scallywag05
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 15 April 2006 - 01:41 PM

Thanks,
I added the code you gave me but i want to run another piece of code that if there are values for that month for the variable selected it outputs them, ive attached the code maybe you can see where im going wrong. thanks for the help as you can tell i dont really know much about php yet!

$curr_month = date('Y-m-d');
$sql2 = "SELECT COUNT(*) FROM pertrack WHERE DATE_FORMAT(date, '%Y-%m-%d') = '$curr_month'";
$res = mysql_query($sql2);

if (mysql_result($res,0)== 0) {

       echo "No records this month";
}
else{
 
        if (isset($_POST['variable'])) {
                foreach ($_POST['variable'] as $k => $dmy) {
                    echo '<ul><li><h3><strong>'.$k . '</strong></h3></ul></li>';
                    $sql = "SELECT * FROM pertrack WHERE username='$username'";
                    $result = mysql_query($sql, $db1);
                        while($row=mysql_fetch_assoc($result)) {
                   echo '<ul><li>Value:'.$row["$k"]."<br></li></ul>"; 
                     }
                }        
    }
}


#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,016 posts

Posted 15 April 2006 - 01:48 PM

I thought you wanted records for the month, not for today.

What's the rest of the code supposed to do?

And what does $_POST['variable'] look like?
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 scallywag05

scallywag05
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 15 April 2006 - 01:56 PM

Hi,
Basically my form goes like this:

Variable one - checkbox
Variable two - checkbox.

There are 5 checkboxes in total and you can choose more than one. Then in order to draw a graph the user then selects the months they want to see the results for, ie:

Jan - checkbox
Feb - checkbox
Mar - checkbox.......

What i want is that when the user picks the variables and the months to track a script will run that checks the databse for values of these variables for the specific months. my first problem was checking to see if there were values, as i have set up an array for the months as $month(1=>'jan', 'feb'........) and the databse has date stored as 0000-00-00, then how was i going to check if there were values entered for these dates and how to then display them in a graph!!!! Bit of a dilema......can you help me please!

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 15 April 2006 - 02:19 PM

Please post the scripts that set up the form and process the form. It sounds like there is probably a better way of attacking your problem.

Ken

#7 scallywag05

scallywag05
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 15 April 2006 - 02:22 PM

Here is the code for the form:

<form name="form1" method="post" action="assessment2.php">
  <p><font color="#FF0000" face="Geneva, Arial, Helvetica, sans-serif"><strong>Please 
    select which of the options below you wish to analyse:</strong></font></p>
  <ul>
    <li> 
      <h3><font face="Geneva, Arial, Helvetica, sans-serif"><strong>Cholesterol 
        <input name="variable[cholesterol_value]" type="checkbox" id="variable[cholesterol]" value="checkbox">
        </strong></font></h3>
    </li>
    <li> 
      <h3><font face="Geneva, Arial, Helvetica, sans-serif"><strong>Blood Pressure: 
        Systolic 
        <input name="variable[bp_value]" type="checkbox" id="variable[bp_value]" value="checkbox">
        <em>ie: <font color="#FF0000">120</font>/80</em></strong></font></h3>
    </li>
    <li>
      <h3><strong><font face="Geneva, Arial, Helvetica, sans-serif">Blood Pressure: 
        Diastolic </font></strong><em><strong><font face="Geneva, Arial, Helvetica, sans-serif"> 
        <input name="variable[bp_value2]" type="checkbox" id="variable[bp_value2]" value="checkbox">
        ie: 120/<font color="#FF0000">80</font></font></strong></em></h3>
    </li>
    <li> 
      <h3><font face="Geneva, Arial, Helvetica, sans-serif"><strong> BMI 
        <input name="variable[bmi_value]" type="checkbox" id="variable[bmi_value]" value="checkbox">
        </strong></font></h3>
    </li>
    <li> 
      <h3><font face="Geneva, Arial, Helvetica, sans-serif"><strong>Weight 
        <input name="variable[weight_value]" type="checkbox" id="variable[weight_value]" value="checkbox">
        </strong></font></h3>
    </li>
    <li> 
      <h3><font face="Geneva, Arial, Helvetica, sans-serif"><strong> Calories 
        Burnt 
        <input name="variable[calories_value]" type="checkbox" id="variable[calories_value]" value="checkbox">
        </strong></font></h3>
    </li>
  </ul>
  <p><font color="#FF0000" face="Geneva, Arial, Helvetica, sans-serif"><strong>Please 
    select the period you would like to track the option you selected over:<br>
    <font color="#000000"><em>NOTE! you must select more than one:</em></font> 
    </strong></font></p>
  <table width="25%" border="1">
    <tr> 
      <td width="62%" bgcolor="#66FFCC"> <div align="left"><strong><font face="Geneva, Arial, Helvetica, sans-serif">January</font></strong></div></td>
      <td width="38%"><div align="center"><font face="Geneva, Arial, Helvetica, sans-serif"> 
          <input name="month[jan]" type="checkbox" id="month[jan]" value="checkbox">
          </font></div></td>
    </tr>
    <tr> 
      <td bgcolor="#66FFCC"> <div align="left"><strong><font face="Geneva, Arial, Helvetica, sans-serif">February 
          </font></strong></div></td>
      <td><div align="center"><font face="Geneva, Arial, Helvetica, sans-serif"> 
          <input name="month[feb]" type="checkbox" id="month[feb]" value="checkbox">
          </font></div></td>
    </tr>
    <tr> 
      <td bgcolor="#66FFCC"> <div align="left"><strong><font face="Geneva, Arial, Helvetica, sans-serif">March 
          </font></strong></div></td>
      <td><div align="center"><font face="Geneva, Arial, Helvetica, sans-serif"> 
          <input name="month[mar]" type="checkbox" id="month[mar]" value="checkbox">
          </font></div></td>
    </tr>
    <tr> 
      <td bgcolor="#66FFCC"> <div align="left"><strong><font face="Geneva, Arial, Helvetica, sans-serif">April</font></strong></div></td>
      <td><div align="center"><font face="Geneva, Arial, Helvetica, sans-serif"> 
          <input name="month[apr]" type="checkbox" id="month[apr]" value="checkbox">
          </font></div></td>
    </tr>
    <tr> 
      <td bgcolor="#66FFCC"> <div align="left"><strong><font face="Geneva, Arial, Helvetica, sans-serif">May</font></strong></div></td>
      <td><div align="center"><font face="Geneva, Arial, Helvetica, sans-serif"> 
          <input name="month[may]" type="checkbox" id="month[may]" value="checkbox">
          </font></div></td>
    </tr>
    <tr> 
      <td bgcolor="#66FFCC"> <div align="left"><strong><font face="Geneva, Arial, Helvetica, sans-serif">June</font></strong></div></td>
      <td><div align="center"><font face="Geneva, Arial, Helvetica, sans-serif"> 
          <input name="month[june]" type="checkbox" id="month[june]" value="checkbox">
          </font></div></td>
    </tr>
    <tr> 
      <td bgcolor="#66FFCC"> <div align="left"><strong><font face="Geneva, Arial, Helvetica, sans-serif">July</font></strong></div></td>
      <td><div align="center"><font face="Geneva, Arial, Helvetica, sans-serif"> 
          <input name="month[july]" type="checkbox" id="month[july]" value="checkbox">
          </font></div></td>
    </tr>
    <tr> 
      <td bgcolor="#66FFCC"> <div align="left"><strong><font face="Geneva, Arial, Helvetica, sans-serif">August 
          </font></strong></div></td>
      <td><div align="center"><font face="Geneva, Arial, Helvetica, sans-serif"> 
          <input name="month[aug]" type="checkbox" id="month[aug]" value="checkbox">
          </font></div></td>
    </tr>
    <tr> 
      <td bgcolor="#66FFCC"> <div align="left"><strong><font face="Geneva, Arial, Helvetica, sans-serif">September</font></strong></div></td>
      <td><div align="center"><font face="Geneva, Arial, Helvetica, sans-serif"> 
          <input name="month[sept]" type="checkbox" id="month[sept]" value="checkbox">
          </font></div></td>
    </tr>
    <tr> 
      <td bgcolor="#66FFCC"> <div align="left"><strong><font face="Geneva, Arial, Helvetica, sans-serif">October</font></strong></div></td>
      <td><div align="center"><font face="Geneva, Arial, Helvetica, sans-serif"> 
          <input name="month[oct]" type="checkbox" id="month[oct]" value="checkbox">
          </font></div></td>
    </tr>
    <tr> 
      <td bgcolor="#66FFCC"> <div align="left"><strong><font face="Geneva, Arial, Helvetica, sans-serif">November</font></strong></div></td>
      <td><div align="center"><font face="Geneva, Arial, Helvetica, sans-serif"> 
          <input name="month[nov]" type="checkbox" id="month[nov]" value="checkbox">
          </font></div></td>
    </tr>
    <tr> 
      <td bgcolor="#66FFCC"> <div align="left"><strong><font face="Geneva, Arial, Helvetica, sans-serif">December</font></strong></div></td>
      <td><div align="center"><font face="Geneva, Arial, Helvetica, sans-serif"> 
          <input name="month[dec]" type="checkbox" id="month[dec]" value="checkbox">
          </font></div></td>
    </tr>
  </table>
  <p> 
    <input type="submit" name="Submit" value="View Personal Assessment">
  </p>
</form>

And then the php that the form calls:

        $mysqlserver = "localhost";
                                $user_id = "****"; 
                                $user_pw = "****"; 
                                $database_name = "database"; 


$username = $_SESSION['username'];

$month = array (1=>"jan","feb","mar","apr","may","june","july","aug","sept","oct","nov","dec");
foreach ($_POST['month'] as $value=> $dmy){
    echo "<li>$value</li>\n";
    }


if($username == '') # if form incomplete
{
    print "<p>Please Complete fields marked with an (*) as these are REQUIRED</p>";
    print '<p><a href="' . $_SERVER['HTTP_REFERER'] . '">Go back to form</a></p>';
}
else
{
    // connect to the MySQL and select database
    $db1 = mysql_connect($mysqlserver, $user_id, $user_pw );
    mysql_select_db($database_name ,$db1);
    
    if($db1===false)
    {
            print "\n<br>Unable to open database";
            die(1);
    } // end if unable to open db
  

?>

<?php 
echo "<h2>The variables and the dates you chose are as follows:</h2>";

$curr_month = date('Y-m');
$sql2 = "SELECT COUNT(*) FROM pertrack WHERE DATE_FORMAT(date, '%Y-%m') = '$curr_month'";
$res = mysql_query($sql2);

if (mysql_result($res,0)== 0) {

       echo "No records this month";
}
else{
 
        if (isset($_POST['variable'])) {
                foreach ($_POST['variable'] as $k => $dmy) {
                    echo '<ul><li><h3><strong>'.$k . '</strong></h3></ul></li>';
                    $sql = "SELECT * FROM pertrack WHERE username='$username'";
                    $result = mysql_query($sql, $db1);
                        while($row=mysql_fetch_assoc($result)) {
                        echo '<ul><li>Value:'.$row["$k"]."<br></li></ul>"; 
                     }
                     
                }        
    }
}



    mysql_close($db1);
}// end if no form submitted

?>


#8 scallywag05

scallywag05
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 15 April 2006 - 02:32 PM

The code here is just displaying the values to the user. Th user then clicks a button to plot the graph after viewing the results. I was going to use jpGraph to do this but dont really know how to get the values and months to display. is there a way instead of having to display the values it will automatically plot the graph depending on what the user selects, both variable and monthly. Thanks for all the help its greatly appreciated.

#9 scallywag05

scallywag05
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 15 April 2006 - 04:17 PM

Barand, I posted a copy of both the form and the php to show the $POST#_variable



#10 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,016 posts

Posted 15 April 2006 - 05:50 PM

[!--quoteo(post=365083:date=Apr 15 2006, 03:32 PM:name=artful_dodger)--][div class=\'quotetop\']QUOTE(artful_dodger @ Apr 15 2006, 03:32 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
The code here is just displaying the values to the user. Th user then clicks a button to plot the graph after viewing the results. I was going to use jpGraph to do this but dont really know how to get the values and months to display. is there a way instead of having to display the values it will automatically plot the graph depending on what the user selects, both variable and monthly. Thanks for all the help its greatly appreciated.
[/quote]

Have you even looked at the reply to your other post asking this same question?

[a href=\"http://www.phpfreaks.com/forums/index.php?showtopic=91014\" target=\"_blank\"]http://www.phpfreaks.com/forums/index.php?showtopic=91014[/a]
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#11 scallywag05

scallywag05
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 15 April 2006 - 06:03 PM

yeah i looked at it but im not really familiar with php so didnt really understand what your code was doing.

#12 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,016 posts

Posted 15 April 2006 - 07:11 PM

I was under the impression that you wanted to know how to code graphs.

A waste of my time then?
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users