We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 2742
    • 13 Posts
    Hello Guys,

    I’m confuse implement placeholder and chunk in snippets.

    What I want to is:

    Expected Output:

    <li><a href="#">Option 1</a></li>
    <li><a href="#">Option 2</a></li>
    <li><a href="#">Option 3</a></li>
    <li><a href="#">Option 4</a></li>

    =========
    On TPL at chunk
    <li><a href="#">[+optionvalue+]</a></li>
    =========
    Here is the snippets

    Loading Template Variable Option Value into Array

    <?php
    $tvname = isset($tvname) ? $tvname : ’’;
    $valueTpl = isset($valueTpl) ? $valueTpl : ’’;

    $result = $modx->db->select("elements", "modx_site_tmplvars", "name = ’". $tvname . "’ ");
    while($row = $modx->db->getRow($result)) {
    $value_string = $row[’elements’];
    }
    $value = explode(’||’,$value_string);

    foreach($value as $key => $isi){
    $modx->setPlaceholder(’optionvalue’, $isi);
    print $isi;
    print $modx->getChunk($valueTpl);
    }
    ?>

    in the bold text,

    the result for print $modx->getChunk($valueTpl); is
    <li><a href="#">Option 4</a></li>
    <li><a href="#">Option 4</a></li>
    <li><a href="#">Option 4</a></li>
    <li><a href="#">Option 4</a></li>

    the result for print $isi is
    Option 1
    Option 2
    Option 3
    Option 4

    The placeholder sent result only the last value of array. How to get chuck filled with looping setplaceholder properly?

    I’m newbie for placeholder, if you have some advice will be great smiley

    Regards

    b4ndeng
      Bali Web Design - www.babonmultimedia.com
      "When my brain got bottleneck of code, get into the garden , drink cup a coffee and enjoy Sun shining and warm weather in tropical island"
      • 22303 MODX Staff
      • 10,725 Posts
      Use the $modx->parseChunk() to parse the placeholders in the loop. Your code is simply getting the chunk and outputting it, then allows the parser to replace the placeholder you reused for each value after the loop is completed; thus you get only the final value.
        • 2742
        • 13 Posts
        Thanks a ton Jason smiley Now it’s worked with parseChunk, Here is the code

        foreach($value as $key => $isi){

        //create all variable in array
        $chunkArr = array(
        ’optionvalue’ => $isi,
        ’link’ => ’[~76~]?tagData=’.$tvname.’&tags=’ . $isi
        );
        //parse to chunk
        $output .= $modx->parseChunk($valueTpl,$chunkArr, ’[+’,’+]’);
        }
        return $output;



          Bali Web Design - www.babonmultimedia.com
          "When my brain got bottleneck of code, get into the garden , drink cup a coffee and enjoy Sun shining and warm weather in tropical island"
          • 4310
          • 2,310 Posts
          I’ve tried and failed to get this snippet with placeholders to work.
          Take pity on me, I want to use placeholders but can’t due to a lack of skills!
          I have a form which POSTs to a page with this snippet on it:

          <?php
          $output = '';
          $grant = $_POST['grant'];
          $name = $_POST['name'];
          $title = $_POST['title'];
          $year = $_POST['year'];
          $la = $_POST['la'];
          $clauses = ' WHERE `Co_Name` LIKE "%'.$name.'%" AND `Project_Title` LIKE "%'.$title.'%" AND `Grant_Award` '.$grant.' AND `Approval_Date` LIKE "'.$year.'%" AND `LA_Area` LIKE"%'.$la.'"';
          $result = $modx->db->query( 'SELECT * FROM `tblprojects` '.$clauses.'  LIMIT 0, 1000');
          while ($row = mysql_fetch_array($result)){
          foreach ($row as $key => $value) {
          $modx->toPlaceholder( $key, $value,  'ESEP-');
          }
          $output .= $modx->parseChunk('datagrid',$row, '[+','+]');
          }
          return $output;
          ?>


          The chunk "datagrid" shows the correct number of records but it’s the same one repeated over and over.
          So the form POST is okay, the query works okay, the chunk is okay, the palaceholders work but with only one repeated value.
          Thanks for looking
          David
            • 22303 MODX Staff
            • 10,725 Posts
            You don’t need placeholders, the $row provides the data for the string replacement in parseChunk().