Output placeholders of modx TV list-box

1k views Asked by At

I am new to modx and have limited knowledge of PHP. I'm writing a snippet and struggling with the syntax. I want to output placeholders of template variable list-box. What I have tried to do is to get the TV list-box IDs and loop through each one returning the html. Not having much luck. Forgive my stupidity.

            <?php
            $o='';
            $docid=$modx->resource->get('id');
            $show = $modx->resource->getTVValue('showHideContacts');
            $heading = $modx->resource->getTVValue('contactTitle');

            $staffVar=$modx->getObject('modTemplateVar',26); 
            $staff = $staffVar->getValue($docid);


            if($show =='value1')
            {

            //opening
            $o.='<div class="row">
            <div class="span9">
            <div class="footerContact">
            <h3><i class="icon-mail-circled"></i>'. $heading .'</h3>
            <div class="row">';


            if (!empty($staff)) {
               foreach($staff as $staff) {

                $name = $staff->resource->get('pagetitle');
                $title = $staff->resource->get('longtitle');
                $number = $staff->resource->get('description');
                $email = $staff->resource->get('introtext');

                  $o .= '<div class="span3">
                        <ul class="contactDetails">
                        <li><b>'. $name .'</b></li>
                        <li>'. $title .'</li>
                        <li>'. $number .'</li>
                        <li><a href="mailto:'. $email .'">'. $email .'</a></li>
                       </ul>
                      </div>';
               }
            } 

            //close 
            $o.='
            </div>
            </div>
            </div>
            </div>';


            }
            return $o;
2

There are 2 answers

0
user2540055 On

I got the result I wanted. Resorted to PHP and SQL instead of modx syntax.

    $staffVar=$modx->getObject('modTemplateVar',26); 
    $staff = $staffVar->getValue($docid);


    if($show =='value1')
    {

    //opening
    $o.='<div class="row">
    <div class="span9">
    <div class="footerContact">
    <h3><i class="icon-mail-circled"></i>'. $heading .'</h3>
    <div class="row">';

    $totalContacts = explode("||", $staff);
    for($i = 0; $i < count($totalContacts); $i++){
         $id =$totalContacts[$i];

    $stmt = $modx->query("SELECT pagetitle, longtitle, description,introtext
    FROM $table
    WHERE id=$id
    AND published=1
    AND deleted=0
    ORDER BY menuindex ASC
    "
    );

    if ($stmt && $stmt instanceof PDOStatement) {

        while ($row= $stmt->fetch(PDO::FETCH_ASSOC)) {


            $o.='<div class="span3">
            <ul class="contactDetails">
            <li><b>' . $row['pagetitle'] . '</b></li>
            <li>' . $row['longtitle'] . '</li>
            <li>' . $row['description'] . '</li>
            <li><a href="mailto:' . $row['introtext'] . '" >'.$row['introtext'] .'</a></li>
      </ul>
    </div>';
    }
    }
    }
0
Kristian Sandström On

It sounds like what you want to do is use the getResources plugin, and do a simple If i understand your code from the question right your looking at a template variable for the current resource and getting the value of it, you then want to loop the contacts when there are some to loop. This could be easier accomplished with the getResources plugin, used directly in your template. Assuming TV with ID of 26 is a comma separated list of ID's. Otherwise you'd have to rework it into such a list before giving it to &resources=

[[getResources? &resources=`[[*nameOfTVWithID26]]` &tpl=`yourChunk`]]

Where yourChunk looks something like

<div class="span3">
    <ul class="contactDetails">
        <li><b>[[+pagetitle]]</b></li>
        <li>[[+longtitle]]</li>
        <li>[[+description]]</li>
        <li><a href="mailto:[[+introtext]]">[[+introtext]]</a></li>
    </ul>
</div>

This helps you extract your HTML from inside a Snippet, and allows you to skip using the snippet at all. It also makes it easier to expand the function by simply adding some params to your getResources instead of having to modify or completely rewrite your custom snippet.