fez Posted May 1, 2011 Share Posted May 1, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/235268-display-depending-on-whether-user-is-logged-in/ Share on other sites More sharing options...
Fadion Posted May 1, 2011 Share Posted May 1, 2011 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"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/235268-display-depending-on-whether-user-is-logged-in/#findComment-1208998 Share on other sites More sharing options...
herghost Posted May 1, 2011 Share Posted May 1, 2011 try this: if isset(($_SESSION['username'])){ Quote Link to comment https://forums.phpfreaks.com/topic/235268-display-depending-on-whether-user-is-logged-in/#findComment-1208999 Share on other sites More sharing options...
wildteen88 Posted May 1, 2011 Share Posted May 1, 2011 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]. Quote Link to comment https://forums.phpfreaks.com/topic/235268-display-depending-on-whether-user-is-logged-in/#findComment-1209000 Share on other sites More sharing options...
fez Posted May 1, 2011 Author Share Posted May 1, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/235268-display-depending-on-whether-user-is-logged-in/#findComment-1209004 Share on other sites More sharing options...
Fadion Posted May 1, 2011 Share Posted May 1, 2011 You had a missing curly brace before the else. Try the code exactly as I posted and than make further adjustments. Quote Link to comment https://forums.phpfreaks.com/topic/235268-display-depending-on-whether-user-is-logged-in/#findComment-1209013 Share on other sites More sharing options...
fez Posted May 1, 2011 Author Share Posted May 1, 2011 Yes! It's now working. Thankyou, Addict. Quote Link to comment https://forums.phpfreaks.com/topic/235268-display-depending-on-whether-user-is-logged-in/#findComment-1209016 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.