What have I done wrong? - Date Range Selector

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
scoopy
Forum Newbie
Posts: 6
Joined: Sun Jun 22, 2008 7:27 pm

What have I done wrong? - Date Range Selector

Post by scoopy »

Hi there friends. I'm a newbie to PHP so please be kind. :?

I'm trying to recreate a date selector for my client but I'm getting this error "Parse error: syntax error, unexpected $end in C:\wamp\www\connectglobal\report_date.php on line 130"

I've grabbed this code from some place on the web and trying to make it work for my situation.

Thanks in advanced.

Code: Select all

<html>
<link href="stylesheet.css" rel="stylesheet" type="text/css" />
 
<title>NZHC Report - Date</title><p><img src="images/hnzc-logo-home.gif" alt="NZHC Logo" /><img src="images/welcome_home_loan_07.gif" alt="Welcome Homeloan Logo" /></p>
| <a href="form_submit.php">Insert client info</a> | <a href="report.php">Total Report</a> | <a href="report_region.php">Region Report</a> | <a href="report_agent.php">Agent Report</a> | <a href="report_date.php">Date Range Report</a> | 
<p>
  <?php
 
// get variable after selecting something from the textbox with name 'dateStart'
$select = $_POST['dateStart'];
 
// if something has been chosen
if (!empty($dateStart)) {
 
// get the chosen value
$dateStart = $_POST['dateStart'];
 
// get variable after selecting something from the textbox with name 'dateEnd'
$select = $_POST['dateEnd'];
 
// if something has been chosen
if (!empty($dateEnd)) {
 
// get the chosen value
$dateEnd = $_POST['dateEnd'];
 
// select the type from the database
// database connection details (change to whatever you need)
$HOST = 'localhost';
$DATABASE = 'connect';
$USER = '****';
$PASSWORD = '****';
 
// connect to database
if(!$conn=mysql_connect('localhost','luke','shark1')) {
echo("<li>Can't connect to $HOST as $USER");
echo("<li>MySQL Error: ".mysql_error());
die;
}
 
// select database
if (!mysql_select_db($DATABASE,$conn)) {
echo("<li>We were unable to select database $DATABASE");
die;
}
 
 
 
// if everything successful create query
// this selects all rows where the type is the one you chose in the textbox
// * means that it will select all columns, ie name and type as i said above
$sql_query = "SELECT * FROM `info` WHERE date BETWEEN '$dateStart' AND '$dateEnd'";
 
// get the data from the database
$result = mysql_query($sql_query,$conn);
 
 
 
echo "<table border='1' cellpadding=5 cellspacing=0 bordercolor=#cccccc>
<tr>
<th>Call No.</th>
<th>Time</th>
<th>Title</th>
<th>Firstname</th>
<th>Surname</th>
<th>Gender</th>
<th>Street No.</th>
<th>Street</th>
<th>City</th>
<th>Region</th>
<th>Int Enquiry</th>
<th>Info Discussed</th>
<th>KiwiSaver</th>
<th>Info Requested</th>
<th>Referred</th>
<th>Source</th>
<th>Other</th>
</tr>";
 
while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['id'] . "</td>";
  echo "<td>" . $row['date'] . "</td>";
  echo "<td>" . $row['title'] . "</td>";
  echo "<td>" . $row['firstname'] . "</td>";
  echo "<td>" . $row['surname'] . "</td>";
  echo "<td>" . $row['gender'] . "</td>";  
  echo "<td>" . $row['number'] . "</td>";
  echo "<td>" . $row['street'] . "</td>";  
  echo "<td>" . $row['city'] . "</td>";
  echo "<td>" . $row['region'] . "</td>";
  echo "<td>" . $row['intEnquiry'] . "</td>";
  echo "<td>" . $row['infoDiscussed'] . "</td>";
  echo "<td>" . $row['kiwiSaver'] . "</td>";
  echo "<td>" . $row['infoRequest'] . "</td>";
  echo "<td>" . $row['referred'] . "</td>";
  echo "<td>" . $row['source'] . "</td>";
  echo "<td>" . $row['other'] . "</td>";
  echo "<td>" . $row['agent'] . "</td>";
  echo "</tr>";
  }
echo "</table>";
 
 
// close mysql connection
mysql_close($conn);
 
}
 
?>
</p>
<form action="report_date.php" method="post">
 
  <p>
    <!-- This creates the textbox in html -->
    Start Date
    <input type="text" name="dateStart">
  &nbsp;&nbsp;eg.  2008-06-20</p>
  <p>End Date
    <input type="text" name="dateEnd">
    &nbsp;&nbsp; eg.  2008-06-20  
 
    <!-- Create a button -->
    </p>
  <p>
    <input type="submit" value="Submit" name="select">
      </p>
