1

Recently I finished working on a small php project, everything was functioning perfectly on my 2 macs using xampp to display my work, however when tested on a windows machine using xampp I keep getting this error:

Parse error: syntax error, unexpected end of file in C:\xampp\htdocs\IT2B\cart.php on line 161

I already know that this error means there is an unclosed bracket somewhere in my code, but after several hours of searching I am still unable to solve this error message. I have counted each of the opening brackets and there is the same number open as closed, indicating all brackets are already closed.

I even tried to use the error_reporting(0); function to surpress all errors (Yes I know this is not a good idea), to see if it would allow my website to display on a windows machine, but still the error was displayed! This is very frustrating and so I would like to know how this can be solved.

Here is my cart.php code:

<?php

session_start();

$page = 'index.php';

mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('cart') or die(mysql_error());

if (isset($_GET['add'])) {
    $quantity = mysql_query('SELECT id, quantity FROM products WHERE id='.mysql_real_escape_string((int)$_GET['add']));
while ($quantity_row = mysql_fetch_assoc($quantity)) {
    if ($quantity_row['quantity']!=$_SESSION['cart_'.(int)$_GET['add']]) {
        $_SESSION['cart_' . (int)$_GET['add']] +='1';
        header('Location: order.php');

    }
}
header('Location: order.php');

}

if (isset($_GET['remove'])) {
    $_SESSION['cart_'.(int)$_GET['remove']]--;
    header ('Location: order.php');

}

if (isset($_GET['delete'])) {
    $_SESSION['cart_' . (int)$_GET['delete']]='0';
    header ('Location: order.php');
}



function products() {
    $get = mysql_query('SELECT id, name, description, price FROM products WHERE quantity > 0 ORDER BY id ASC');
    if (mysql_num_rows($get) == 0) {
        echo "There are no products to display.";
    }
    else {
        echo "<center>\n";
        echo "  <table class='menufinal' border=0 width=75%>\n";
        echo "      <tr>\n";
        echo "      <th>View</th>\n";
        echo "      <th>Dish</th>\n";
        echo "      <th>Description</th>\n";
        echo "      <th>Item Price</th>\n";
        echo "      </tr>\n";
        while ($get_row = mysql_fetch_assoc($get)) {

    ?>
    <tr>
        <td><img src="template.png" height="110" width="110"/> </td>
        <td> <?echo '<p><strong>'.$get_row['name'] . '</strong>'?> </td>
        <td> <?echo $get_row['description']?> </td>
        <td><strong> <?echo '<br>&pound'.number_format($get_row['price'],2).'<br><br> <a href="cart.php?add='.$get_row['id'].'"><button>Add</button></a></p>';?> </strong></td>
    </tr>
    <?
    } 
    echo "</table>\n";
    echo "</center>\n";
}
} 

function cart() {
$total=0;

$output = '';
$output .= '<center>';
$output .= '<table class="menufinal" border=0 width=75%>';
$output .= '<tr>';
$output .= '<th>Remove Item</th>';
$output .= '<th>Item Name</th>';
$output .= '<th>Item Price</th>';
$output .= '<th>Quantity</th>';
$output .= '<th>Line Total</th>';
$output .= '</tr>';

foreach($_SESSION as $name => $value) {
    if ($value>0){
        if (substr($name, 0, 5)=='cart_') {
           $id = substr($name, 5, (strlen($name)-5));
           $get = mysql_query('SELECT id, name, price FROM products WHERE id='.mysql_real_escape_string((int)$id));
           while ($get_row = mysql_fetch_assoc($get)) {
                $sub = $get_row['price']*$value;
                $total += $sub;
                $output .= '<tr>';
                $output .= '<td><a href="cart.php?delete=' .$id.'"><img src="x.png"></a><br></td>';
                $output .= '<td>' . $get_row['name'] . '</td>';
                $output .= '<td>&pound ' . number_format($get_row['price'], 2) . '</td>';
                $output .= '<td><a href="cart.php?remove=' .$id. '"style="text-decoration:none"><strong>- </strong></a>' .$value. '<a href="cart.php?add=' .$id. '"style="text-decoration:none"> +</a></td>';
                $output .= '<td>&pound ' . number_format($sub, 2) . '</td>';
                $output .= '</tr>';
            }
        } 
        $_SESSION['total'] = $total;
    }
}

$output .= '</table>';

if (empty($total)){
    session_destroy();
    header ('Location: index.php');
    exit;    
}

$output .=  '<br><br><br><br><div id="finalPrice">Total: &pound ' . number_format($total, 2) . '<br></div>';
$output .=  '<br><br><br><p><a href="index.php"><img src="dishes.png" width="240" height="152"></a> <img src="spacer.png" width="110"> <a href="checkout.php"><img src="checkout.png" width="240" height="151"></a>';

echo $output;
}

