14 Replies - 742 Views - Last Post: 24 November 2013 - 07:09 AM Rate Topic: -----

#1 chris98  Icon User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 898
  • Joined: 06-July 13

If statement trigger problem

Posted 21 November 2013 - 10:46 AM

I am trying to create a system for my staff page in which if the user currently logged in is the user that the paragraph about on the staff list, then they can edit the paragraph. Otherwise, if they aren't that user or an administrator, they can't edit it. So far, I have this, but it's not triggering.

I have echoed the contents of $row['userid'] and in all cases, there is something there.

<?php
$ps = $pdo_2->query("SELECT `staff_id`,`avatar`, `username`, `staff_position`, `email`, `admin_note`, `text`, `userid` FROM `staff` WHERE `position` = 'Management'");
foreach ($ps as $row)
{
?>
<table width="99%">
<tr><td><img src="<?php echo $row['avatar']; ?>" hspace="10" vspace="20" align="left" />
<font color="red"><?php echo $row['username']; ?></font color><br />
<b><font color="#01DFD7"><?php echo $row['staff_position']; ?></font color></b><br />
E-Mail: <?php if ($row['email'] == 'private')
{
echo "<i><strong>Private</strong</i>";
}
else
{
echo "<b><a href='mailto:".$row['email']."'>".$row['email']."</a></b>";
}
?><br />
<img src="http://forums.strongholdnation.co.uk/img/image_group/1.png"></p></td></tr>
<tr><td><br>
<p><?php if ($row['text'] < '1') 
echo "No text for this staff member found.";
else
echo nl2br($row['text']); 
?><?php if ($pun_user['g_id'] != PUN_ADMIN || $pun_user['id'] !== ''.$row['userid'].'')
{
echo "";
}
else
{
echo "<br /><br /><a href='staff/edit_staff.php?id=".$row['staff_id']."'><img src='images/edit_file.gif' border='0'>Edit User's Paragraph</a>";
}
?></p></td></tr></table><br><br>
<?php } ?>



Live demo:

http://strongholdnation.co.uk/staff

Is This A Good Question/Topic? 0
  • +

Replies To: If statement trigger problem

#2 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3833
  • View blog
  • Posts: 13,580
  • Joined: 12-December 12

Re: If statement trigger problem

Posted 21 November 2013 - 10:56 AM

Please explain what you mean by "it's not triggering".

if ($pun_user['g_id'] != PUN_ADMIN || $pun_user['id'] !== ''.$row['userid'].'')

Are $pun_user and PUN_ADMIN correctly defined, and contain the values you expect? Use print_r() to display these values.

In particular, you have switched from 'g_id' to 'id' for $pun_user: is this correct.
Was This Post Helpful? 0
  • +
  • -

#3 chris98  Icon User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 898
  • Joined: 06-July 13

Re: If statement trigger problem

Posted 21 November 2013 - 11:03 AM

Yes, these are correctly defined. My login system is from fluxbb.

'g_id' is the group id of the user, whilst 'id' is the individual user id.

Fluxbb variables

http://fluxbb.org/do...iables#pun_user

I will use print_r and tell you the results.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3833
  • View blog
  • Posts: 13,580
  • Joined: 12-December 12

Re: If statement trigger problem

Posted 21 November 2013 - 11:08 AM

View Postchris98, on 21 November 2013 - 06:03 PM, said:

I will use print_r and tell you the results.

..view the source in the browser as it will be easier to read the values from there.
Was This Post Helpful? 0
  • +
  • -

#5 chris98  Icon User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 898
  • Joined: 06-July 13

Re: If statement trigger problem

Posted 21 November 2013 - 12:30 PM

What do you mean, there aren't any values there?

All I can see is the HTML that was added before that.
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3833
  • View blog
  • Posts: 13,580
  • Joined: 12-December 12

Re: If statement trigger problem

Posted 21 November 2013 - 12:36 PM

When you are looking at the page in the browser, using View Source (or similar) will display the variables you are printing in a more readable format than can be read directly from the browser.

Alternatively, use:

echo '<pre>';
print_r($pun_user);
echo '</pre>';