</form> 
 
 
 
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: What have I done wrong? - Date Range Selector

Post by Eran »

an 'unexpected $end' error means that you have an unclosed control structure - either an if statement or a loop. Try indenting your control structures, it will help you spot where they begin and end.
scoopy
Forum Newbie
Posts: 6
Joined: Sun Jun 22, 2008 7:27 pm

Re: What have I done wrong? - Date Range Selector

Post by scoopy »

pytrin wrote:an 'unexpected $end' error means that you have an unclosed control structure - either an if statement or a loop. Try indenting your control structures, it will help you spot where they begin and end.
hey thanks for the reply. But I'm a newbie. I have no idea what you are talking about. Sorry :cry:
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: What have I done wrong? - Date Range Selector

Post by Eran »

User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: What have I done wrong? - Date Range Selector

Post by Kieran Huggins »

I see two if() blocks, and you only seem to be closing one of them.

indenting your code helps a lot. A LOT.

Example:

Code: Select all

if(something){
  if(something else){
    // some code here
  }
  // more code here
}
This way you can clearly see what code is in what block by the amount of indentation.
scoopy
Forum Newbie
Posts: 6
Joined: Sun Jun 22, 2008 7:27 pm

Re: What have I done wrong? - Date Range Selector

Post by scoopy »

Kieran Huggins wrote:I see two if() blocks, and you only seem to be closing one of them.

indenting your code helps a lot. A LOT.

Example:

Code: Select all

if(something){
  if(something else){
    // some code here
  }
  // more code here
}
This way you can clearly see what code is in what block by the amount of indentation.
Thanks. Can you tell me what I need to change the code to to make it work?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: What have I done wrong? - Date Range Selector

Post by Kieran Huggins »

Kieran Huggins wrote:I see two if() blocks, and you only seem to be closing one of them.
You need to figure out where you need to close the second if block- indent your code and all will become clear!
scoopy
Forum Newbie
Posts: 6
Joined: Sun Jun 22, 2008 7:27 pm

Re: What have I done wrong? - Date Range Selector

Post by scoopy »

Kieran Huggins wrote:
Kieran Huggins wrote:I see two if() blocks, and you only seem to be closing one of them.
You need to figure out where you need to close the second if block- indent your code and all will become clear!
Thanks for you help. However, as I said, I'm thoroughly new to PHP and I have no ideas what you mean. What is a block? what is closing?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: What have I done wrong? - Date Range Selector

Post by Kieran Huggins »

basically, a block is the space between { and }

Every time you open a block with a {, you must eventually close it with a }

You'll see blocks when you define functions, use conditional statements like if or loops like while or foreach.

Code: Select all

if($something == true){ // block opens
  // stuff in here only happens if $something is true
  if($something_else == false){ // another block opens INSIDE the parent block
    // stuff in here only executes if $something is true AND $something_else is false
  } // the inner block closes 
} // outer block closes
// stuff out here executes no matter what
read: http://ca.php.net/manual/en/language.co ... ctures.php
scoopy
Forum Newbie
Posts: 6
Joined: Sun Jun 22, 2008 7:27 pm

Re: What have I done wrong? - Date Range Selector

Post by scoopy »

Thanks again!!

OK I've found the unclosed block and have indented all my code. now nothing happens. Any suggestions?

Code: Select all

<html>
<link href="stylesheet.css" rel="stylesheet" type="text/css" />
 
<title>NZHC Report - Date</title><p><img src="images/hnzc-logo-home.gif" alt="NZHC Logo" /><img src="images/welcome_home_loan_07.gif" alt="Welcome Homeloan Logo" /></p>
| <a href="form_submit.php">Insert client info</a> | <a href="report.php">Total Report</a> | <a href="report_region.php">Region Report</a> | <a href="report_agent.php">Agent Report</a> | <a href="report_date.php">Date Range Report</a> | <a href="update_form.php">Update Form</a> | <a href="mysqldump.php">Backup Database</a> |
<p>
  <?php
 
// get variable after selecting something from the textbox with name 'dateStart'
$select = $_POST['dateStart'];
 