function cartconfirm() {
$total=0;

$output = '';
$output .= '<center>';
$output .= '<table class="menufinal" border=0 width=75%>';
$output .= '<tr>';
$output .= '<th>Item Name</th>';
$output .= '<th>Item Price</th>';
$output .= '<th>Quantity</th>';
$output .= '<th>Line Total</th>';
$output .= '</tr>';

foreach($_SESSION as $name => $value) {
    if ($value>0){
        if (substr($name, 0, 5)=='cart_') {
           $id = substr($name, 5, (strlen($name)-5));
           $get = mysql_query('SELECT id, name, price FROM products WHERE id='.mysql_real_escape_string((int)$id));
           while ($get_row = mysql_fetch_assoc($get)) {
                $sub = $get_row['price']*$value;
                $total += $sub;
                $output .= '<tr>';
                $output .= '<td>' . $get_row['name'] . '</td>';
                $output .= '<td>&pound ' . number_format($get_row['price'], 2) . '</td>';
                $output .= '<td>' .$value. '</td>';
                $output .= '<td>&pound ' . number_format($sub, 2) . '</td>';
                $output .= '</tr>';
            }
        } 
        $_SESSION['total'] = $total;
    }
}

$output .= '</table>';

if (empty($total)){
    session_destroy();
    header ('Location: index.php');
    exit;    
}

$output .=  '<br><br><br><br><div id="finalPrice">Total: &pound ' . number_format($total, 2) . '<br></div>';

echo $output;
}

?>
5
  • 6
    Most likely short tags aren't enabled. Try changing <? to <?php. Plus these <?echo don't help. So it could be either/all. If that's the case, you can delete the question. You should also add exit; to all your headers. Commented Jan 13, 2015 at 15:17
  • 2
    @Fred-ii- Good catch. Consider using <?= instead of <?echo Commented Jan 13, 2015 at 15:18
  • @Fred-ii- cheers man, I tried this before but it didn't work, however now it seems to solve the error which is great :D Commented Jan 13, 2015 at 15:26
  • 1
    Glad to hear it James, cheers. I take it problem solved then? Commented Jan 13, 2015 at 15:27
  • 1
    @JamesPatterson There seems to be a world of confusion happening in the answers below. This question should be closed. Commented Jan 13, 2015 at 15:39

2 Answers 2

3

As mentioned above, it seems your PHP parser isn't correctly reading short tags. Sorry to the commenters, I'm not trying to steal an answer, I just pasted the code into NetBeans, and then made the correction in there.

PS. Netbeans is a great debugging tool that could have easily helped you with this! It's a free download, and is great when you're working with PHP projects.

Change line 59 from <? to <?php, and the file should continue reading down to the end.

Sign up to request clarification or add additional context in comments.

8 Comments

The downvote is probably from there being no explanation of your answer, and the fact that this was answered in the comments 8 minutes before you posted this.
I didn't actually see the comments until after I posted.
You're forgetting about the <?echo. ;-)
That alone <?echo will cause a Parse error: syntax error, unexpected end of file. Always "run" a file.
@Lee It's no worries. It simply wasn't in your edit history, probably because you edited it in fast.
|
1

Just change <? to <?php . Use the code below

<?php

session_start();

$page = 'index.php';

mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('cart') or die(mysql_error());

if (isset($_GET['add'])) {
    $quantity = mysql_query('SELECT id, quantity FROM products WHERE id='.mysql_real_escape_string((int)$_GET['add']));
while ($quantity_row = mysql_fetch_assoc($quantity)) {
    if ($quantity_row['quantity']!=$_SESSION['cart_'.(int)$_GET['add']]) {
        $_SESSION['cart_' . (int)$_GET['add']] +='1';
        header('Location: order.php');

    }
}
header('Location: order.php');

}

if (isset($_GET['remove'])) {
    $_SESSION['cart_'.(int)$_GET['remove']]--;
    header ('Location: order.php');

}

if (isset($_GET['delete'])) {
    $_SESSION['cart_' . (int)$_GET['delete']]='0';
    header ('Location: order.php');
}



