This PHP error is driving me nuts. I have no idea what’s causing it.
Error: Parse error: syntax error, unexpected end of file in C:\Program Files (x86)\Ampps\www\includes\class-query.php on line 176
I would say you have an extra } somewhere and it might not be on line 176. Without seeing everything above this it is a bit difficult to see which it is.
Try doing some indenting to help find it also aligning the opening { above the closing } can also help see the problem
On quick glance, this looks janky…you’ve got a foreach loop which doesn’t do anything? My guess is the closing bracket before the ?> isn’t supposed to be there…
if (is_array($message_objects)){
foreach ( $message_objects as $message )
{
}?>
That was my thought - the extra } is spurious, and if the parse error wasn’t there, it would throw errors in the merged php/html code for undefined objects or something similar. The parse error on the last line is usually a giveaway that something was opened and not closed, or closed but not opened.
I’ve removed the extra bracket and made some other changes, and now I get this error:
“Parse error: syntax error, unexpected ‘?>’, expecting function (T_FUNCTION) in C:\Program Files (x86)\Ampps\www\includes\class-query.php on line 176”
Here’s the full code:
<?php
require_once('class-db.php');
if ( !class_exists('query') ) {
class query {
public function load_user_object($user_id) {
global $db;
$table = 's_users';
$query = "
SELECT * FROM $table
WHERE ID = $user_id
";
$obj = $db->select($query);
if ( !$obj ) {
return "No user found";
}
return $obj[0];
}
public function load_all_user_objects() {
global $db;
$table = 's_users';
$query = "
SELECT * FROM $table
";
$obj = $db->select($query);
if ( !$obj ) {
return "No user found";
}
return $obj;
}
public function get_friends($user_id) {
global $db;
$table = 's_friends';
$query = "
SELECT ID, friend_id FROM $table
WHERE user_id = '$user_id'
";
$friends = $db->select($query);
if (is_array($friends)){
foreach ( $friends as $friend )
$friend_ids[] = $friend->friend_id;
}
}
public function get_status_objects($user_id) {
global $db;
$table = 's_status';
$friend_ids = $this->get_friends($user_id);
if ( !empty ( $friend_ids ) ) {
array_push($friend_ids, $user_id);
} else {
$friend_ids = array($user_id);
}
$accepted_ids = implode(', ', $friend_ids);
$query = "
SELECT * FROM $table
WHERE user_id IN ($accepted_ids)
ORDER BY status_time DESC
";
$status_objects = $db->select($query);
return $status_objects;
}
public function get_message_objects($user_id) {
global $db;
$table = 's_messages';
$query = "
SELECT * FROM $table
WHERE message_recipient_id = '$user_id'
";
$messages = $db->select($query);
return $messages;
}
public function do_user_directory() {
$users = $this->load_all_user_objects(); // assuming FALSE is returned if no users
if( $users ) {
foreach ( $users as $user ) {
echo '<div class="directory_item">';
echo '<h3><a href="profile-view.php?uid=' .$user->id .'">' .$user->user_nicename .'></a></h3>';
echo '<p>' .$user->user_email .'</p>';
echo '</div>';
} // endforeach
}else{
echo 'Opportunity to resume with a graceful error message';
} // endforeach
}// endfunc do_user_directory()
function do_friends_list($friends_array) {
if (is_array($friends_array)){
foreach ( $friends_array as $friendid )
$users[] = $thia2s->load_user_object($friend_id);
}
if (is_array($users)){
foreach ( $users as $user )
{
}
?>
<div class="directory_item">
<h3><a href="profile-view.php?uid=<?php echo $user->ID; ?>"><?php echo $user->user_nicename; ?></a></h3>
<p><?php echo $user->user_email; ?></p>
</div>
<?php
}
}
function do_news_feed($user_id) {
$status_objects = $this->get_status_objects($user_id);
foreach ( $status_objects as $status ) {?>
<div class="status_item">
<?php $user = $this->load_user_object($status->user_id); ?>
<h3><a href="profile-view.php?uid=<?php echo $user->ID; ?>"><?php echo $user->user_nicename; ?></a></h3>
<p><?php echo $status->status_content; ?></p>
</div>
<?php
}
}
function do_inbox($user_id) {
$message_objects = $this->get_message_objects($user_id);
if (is_array($message_objects)){
foreach ( $message_objects as $message )
{
}?>
<div class="status_item">
<?php $user = $this->load_user_object($message->message_sender_id); ?>
<h3>From: <a href="profile-view.php?uid=<?php echo $user->ID; ?>"><?php echo $user->user_nicename; ?></a></h3>
<p><?php echo $message->message_subject; ?></p>
<p><?php echo $message->message_content; ?></p>
</div>
<?php
}
}
?>
Adding and removing things in hopes it will make the code OK may work at rare times, but it isn’t the best way to do it. Instead of stumbling around blindly there are some things you can do so you can at least make best guesses even if you don’t understand what the code is doing.
First, find a coding style you like and try to be consistent using it in your code.
IMHO it doesn’t matter so much if you indent using tabs or spaces, have spaces after commas and around parentheses or not, whether curly braces are on their own lines or not. etc.
The key is find a style you like and stick with it.
Arguably the most useful habit you can establish is matched indentation. A bit of extra whitespace won’t affect code efficiency to any noticeable amount and will make your code much easier to read, troubleshoot and maintain.
The second thing is use a text editor / IDE that can help you.
There are many and there are probably as many favorites as there are those using them.
Some have more features, some less.
I use Notepad++ and if the code has a mess of nested parenthesis I can select one and it’s corresponding “partner” will highlight. It does the same thing for nested curly braces. (and HTML tags).
Here’s your code with what I believe are non-offenders (and empty lines, which isn’t a good idea to do. I only did it to condense the example) removed. Do you see the problem?
<?php
require_once('class-db.php');
if ( !class_exists('query') ) {
class query {
public function get_friends($user_id) {
global $db;
$table = 's_friends';
$query = "
SELECT ID, friend_id FROM $table
WHERE user_id = '$user_id'
";
$friends = $db->select($query);
if (is_array($friends)){
foreach ( $friends as $friend )
$friend_ids[] = $friend->friend_id;
}
}
function do_friends_list($friends_array) {
if (is_array($friends_array)){
foreach ( $friends_array as $friendid )
$users[] = $thia2s->load_user_object($friend_id);
}
if (is_array($users)){
foreach ( $users as $user )
{
}
?>
<div class="directory_item">
<h3><a href="profile-view.php?uid=<?php echo $user->ID; ?>"><?php echo $user->user_nicename; ?></a></h3>
<p><?php echo $user->user_email; ?></p>
</div>
<?php
}
}
function do_inbox($user_id) {
$message_objects = $this->get_message_objects($user_id);
if (is_array($message_objects)){
foreach ( $message_objects as $message )
{
}
?>
<div class="status_item">
<?php $user = $this->load_user_object($message->message_sender_id); ?>
<h3>From: <a href="profile-view.php?uid=<?php echo $user->ID; ?>"><?php echo $user->user_nicename; ?></a></h3>
<p><?php echo $message->message_subject; ?></p>
<p><?php echo $message->message_content; ?></p>
</div>
<?php
}
}
?>
Thanks for the organization help! After small tweaks, I solved the problem, but I’ve now encountered this one:
“Parse error: syntax error, unexpected ‘<’ in C:\Program Files (x86)\Ampps\www\includes\class-query.php on line 43”
I’ve attached the code in a Notepad++ file as a reference.class-query.php (1.5 KB)