Jump to content

Would an array do the trick?


penguin0

Recommended Posts

I am once again trying to hide a row of mysql output if a variable is false.

 

$result = mysql_query( "SELECT number, name, link, view, isparent, private FROM menu WHERE private = 1 ORDER BY position" );
$num_rows = mysql_num_rows( $result );

print "<table width=\"156\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"ptable\">";
while ( $row = mysql_fetch_array( $result ) ) {

$mnumber = $row['number'];
$mname = $row['name'];
$mlink = $row['link'];
$misparent = $row['isparent'];
$mprivate = $row['private'];
$mview = $row['view'];

if ($misparent == "0") {
$starttag = "<td class=\"off\" onmouseover=\"this.className=\" onmouseout=\"this.className=\" height=\"30\" align=\"left\" valign=\"middle\"><a href=\"/romac$mlink\" class=\"left_menu_text\">";
$endtag = "</a></td></tr>";
} else {

$starttag = "<th><br />";
$endtag = "<br /><br /></th></tr>";
}

$menu_block .= "
<tr>$starttag$mname$endtag";

 

mview = the word "admin" lets say, and the check if the current user has the perm admin would be $admin, returning a 0 or 1.

 

I need to hide a whole table row if the user does not have the correct view perm.  How is the best way to do this?

 

the perms are:

 

$admin

$pageman

$menuman

$userman

$rateman

$users

 

the view columns for the menu are the var without the $...

 

please help!

 

Link to comment
https://forums.phpfreaks.com/topic/52752-would-an-array-do-the-trick/
Share on other sites

what would the mysql statement look like then?  SELECT * from menu WHERE view =?

 

the problem is I have to hide certain menu items, not rows.  I have the menus entered into the database.

 

Hers is the mysql dump (some of it) maybe this will help you understand how I am doing it?

Table structure for table `menu`

CREATE TABLE `menu` (
  `number` mediumint(4) NOT NULL auto_increment,
  `name` varchar(15) NOT NULL default '',
  `link` varchar(40) default NULL,
  `position` tinyint(2) NOT NULL default '0',
  `isparent` tinyint(1) NOT NULL default '0',
  `private` tinyint(1) NOT NULL default '0',
  `view` varchar(15) NOT NULL default 'users',
  `modified` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  PRIMARY KEY  (`number`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=22 ;


-- Dumping data for table `menu`


INSERT INTO `menu` VALUES (1, 'Romac Express', '', 1, 1, 1, 'users', '2007-05-20 20:28:00');
INSERT INTO `menu` VALUES (2, 'Welcome', '/index2.php', 2, 0, 1, 'users', '2007-05-20 12:21:31');
INSERT INTO `menu` VALUES (20, 'Profile', '/profile.php', 3, 0, 1, 'users', '2007-05-20 15:12:41');

the query you provided would show every menu item with a view of admin, but I want people that are not admins to not be able to see that.

 

here is the code for selecting the current logged in user's permissions:

 

 

$result = mysql_query( "SELECT * FROM users WHERE session = '$userssession'" );
$num_rows = mysql_num_rows( $result );

while ( $a_row = mysql_fetch_array( $result ) ) {
$usersid = $a_row['id'];
$usersname = $a_row['name'];
$usersposition = $a_row['position'];
$usersusername = $a_row['username'];
$usersemail = $a_row['email'];
$userscreated = $a_row['created'];
$usersidle = $a_row['idle'];
$online = $a_row['online'];
$admin = $a_row['admin'];
$pageman = $a_row['pageman'];
$userman = $a_row['userman'];
$rateman = $a_row['rateman'];
$menuman = $a_row['menuman'];
$users = $a_row['users'];

}

 

yes I have a seperate field for each permission in the users table.

 

login process is like this:

 

<?
if ((!$_POST[username]) || (!$_POST[password])) {
echo "please enter both Username and Pass";
exit;

}

$time = time();			// not really constants, but stuff used for all pages
$datetime = date('r');
$cookie = $_COOKIE;

function randomize ()
{
mt_srand((double)microtime()*1000000);
}
$sql = "SELECT * FROM users WHERE username = \"$_POST[username]\" AND password = md5(\"$_POST[password]\")";
$result = @mysql_query($sql, $link) or die(mysql_error());
$num = mysql_num_rows($result);

if ($num != 0) {

setcookie("romacuser",$_POST[username],0);
$session = md5($time.$_POST[username].mt_rand());
setcookie("romacsession",$session,0);
$ssession = md5($session);

$sql = "UPDATE users SET session = '$ssession', online = 1, idle = '$time' WHERE username = '$_POST[username]'";
$result = @mysql_query($sql, $link) or die(mysql_error());

header("Location: index2.php");
} else {
$msg = "<font color=\"red\" size=\"4\">Invalid Username and/or Password!</font>";

}
?>

Sorry.. Im still a little lost as to how your doing this.

 

Do you mean to say the value of the field menuman hols the permissions for the menu?

 

If so.. all you would need to do is store this value in a session upon login (or even a cookie if you like). Then, when you need to query the database for the menu you would use...

 

$sql = "SELECT * FROM menu WHERE view = '{$_SESSION['menuman']}'";

 

or something simular.

menu is a table in which certain rows will have a view of admin, menuman, userman and so on.  users is the table that has the perms as fields:  admin, userman, and so on.

 

$admin is the var from users

 

$mview will print the words users, or admin, or menuman based on what the "view" of that link is set to.

 

 

Does no other moderator / PHP guru understand what I am doing?

 

I have all my links in a table, I am trying to hide one or more of the links by checking for the users permissions, which are stored in the variables:

 

$admin

$users

$pageman

$userman

$menuman

$rateman

Here's an alternative method

 

<?php 
$admin        = 32;
$pageman      = 16;
$menuman      = 8;
$userman      = 4;
$rateman      = 2;
$users        = 1;

/**
* Now, if an option is available to admin, pageman and menuman
*/
$perms = $admin + $pageman + $menuman;

/**
* if you are an admin, with userlevel 32
*/
$userlevel = $admin;
$can_view = $perms & $userlevel;
echo "<br>Admin : ";
echo $can_view ? 'Can view' : 'No can view';             // can view

/**
* but if you are a user
*/
$userlevel = $user;
$can_view = $perms & $userlevel;
echo "<br>User : " ;
echo $can_view ? 'Can view' : 'No can view';            // no can view
?>

Thank you barand, I will try this when I get home.  I did not think of adding "permlevels" togather to get a userlevel.  So for the SQL it would be  WHERE view = "what?"  Could this work with my current menu database, or do I need to change something?

 

menu database:

 

Table structure for table `menu`

CREATE TABLE `menu` (
  `number` mediumint(4) NOT NULL auto_increment,
  `name` varchar(15) NOT NULL default '',
  `link` varchar(40) default NULL,
  `position` tinyint(2) NOT NULL default '0',
  `isparent` tinyint(1) NOT NULL default '0',
  `private` tinyint(1) NOT NULL default '0',
  `view` varchar(15) NOT NULL default 'users',
  `modified` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  PRIMARY KEY  (`number`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=22 ;


-- Dumping data for table `menu`


INSERT INTO `menu` VALUES (1, 'Romac Express', '', 1, 1, 1, 'users', '2007-05-20 20:28:00');
INSERT INTO `menu` VALUES (2, 'Welcome', '/index2.php', 2, 0, 1, 'users', '2007-05-20 12:21:31');
INSERT INTO `menu` VALUES (20, 'Profile', '/profile.php', 3, 0, 1, 'users', '2007-05-20 15:12:41');

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.