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

    I wrote a small snippet for a private radio site. It checks wheather a stream is running or not and then puts the information in two placeholders.
    Here is the code:
    <?php
    $dbase = $modx->dbConfig['dbase'];
    $table_prefix = $modx->dbConfig['table_prefix'];
    
    $sql = "SELECT DJserverIP, DJserverPORT, DJonline FROM modx_web_user_attributes_extended WHERE DJonline LIKE 'on' ";
    $result = $modx->dbQuery($sql);
    
    $number = $modx->db->getRecordCount($result);
    
       if($number==0 || $number>1) {
          $outputLink .= '<h1><a id="big-button" href="index.php?id=54" title="Kein Stream am laufen!">Kein Stream am laufen!</a></h1>';
          $outputStats = '<strong>Kein Stream am laufen! Schau am besten mal ins <a href="index.php?id=53" title="Programm">Programm</a>.</strong>';
       }      
       else {
         $row = $modx->db->getRow($result);
          $DJserverIP       = $row['DJserverIP'];
          $DJserverPORT = $row['DJserverPORT'];
          $DJonline           = $row['DJonline'];
         $outputLink .= '<h1><a id="big-button" href="http://' . $DJserverIP . ':' . $DJserverPORT . '/listen.pls" title="Listen to our Radio Stream!">TUNE IN</a></h1>';
         $outputStats .= 'http://' . $DJserverIP . ':' . $DJserverPORT . '/';
       }
    
    //echo $outputStats;
     $modx->setPlaceholder('DJonlineStats',$outputStats);
     $modx->setPlaceholder('DJonlineLink',$outputLink);
    
    
    return;
    ?>


    I know it is not the nicest code but it worked fine, until i added the second placeholder (DJonlineStats, with $outputStats). It seems like the placeholder is recognized, but is not displaying its content. The rest is still working fine.
    The strange thing is after adding the "echo $outputStats;" at the end of the script, the information was succesfully shown as the echo and the placeholder.
    I have no idea why it works with the echo command and not without it..

    Any suggestions?


    regards,
    AvrantiS
      • 4041
      • 788 Posts
      Try this and see if it helps, only changes are the added empty variables and then make sure and use the ".=" to add to the variables.

      <?php
      $dbase = $modx->dbConfig['dbase'];
      $table_prefix = $modx->dbConfig['table_prefix'];
      // set empty variables
      $outputLink ='';
      $outputStats ='';
      
      $sql = "SELECT DJserverIP, DJserverPORT, DJonline FROM modx_web_user_attributes_extended WHERE DJonline LIKE 'on' ";
      $result = $modx->dbQuery($sql);
      
      $number = $modx->db->getRecordCount($result);
      
         if($number==0 || $number>1) {
            $outputLink .= '<h1><a id="big-button" href="index.php?id=54" title="Kein Stream am laufen!">Kein Stream am laufen!</a></h1>';
            $outputStats .= '<strong>Kein Stream am laufen! Schau am besten mal ins <a href="index.php?id=53" title="Programm">Programm</a>.</strong>';
         }      
         else {
           $row = $modx->db->getRow($result);
            $DJserverIP       = $row['DJserverIP'];
            $DJserverPORT = $row['DJserverPORT'];
            $DJonline           = $row['DJonline'];
           $outputLink .= '<h1><a id="big-button" href="http://' . $DJserverIP . ':' . $DJserverPORT . '/listen.pls" title="Listen to our Radio Stream!">TUNE IN</a></h1>';
           $outputStats .= 'http://' . $DJserverIP . ':' . $DJserverPORT . '/';
         }
      
      //echo $outputStats;
       $modx->setPlaceholder('DJonlineStats',$outputStats);
       $modx->setPlaceholder('DJonlineLink',$outputLink);
      
      
      return;
      ?>
        xforum
        http://frsbuilders.net (under construction) forum for evolution
        • 2506
        • 3 Posts
        Thanks for your idea, but nothing changed..

        Any other ideas?
          • 22303 MODX Staff
          • 10,725 Posts
          How are you rendering the placeholders? Is the page cacheable? If so, you’ll need to clear the cache to see changes to the placeholder content after the first visitor hits it. Otherwise, you’ll have to make the page uncacheable or use another non-cacheable snippet to grab the value of the placeholder set by this snippet (which also has to be called like [!Snippet!] to make sure it executes on each request).
            • 2506
            • 3 Posts
            Page and snippet call are both not cacheable. I tried clearing the cache anyway but without any luck.

            Weird thing is that i CAN see the changes if I uncomment "echo $outputStats;" right before I set the placeholders.

            Is the echo command doing something else than displaying the variable?
            Or is there some generall error in my code?

            regards,
            AvrantiS