Jump to content

[SOLVED] Array problems


Skepsis

Recommended Posts

I'm having problems with my permissions system. What I'm trying to add is a feature to add a user group.

 

When adding the group, we have checkboxes with certain fields that come from information that is stored in the $perms array.

 

First off, I'm trying to make it so when a checkbox is checked by the permission value, it is 1, if its not checked, the value is 0, that is already done.

 

When I post my form, the error I receive is:

 

Fatal error: Unsupported operand types in C:Program Filesxampphtdocslenixmodules  est.php on line 50

 

I have never even had that error before.

 

The $perms array looks like this:

<?php
$perms = array(    'forum' => array(
        1 => 'f_view',
        2 => 'f_post_topic',
        3 => 'f_post_reply',
        4 => 'f_attach',
        5 => 'f_manage_comment',
        6 => 'f_manage_cat',
        7 => 'f_moderator',
        8 => 'f_bypass_flood',
    ),    
    'users' => array(
        1 => 'u_view_users',
        2 => 'u_view_online',
        3 => 'u_view_profile',
        4 => 'u_manage_users',
        5 => 'u_manage_groups',
    ),
)    

// So how I display these is like this:

foreach($GLOBALS['perms'] as $permgroup => $perms )
{
    echo $permgroup.'<br>';

    foreach( $perms as $bit => $name )
    {
        echo '--'.$name.'<br>';
    }
}
?>

 

Notice this isn't the full html, I have certain code for the check boxes, but that's not important right now.

 

All that displays all fine and everything, the html is perfectly fine, getting the post data shouldn't be a problem.

 

This is where my code goes wrong:

 

<?php
// if any permissions were checked
if( $_POST['perms'] )
{
    // for all of the groups that had any checked boxes
    foreach($_POST['perms'] as $permgroup => $p)
    {
        // for each item checked in the bitfield
        foreach($p as $bit)
        {
            // add the bit to the permission group
            $perms[$permgroup] += $bit;
        }
    }
}

// the error is on this line of code:

// add the bit to the permission group
$perms[$permgroup] += $bit;
?>

 

Does anybody see anything out of the ordinary? I sure don't. This is why I'm asking for help, lol.

 

If anybody can point me in the right, or a better direction, that would be completely awesome, and I would be very thankful.

 

Thanks in advance.

Link to comment
Share on other sites

When I post my form, the error I receive is:

 

Fatal error: Unsupported operand types in C:Program Filesxampphtdocslenixmodules  est.php on line 50

 

I have never even had that error before.

This is most probably because you're using double quotes for your include paths.

 

Either use single quotes

include 'C:\program files\xampp\htodcs\leniz\modules\test.php';

 

or use backslashes (/) instead of forward slashes (\) as the path seperater.

include "C:/program files/xampp/htodcs/leniz/modules/test.php";

Link to comment
Share on other sites

The file being included is not a problem, I have a certain function that includes files in the modules/ folder, i've never had a problem with including the file.

 

Plus the error states that the error occurrs on line 50, which is:

<?php  $perms[$permgroup] += $bit; ?>

 

 

Here is a link of what the code is producing, you can do whatever you can try to add a group yourself.

http://beta4.comoj.com/index.php?n=modules/test&a=1

 

And here is the source code:

