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

    I'm trying to conform to the MVC pattern by keeping HTML out of Snipper code. I have the snippet below but not sure the best way to do this:

    if (isset($_POST['search'])) {
    
        $search = $_POST['search'];
     
        $results = $modx->myDB->getCollection('LogData',array('diallednumber' => $search));
    
        if ($results) {
    	   $output .= '<table class="table table-striped">';
    	   $output .= '<thead>';
    	   $output .= '<tr>';
    	   $output .= '<th>Call Id:</th>';
    	   $output .= '<th>Dialled Number:</th>';
    	   $output .= '<th>Call Date</th>';
    	   $output .= '<th>Delete</th>';
    	   $output .= '</tr>';
    	   $output .= '</thead>';
    	   $output .= '<tbody>';
         foreach($results as $result) {
          $fields = $result->toArray();
           $output .= $modx->getChunk('ShowData', $fields);
         }
    	   $output .= '</tbody>';
               $output .= '</table>';
        }else {
         return 'No items found.';
        }
    }
    return $output;
    


    The only thing I can think of is creating 2 chunks with the following code:

    <table class="table table-striped">
    <thead>
    <tr>
    <th>Call Id:</th>
    <th>Dialled Number:</th>
    <th>Call Date</th>
    <th>Delete</th>
    </tr>
    </thead>
    <tbody>
    


    and

    </tbody>
    </table>
    


    and using getChunk to pull them in. That doesn't to me seem at though that would be the best way to do it and maybe there is a better way to use just one chunk?

    Many thank.

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

    • discuss.answer
      • 4172
      • 5,888 Posts
      you can try something like this:

      <?php
      
      if (isset($_POST['search'])) {
      
          $search = $_POST['search'];
          if ($results = $modx->myDB->getCollection('LogData', array('diallednumber' => $search))) {
              foreach ($results as $result) {
                  $fields = $result->toArray();
                  $rows[] = $modx->getChunk('ShowData', $fields);
              }
              
              $ph['rows'] = implode('',$rows);
              $output = $modx->getChunk('ShowDataOuter',$ph);
      
          } else {
              $output = $noresults;
          }
      }
      return $output;
      


      chunk ShowDataOuter:
      <table class="table table-striped">
      <thead>
      <tr>
      <th>Call Id:</th>
      <th>Dialled Number:</th>
      <th>Call Date</th>
      <th>Delete</th>
      </tr>
      </thead>
      <tbody>
      [[+rows]]	
      </tbody>
      </table>


      [[yoursnippet? &noresults=`No items found.`]]
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 40541
        • 85 Posts
        That worked great and just what a needed to understand how to get around this in the future. Thanks