Oscar
Oscar

Reputation: 541

php - Strange "unexpected end of file" error

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;
}

?>

Upvotes: 1

Views: 608

Answers (2)

Utkarsh Dixit
Utkarsh Dixit

Reputation: 4275

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

Upvotes: 1

Lee
Lee

Reputation: 4323

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.

Upvotes: 3

Related Questions