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
It dosent seem to work thoughCode://DISPLAY ADMIN LINK $result = mysql_query("SELECT * FROM regusers WHERE admin='$admin'"); if(mysql_result($admin) == yes) { //echo "Test"; //}else{ }
Anyone help me!?
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.
Last edited by webcodez; 02-16-2010 at 08:25 AM.
This would explain why its not working.
Fail!
How would I achieve this then?
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![]()
Last edited by webcodez; 02-16-2010 at 08:25 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!
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:
It's also possible in another way, by adding inside the WHERE clause: & opt_value = 'Yes'Code:<?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
}
?>
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:
ANOTHER method would be using COUNT, but I guess 2 methods is enoughCode:<?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
}
?>![]()
Just use one of them and you should be fine.
Table SQL
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.Code: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');
Last edited by webcodez; 02-16-2010 at 08:24 AM.
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!
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.
Last edited by webcodez; 02-16-2010 at 08:24 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!!
Last edited by Bioshox; 02-16-2010 at 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:
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.Code:<?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
}
?>
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:
Now use the SQL of the previous post and add a field 'role' to the user accounts table, and should workCode:<?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
}
}
?>
If this isn't what you meant either just tell me
Hope this helped,
Cheers.
Last edited by webcodez; 02-16-2010 at 08:24 AM.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks