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

    Description of Problem:

    I've created a custom snippet that successfully searches through the gallery_items table. Problem is, I want to echo
    [[!GalleryItem? &id=`id`]]
    with each result. Is this possible? Because so far, nothing gets spit out, just the name of the result (see PHP snippet code below).

    Here's my code for my custom "CustomSearchGallery" snippet below so you have a better idea:

    HTML
    <form method="post" action="[[~[[*id]]]]">
    <input type="text" value="Search..." name="query" />
    <input type="submit" value="Find" name="completedsearch" />
    </form>


    PHP
    <?php
    if(isset($_POST['completedsearch']))
    {
    $term = $_POST['query'];
    $mysql = mysql_connect("localhost","username","password");
    mysql_select_db("databasename
    $qu = mysql_query("SELECT * FROM database__gallery_table WHERE name LIKE '%{$term}%' OR description LIKE '%{$term}%'"); //selects the row that contains ANYTHING like the submitted string
    echo "<h2>Gallery Content</h2>";
    while($row = mysql_fetch_array($qu))
    {
    echo $row['name'];
    echo "[[!GalleryItem? &id=`ID OF THE GALLERY ITEM`]]";
    }
    }



    • MODX Version: 2.2.7
    • PHP Version:5.3.25
    • Database (MySQL, SQL Server, etc) Version:5.1.66

    Sources I've tried looking at :
    http://cormacscode.wordpress.com/2010/04/22/call-a-modx-snippet-from-within-a-snippet/
    http://wiki.modxcms.com/index.php/API:runSnippet

    Thanks for your time!
    Sar

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

    • discuss.answer
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Try using
      $itemId = $row['id'];
      $output .= $row['name'];
      $output .= $modx->runSnippet('GalleryItem', array('id'=>$itemId, 'tpl'=>'myTpl', ...));
      return $output;

      Echo should be avoided whenever possible. Test your GalleryItem snippet on a page as a snippet to make sure you don't have errors in the snippet properties.
        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
        • 37210
        • 131 Posts
        Hi Sottwell

        Thanks for the advice about echo, I didn't know! I think I did read about that in my readings a long time ago but this is the first time I've delved into custom snippets.

        The code still doesn't spit anything out...have I implemented correctly? I had to get rid of the
        ,...
        after the tpl coz it was breaking the page.

        <?php
        if(isset($_POST['completedsearch']))
        {
        $term = $_POST['query'];
        $mysql = mysql_connect("localhost","username","password");
        mysql_select_db("database name");
        $qu = mysql_query("SELECT * FROM website_gallery_items WHERE name LIKE '%{$term}%' OR description LIKE '%{$term}%'"); //selects the row that contains ANYTHING like the submitted string
        echo "<h2>Gallery Content</h2>";
        while($row = mysql_fetch_array($qu))
        {
        $itemId = $row['id'];
        $output .= $row['name'];
        $output .= $modx->runSnippet('GalleryItem', array('id'=>$itemId, 'tpl'=>'CHK-TPL-gallery-item'));
        return $output;
        }
        }


        I'll keep plugging away! I'll let you know once it starts up and running.

        Sar
          • 37210
          • 131 Posts
          Hi again sottwell!

          It helped to play around with a GalleryItem snippet call just on its own because it made me realize that I had to add:
          &toPlaceholders=`0`


          So here's the final code!
          HTML
          <form method="post" action="[[~[[*id]]]]">
          <input type="text" value="Search..." name="query" />
          <input type="submit" value="Find" name="completedsearch" />
          </form>


          PHP Snippet
          if(isset($_POST['completedsearch'])) 
          {
          $term = $_POST['query']; 
          $mysql = mysql_connect("localhost","username","password");
          mysql_select_db("database_name");
          $qu = mysql_query("SELECT * FROM modx_gallery_items WHERE name LIKE '%{$term}%' OR description LIKE '%{$term}%'"); //selects the row that contains ANYTHING like the submitted string
          echo "<h2>Gallery Content</h2>";
          while($row = mysql_fetch_array($qu)) {
              $itemId = $row['id'];
              $output .= $modx->runSnippet('GalleryItem', array(
              'id'=>$itemId,
              'tpl'=>'myTpl',
              'toPlaceholders'=>'0'
              ));
          }
          return $output;
          }