We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 13352
    • 26 Posts
    Hi,

    First of all, I don’t know this is the right SubForum for this question or not. Sorry if I’m wrong. This is about coding.

    I have some groups in Modx, and I tried to check, if the logged-in user belongs to that group, he may edit the data of that group. But I got always error (it says that the user is not belongs to group, so may not edit it. But actually the user is belongs to that group), I don’t know it’s because of : $modx->db->getRecordCount , $myshp = $modx->db->getRow or others.

    Could please OpenGeek or some one other help me to see the code below ?

    Thanks in advance.
    - jack -

    -----------------------------------------------------------------------

    if(isset($_REQUEST[’submit’]) && $_POST[’edit’] > 0) {
    $id = intval($_POST[’edit’]);
    $current_shop = $modx->db->getValue($modx->db->select(’shop’, $modx->getFullTableName(’result’), "id=$id"));

    $samegrp = ’0’; // init

    $sumshop = $modx->db->select("mgn.*" , "{$modx->getFullTableName(’membergroup_names’)} mgn, {$modx->getFullTableName(’member_groups’)} mg", "mg.user_group = mgn.id AND mg.member=’" . $_SESSION[’mgrInternalKey’] . "’");

    if ($modx->db->getRecordCount($sumshop) > 1) {
    // belongs to some shop

    while ($myshp = $modx->db->getRow($sumshop)) {
    if ($myshp[’name’] == $current_shop) {
    $samegrp = ’1’;
    }
    }
    }
    else { // just belongs to 1 shop
    $samegrp = ’1’;
    }

    if ($samegrp !== ’1’) {
    die(’{"success":"0", "message":"You have no permission to edit this shop..."}’);
    }

    $insert[’shop’] = $current_shop;
    ........ etc ... etc ...

    • I think you should simplify your code a bit... I’m not exactly sure what I’m looking at here. Can you isolate your queries at least and tell us what values you’re getting?
        • 13352
        • 26 Posts
        Quote from: Everett at May 05, 2010, 04:05 PM

        I think you should simplify your code a bit... I’m not exactly sure what I’m looking at here. Can you isolate your queries at least and tell us what values you’re getting?

        Hi Everett,

        Thanks for your reply. I will try to explain my code :

        Line 1-4:
        It get the _POST value for submit, and if it’s for edit, it will get the value of ’shop’ from table ’result’ where id is the _POST value for editing the specific row of result table ($id):

        In normal PHP, I made it like this :
        if(isset($_REQUEST['submit']) && $_POST['edit'] > 0) {
                $id = (int)($_POST['edit']);
                mysql_connect("localhost", "admin", "1admin") or die(mysql_error());
                mysql_select_db("test") or die(mysql_error());
                $current_shop = mysql_query("SELECT shop from 'result' WHERE id=$id");
                $samegrp = '0'; // init
        


        I tried in Modx :
          if(isset($_REQUEST['submit']) && $_POST['edit'] > 0) {
                $id = intval($_POST['edit']);
                $current_shop = $modx->db->getValue($modx->db->select('shop', $modx->getFullTableName('result'), "id=$id"));
                $samegrp = '0'; // init
        


        Line 5 :
        I don’t know to make it in Modx, but in normal PHP , it’s something like this :
          $theid = $_SESSION['mgrInternalKey'];
          mysql_connect("localhost", "admin", "1admin") or die(mysql_error());
          mysql_select_db("test") or die(mysql_error());
          $result = mysql_query("SELECT mgn.* from 'modx_membergroup_names' mgn, 'modx_member_groups' mg WHERE mg.user_group = mgn.id AND mg.member = $theid");
        
        


        I tried to do like this in Modx (is this right ?):
            $sumshop = $modx->db->select("mgn.*" , "{$modx->getFullTableName('membergroup_names')} mgn, {$modx->getFullTableName('member_groups')} mg",  "mg.user_group = mgn.id AND mg.member='" . $_SESSION['mgrInternalKey'] . "'");
        


        Line 6 -15:
        In normal PHP :
              if (mysql_num_rows($result) > 1) {
                while($row = mysql_fetch_array( $result )) {
                  if ($row['name'] == $current_shop) {
                        $samegrp = '1';
                  }
                }
              } // end if      
              else {
                 $samegrp = '1';
              }
        


        I tried in Modx :
                if ($modx->db->getRecordCount($sumshop) > 1) {
                // belongs to some shop
        
                  while ($myshp = $modx->db->getRow($sumshop)) {
                    if ($myshp['name'] == $current_shop) {
                        $samegrp = '1';
                    }
                  }
                }
                else { // just belongs to 1 shop
                        $samegrp = '1';
                }
        


        Then, in PHP :
                if ($samegrp !== '1') {
                        die('You have no permission to edit this shop');
                }
        
                $insert['shop'] = $current_shop;
                etc ... etc ....
        


        I tried in Modx :
                if ($samegrp !== '1') {
                        die('{"success":"0", "message":"You have no permission to edit this shop..."}');
                }
        
                $insert['shop'] = $current_shop;
                etc ... etc ...
        


        The result : $samegrp is ALWAYS ’0’ , and I always got the error message ’You have no permission to edit this shop ....’ , I check in Mysql database, this user is belongs to some groups, included the current group, so $samegrp must be ’1’.

        What is wrong with my modx code ? Can we use more then 1 modx query in the same line, like : $modx->db->select( ... {$modx->getFullTableName(’membergroup_names’)} .... {$modx->getFullTableName(’member_groups’)} ..... ?? => here I use {} , not () for the {$modx->getFullTableName ...} inside the main $modx->db->select query.

        Can we use $modx->db->getRecordCount($sumshop) in modx instead of ’mysql_num_rows’
        And $modx->db->getRow($sumshop) instead of ’mysql_fetch_array’ ?

        Thanks
        - jack -
        • Man, I think you just made that about 10x more complicated. If you can boil this down to about a paragraph, then it would be much easier to troubleshoot. You need to get this into a couple lines of code so you can isolate what exactly is or isn’t working. For example: can you isolate the queries and check if they work alone? It’s a lot easier to look at a small amount of code than it is to pour over pages and pages of it.
            • 13352
            • 26 Posts
            Quote from: Everett at May 06, 2010, 02:22 AM

            Man, I think you just made that about 10x more complicated. If you can boil this down to about a paragraph, then it would be much easier to troubleshoot. You need to get this into a couple lines of code so you can isolate what exactly is or isn’t working. For example: can you isolate the queries and check if they work alone? It’s a lot easier to look at a small amount of code than it is to pour over pages and pages of it.


            Hi Everett,

            You’re right. After split the codes, I fount that I have taken the wrong field, it must be $row[’id’], but I take the field $row[’name’] .... sad

            if (mysql_num_rows($result) > 1) {
                    while($row = mysql_fetch_array( $result )) {
                      if ($row['id'] == $current_shop) {
                            $samegrp = '1';
                      }
                    }
                  } // end if      
                  else {
                     $samegrp = '1';
                  }
            


            I just found, that Result of $modx->db->getRecordCount is similar to $row = mysql_num_rows
            and $modx->db->getRow is similar to mysql_fetch_assoc.

            Thanks Everett,
            - jack -