<?php
switch($_GET['a'])
{

    case 1:
    
        //-----------------------------------------------------------------
        //  Post Action
        //-----------------------------------------------------------------

        if($_POST['a'] == 1)
        {
            //-----------------------------------------------------------------
            //  Do Checks
            //-----------------------------------------------------------------

            if(!$_POST['name']) $i = 1;
            elseif(!$_POST['title']) $i = 2;
            else
            {
                //-----------------------------------------------------------------
                //  SQL Checks
                //-----------------------------------------------------------------

                $group['name'] = $db->getLine("SELECT name FROM groups WHERE name = '{$db->escape($_POST['name'])}'");
                $group['title'] = $db->getLine("SELECT title FROM groups WHERE title = '{$db->escape($_POST['title'])}'");

                //-----------------------------------------------------------------
                //  Further Checks
                //-----------------------------------------------------------------

                if($group['name'] == TRUE) $i = 3;
                elseif($group['title'] == TRUE) $i = 4;
                else
                {
                    //-----------------------------------------------------------------
                    //  Define Variables
                    //-----------------------------------------------------------------                
                    
                    if( $_POST['perms'] )
                    {
                        // for all of the groups that had any checked boxes
                        foreach($_POST['perms'] as $permgroup => $p)
                        {
                            // for each item checked in the bitfield
                            foreach($p as $bit)
                            {
                                $perms[$permgroup] += $bit;
                            }
                        }
                    }
                    //-----------------------------------------------------------------
                    //  Define Variables
                    //-----------------------------------------------------------------

                    $info['type'] = "user_defined";
                    $info['name'] = $_POST['name'];
                    $info['title'] = $_POST['title'];
                    $info['description'] = $_POST['JCM_textarea'];
                    $info['perms'] = serialize($_POST['perms']);

                    //-----------------------------------------------------------------
                    //  Auto Incriment
                    //-----------------------------------------------------------------

                    $table_status = $db->getLine("SHOW TABLE STATUS LIKE 'groups'");

                    //-----------------------------------------------------------------
                    //  Add To Database
                    //-----------------------------------------------------------------

                    $db->insertRow("groups", $info, "New Group Added - <a href='$PHP_SELF?n=modules/groups&a=4&d={$table_status['Auto_increment']}'>{$info['name']}</a>");

                    //-----------------------------------------------------------------
                    //  Reset Groups Cache
                    //-----------------------------------------------------------------

                    $cache->reset_cache("groups_db", "cache_groups.php", "SELECT id, type, name, description, title, perms FROM groups");

                    //-----------------------------------------------------------------
                    //  Redirect User
                    //-----------------------------------------------------------------

                    redirect("{$PHP_SELF}?n=modules/groups&o=1");
                }
            }
        }
    
        //-----------------------------------------------------------------
        //  Take care of defaults
        //-----------------------------------------------------------------

        if(!isset($_POST['allow_access'])) $_POST['allow_access'] = '1';

        //-----------------------------------------------------------------
        //  Define the alert and normal classes and fonts
        //-----------------------------------------------------------------

        $alert_class = "class='alert'"; $alert_font = "color='red'";
        $normal_class = "class='con1'"; $normal_font = "color='green'";

        //-----------------------------------------------------------------
        //  Define the error messages
        //-----------------------------------------------------------------

        if($i == 1) $status = " [ <font color='red'>Group Name Cannot Be Blank</font> ]";
        elseif($i == 2) $status = " [ <font color='red'>Group Title Cannot Be Blank</font> ]";
        elseif($i == 3) $status = " [ <font color='red'>Group Name Taken</font> ]";
        elseif($i == 4) $status = " [ <font color='red'>Group Title Taken</font> ]";
        else $status = NULL;
    ?>
        <table class="main" cellpadding="4" cellspacing="1">
        <tr class='con1'><td><a href="<?= $PHP_SELF ?>?n=modules/admin">Administration</a> » <a href="<?= $PHP_SELF ?>?n=modules/groups">Groups Management</a> » Add Group</td></tr>
        </table>

        <table><tr><td></td></tr></table>

        <script language="JavaScript">function submitForm() { updateRTEs(); return true; }</script>
        <form onSubmit="return submitForm();" action="<?php echo htmlspecialchars($_SERVER['REQUEST_URI']);?>" method="post">
        <div>
        <script language="JavaScript" type="text/javascript" src="templates/<?=$template?>/extra/richtext.js"></script>
        <script language="JavaScript" type="text/javascript">initRTE("templates/<?=$template?>/images/bbcode/", "templates/<?=$template?>/rte/", false); </script>
        <table class="main" cellpadding="4" cellspacing="1">
        <tr class='head'><td colspan="3">Add User Group<?= $status ?></td></tr>
        <tr class='bottom'><td colspan="3">Details</td></tr>
        <tr <?=($i==1||$i==3?$alert_class:$normal_class)?>>
        <td><font <?=($i==1||$i==3?$alert_font:$normal_font)?>>Group Name:</font></td>
        <td colspan="2"><input type="text" name="name" style="width: 80%" autocomplete="off" class="box" value="<?=(isset($_POST['name'])?$_POST['name']:"")?>"></td>
        </tr>
        <tr <?=($i==2||$i==4?$alert_class:$normal_class)?>>
        <td><font <?=($i==2||$i==4?$alert_font:$normal_font)?>>Group Title:</font></td>
        <td colspan="2"><input type="text" name="title" style="width: 80%" autocomplete="off" class="box" value="<?=(isset($_POST['title'])?$_POST['title']:"")?>"></td>
        </tr>
        <tr class="con1"><td valign="top">Description:</td><td>
        <table class="empty" cellspacing="0" cellpadding="0"><tr><td><table class="main" cellspacing="1" cellpadding="2">
        <tr class="empty"><td><script language="JavaScript" type="text/javascript">writeRichText('JCM_textarea', 'emoticons', '<?=insertSmilies('JCM_textarea');?>');</script></td></tr>
        <tr class="empty"><td><script language="JavaScript" type="text/javascript">writeRichText('JCM_textarea', 'bbcode', '', '<?=$config['allow_RTE_upload']?>');</script></td></tr>
        </table></td></tr></table>
        <table><tr><td></td></tr></table>
        <script language="JavaScript" type="text/javascript">writeRichText('JCM_textarea', 'textarea', '', '', '', '<?=(isset($_POST['JCM_textarea'])?rteSafe($_POST['JCM_textarea']):"")?>', 418, 130);</script>
        </td></tr>
        </table>

        <table><tr><td></td></tr></table>
            <script type="text/javascript">
            <!--
                /**
                * (un)checks all items in a permission group
                * @param DOMobject obj : The checkbox at the top of the group
                * @return void
                */
                function ug_checkem(obj)
                {
                    // whether or not to check all the permissions in the group
                    var checkem = obj.checked;
                    
                    // init vars
                    var objs, len, id = obj.id;
                    
                    // if the div exists
                    if( (obj = document.getElementById("div_"+ id)) )
                    {
                        // fetch all inputs from the div, and set len to the number of checkbxoes
                        len = (objs = obj.getElementsByTagName("input")).length;
                        
                        // for all the checkboxes
                        for( var i=0; i<len; i++ )
                        {
                            // check or uncheck
                            objs[i].checked = checkem;
                        }
                    }
                }
            // -->
            </script>
            
            <h2>Permissions</h2>
            <?php
            // for each permission group
            foreach($perms as $permgroup => $perms )
            {
                ?>
                <div id="div_ug_perms_<?php echo $permgroup;?>">
                    <div style="border-bottom: 1px solid #dddddd; font-size: 14px; width: 160px;">
                        <label for="ug_perms_<?php echo $permgroup;?>" style="text-align: center;"><?php echo $permgroup;?></label>
                        <input type="checkbox" id="ug_perms_<?php echo $permgroup;?>" value="0" onclick="ug_checkem(this);" />
                        <br class="clear" />
                    </div>
                    <?php
                    // for each item in the bitfield
                    foreach( $perms as $bit => $name )
                    {
                        // if the bit is set for the permission make the checkbox checked
                        ?>
                        <label for="ug_perms_<?php echo $permgroup;?>_<?php echo $bit;?>"><?php echo $name;?></label>
                        <input type="checkbox" id="ug_perms_<?php echo $permgroup;?>_<?php echo $bit;?>" name="perms[<?php echo $permgroup;?>][]" value="<?php echo $bit;?>" />
                        <br class="clear" />
                        <?php
                    }
                echo "</div><br />";
            }
            ?>
            <table><tr><td></td></tr></table>
            <table class='main' cellpadding='4' cellspacing='1'>
            <tr class='con1'>
            <td>Actions:</td>
            <td style='float:right;'><input type='submit' name='submit' value='Add Group' class='box' /></td>
            </tr>
            </table>
            <input type="hidden" name="a" value="1" />
            </form>
    <?php
break;

}
?>

Link to comment
Share on other sites

<?php
                    if( $_POST['perms'] )
                    {
					$perms = array();
                        // for all of the groups that had any checked boxes
                        foreach($_POST['perms'] as $permgroup => $p)
                        {
						$perms[$permgroup] = 0;
                            // for each item checked in the bitfield
                            foreach($p as $bit)
                            {
                                $perms[$permgroup] += $bit;
                            }
                        }
                    }
?>

 

try this, otherwise I see nothing wrong >.>

Link to comment
Share on other sites

please click solved :) otherwise all the helpers get sidetracked reading your question gettin ready to help you and then find out you've alrdy been helped :) keep the ball rolling, click solved :)!

 

And, anytime bro, if you're ever in a mess and need some help, phpfreaks is ALWAYS open and of all the forums I've ever helped on.. the speedy responses beats every other forum, hands down, I came here with a problem since the day I signed up.. and like not even 2 minutes b4 getting a response.. great forums here..

 

but if you have MSN you can add me, I'm just as free as PHPFreaks here aslong as its help, and not a script request.. and I'm building a contact army on MSN, so let me recruit you ~.~ :P

 

RussellonMSN@hotmail.com

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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