Jump to content

Display depending on whether user is logged in


fez

Recommended Posts

Hi,

 

I'm trying to display a user review system allowing user's to vote. This works fine, but I'm trying to user php to only display the rating system if the user is logged in and display alternate text if they are not. I am getting the following error:

 

Parse error: syntax error, unexpected T_ELSE in XXXXXX on line 182

 

Here's the code:

 

<?php
if ($_SESSION['username']){


$query = mysql_query("SELECT * FROM locations WHERE name = '$location'");

	while($row = mysql_fetch_array($query)) {
		$rating = (int)$row[rating]
			?>
				<div class="floatleft">
					<div id="rating_<?php echo $row[id]; ?>">
						<span class="star_1"><img src="fivestars/star_blank.png" alt="" <?php if($rating > 0) { echo"class='hover'"; } ?> /></span>
						<span class="star_2"><img src="fivestars/star_blank.png" alt="" <?php if($rating > 1.5) { echo"class='hover'"; } ?> /></span>
						<span class="star_3"><img src="fivestars/star_blank.png" alt="" <?php if($rating > 2.5) { echo"class='hover'"; } ?> /></span>
						<span class="star_4"><img src="fivestars/star_blank.png" alt="" <?php if($rating > 3.5) { echo"class='hover'"; } ?> /></span>
						<span class="star_5"><img src="fivestars/star_blank.png" alt="" <?php if($rating > 4.5) { echo"class='hover'"; } ?> /></span>
					</div>
				</div>
				<div class="star_rating">
					(Rated <strong><?php echo $rating; ?></strong> Stars)
				</div>

				<div class="clearleft"> </div>
				}
			}

<?php
else
{

echo "Log in to review";


}
?>

 

Thanks in advance for any help. I'm sure it's something trivial but I can't see it!

 

Link to comment
Share on other sites

The code with the error and a few minor problems fixed.

 

<?php
if ($_SESSION['username']){
$query = mysql_query("SELECT id, rating FROM locations WHERE name = '$location'");

while($row = mysql_fetch_array($query)) {
	$rating = (int) $row['rating'];
	?>
        <div class="floatleft">
            <div id="rating_<?php echo $row['id']; ?>">
                <span class="star_1"><img src="fivestars/star_blank.png" alt="" <?php if($rating > 0) { echo"class='hover'"; } ?> /></span>
                <span class="star_2"><img src="fivestars/star_blank.png" alt="" <?php if($rating > 1.5) { echo"class='hover'"; } ?> /></span>
                <span class="star_3"><img src="fivestars/star_blank.png" alt="" <?php if($rating > 2.5) { echo"class='hover'"; } ?> /></span>
                <span class="star_4"><img src="fivestars/star_blank.png" alt="" <?php if($rating > 3.5) { echo"class='hover'"; } ?> /></span>
                <span class="star_5"><img src="fivestars/star_blank.png" alt="" <?php if($rating > 4.5) { echo"class='hover'"; } ?> /></span>
            </div>
        </div>
        <div class="star_rating">
            (Rated <strong><?php echo $rating; ?></strong> Stars)
        </div>

        <div class="clearleft"> </div>
<?php
}
} else {
echo "Log in to review";
}
?>

Link to comment
Share on other sites

I guess line 182 is

			$rating = (int)$row[rating]

You have forgotten to add ; at the end of that line. Also when using associative arrays you should ideally wrap your keys within quotes $row['rating'] rather than $row[rating].

Link to comment
Share on other sites

Thank you for your help but it still doesn't work!

 

Addict, I inserted your code and it still doesn't seem to work. This is the error:

 

Parse error: syntax error, unexpected T_ELSE in XXXXXX on line 180

 

(line 180 is the line with just 'else')

 

Herghost, this doesn't seem to work either.

 

wildteen88, I did miss this on the code snippet I posted but this doen't seem to be the problem. Line 182 on the original code snippet was also the line with just 'else' on it.

 

Thanks again, any more help appreciated!

Link to comment
Share on other sites

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.