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

    I'm still learning MODX, and what I yet have to learn is how to convert a php script to a modx snippet.

    Is there anyone of you who can help me on how to get the following script on a Document? I'll guess thats whats snippets is for? But as i have understood, I cant use any HTML in a snippet.

    <?php 
    	require("clients/dbconnect.php");
    
    	if ($_SESSION['uid'])
    	{
    	    	$query="SELECT * FROM tblclients WHERE id='" .sanitize($_SESSION['uid']) . "'";
    		$result = mysql_query($query) or die(mysql_error());
    		if($row = mysql_fetch_array($result, MYSQL_ASSOC)) 
    		{
    			$clientsdetails['firstname']=$row['firstname'];
    			$clientsdetails['lastname']=$row['lastname'];
    			$clientsdetails['email']=$row['email'];
    		}
    		echo "Welcome Back, <strong>".($row['firstname'])."</strong>   <img src=\"/clients/templates/portal/images/icons/details.gif\" alt=\"My Details\" width=\"16\" height=\"16\" border=\"0\" class=\"absmiddle\" /> <a href=\"/clients/clientarea.php?action=details\" title=\"My Details\"><strong>My Details</strong></a>   <img src=\"/clients/templates/portal/images/icons/logout.gif\" alt=\"Logout\" width=\"16\" height=\"16\" border=\"0\" class=\"absmiddle\" /> <a href=\"/clients/logout.php\" title=\"Logout\"><strong>Logout</strong></a>";
    	}
    	else
    	{
    		echo "Please <a href=\"/clients/clientarea.php\" title=\"Login\"><strong>Login</strong></a> or <a href=\"/clients/register.php\" title=\"Register\"><strong>Register</strong></a>";
    	}
    ?>


    Regards
      • 3749
      • 24,544 Posts
      You can use HTML in a snippet like this:

      <?php
      $output = "";
      /* some code */
      
      $output .= 'Some HTML';
      
      /* some more code */
      
      $output .= 'Some more HTML';
      
      
      return $output;
      


      I think your query code will have to be adjusted, but I'm so used to using xPDO in MODX Revolution that I can't remember how to do it.

      What version of MODX are you using?
        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
        • 36776
        • 31 Posts
        Hi,

        I'm using Revo RC3
          • 3749
          • 24,544 Posts
          You can use regular PDO for your query, but you'll get the advantages of xPDO if you create the classes and maps necessary for xPDO to treat the rows in your table as MODX objects: http://bobsguides.com/custom-db-tables.html
            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
            • 36776
            • 31 Posts
            I'm not sure if this is what I'm looking for.

            What I want to accomplish is:
            Template

            --Template HTML--
            If logged in to WHMCS, then show: Welcome back User.
            Else: Log in to access client area.
            --Template HTML header--
            --Template HTML main menu--
            If logged in to WHMCS, then show: WHMCS Client Menu.
            Else: Show nothing
            --Template HTML the rest--


            After browsing the WHMCS forums, i found that script.
            Got to say that I'm a far way from understanding PHP. Just know very, very basic stuff.
              • 3749
              • 24,544 Posts
              Synchronizing logins with another site is fairly tricky. Do you have a way of knowing, in code, whether the user is logged in to WHMCS or not (do they have an API for that)? If the user is logged in to WHMCS, do you need to also log them in automatically to your MODX site?

                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
                • 36776
                • 31 Posts
                No. Our MODX dont have any logins. The script on the top is to check if the user are logged in or not. The only loginsystem vi have for guests and customers is the one at WHMCS in a subfolder. The script above workes, just trying to figure out how to integrate in MODx, so i can just add a snippet wherever i want in the template.
                Just write:
                [[MySnippet? &LoggedIn=`1` &Stop=`0`]] Insert my Template text inside here[[MySnippet? &Stop=`1`]]


                Settings:
                LoggedIn = 1 show if user is logged inn, 0 if user is logged out
                Stop = 1 Start of the script, 0 end of the script

                That's how I could think it working..
                  • 3749
                  • 24,544 Posts
                  I'm afraid snippets don't work like that. They return a single value which replaces the tag.

                  Here's a simple solution:

                  <?php
                  
                  $loggedIn = // Your code here to check login status
                  
                  if (! $loggedIn) {
                      $modx->sendRedirect('Full/URL/of/Login/page');
                  }
                  
                  return "Welcome Back, <strong>".($row['firstname'])."</strong>   <img src=\"/clients/templates/portal/images/icons/details.gif\" alt=\"My Details\" width=\"16\" height=\"16\" border=\"0\" class=\"absmiddle\" /> <a href=\"/clients/clientarea.php?action=details\" title=\"My Details\"><strong>My Details</strong></a>   <img src=\"/clients/templates/portal/images/icons/logout.gif\" alt=\"Logout\" width=\"16\" height=\"16\" border=\"0\" class=\"absmiddle\" /> <a href=\"/clients/logout.php\" title=\"Logout\"><strong>Logout</strong></a>";


                  That will show the welcome message if they're logged in and forward them to the login page if not.





                    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