Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Display Admin Link


  • Please log in to reply
25 replies to this topic

#1 Bioshox

Bioshox

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 210 posts

Posted 15 February 2010 - 09:42 AM

Hey

Iv been trying to get my blog system to display a Login In Admin Center link but I cant seem to get it to work

Iv been trieing to get it to display the link if the field in the SQL database says 'Yes' the field is called admin

//DISPLAY ADMIN LINK
	$result = mysql_query("SELECT * FROM regusers WHERE admin='$admin'");

	if(mysql_result($admin) == yes) {
		//echo "Test";
		//}else{
	}

It dosent seem to work though

Anyone help me!?
  • 0

#2 webcodez

webcodez

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 143 posts

Posted 15 February 2010 - 10:06 AM

First off in the query you supplying you search for a row where the field admin is equal to the value of the variable $admin. Why is this? Because as from your description you want to check whether the field 'admin' is set to 'yes' and not to '$admin', Maybe you set the wrong field inside the query where clause?

Another thing is I don't get what you're trying to achieve with ' mysql_result($admin)' ?

And what row needs to have the field ' admin ' set to ' Yes '? As now you're checking a field on all rows which isn't really what you're meaning to do I suppose.

Edited by webcodez, 16 February 2010 - 08:25 AM.

  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#3 Bioshox

Bioshox

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 210 posts

Posted 15 February 2010 - 10:16 AM

This would explain why its not working.

Fail!

How would I achieve this then?
  • 0

#4 webcodez

webcodez

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 143 posts

Posted 15 February 2010 - 10:19 AM

Well, is it an option you want to be able to be set to 'Yes' or 'No'? Just a global option?
So when this option is set to 'Yes' the admin link is shown, if set to 'No' it's hidden?
Because in that case it shouldn't be a field of the 'regusers' table which I assume to be having a list of all members (rows), and this option would just require ONE row, as it's just a global option in that case, with one value ( not varying for different members or something ).

let me know, so I can see if we're on the same line :)

Edited by webcodez, 16 February 2010 - 08:25 AM.

  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#5 Bioshox

Bioshox

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 210 posts

Posted 15 February 2010 - 10:31 AM

Yeah Thats Right

The Database Holds The Passwords, Email and Username with the Admin Field Display Either Yes or No

If Yes Display the link
if no dont.

Easyer to edit in the admin panel then!
  • 0

#6 webcodez

webcodez

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 143 posts

Posted 15 February 2010 - 10:44 AM

Yes, well but you don't want to set the Admin field to Yes or No for all new users as well right? As it's user 'independent', not depending on what user, it's a global option you want to set so it would be weird to set it for each user and beside that it would be able to cause errors as it will select only ONE row (member) its value for the field 'Admin', so when there are more members with different values for that field it can lead to errors/selecting the wrong value.

So what I'd suggest is creating another table with all options for your blog, which can beside this option as well provide you the ability to add & check more options for your blog easily.

Example of table:
Table name: options
field1
name: ID
type: 250(INT)
auto-increment & Primary Key
field2
name: opt_name
type: 20(VARCHAR)
field3
name: opt_value
type: 250(VARCHAR)

Add a row for the 'admin' option
Set: field 'opt_name' = 'admin' , 'opt_value' = 'Yes' (or 'No')

Example of script:


<?php

$check_admin = mysql_query("SELECT opt_value FROM options WHERE opt_name = 'admin' "); //select the value of the option 'admin'
$opt_admin = mysql_fetch_assoc($check_admin); //get the result
$opt_admin = $opt_admin['opt_value']; //get the value of the option

if($opt_admin == 'Yes') {

//show the link

}
?>


It's also possible in another way, by adding inside the WHERE clause: & opt_value = 'Yes'
And then just counting the rows found as result using mysql_num_rows. If this results in a number higher than 0 ( if properly done it should be 0 or 1, 1 if the row was foujnd with as value 'Yes', 0 if the rows wasn't found with the value 'Yes' ( so it contains another value: 'No' assumed )).

2nd method:


<?php

$check_admin = mysql_query("SELECT opt_value FROM options WHERE opt_name = 'admin' AND opt_value ='Yes' "); //select the value of the option 'admin'
$opt_admin = mysql_num_rows($check_admin); //count the rows found

if($opt_admin > 0) { //if any rows where found with option name = 'admin' and option value 'Yes'

//show the link

}
?>


ANOTHER method would be using COUNT, but I guess 2 methods is enough :D
Just use one of them and you should be fine :).

Table SQL