print_r() the docs
Was This Post Helpful? 0
  • +
  • -

#7 chris98  Icon User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 898
  • Joined: 06-July 13

Re: If statement trigger problem

Posted 21 November 2013 - 12:39 PM

Would $pun_user not give the current user's details though?

Or was that what you were wanting?
Was This Post Helpful? 0
  • +
  • -

#8 chris98  Icon User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 898
  • Joined: 06-July 13

Re: If statement trigger problem

Posted 21 November 2013 - 01:10 PM

I have used print_r() on $pun_user, and there doesn't appear to be any problems.


However, I have noticed that when I am logged in as an admin, AND I hav my own user id, my edit link appears but no others do.
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3833
  • View blog
  • Posts: 13,580
  • Joined: 12-December 12

Re: If statement trigger problem

Posted 21 November 2013 - 01:18 PM

Perhaps your logic is incorrect here:

if ($pun_user['g_id'] != PUN_ADMIN || $pun_user['id'] !== ''.$row['userid'].'')

Perhaps you meant and (&&) :dontgetit:
Was This Post Helpful? 0
  • +
  • -

#10 chris98  Icon User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 898
  • Joined: 06-July 13

Re: If statement trigger problem

Posted 21 November 2013 - 01:21 PM

(Maybe I'm wrong, but) I thought that at first. Later I realised that some staff are moderators, so that would trigger if you are not an admin and your user id is not a specific one. Wouldn't that mean that you would have to be an admin and have that id?

This post has been edited by chris98: 21 November 2013 - 01:25 PM

Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3833
  • View blog
  • Posts: 13,580
  • Joined: 12-December 12

Re: If statement trigger problem

Posted 21 November 2013 - 01:30 PM

It is generally easier to handle the most direct, clearest, condition first:

  • If they are a MOD or an ADMIN or the user-id matches.. let them edit.
  • For anyone else, they can't edit.

If there are other relevant roles (other than MOD or ADMIN) then modify accordingly.
Was This Post Helpful? 0
  • +
  • -

#12 chris98  Icon User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 898
  • Joined: 06-July 13

Re: If statement trigger problem

Posted 21 November 2013 - 01:30 PM

Sorry, I do apologise. It appears you are right, that's all I needed to change.

Now only admins can edit - I don't understand how that's all, but it is.

This post has been edited by chris98: 21 November 2013 - 01:32 PM

Was This Post Helpful? 0
  • +
  • -

#13 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3103
  • View blog
  • Posts: 10,898
  • Joined: 08-August 08

Re: If statement trigger problem

Posted 21 November 2013 - 02:07 PM

Mixing languages makes code harder to read and therefore harder to debug. While it's often convenient to put small amounts of HTML in with PHP code it quickly becomes confusing with longer code and when the split is roughly equal as it is in your code. Think about using different files for different languages.
Was This Post Helpful? 0
  • +
  • -

#14 chris98  Icon User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 898
  • Joined: 06-July 13

Re: If statement trigger problem

Posted 23 November 2013 - 07:20 AM

I have read your tutorial several times, but I don't fully understand it. However, would something like the following be OK?

<?php
$action = isset($_GET['action']) ? $_GET['action'] : null;
if ($action == 'something')
{
//php action code
?>
<p> This is the html for the action </p>
<?php } ?>


This post has been edited by chris98: 23 November 2013 - 07:20 AM

Was This Post Helpful? 0
  • +
  • -

#15 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3103
  • View blog
  • Posts: 10,898
  • Joined: 08-August 08

Re: If statement trigger problem

Posted 24 November 2013 - 07:09 AM

The computer does not care if you mix PHP with HTML, CSS, and Javascript in one file. The key is the human reading it: can they easily follow the code? There are two things that mixing languages does to make it harder to read. One is that it makes it longer and the other is that it changes the thought process from one language to another and back, often frequently. In short snippets like yours above neither factor is in play. The code is short and only breaks out of PHP once so it's easily read. Your initial post is still relatively short but already a jumble of HTML and PHP. By the time you get to 100 lines of code you'll likely be unable to follow it.

By the way, if you don't understand something including my tutorial you should ask specific questions about it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1