We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 24414
    • 45 Posts
    Split off from my previous question about @EVAL & PHx as the question is slightly different now.

    Looking for some insight on the process of populating custom placeholders created with Ditto extenders.

    I’ve got the following code currently:

    Ditto call:
    [!Ditto? &parents=`21,23,26` &total=`3` &tpl=`tcTplShopItem` &tplLast=`tcTplShopItemLast` &extenders=`tcPriceGetter`!]


    tcPriceGetter.extender.inc.php:
    <?php
    
    if (!function_exists("tcPriceGetter")) {
    	function tcPriceGetter($resource) {
    		$productID = $resource["tcProductID"];
    		$query = "SELECT amount FROM modx_treasure_chest WHERE id=$productID";
    		$tcPrice = $modx->db->query($query);
    		return $tcPrice;
    	}
    }
    
    $placeholders['tcPrice'] = array("tcPriceGetter","tcProductID");
    
    ?>


    This pulls the value of [*tcProductID*] (same as row ID) and uses this to pull the relevant price value from the table modx_treasure_chest.

    However on trying this it errors with:

    Error:	call_user_func(tcProductID) [function.call-user-func]: First argument is expected to be a valid callback	 
    Line 259 source:	 $placeholders[$name] = call_user_func($value[1],$resource);


    Where am I going wrong?
      • 18397
      • 3,250 Posts
      You have the source and callback swapped. Use this instead:
      $placeholders['tcPrice'] = array(array("tcProductID","*"),"tcPriceGetter");
      
        • 24414
        • 45 Posts
        It doesn’t seem to like that!

        I just get a blank screen returned when I add that to the extender.

        There’s no error output in the system event log either.

        Ditto call:
        [!Ditto? &parents=`21,23,26` &total=`3` &tpl=`tcTplShopItem` &tplLast=`tcTplShopItemLast` &extenders=`tcPriceGetter`!]


        Template:
        <li>
        	<a class="imgPreviewLink" href="[~[+id+]~]" title="[+longtitle+]">
        		<img src="/assets/snippets/treasure_chest/images/phpthumb/phpThumb.php?src=../products/[+alias+].jpg&w=125&f=png" alt="[+pagetitle+]">
        	</a>
        	<h3>
        		<a href="[~[+id+]~]" title="[+longtitle+]">[+pagetitle+]</a>
        	</h3>
        	<p class="productAdd"><a href="#" onclick="return false;" class="addToCart" title="Add this piece to your cart" name="[+tcProductID+]" rel="id"><span>Add To Cart</span></a></p>
        	<p>[+tcPrice+]</p>
        </li>


        tcPriceGetter.extender.inc.php
        <?php
        if (!function_exists("tcPriceGetter")) {
        	function tcPriceGetter($resource) {
        		$productID = $resource["tcProductID"];
        		$query = "SELECT amount FROM modx_treasure_chest WHERE id=$productID";
        		$tcPrice = $modx->db->query($query);
        		return $tcPrice;
        	}
        }
        
        $placeholders['tcPrice'] = array(array("tcProductID","*"),"tcPriceGetter");
        ?>


          • 18397
          • 3,250 Posts
          Usually a completely blank screen output from MODx is indicative of a error in your PHP syntax. Check to make sure that you have PHP set to display errors so you can locate the typo.
            • 24414
            • 45 Posts
            Here’s what I got in server error Iog:

            [error] PHP Fatal error:  Call to a member function query() on a non-object in /path/assets/snippets/ditto/extenders/tcPriceGetter.extender.inc.php on line 9


            Line 9 is the closing bracket of the tcPriceGetter function.

            I’m quite lost here!
              • 24414
              • 45 Posts
              Here we go -

              Have got it! Works wonderfully! Thanks for the help.

              <?php
              
              if (!function_exists("tcPriceGetter")) {
              	function tcPriceGetter($resource) {
              		global $modx;
              		$productID = $resource["tcProductID"];
              		$table = $modx->getFullTableName('treasure_chest');
              		
              		$query = "SELECT amount FROM $table WHERE id=$productID";
              		$result = $modx->db->query($query);
              		$tcPrice = $modx->db->getValue($result);
              		
              		return ($tcPrice < 1) ? 'No price found' : $tcPrice;
              	}
              }
              
              $placeholders['tcPrice'] = array(array("tcProductID","*"),"tcPriceGetter");
              
              ?>
              


              Used $modx->db->getValue to get the value (only want one), all other ways were returning an array, and I wasn’t catering for that at all before. Also did not register $modx as global. Really should make more time for PHP learning!