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

    I am using two different PHP scripts, one to access my MySQL database that my WordPress is hosted on, and another to grab the latest 12 posts and view them on the homepage.

    The PHP script to access the database: [[wp-log]] Snippet
    <?php
    //db parameters
    $db_username = '#########';
    $db_password = '#########';
    $db_database = '#########';
    $db_host = '#########';
    
    //connect to the database
    mysql_connect($db_host, $db_username, $db_password);
    @mysql_select_db($db_database) or die("Unable to select database");
    
    //get data from database -- !IMPORTANT, the "LIMIT 5" means how many posts will appear. Change the 5 to any whole number.
    $query = "Select * FROM wp_posts WHERE post_type='post' AND post_status='publish' ORDER BY post_date DESC LIMIT 12"; 
    
    $query_result = mysql_query($query);
    $num_rows = mysql_numrows($query_result);
    
    //close database connection
    mysql_close();
    
    ?>


    I put that into a snippet called [[wp-log]] and then put it above all of the coding in my template (dak films) as [*wp-log*]

    Then In the right column of my page I have the snippet [[l12p]] which has the following PHP code in it:
              <?php
    
    for($i=0; $i< $num_rows; $i++){ 
    
    $blog_date = mysql_result($query_result, $i, "post_date");
    $blog_title = mysql_result($query_result, $i, "post_title");
    $blog_content = mysql_result($query_result, $i, "post_content");
    $blog_permalink = mysql_result($query_result, $i, "id");
    
    $blog_date = strtotime($blog_date);
    $blog_date = strftime("%b %e", $blog_date);
    
    ?>
              <li class="home-list"><a href='http://blog.dakfilms.org/?p=<?php echo $blog_permalink; ?>' target="_parent"><span class="date"><?php echo $blog_date; ?>:</span> <?php echo $blog_title; ?></a></li>
              <?php
    }
    ?>


    This is used in my home page, not in the template, but in an actual page as [*l12p*].

    Yet it does not show up.

    This method does work if I do not use ModX and just simply have an index.php file with them in it.

    Ex. http://www.dakfilms.org (not a modx page)
    http://www.dakfilms.org/index.php.modx (the modx page)

    Any help would be greatly appreciated!! Thanks!!!
      • 4041
      • 788 Posts
      I think you will have to integrate both parts into the same snippet due to the way the parser works. You could execute this a couple ways but I think this method would be simpler.

      <?php
      //db parameters
      $db_username = '#########';
      $db_password = '#########';
      $db_database = '#########';
      $db_host = '#########';
      
      //connect to the database
      mysql_connect($db_host, $db_username, $db_password);
      @mysql_select_db($db_database) or die("Unable to select database");
      
      //get data from database -- !IMPORTANT, the "LIMIT 5" means how many posts will appear. Change the 5 to any whole number.
      $query = "Select * FROM wp_posts WHERE post_type='post' AND post_status='publish' ORDER BY post_date DESC LIMIT 12"; 
      
      $query_result = mysql_query($query);
      $num_rows = mysql_numrows($query_result);
      $output ='';
      
      for($i=0; $i< $num_rows; $i++){ 
      
      $blog_date = mysql_result($query_result, $i, "post_date");
      $blog_title = mysql_result($query_result, $i, "post_title");
      $blog_content = mysql_result($query_result, $i, "post_content");
      $blog_permalink = mysql_result($query_result, $i, "id");
      
      $blog_date = strtotime($blog_date);
      $blog_date = strftime("%b %e", $blog_date);
      
                $output .='<li class="home-list"><a href="http://blog.dakfilms.org/?p='.$blog_permalink.'" target="_parent"><span class="date">'.$blog_date.': </span> '.$blog_title.'</a></li>';
      }
      
      return $output;
      ?>
        xforum
        http://frsbuilders.net (under construction) forum for evolution
        • 22303 MODX Staff
        • 10,725 Posts
        Correct Breezer (or set a placeholder in the first snippet and use that placeholder elsewhere on the page, except the page would have to be uncached), but that code is also changing the database connection that modx is using and closing it. See the new_link parameter at mysql_connect() to see how to open a second connection handle without hijacking the existing one. You’ll need then pass around the specific connection you create to your other mysql_ function calls.
          • 2722
          • 11 Posts
          THANK YOU!!!
          It worked!!!
          Thank You so much!