Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Sep 2009
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Unhappy Parse error in PHP script

    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:

    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();
    }
    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:

    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); ?>

  • #2
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    this is the only issue that I see:

    Code:
    case 'we'
    should be

    Code:
    case 'we':

  • #3
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    This line
    PHP Code:
    $sort = (isset($_GET['sort'])) ? $_GET['sort'] : 'id'
    Has a misplaced right paren. It should be:

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

  • #4
    New Coder
    Join Date
    Sep 2009
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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

  • #5
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    Quote Originally Posted by Fumigator View Post
    This line
    PHP Code:
    $sort = (isset($_GET['sort'])) ? $_GET['sort'] : 'id'
    Has a misplaced right paren. It should be:

    PHP Code:
    $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.

  • #6
    Regular Coder bacterozoid's Avatar
    Join Date
    Jun 2002
    Location
    USA
    Posts
    490
    Thanks
    24
    Thanked 35 Times in 35 Posts
    It works because it's useless in both cases. The simplest form is this:

    PHP Code:
    $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.

    PHP Code:
    $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.

    PHP Code:
    $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:

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


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •