Jump to content

Recommended Posts

Hi

 

I found this code to help with my previous question however it doesnt seem to work because im getting the error

 

Parse error: syntax error, unexpected '=', expecting ')' relating to line 35, any idea what the prob could be?

 

<?php
/* set the allowed order by columns */
$default_sort = 'CustomerID';
$allowed_order = array ('Name', 'Address','Postcode','Telephone','Email');

/* if order is not set, or it is not in the allowed
* list, then set it to a default value. Otherwise, 
* set it to what was passed in. */
if (!isset ($_GET['order']) || 
    !in_array ($_GET['order'], $allowed_order)) {
    $order = $default_sort;
} else {
    $order = $_GET['order'];
}

/* connect to db */
mysql_connect ('localhost','user','pass);
mysql_select_db ('database');

/* construct and run our query */
$query = "SELECT * FROM table ORDER BY $order";
$result = mysql_query ($query);

/* make sure data was retrieved */
$numrows = mysql_num_rows($result);
if ($numrows == 0) {
    echo "No data to display!";
    exit;
}

/* now grab the first row and start the table */
$row = mysql_fetch_assoc ($result);
echo "<TABLE border=1>\n";
echo "<TR>\n";
foreach ($row as $heading=>$column) {
    /* check if the heading is in our allowed_order
     * array. If it is, hyperlink it so that we can
     * order by this column */
    echo "<TD><b>";
    if (in_array ($heading, $allowed_order)) {
        echo "<a href=\"{$_SERVER['PHP_SELF']}?order=$heading\">$heading</a>";
    } else {
        echo $heading;
    }                
    echo "</b></TD>\n";
}
echo "</TR>\n";

/* reset the $result set back to the first row and 
* display the data */
mysql_data_seek ($result, 0);
while ($row = mysql_fetch_assoc ($result)) {
    echo "<TR>\n";
    foreach ($row as $column) {
        echo "<TD>$column</TD>\n";
    }
    echo "</TR>\n";
}
echo "</TABLE>\n";
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>

<body>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/82942-solved-gt-will-this-work/
Share on other sites

This line:

foreach ($row as $heading=>$column) {

which I presume is line 35 is supposed to be:

foreach ($row as $heading => $column) {

 

Also it looks like for some reason the HTML has been converted to htmlspecialcars you will need to convert any instances of < to < and > to > in order for your html code to work properly when outputted by the PHP code.

 

 

Also when posting code please use the code tags (


)

I cannot see why you are your code is outputting a ; at the top of the page. You mostly likely have a random ; somewhere within your HTML when your output the table. Run the code again but this time view the source code (View > Source Code) and have a look to see where the ; is within the outputted HTML. That will give an idea of where it is within your PHP code.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.