We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 44891
    • 35 Posts
    I have a resource (a HTML page) and I try to get the ID of that resource.
    Thereafter I want to send that information (stored in the variable $id) to a snippet.

    <!doctype html>
    <html lang="en">
    <head>
    	<meta charset="utf-8">
    </head>
    <body>
    
    <?php
        $id = $modx->resource->get('id');
    ?>
    
    [[!message? &pageid=`$id`]]
    </body>
    </html>
    


    This renders in:
    resource->get('id'); ?> - - -

    So it's obvious I took the wrong approach.
    My question is, how to fetch the ID of the displayed resource and use that in a snippet?

    Using:MODX Revolution 2.6.1-pl

    This question has been answered by strooman. See the first response.

      • 17301
      • 932 Posts
      You need to put your php into a snippet. For example create a snippet called message and inside place

      $id = $modx->resource->get('id');
      echo $id;


      Now when you call [[message]] on a page you'll see that it echos out the value of the current resource that the snippet has executed on. Hopefully that helps? Sorry if I've misunderstood what you're trying to do.
        ■ email: [email protected] | ■ website: https://alienbuild.uk

        The greatest compliment you can give back to us, is to spend a few seconds leaving a rating at our trustpilot: https://uk.trustpilot.com/review/alienbuild.uk about the service we provided. We always drop mention of services offered by businesses we've worked with in the past to those of interest.
        • 44891
        • 35 Posts
        Quote from: lkfranklin at Jan 17, 2018, 08:17 PM
        You need to put your php into a snippet. For example create a snippet called message and inside place

        $id = $modx->resource->get('id');
        echo $id;


        Now when you call [[message]] on a page you'll see that it echos out the value of the current resource that the snippet has executed on. Hopefully that helps? Sorry if I've misunderstood what you're trying to do.

        Your solution was helpfull for me.
        I was aware that php and html doesn't work together very well but in other resources I mix it sometimes and I didn't encounter any errors.

        Thanks in advance for your quick answer
          • 17301
          • 932 Posts
          No problem.

          I assume you're trying to do something more complex than that, but alternatively if you just want to return the id of the current resource then just using [[*id]] anywhere on the page would do the same thing.

          <p>The current resource id is: [[*id]]</p>


          And you can of course use it in other calls such as

          [[pdoResources?
              &parents=`[[*id]]`
              &depth=`0`
              &tpl=`ListRowTpl`
          ]]


          As a side note php code that isn't in a snippet shouldn't actually execute when working in modx.
            ■ email: [email protected] | ■ website: https://alienbuild.uk

            The greatest compliment you can give back to us, is to spend a few seconds leaving a rating at our trustpilot: https://uk.trustpilot.com/review/alienbuild.uk about the service we provided. We always drop mention of services offered by businesses we've worked with in the past to those of interest.
          • discuss.answer
            • 44891
            • 35 Posts
            Quote from: lkfranklin at Jan 17, 2018, 09:34 PM

            I assume you're trying to do something more complex than that

            Ha ha, yeah sure. Your thinking was right.
            The bottomline is that I wanted to insert some data into the database and I was thinking way to complicated how to grab the id of the resource. This is a part of the code were I used your suggestion and it worked well. So for others to benefit:

            	$stmt = $dbh->prepare("INSERT INTO messages (name, email, message, pageid)VALUES (:name, :email, :message, :pageid)");
            		$stmt->bindParam(':name', $name, PDO::PARAM_STR);
            		$stmt->bindParam(':email', $email, PDO::PARAM_STR);
            		$stmt->bindParam(':message', $message, PDO::PARAM_STR);
            		$stmt->bindParam(':pageid', $pageid, PDO::PARAM_STR);
            		
            		
            		// set parameters and execute
            		$name = $_POST["name"];
            		$email = $_POST["email"];
            		$message = $_POST["message"];
            		$pageid = $modx->resource->get('id');
            		$stmt->execute();
                            echo "<script type= 'text/javascript'>alert('Your Message is succesfully submitted.');</script>";
            		}
            	
            	$dbh = NULL;
            
              • 36686
              • 165 Posts
              I have a similar question, I'm currently migrating a MODX site from Evolution to Revolution and I have this:

              $currentUrl = $modx->makeUrl($modx->documentIdentifier, "", "page=".$page, "full");
              


              What would the equivalent be in Revo?
                • 3749
                • 24,544 Posts
                That may work in Revo as well, though I think this might be more reliable:

                $currentUrl = $modx->makeUrl($modx->resource->get('id'), "", "page=".$page, "full");

                  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