// if something has been chosen
    if (!empty($dateStart)) {
    
    // get the chosen value
    $dateStart = $_POST['dateStart'];
    
    // get variable after selecting something from the textbox with name 'dateEnd'
    $select = $_POST['dateEnd'];
    
    // if something has been chosen
        if (!empty($dateEnd)) {
        
        // get the chosen value
        $dateEnd = $_POST['dateEnd'];
        
        // select the type from the database
        // database connection details (change to whatever you need)
        $HOST = 'localhost';
        $DATABASE = 'db_name';
        $USER = '****
        $PASSWORD = '****';
        
        // connect to database
            if(!$conn=mysql_connect('localhost','****','****')) {
            echo("<li>Can't connect to $HOST as $USER");
            echo("<li>MySQL Error: ".mysql_error());
            die;
            }
        
        // select database
            if (!mysql_select_db($DATABASE,$conn)) {
            echo("<li>We were unable to select database $DATABASE");
            die;
            }
        
        
        
        // if everything successful create query
        // this selects all rows where the type is the one you chose in the textbox
        // * means that it will select all columns, ie name and type as i said above
        $sql_query = "SELECT * FROM `info` WHERE date BETWEEN '$dateStart' AND '$dateEnd'";
        
        // get the data from the database
        $result = mysql_query($sql_query,$conn);
        
        
        
        echo "<table border='1' cellpadding=5 cellspacing=0 bordercolor=#cccccc>
        <tr>
        <th>Call No.</th>
        <th>Time</th>
        <th>Title</th>
        <th>Firstname</th>
        <th>Surname</th>
        <th>Gender</th>
        <th>Street No.</th>
        <th>Street</th>
        <th>City</th>
        <th>Region</th>
        <th>Int Enquiry</th>
        <th>Info Discussed</th>
        <th>KiwiSaver</th>
        <th>Info Requested</th>
        <th>Referred</th>
        <th>Source</th>
        <th>Other</th>
        </tr>";
        
        while($row = mysql_fetch_array($result))
              {
              echo "<tr>";
              echo "<td>" . $row['id'] . "</td>";
              echo "<td>" . $row['date'] . "</td>";
              echo "<td>" . $row['title'] . "</td>";
              echo "<td>" . $row['firstname'] . "</td>";
              echo "<td>" . $row['surname'] . "</td>";
              echo "<td>" . $row['gender'] . "</td>";  
              echo "<td>" . $row['number'] . "</td>";
              echo "<td>" . $row['street'] . "</td>";  
              echo "<td>" . $row['city'] . "</td>";
              echo "<td>" . $row['region'] . "</td>";
              echo "<td>" . $row['intEnquiry'] . "</td>";
              echo "<td>" . $row['infoDiscussed'] . "</td>";
              echo "<td>" . $row['kiwiSaver'] . "</td>";
              echo "<td>" . $row['infoRequest'] . "</td>";
              echo "<td>" . $row['referred'] . "</td>";
              echo "<td>" . $row['source'] . "</td>";
              echo "<td>" . $row['other'] . "</td>";
              echo "<td>" . $row['agent'] . "</td>";
              echo "</tr>";
              }
        echo "</table>";
        
        
        // close mysql connection
        mysql_close($conn);
        
        }
}
 
?>
</p>
<form action="report_date.php" method="post">
 
  <p>
    <!-- This creates the testbox in html -->
    Start Date
    <input type="text" name="dateStart">
  &nbsp;&nbsp;eg.  2008-06-20</p>
  <p>End Date
    <input type="text" name="dateEnd">
    &nbsp;&nbsp; eg.  2008-06-20  
 
    <!-- Create a button -->
    </p>
  <p>
    <input type="submit" value="Submit" name="select">
      </p>
</form> 
<p>| <a href="form_submit.php">Insert client info</a> | <a href="report.php">Total Report</a> | <a href="report_region.php">Region Report</a> | <a href="report_agent.php">Agent Report</a> | <a href="report_date.php">Date Range Report</a> |</p>
 
 
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Re: What have I done wrong? - Date Range Selector

Post by WebbieDave »

scoopy wrote:now nothing happens
And by nothing you mean? You'll need to be way more descriptive to get good help. For instance, what is supposed to happen? Have you tried changing the code to fix this new problem? If so, what have you tried and what was the result? Have you at least acquired a good book on beginning PHP? This will go a long way in helping you hunt down problems with PHP code.
scoopy
Forum Newbie
Posts: 6
Joined: Sun Jun 22, 2008 7:27 pm

Re: What have I done wrong? - Date Range Selector

Post by scoopy »

WebbieDave wrote:
scoopy wrote:now nothing happens
And by nothing you mean? You'll need to be way more descriptive to get good help. For instance, what is supposed to happen? Have you tried changing the code to fix this new problem? If so, what have you tried and what was the result? Have you at least acquired a good book on beginning PHP? This will go a long way in helping you hunt down problems with PHP code.
Hey thank for your help! Sorry, the page displays with the boxes that I've create, but when I insert dates and click the submit button the two text boxes go blank. I'm hoping that my code will create a table of all rows between the dates supplied. The date field is a timestamp type. I'm using 2008-06-25 as the format in the textboxes.

Any suggestions for good php mysql newbie books?

Scoopy
Post Reply