We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10076
    • 1,024 Posts
    Hi I am finnaly trying to move an external PHP file to MODX as a snippet.
    I cant seem to find out how to properly connect to MODX with that script.

    The external script reads out dbuser,dbpw etc from an include(’connections.php’);
    But I thougyt I could use something like
    global $modx; ?

    Problem is that the script stops here:

    $res = mysql_query($query, $sqlConn);


    As the $sqlConn var is comming in through the connections.php it would need to be replaced by something like global Modx ?

    Any suggestion. I looked into dbapi query but that is still somewhat to abstract for me,

    In short how to change this
      if ($first_is_number)
    
         $query = "select * from publisher WHERE abbreviation  >= '0%' and abbreviation  <= '9%'";
    
      else $query = "select * from publisher WHERE abbreviation  like '".$pubchar."%'";
    
    
    
      $query .= " order by abbreviation  ASC";
    
      $res = mysql_query($query, $sqlConn);
    
    


    To this

    $output = '';
    $result = $modx->db->query( 'SELECT id, name, joined FROM `user_table` GROUP BY `member_type` ORDER BY name ASC' );
     
    while( $row = $modx->db->getRow( $result ) ) {
    	$output .= '<br /> ID: ' . $row['id'] . '<br /> Name: ' . $row['name'] . '<br /> Joined: ' . $row['joined']
    	. '<br />---------<br />';
    }
    echo $output;


    • As long as the table you are querying is in the same database as MODx, you can use the $modx->db object; the wiki has a description of the DB API functions that you can use. You can also use the API to open a connection to another database, but you have to make sure to close it after your query.

      You only need to declare $modx global if it’s in a discrete function; snippets have direct access to the $modx object.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 10076
        • 1,024 Posts
        Okay that did help, great.
        Reason I wanted this script inside MODx as a snippet is to check if a webuser is logged in and then display a delete icon, like so:

        if (isset($_SESSION['webInternalKey'])) { global $modx;
          // User logged in so show icon
           echo"
        <a href=\"/index.php?id=".$idpubdelete."&idpub=".$idpub."&pubchar=".$pubchar."&abbreviation=".$abbreviation."\">
        <img src=\"http://www.pianoquartet.nl/assets/snippets/nbscripts/images/delete.png\" alt=\"Delete $name\" title=\"Delete $name\" style=\"border:0\"></a></td>";}
            
        else {
          // user not logged in
        }
        }


        And that does seem to work, thanks loads.
        • You might be interested in the CheckUser snippet.
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 10076
            • 1,024 Posts
            Hi Susan,

            I might indeed. If you could let me know how it fits in in the line of script above.
            It is not related to a page but to a line in a form.
            So (I m sure you saw that) if user logged in, show something (on that page) if not only show allowed.
            Thanks.
              • 3749
              • 24,544 Posts
              For what you’re trying to do, the Personalize snippet might work. It shows one chunk to logged-in users and another chunk to not-logged-in users.

              http://modxcms.com/extras/package/?package=336
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 10076
                • 1,024 Posts
                Thanks Susan, Bob,

                conservative as I am, dont touch stuff that works. And this does work.
                On to the next challenge, do a table join, how exiting.

                Frank.

                PS and cut down that huge tree that fell over during last weeks storm. But thats a different challenge.