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

    This should be fairly simple to some of you... I’m developing a profile system for integration with the Invision Power Board software. I’m using a chunk to return the profile layout, and placeholders within this chunk to return the dynamic profile data. Unfortunately, it seems that my code is replacing only one placeholder, instead of multiple. I’m not much of a programmer, really, but this is a great way to learn what it’s about...

    Here’s the code that’s supposed to replace the placeholders:
    	// IF NO ERRORS HAS OCCURED
    	if (empty($prError)) {
    	
    	// FETCH THE PROFILE DATA AND FIX THE DATE FORMAT
    		$prDbProfileDate =  date('l dS F Y h:i:s A',$prDbProfile['last_visit']);
    		
    		// GET THE CHUNK AND REPLACE THE PLACEHOLDERS
    		$prChunkname = 'profil-test';
    		$prChunk = $modx->getChunk($prChunkname);
    
    		$output = str_replace('[+name+]', $prDbProfile['members_display_name'], $prChunk);		
    		$output = str_replace('[+lastvisit+]', $prDbProfileDate, $prChunk);
    	
    		return $output;
    	}

    In the case above, only [+lastvisit+] gets replaced, not [+name+]. If they switch places (by putting lastvisit on top), [+name+] gets replaced, but not the other one.

    Anybody with an idea of what’s going on?

    Thanks smiley
      • 11975
      • 2,542 Posts
      Hi,

      change:
      $output = str_replace(’[+lastvisit+]’, $prDbProfileDate, $prChunk);


      to:
      $output = str_replace(’[+lastvisit+]’, $prDbProfileDate, $output);

      And you should have a look to the parseChunk function in modxapi

      :-)
        Made with MODx : [url=http://www.copadel.com]copadel, fruits et l
        • 30223
        • 1,010 Posts
        I’m not suprised really...

        Here you are setting the $output variable to the result of the first replacement.
        $output = str_replace(’[+name+]’, $prDbProfile[’members_display_name’], $prChunk);

        And now you are setting the same variable again (throwing away the old value) with the result of the second replacement and what’s more,.. starting from a fresh $prChunk which hasn’t changed and still contains both placeholders, so only [+lastvisit+] is replaced. The other placeholder is cleaned up by modx at some stage.
        $output = str_replace(’[+lastvisit+]’, $prDbProfileDate, $prChunk);

        Try again...

        $output = str_replace(’[+name+]’, $prDbProfile[’members_display_name’], $prChunk);
        $output = str_replace(’[+lastvisit+]’, $prDbProfileDate, $output);

        Here it’s using the $output variable as the source of the of the second replacement. Should make you much happier.

        Of course you could also let MODx deal with the placeholders:

        <?php
                // IF NO ERRORS HAS OCCURED
        	if (empty($prError)) {
        	
        	        // FETCH THE PROFILE DATA AND FIX THE DATE FORMAT
        		$prDbProfileDate =  date('l dS F Y h:i:s A',$prDbProfile['last_visit']);
        
        		//SET PLACEHOLDERS
        		$modx->setPlaceholder('name', $prDbProfile['members_display_name']);		
        		$modx->setPlaceholder('lastvisit', $prDbProfileDate);
        
        		// RETURN THE CHUNK
        		return $modx->getChunk('profil-test');
        	}
        ?>
        


        Edit: Oops,... cross-posted with heliotrope smiley I agree, parseChunk() is even simpler. Have a look at the wiki
          • 3493
          • 66 Posts
          Swell! that’s great, guys! It’s really logical, but I’m not tongue
          Thanks for taking care of us hobby programmers as well. I’ll use parseChunk() from now on... I’ll also add this to the Create snippet WIKI article, as I’m probably not the only "dumb" one around grin