We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26182
    • 164 Posts
    I have been trying to print just the pagetitle as a link for a particular user. my php is pretty basic to say the least at the moment so any help i could get would be greatly appreciated. here’s the code i have for the snippet:

    <?php
    //Author: Sarah Vardy
    //Date: 14-03-2007
    
    //This snippet displays the posts made by the user who is currently logged in.
    
    # set page to redirect to if user is not logged in, site_start by default
    if(!isset($_SESSION['webInternalKey']) || empty($_SESSION['webInternalKey'])) {
      $logout = isset($logout) ? $logout : $modx->config['site_start'];
      $modx->sendRedirect($modx->makeUrl($logout));
    }
    
    //Get user login name and store it in variable userLogin
    $userLogin = $modx->getLoginUserID();
    
    //Find the correct entries in the database that correspond to posts made by this user
    
    //Select the modx table where the posts are stored
    $tbl = $modx->getFullTableName('site_content');
    
    $result = $modx->db->select("*", $tbl);
    
    $count = 0;
    $post_count = array();
    
        while($row = $modx->db->getRow($result)) {
    
         //if createdby id matches the userLogin id then display this row
         if ($row["createdby"] == -($userLogin))
         {
              echo " <br />";          
              //display this post
              $post_array[$count] = $row["'pagetitle'"];
              //return $result;
              echo "Post " . $count . " " . $post_array;
              $count ++;
              echo " <br /> ";
          }
         
       }
    
    //return $count;
    //return $post_array;
    //return count($post_array);
    
    
    //$tempUserPosts = $modx->db->select("*", $tbl, "pagetitle, longtitle, pub_date ", "pub_date DESC", "10");
    //$tempUserPosts = $modx->db->select("*", $tbl, "createdby=".$userLogin, "pub_date DESC", "10");
    
    //return $tempUserPosts;
    ?>
      • 18397
      • 3,250 Posts
      You can do this using Ditto. Here is a quick call to get you started:

      [!Ditto? &parents=`0` &depth=`5` &filter=`createdby,[[current_user_id]]` !]

      Where the snippet current_user_id has the contents:

      return $modx->getLoginUserID();
      


      Regarding your code, always use MySQL’s filtering abilities as they are much faster than PHP’s. Thus, you should add a where statement to your query to use "createdby"

      Next-time, please post the error you are getting and on what line.