function products() {
    $get = mysql_query('SELECT id, name, description, price FROM products WHERE quantity > 0 ORDER BY id ASC');
    if (mysql_num_rows($get) == 0) {
        echo "There are no products to display.";
    }
    else {
        echo "<center>\n";
        echo "  <table class='menufinal' border=0 width=75%>\n";
        echo "      <tr>\n";
        echo "      <th>View</th>\n";
        echo "      <th>Dish</th>\n";
        echo "      <th>Description</th>\n";
        echo "      <th>Item Price</th>\n";
        echo "      </tr>\n";
        while ($get_row = mysql_fetch_assoc($get)) {

    ?>
    <tr>
        <td><img src="template.png" height="110" width="110"/> </td>
        <td> <?echo '<p><strong>'.$get_row['name'] . '</strong>'?> </td>
        <td> <?echo $get_row['description']?> </td>
        <td><strong> <?echo '<br>&pound'.number_format($get_row['price'],2).'<br><br> <a href="cart.php?add='.$get_row['id'].'"><button>Add</button></a></p>';?> </strong></td>
    </tr>
    <?php
    } 
    echo "</table>\n";
    echo "</center>\n";
}
} 

function cart() {
$total=0;

$output = '';
$output .= '<center>';
$output .= '<table class="menufinal" border=0 width=75%>';
$output .= '<tr>';
$output .= '<th>Remove Item</th>';
$output .= '<th>Item Name</th>';
$output .= '<th>Item Price</th>';
$output .= '<th>Quantity</th>';
$output .= '<th>Line Total</th>';
$output .= '</tr>';

foreach($_SESSION as $name => $value) {
    if ($value>0){
        if (substr($name, 0, 5)=='cart_') {
           $id = substr($name, 5, (strlen($name)-5));
           $get = mysql_query('SELECT id, name, price FROM products WHERE id='.mysql_real_escape_string((int)$id));
           while ($get_row = mysql_fetch_assoc($get)) {
                $sub = $get_row['price']*$value;
                $total += $sub;
                $output .= '<tr>';
                $output .= '<td><a href="cart.php?delete=' .$id.'"><img src="x.png"></a><br></td>';
                $output .= '<td>' . $get_row['name'] . '</td>';
                $output .= '<td>&pound ' . number_format($get_row['price'], 2) . '</td>';
                $output .= '<td><a href="cart.php?remove=' .$id. '"style="text-decoration:none"><strong>- </strong></a>' .$value. '<a href="cart.php?add=' .$id. '"style="text-decoration:none"> +</a></td>';
                $output .= '<td>&pound ' . number_format($sub, 2) . '</td>';
                $output .= '</tr>';
            }
        } 
        $_SESSION['total'] = $total;
    }
}

$output .= '</table>';

if (empty($total)){
    session_destroy();
    header ('Location: index.php');
    exit;    
}

$output .=  '<br><br><br><br><div id="finalPrice">Total: &pound ' . number_format($total, 2) . '<br></div>';
$output .=  '<br><br><br><p><a href="index.php"><img src="dishes.png" width="240" height="152"></a> <img src="spacer.png" width="110"> <a href="checkout.php"><img src="checkout.png" width="240" height="151"></a>';

echo $output;
}

function cartconfirm() {
$total=0;

$output = '';
$output .= '<center>';
$output .= '<table class="menufinal" border=0 width=75%>';
$output .= '<tr>';
$output .= '<th>Item Name</th>';
$output .= '<th>Item Price</th>';
$output .= '<th>Quantity</th>';
$output .= '<th>Line Total</th>';
$output .= '</tr>';

foreach($_SESSION as $name => $value) {
    if ($value>0){
        if (substr($name, 0, 5)=='cart_') {
           $id = substr($name, 5, (strlen($name)-5));
           $get = mysql_query('SELECT id, name, price FROM products WHERE id='.mysql_real_escape_string((int)$id));
           while ($get_row = mysql_fetch_assoc($get)) {
                $sub = $get_row['price']*$value;
                $total += $sub;
                $output .= '<tr>';
                $output .= '<td>' . $get_row['name'] . '</td>';
                $output .= '<td>&pound ' . number_format($get_row['price'], 2) . '</td>';
                $output .= '<td>' .$value. '</td>';
                $output .= '<td>&pound ' . number_format($sub, 2) . '</td>';
                $output .= '</tr>';
            }
        } 
        $_SESSION['total'] = $total;
    }
}

$output .= '</table>';

if (empty($total)){
    session_destroy();
    header ('Location: index.php');
    exit;    
}

$output .=  '<br><br><br><br><div id="finalPrice">Total: &pound ' . number_format($total, 2) . '<br></div>';

return $output;
}

?>

Explanation: Get the explanation from this link http://uk.php.net/ini.core

Hope this helps you

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.