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

    I'm trying to add this counter tutorial to my modx revolution website : http://www.free-php-counter.com/mysql-counter.php

    It's all in one file called "counter.php" so I copied the counter.php content to a new snippet called Counter.

    And I added
    [[!Counter]]
    to the page where I want to see the stats but nothing is showing.

    This is the code of the counter.php file and so the code I put into Snippet called Counter :

    <?
    /*
    Counter Information
    
    Website: http://www.free-php-counter.com/
    Version: Mysql version
    
    Installation help:
    
    http://www.free-php-counter.com/mysql-counter.php
    
    
    You like to remove the link on the counter? Click here and get an link free license:
    
    http://www.free-php-counter.com/adfree_counter.php
    */
    
    
    // edit counter settings here
    
    
    // database configuration
    $counter_host = "localhost";
    $counter_user = "root";
    $counter_password = "root";
    $counter_database = "counter";
    
    // http path to the folder containing counter.php and counter.gif (do not forget the / at the end)
    // set http://www.your-homepage.com/ when the counter is in root dir
    $counter_path_http = "http://www.your-homepage.com/counter/";
    
    // ip-protection in seconds
    $counter_expire = 600;
    
    
    
    // do not edit from here
    
    // connect to database
    $counter_connected = true;
    $link = @mysql_connect($counter_host, $counter_user, $counter_password);
    if (!$link) 
    {
     	// can't connect to database
    	$counter_connected = false;
    	echo "Counter: " . mysql_error();
    }
    else
    {
    	// select database
    	$db_selected = @mysql_select_db($counter_database, $link);
    	if (!$db_selected) 
    	{
    		// can't select database
    		$counter_connected = false;
    		echo "Counter: " . mysql_error();
    	}
    }
    
    if ($counter_connected == true) 
    {
       $ignore = false; 
       
       // get counter information
       $sql = "select * from counter_values";
       $res = mysql_query($sql);
       
       // fill when empty
       if (mysql_num_rows($res) == 0)
       {	  
    	  $sql = "INSERT INTO `counter_values` (`id`, `day_id`, `day_value`, `yesterday_id`, `yesterday_value`, `week_id`, `week_value`, `month_id`, `month_value`, `year_id`, `year_value`, `all_value`, `record_date`, `record_value`) VALUES ('1', '" . date("z") . "',  '1', '" . (date("z")-1) . "', '0', '" . date("W") . "', '1', '" . date("n") . "', '1', '" . date("Y") . "',  '1',  '1',  NOW(),  '1')";
    	  mysql_query($sql);
    
    	  $sql = "select * from counter_values";
          $res = mysql_query($sql);
    	  
    	  $ignore = true;
       }   
       $row = mysql_fetch_assoc($res);
       
       $day_id = $row['day_id'];
       $day_value = $row['day_value'];
       $yesterday_id = $row['yesterday_id'];
       $yesterday_value = $row['yesterday_value'];
       $week_id = $row['week_id'];
       $week_value = $row['week_value'];
       $month_id = $row['month_id'];
       $month_value = $row['month_value'];
       $year_id = $row['year_id'];
       $year_value = $row['year_value'];
       $all_value = $row['all_value'];
       $record_date = $row['record_date'];
       $record_value = $row['record_value'];
       
       $counter_agent = (isset($_SERVER['HTTP_USER_AGENT'])) ? addslashes(trim($_SERVER['HTTP_USER_AGENT'])) : "";
       $counter_time = time();
       $counter_ip = trim(addslashes($_SERVER['REMOTE_ADDR'])); 
       
       // ignorore some bots
       if (substr_count($counter_agent, "bot") > 0)
          $ignore = true;
          
       // delete free ips
       if ($ignore == false)
       {
          $sql = "delete from counter_ips where unix_timestamp(NOW())-unix_timestamp(visit) > $counter_expire"; 
          mysql_query($sql);
       }
          
       // check for entry
       if ($ignore == false)
       {
          $sql = "select * from counter_ips where ip = '$counter_ip'";
          $res = mysql_query($sql);
          if (mysql_num_rows($res) == 0)
          {
             // insert
    	     $sql = "INSERT INTO counter_ips (ip, visit) VALUES ('$counter_ip', NOW())";
       	     mysql_query($sql);
          }
          else
          {
             $ignore = true;
    	     $sql = "update counter_ips set visit = NOW() where ip = '$counter_ip'";
    	     mysql_query($sql);
          }
       }
       
       // online?
       $sql = "select * from counter_ips";
       $res = mysql_query($sql);
       $online = mysql_num_rows($res);
          
       // add counter
       if ($ignore == false)
       {     	  
          // yesterday
    	  if ($day_id == (date("z")-1)) 
    	  {
    	     $yesterday_value = $day_value; 
    		 $yesterday_id = (date("z")-1);
    	  }
    	  else
    	  {
    	     if ($yesterday_id != (date("z")-1))
    		 {
    		    $yesterday_value = 0; 
    		    $yesterday_id = date("z")-1;
    		 }
    	  }
    	  
    	  // day
    	  if ($day_id == date("z")) 
    	  {
    	     $day_value++; 
    	  }
    	  else 
    	  {
    	     $day_value = 1;
    		 $day_id = date("z");
    	  }
    	  
    	  // week
    	  if ($week_id == date("W")) 
    	  {
    	     $week_value++; 
    	  }
    	  else 
    	  { 
    	     $week_value = 1;
    		 $week_id = date("W");
          }
    	  
          // month
    	  if ($month_id == date("n")) 
    	  {
    	     $month_value++; 
    	  }
    	  else 
    	  {
    	     $month_value = 1;
    		 $month_id = date("n");
          }
    	  
    	  // year
    	  if ($year_id == date("Y")) 
    	  {
    	     $year_value++; 
    	  }
    	  else 
    	  {
    	     $year_value = 1;
    		 $year_id = date("Y");
          }
    	  
    	  // all
    	  $all_value++;
    		 
    	  // neuer record?
    	  if ($day_value > $record_value)
    	  {
    	     $record_value = $day_value;
    	     $record_date = date("Y-m-d H:i:s");
    	  }
    		 
    	  // speichern und aufräumen
    	  $sql = "update counter_values set day_id = '$day_id', day_value = '$day_value', yesterday_id = '$yesterday_id', yesterday_value = '$yesterday_value', week_id = '$week_id', week_value = '$week_value', month_id = '$month_id', month_value = '$month_value', year_id = '$year_id', year_value = '$year_value', all_value = '$all_value', record_date = '$record_date', record_value = '$record_value' where id = 1";
    	  mysql_query($sql);  
       }	    	
    ?>
    <table cellpadding="1" cellspacing="0" style="border:1px solid #000000">
      <tr> 
        <td width="160">
          <b><font face="Arial, Helvetica, sans-serif" size="2"><a href="http://www.compredia.fr/" target="_blank"><img src="<? echo $counter_path_http; ?>counter.gif" alt="Ad" width="16" height="16" border="0" /></a> Statistiques de visiteurs</font></b>
        </td>
      </tr>
      <tr> 
        <td style="border-top:1px solid #000000"> 
          <font face="Arial, Helvetica, sans-serif" size="1">
          » <? echo $online; ?> En ligne
    
          » <? echo $day_value; ?> Aujourd'hui
    
          » <? echo $yesterday_value; ?> Hier
    
    	  » <? echo $week_value; ?> Semaine
    
    	  » <? echo $month_value; ?> Mois
    
    	  » <? echo $year_value; ?> Année
    
          » <? echo $all_value; ?> Total
          </font>
        </td>
      </tr> 
      <tr> 
        <td style="border-top:1px solid #000000" width="160" align="center">
          <font face="Arial, Helvetica, sans-serif" size="1">
          Record: <? echo $record_value; ?> (<? echo date("d.m.Y", strtotime($record_date)) ?>)
    
          (<a href="http://www.free-php-couter.com/mysql-counter.php" target="_blank">Gratuit MySQL Counter</a>)
          </font>
        </td>
      </tr> 
    </table>
    <?
    }
    ?>
    


    Is there a way I could use this counter into my Modx Revolution site?

    Thanks for your help and advice,

    Sub
      • 3749
      • 24,544 Posts
      You can't put HTML code directly in a snippet. Also, you shouldn't use echo or print.

      Use a variable called $output and append to it as you go:


      $output .= '$week_value . 'Semaine';


      Then, at the end of your snippet, put:

      return $output;



      ---------------------------------------------------------------------------------------------------------------
      PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
      MODx info for everyone: http://bobsguides.com/MODx.html
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 36098
        • 46 Posts
        Thank you, it works fine !

        Another question cfr. database.

        To test it I connected this script to another database than my Modx database.

        Should I let it connect to the same database than my Modx site or is it better to let it connect to a second database?

        I would call this snippet on all the pages of my site, will it slow down the speed of my site?

        Thanks for the info and help,

        Sub
          • 3749
          • 24,544 Posts
          I think it will be faster to put the data in the MODX database. It will slow down page loads, but I'm not sure how much.


          ---------------------------------------------------------------------------------------------------------------
          PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
          MODx info for everyone: http://bobsguides.com/MODx.html

            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 36098
            • 46 Posts
            Ok, thanks

            I know how to export and import the tables to the Modx database but

            Do you know what I should change in the code to let it connect to the Modx Database instead of the old one : I suppose I don't need the databse configuration part; but for the //connect to databese part I don't know what values to change...

            // database configuration
            $counter_host = "localhost";
            $counter_user = "root";
            $counter_password = "root";
            $counter_database = "counter";
             
            // connect to database
            $counter_connected = true;
            $link = @mysql_connect($counter_host, $counter_user, $counter_password);
            if (!$link)
            {
                // can't connect to database
                $counter_connected = false;
                echo "Counter: " . mysql_error();
            }


            It would be a good add-on to ModX Revolution if it's possible to integer to the ModX db

            Again thanks for your precious help ! [ed. note: subran last edited this post 12 years, 3 months ago.]
              • 3749
              • 24,544 Posts
              In MODX, you are always connected to the MODX database, so there's no need for that code.

              This is an old thread and I'm not sure the methods are still correct: http://forums.modx.com/thread/35321/querying-a-non-modx-table-in-revolution.

              I generally convert my tables for use with xPDO: http://bobsguides.com/custom-db-tables.html.

              Here is some old code of mine that I know works with MODX 2.0+ :

              <?php
              $sql = "SELECT *, 
              			MATCH(author, quote, topic) AGAINST ('$searchstring' IN BOOLEAN MODE) AS score 
              			FROM modx_quotations 
              			WHERE MATCH(author, quote, topic) AGAINST('$searchstring' IN BOOLEAN MODE) 
              			ORDER BY id DESC"; 
              
              $result = $modx->query($sql) or die (mysql_error()); 
              
              while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
              	
                  echo "<div class=\"quotation\">".stripslashes($row['quote'])."</div>";
                  echo "<div class=\"author\">&#8212  "  . stripslashes(htmlspecialchars($row['author'])). "</div>";
              } 
              





              ---------------------------------------------------------------------------------------------------------------
              PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
              MODx info for everyone: http://bobsguides.com/MODx.html [ed. note: BobRay last edited this post 12 years, 3 months ago.]
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting