Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parse error: syntax error, unexpected $end in test.php on line 237 2

Status
Not open for further replies.

evil1966

MIS
Dec 2, 2013
57
US
I need help finding the error. I have this code working to return data from a table.

Code:
   <?php 
   
 
	$username="me";
	$host="localhost";
	$password="********";
	$database="mydatabase";

	$con = mysql_connect($host,$username,$password);
    if(!$con)
    {
	 die("Unable to select database");
    }
    mysql_select_db($database,$con);
		
	$sql = "SELECT * FROM donors WHERE lastname LIKE '%$letter' ORDER BY lastname, firstname";
	
    $result = mysql_query($sql);

  ?>
  
   <h1>Donor List</h1>
   <p style="margin-left:25px;"> Click on any letter button or a column header to sort this list</p>
   <script> document.write(btns);</script>

  <table class="example table-autosort table-autofilter table-autopage:10 table-stripeclass:alternate    table-page-number:t1page table-page-count:t1pages table-filtered-rowcount:t1filtercount table-rowcount:t1allcount" id="t1">
  <thead>
    <tr>
        <th class="table-sortable:default" title="Click to sort">Greeting</th>
        <th class="table-sortable:default" title="Click to sort">First Name</th>
        <th class="table-sortable:default" title="Click to sort">Last Name</th>
        <th class="table-sortable:default" title="Click to sort">Company</th>
        <th class="table-sortable:default" title="Click to sort">Job Title</th>
        <th class="table-sortable:default" title="Click to sort">EMail</th>
        <th class="table-sortable:default" title="Click to sort">Business Phone</th>
        <th class="table-sortable:default" title="Click to sort">Home Phone</th>
        <th class="table-sortable:default" title="Click to sort">Cell Phone</th>
        <th class="table-sortable:default" title="Click to sort">Fax</th>
        <th class="table-sortable:default" title="Click to sort">Address</th>
        <th class="table-sortable:default" title="Click to sort">Address2</th>
        <th class="table-sortable:default" title="Click to sort">City</th>
        <th class="table-sortable:default" title="Click to sort">State</th>
        <th class="table-sortable:default" title="Click to sort">Zip</th>
    </tr>
  </thead>
 <tbody>

 <?php 
    
    while($row = mysql_fetch_array($result))
  {  
  
 ?>
       
 <tr>
     <td><?php echo $row['greeting']?></td>
     <td><?php echo $row['firstname']?></td>
     <td><?php echo $row['lastname']?></td>
     <td><?php echo $row['company']?></td>
     <td><?php echo $row['jobtitle']?></td>
     <td><?php echo $row['eMail']?></td>
     <td><?php echo $row['businessphone']?></td>
     <td><?php echo $row['homephone']?></td>
     <td><?php echo $row['cellphone']?></td>
     <td><?php echo $row['fax']?></td>
     <td><?php echo $row['address1']?></td>
     <td><?php echo $row['address2']?></td>
     <td><?php echo $row['city']?></td>
     <td><?php echo $row['state']?></td>
     <td><?php echo $row['zip']?></td>
  </tr>
  <?php 
  }
  mysql_close($con);
  ?> 
  
  
  
 </tbody>
 <tfoot>
    
    <tr>
        <td colspan="2" style="cursor:pointer;" class="table-page:previous">&lt; &lt; Previous</td>
        <td colspan="4" style="text-align:center;" colspan="1">Page <span id="t1page">1</span>&nbsp;of <span id="t1pages">11</span></td>
        <td colspan="2" style="cursor:pointer;" class="table-page:next">Next &gt; &gt;</td>
    </tr>
    <tr>
        <td colspan="8"><span id="t1filtercount">105</span>&nbsp;of <span id="t1allcount">105</span>&nbsp;rows match filter(s)</td>
    </tr></tfoot>
 </table>

I added script to add buttons for sorting the data.

Code:
<script>
var btns = "";
var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var letterArray = letters.split("");
for(var i = 0; i < 26; i++){
    var letter = letterArray.shift();
    btns += '<button class="mybtns" onclick="alphabetSearch(\''+letter+'\');">'+letter+'</button>';
}
function alphabetSearch(let){}
</script>

That works, but the code to return the sorted data based on the button click throws the error.

Code:
  <?php 
   
[b][COLOR=#CC0000]   $results = "";
   $letter = "";
   if(isset($_GET['letter']) && strlen($_GET['letter']) == 1){
    $letter = preg_replace('#[^a-z]#i', '', $_GET['letter']);
    if(strlen($letter) != 1){
        echo "ERROR: Hack Attempt, after filtration the variable is empty.";
        exit();
    }[/color][/b]
    
	$username="me;
	$host="localhost";
	$password="*********";
	$database="mydatabase";

	$con = mysql_connect($host,$username,$password);
    if(!$con)
    {
	 die("Unable to select database");
    }
    mysql_select_db($database,$con);
		
	$sql = "SELECT * FROM donors WHERE lastname LIKE '%$letter' ORDER BY lastname, firstname";
	
    $result = mysql_query($sql);

  ?>

Thanks for the help.
 
What is on line 237? and a few lines before it? The code in red?

The error is telling you that its reaching the end of the PHP script but there is some unmatched quote or parenthesis or unterminated string etc... something that was not properly ended so the PHP parser keeps going through the code only to find a closing php tag before the offending line ends.





----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Hi Vacunita,

Line 237 is the last line of the page. Looking at the code I need to add a } somewhere after the code in red, but hell if I know where and at I'm not convinced that code in red will work correctly as it's written. My goal here is to default the page to open and display all the donors from the database and then let the user to be able to sort the list by the buttons. Once this works I also need to add links or buttons for each name that will return that donor's info on a new page so it can be edited or new donations added to a donation table.

I'll keep trying to get this to work...LOL.

Thanks!
 
Okay I've removed a couple of lines that don't really do anything in the red code above and it works, but now the buttons are not returning the correct results. Back to work.
 
Everything sees to be working now. I did not know that "SELECT * FROM donors WHERE lastname LIKE '%$letter' ORDER BY lastname, firstname"; would sort by the last letter of the last name and not the first letter. Changed '%$letter' to '$letter%' and the sql worked.

On to the next step giving the user the ability to edit the donor data.
 
I did not know that "SELECT * FROM donors WHERE lastname LIKE '%$letter' ORDER BY lastname, firstname"; would sort by the last letter of the last name and not the first letter.

To explain why, '%' is the MySQL wildcard and will match any number of characters in the string, so

%c matches strings that ends with 'c'

c%c matches strings that begin and end with 'c'

c% matches strings that start with 'c'

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
in your original code this line
Code:
if(isset($_GET['letter']) && strlen($_GET['letter']) == 1){
opened a conditional that was not later closed. you probably needed just to add } before the ?> tag.
 
What IDE/text editor are you using? Many will allow you to jump between brackets (or to collapse them) for one's overall sanity when dealing with large amounts of code.
 
jpadie, thanks that did work. I found during yesterday's testing I had a different issue with the button code that made troubleshooting this issue difficult. Now everything is working.

spamjim, I'm using dreamweaver dw.


Anyone - How do I make the results in the lastname field into links that onClick would open a new page with all that person's info on it?

Thanks everyone for your help.
 
jpadie - opps I posted to soon. Putting the } before the ?> throws two errors:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in test.php on line 107

Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in test.php on line 132

Line 107 is my while statement and line 132 is my mysql_close($con);
 
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in test.php on line 107

Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in test.php on line 132

This means either your query is not running or the query has an error and as such the $result variable is not valid. This is probably because if you place the } right before the ?> you are conditionalizing the mysql_query call to only run when $_GET['letter'] exists and is equal to 1 as per your IF statement. If you send anything else but a 1 . Nothing inside the If statement gets run.
Code:
 $results = "";
   $letter = "";
[b]   [COLOR=#A40000]if(isset($_GET['letter']) && strlen($_GET['letter']) == 1){[/color][/b]
    $letter = preg_replace('#[^a-z]#i', '', $_GET['letter']);
    if(strlen($letter) != 1){
        echo "ERROR: Hack Attempt, after filtration the variable is empty.";
        exit();
    }
    
	$username="me;
	$host="localhost";
	$password="*********";
	$database="mydatabase";

	$con = mysql_connect($host,$username,$password);
    if(!$con)
    {
	 die("Unable to select database");
    }
    mysql_select_db($database,$con);
		
	$sql = "SELECT * FROM donors WHERE lastname LIKE '%$letter' ORDER BY lastname, firstname";
	
    $result = mysql_query($sql);

  ?>

Basically you need to determine what needs to happen when there is no valid letter. Perhaps have a default value. That would determine how you structure your If statement and the code within.

if(this)
{
Then do this
}
else
{
otherwise do this instead.
}





----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Vacunita - it doesn't return data on page load which should return all since no button was pushed. I should rebuild it to add an "all" button to the others. As it is if I remove these lines it runs fine.

Code:
    if(strlen($letter) != 1){
        echo "ERROR: Hack Attempt, after filtration the variable is empty.";
        exit();

{/code]

Thanks.
 
Then you'll want to place the } right before your second if. i.e.

Code:
  if(isset($_GET['letter']) && strlen($_GET['letter']) == 1){
    $letter = preg_replace('#[^a-z]#i', '', $_GET['letter']);
[indent][b][highlight #A40000][COLOR=#FFFFFF]}[/color][/highlight][/b][/indent]
    if(strlen($letter) != 1){
        echo "ERROR: Hack Attempt, after filtration the variable is empty.";
        exit();
    }

For the info page, output the correct html to make a link. i.e ; <a href=detailspage.php?id=[vendorid]">VENDOR</a>

Then simply re-query the DB with the vendor Id you are passing to the next page.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Thanks Vacunita. I have it working now though. Follow-up on the href :

Code:
 <tr>
     <td><?php echo $row['greeting']?></td>
     <td><?php echo $row['firstname']?></td>
     <td><?php echo $row['lastname']?></td>
     <td><?php echo $row['company']?></td>
     <td><?php echo $row['jobtitle']?></td>
     <td><?php echo $row['eMail']?></td>
     <td><?php echo $row['businessphone']?></td>
     <td><?php echo $row['homephone']?></td>
     <td><?php echo $row['cellphone']?></td>
     <td><?php echo $row['fax']?></td>
     <td><?php echo $row['address1']?></td>
     <td><?php echo $row['address2']?></td>
     <td><?php echo $row['city']?></td>
     <td><?php echo $row['state']?></td>
     <td><?php echo $row['zip']?></td>
  </tr>

I'd line the lastname in each row to be the linnk to a details.php page where the donor_id in the donors table equals the donor_id in the donations table. I've tried to wrap the line below in an <a href line a couple of different ways and still haven't gotten it to work properly. I also tried to remove the php tags in each line and wrap the whole table in a single set of tags and that didn't work either.

Code:
<td><?php echo $row['lastname']?></td>

Thanks
 
okay I got this to work to create the link in each row correctly.

Code:
     <td><a href=details.php?id=[donor_id]><?php echo $row['lastname']?></a></td>

Now I have to work on the details page. So thanks!
 
On further testing no matter which lastname link I click on in the test.php results table, the page details.php returns the donor_id for the first donor table row. So if the first donor is donor_id: 1, firstname: Jim, lastname: Able,... I always get the details for Jim Able even if I'm clicking on Robert Zimmerman with a donor_id of 10.

 
well ... you have hard coded the donor_id to be [donor_id]

perhaps you meant this
Code:
<td><a href=details.php?id[COLOR=#EF2929]=<?php echo $row['donor_id'];?>[/color]><?php echo $row['lastname']?></a></td>
 
Thanks jpadie! That passes the correct id value. Now I have to go back to the detail page and find out why it's still returning the first donor record id value details. I'm using $id = isset($_GET['id']); to get the value and $sql = "SELECT * FROM donations WHERE donor_id = '$id' ORDER by date"; to querry the database. Should I be using SESSION across these pages?

Thanks again.
 
I changed the line $id = isset($_GET['id']); to $id = $_GET['id']; and that works. I'd like to make sure I'm doing this the correct way and I'm I'm seeing a lot of programmers talk about SESSION. If I should be doing this differently please let me know.

Thanks!
 
Just to explain why it did not work originally: This $id = isset($_GET['id']);

sets $id = 1 always. because the isset() function returns true if the value exists or is set, and false if its not. true is usually equal to 1 so when you use itnin a query i returns a row with an id of 1.

Perhaps what you wanted to do was an inline if.
$id = isset($_GET['id']) ? $_GET['id'] : '';

That translates roughly to If $_GET['id'] is set assign it to $id otherwise assign an empty string to it.


In regards to sessions; they can be used to transfer data from one page to another, but in this case they would be of no use because you need to pass a specific value. Once that is set depending on the link you click.

Sessions would have to have been set, before the page is even shown to the user, to some particular value which is not something you would be able to do, because you need to wait for user input.

The way you are doing it is correct. Sessions are used for other things.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top