...

View Full Version : Parse error in PHP script



pindi
09-18-2009, 05:53 PM
I hate to post about something as straightforward as a parse error, but I honestly can't puzzle this one out. The error is as follows:


Parse error: parse error in C:\XAMPP\xampp\htdocs\pindi_2009\loggedin.php on line 26

Although the problem isn't on line 26 (that's the line that says $order_by = "PortfolioNumber ASC"; ) because if I comment out that line, the line with the error moves 'up' (e.g. "parse error...on line 24, 21, 8 and so on). So I tried commenting out and moving up and I got to this section on or about here:


<?php #Script 11.9 loggedin.php #2
//The use is redirected here from login.php.
session_start(); //Start the session.
//If no session value is present, redirect the user.
if (!isset($_SESSION['ID_num'])) {
require_once('login_functions.php');
$url=absolute_url();
header("Location: $url");
exit();
}

So basically right at the beginning.

Everything worked fine until I added the 'sort order' code (at about line 17). But I guess I must have deleted or added a space or something that shouldn't be there. I've tried playing around with quotation marks and spacing to no avail. Tried to check the brackets and braces and it seems to jive ok.

I get the sinking feeling I'm missing something obvious, but what? Can it be some kind of error passed along from the 'login_functions.php' page that's causing the parse error?

For reference, here is the entire page of code:


<?php #Script 11.9 loggedin.php #2
//The use is redirected here from login.php.
session_start(); //Start the session.
//If no session value is present, redirect the user.
if (!isset($_SESSION['ID_num'])) {
require_once('login_functions.php');
$url=absolute_url();
header("Location: $url");
exit();
}
$page_title='Logged In!';
//Print a customized message:
echo "<p>You are now logged in, {$_SESSION['First']}!</p>
<a href=\"logout.php\">Logout</a></p>";
require_once('connect.php');
//fixing the sort order
$sort = (isset($_GET['sort'])) ? $_GET['sort'] : 'id';
switch ($sort){
case 'id':
$order_by = "IDNumber ASC";
break;
case 'ti':
$order_by = "Title ASC";
break;
case 'po':
$order_by = "PortfolioNumber ASC";
break;
case 'me':
$order_by = "Medium ASC";
break;
case 'we'
$order_by = "Web ASC";
break;
}



//Getting the info
$q = "SELECT * FROM art ORDER BY $order_by";
$r = @mysqli_query ($dbc, $q);
?>
<table border="1px"><tr><b><td><a href = "loggedin.php?sort=id">IDNumber</a></td><td><a href = "loggedin.php?sort=ti">Title</a></td>
<td><a href = "loggedin.php?sort=po">Portfolio Number</a></td><td><a href = "loggedin.php?sort=me">Medium</a></td><td><a href = "loggedin.php?sort=we">On web</a></td></b></tr>
<?php
if($r){
while ($row = mysqli_fetch_array($r,MYSQLI_ASSOC)){
?>
<tr><td><?=$row['IDNumber']?></td><td><?=$row['Title']?></td><td><?=$row['PortfolioNumber']?></td><td><?=$row['Medium']?></td><td><?=$row['Web']?></td></td>
<?php
}
}
else
{echo mysqli_error($dbc);}
?>
</table>
<?php mysqli_close($dbc); ?>

angst
09-18-2009, 06:36 PM
this is the only issue that I see:



case 'we'


should be



case 'we':

Fumigator
09-18-2009, 06:37 PM
This line


$sort = (isset($_GET['sort'])) ? $_GET['sort'] : 'id';

Has a misplaced right paren. It should be:


$sort = (isset($_GET['sort']) ? $_GET['sort'] : 'id');

pindi
09-18-2009, 06:41 PM
Hey angst, weirdly enough that fixed the problem: strange that the parse error was moving 'up' but the actual error was way down on line 31. I was looking too high and not low!

As for the parenthesis suggestion, Fumigator, thanks and (maybe just as strange) it works both ways. Maybe your way is better form?

Thanks,
J

angst
09-18-2009, 06:45 PM
This line


$sort = (isset($_GET['sort'])) ? $_GET['sort'] : 'id';

Has a misplaced right paren. It should be:


$sort = (isset($_GET['sort']) ? $_GET['sort'] : 'id');

it seems to me that the only time is needs to be formatted like that is when it's in an IF statement.

bacterozoid
09-18-2009, 07:43 PM
It works because it's useless in both cases. The simplest form is this:


$sort = isset($_GET['sort']) ? $_GET['sort'] : 'id';

However, you could add parenthesis if you REALLY wanted around the whole thing like fumigator suggested...but that doesn't make much sense.


$sort = (isset($_GET['sort']) ? $_GET['sort'] : 'id');

You could also add them around the isset condition before the ?. This might actually help you read it, but isn't necessary.


$sort = (isset($_GET['sort'])) ? $_GET['sort'] : 'id';

The only time you would need the extra set here is if you had multiple conditions before the ?, like so:


$sort = (isset($_GET['sort']) && $var == 'something') ? $_GET['sort'] : 'id';



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum