We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • That’s because the object returned from the query needs to be "unpacked" as it were.

    The example given in the php documentation:
    $result = mysql_query($query);
    ...
    while ($row = mysql_fetch_assoc($result)) {
        echo $row['firstname'];
        echo $row['lastname'];
        echo $row['address'];
        echo $row['age'];
    }
    

    From the Wayfinder snippet:
    $result = $modx->dbQuery($sql);
    ...
    $numResults = @$modx->recordCount($result);
    ...
    for($i=0;$i<$numResults;$i++)  {
    $tempDocInfo = $modx->fetchRow($result);
    $resultIds[] = $tempDocInfo['id'];
    ...
    
      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
      • 26182
      • 164 Posts
      So I have this and all I’m getting is array, array, array... do I have to use the str_replace function or something. Sorry not so advanced at php.

      <?php
      /*
       ****************************************
       *
       * Snippet Name: ShowHits
       * Short Desc: Shows hits on pages of the IT Newsletters site
       * Created By: Sarah Vardy
       *
       * Version: 1.0 beta
       * Modified: November 8th, 2007
       *
       * Changelog: 
       *   
       *
       ****************************************
       *
       * Description: 	
       *   Display a list of articles that have had the most hits in descending order
       *   Show the number of hits on a page at the bottom of a page
       *
       ****************************************
       *  
       * Example: [[ShowHits?]]
      */
      
      //connect to table that stores site content
      $table_content = $modx->getFullTableName("site_content");
      $table_hits = $modx->getFullTableName("page_hit_counter");
      
      $fields = 'pagetitle';
      
      $sql= "SELECT {$fields} FROM {$table_content} table_content JOIN {$table_hits} table_hits ON table_hits.page_id = table_content.id  WHERE table_content.donthit = '0' ORDER BY table_hits.page_count DESC";
      
      $query = $modx->db->query($sql);
      $hits= $modx->db->getRow($query);
      
      
          while ($row= $modx->db->getRow($query))
          {
              $output .= $row . "<br>";
          }
      
      return $output;
      ?>

        • 26182
        • 164 Posts
        Does this mean that I have to create a folder in the snippets directory and write a complete snippet for what I want to do?

        • When you get that "Array" business, that means that what you’re getting is an array, and it needs to be looped through. I will temporarily use a print_r block to get the whole array output so I can see what’s going on:
          while($row=blah blah) {
          echo "<pre>";
          print_r($row);
          echo "</pre>";
          }
          
            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
            • 26182
            • 164 Posts
            Thanks so much for your help!

            Ok, I’m getting three lines now:

            Array ( [id] => 90 [pagetitle] => Microsoft enters enterprise search market )
            Array ( [id] => 89 [pagetitle] => Local Blackberry users without email )
            Array ( [id] => 88 [pagetitle] => 2 million child porn images seized in QLD, nine arrested )

            Do I strip the characters I don’t want and format the output?

            Thanks again!

            Sarah
              • 26182
              • 164 Posts
              Ah cool. Thanks so much. I understand now. I’ll post up the code for the snippet when I finish! You rock Sottwell! wink
                • 26182
                • 164 Posts
                Here’s the code for the snippet to display the 3 most viewed documents:

                <?php
                /*
                 ****************************************
                 *
                 * Snippet Name: ShowHits
                 * Short Desc: Shows hits on pages of the IT Newsletters site
                 * Created By: Sarah Vardy
                 *
                 * Version: 1.0 beta
                 * Modified: November 8th, 2007
                 *
                 * Changelog: 
                 *   
                 *
                 ****************************************
                 *
                 * Description: 	
                 *   Display a list of articles that have had the most hits in descending order
                 *   Show the number of hits on a page at the bottom of a page
                 *
                 ****************************************
                 *  
                 * Example: [[ShowHits?]]
                */
                $output = '';
                 
                
                //connect to table that stores site content
                $table_content = $modx->getFullTableName("site_content");
                $table_hits = $modx->getFullTableName("page_hit_counter");
                
                $fields = 'id,pagetitle,page_count';
                
                $sql= "SELECT {$fields} FROM {$table_content} table_content JOIN {$table_hits} table_hits ON table_hits.page_id = table_content.id  WHERE table_content.donthit = '0' ORDER BY table_hits.page_count DESC LIMIT 3";
                $results= $modx->db->query($sql);
                
                while ($row=$modx->db->getRow($results)) {
                   
                   $output .= '<a href="index.php?id=' . $row[id] . '"/>' . $row[pagetitle] . '</a>' . "<br>" .  'Viewed: ' . $row[page_count] . ' times' . "<br>";
                
                }
                
                return $output;
                ?>


                laugh
                  • 26182
                  • 164 Posts
                  How do I modify this so I can get a username for each post ie who created it?

                  I have made another post to the general how to forum about this.

                  Please see: http://modxcms.com/forums/index.php/topic,19690.0.html

                  Thanks in advance,

                  Sarah smiley


                  • It seems that this module doesn’t die correctly if you have your site turned off. When I visited my site that was disabled, I got a MySQL error printed to the page that came from this module.
                      • 27749
                      • 53 Posts
                      I just love this. Every time you need something - you find it in the forums ;-)
                      THX for the snippet.

                      Anyhow - does someone know another way to display the most viewed documents, say, using ditto?