CREATE TABLE IF NOT EXISTS `options` (
`id` int(250) NOT NULL AUTO_INCREMENT,
`opt_name` varchar(20) NOT NULL,
`opt_value` varchar(250) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

INSERT INTO `options` (`id`, `opt_name`, `opt_value`) VALUES
(1, 'admin', 'Yes');


Note: Would rather use something like 0 or 1 to indicate the admin field to be 'Yes' (true = 1) or 'No' (false = 0), but doesn't matter too much I guess.

Edited by webcodez, 16 February 2010 - 08:24 AM.

  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#7 Bioshox

Bioshox

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 210 posts

Posted 15 February 2010 - 01:50 PM

Thanks so much for your help!!

So would i need to re integrate username and a password field into this new table for it to work?!

I'll let you know if it works!
  • 0

#8 webcodez

webcodez

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 143 posts

Posted 16 February 2010 - 12:42 AM

You're Welcome :)

No there's no need to integrate any username or password field as it's just a global option. It's not working different for different users right? So no need to put username or password field as it hasn't got anything to do with it.

Edited by webcodez, 16 February 2010 - 08:24 AM.

  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#9 Bioshox

Bioshox

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 210 posts

Posted 16 February 2010 - 02:32 AM

Sorry for sounding abit of an idiot here

But how would I then set that global option for a user?

Imagine if you was an Admin on this forum, the Admin Link shows up because another Admin has made you an Admin, but if you are a regular member, it wont.

I'm also guessing I could put the PHP code in a separate PHP File and include it on the pages its needed

I could also incorporate it to only show parts of the admin section if you are an admin?


Thank's!!

Edited by Bioshox, 16 February 2010 - 07:11 AM.

  • 0

#10 webcodez

webcodez

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 143 posts

Posted 16 February 2010 - 07:11 AM

Then what you want to do is check 2 things: Whether the user logged in is an Admin, and whether the global option to show the admin link for admins is set to 'Yes' right?

Or you just want to show the admin link for admins always? (or depending on whether this global option is set to 'Yes' or 'No'?)

EDIT: in the first case you would want to set a field like 'Role' for each user. Then you could have for example '1' representing the role of an administrator, value '2' representing the role of a moderator, role '3' representing the role of a normal member - for example. Then all you'd need to do is check the role of the currently logged in user. If he's got the role of a admin ( or moderater for example, if you want these to allow the admin section as well ) then show the link. If this is what you want to do, let me know and I'll write an example :).

EDIT2:

Example:


<?php
session_start();

//assuming the username of the user logged in is put in $_SESSION['username']

$get_role = mysql_query("SELECT role FROM accounts WHERE username = '".$_SESSION['username']."' ");
$get_role = mysql_fetch_assoc($get_role);
$role = $get_role['role'];

if($role == '1') { //if the user is an administrator

//show admin link

}

?>


Then for the register system you should by default add the value 0 or 3 ( all depends on what you want to represent the role of a normal user of ) when creating a new normal user account.

You could also expand it more by creating another table with all roles and priveleges but that's however more advanced to do but same idea.

If you only want to show the admin links for admin users when the global option 'admin_link' is set to 'Yes' as well (for example), you could have a script like this:

Example:


<?php
session_start();

//assuming the username of the user logged in is put in $_SESSION['username']

$get_option = mysql_query("SELECT opt_value FROM options WHERE opt_name = 'admin_link' ");
$get_option = mysql_fetch_assoc($get_option);
$option = $get_option['opt_value']; //value of global option 'admin_link' which allows you to define whether to show an admin links for admins or not


//show any admin link at all if administrator logged in? :
if($option == 'Yes') { //yes -> check if user logged in is an admin

$get_role = mysql_query("SELECT role FROM accounts WHERE username = '".$_SESSION['username']."' ");
$get_role = mysql_fetch_assoc($get_role);
$role = $get_role['role'];

if($role == '1') { //if the user is an administrator

//show admin link

}

}

?>


Now use the SQL of the previous post and add a field 'role' to the user accounts table, and should work :)

If this isn't what you meant either just tell me :D

Hope this helped,

Cheers.

Edited by webcodez, 16 February 2010 - 08:24 AM.

  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#11 Bioshox

Bioshox

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 210 posts

Posted 16 February 2010 - 09:59 AM

Haii

I went with this method
Although when I implement it into the code I get a MySQL error

Code:

<?php 
//assuming the username of the user logged in is put in $_SESSION['username'] 

$get_role = mysql_query("SELECT role FROM users WHERE username = '".$_SESSION['username']."' "); 
$get_role = mysql_fetch_assoc($get_role); 
$role = $get_role['role']; 


if($role == '1') { //if the user is an administrator 

   //show admin link 
   echo 'test';

} 

?>

Error

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/a2469246/public_html/live/index.php on line 92


  • 0

#12 webcodez

webcodez

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 143 posts

Posted 16 February 2010 - 10:16 AM

Did you set session_start() and are you sure $_SESSION['username'] contains the valid username of the logged in user ( and that the user is logged in at all ^^ )?
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download