Insert specific content.

5 posts by 2 authors in: Forums > CMS Builder
Last Post: August 30, 2013   (RSS)

Hi Tim,

Does your insertOne.php file contain dynamic PHP content, or is it just html code? If its just html code the easiest way to swap out the placeholder is like this:

  $filterOn = '#insert_content#';

  //Get the new content to display from a file
  $new_content = file_get_contents('test.php'); 

  //Add new content to content variable
  $blogRecord['content'] = str_replace($filterOn, $new_content, $blogRecord['content']);

So the placeholder in this example is called #insert_content#, the content of test.php is stored as a string in the variable new_content. Then str_replace is used to swap the placeholder with the string of content from test.php. 

If you have PHP code in insertOne that needs to be run on the page, I would recommend using this method instead:

  $filterOn = '#insert_content#';

  //Check if the string contains the filterOn placeholder, if it does store the num where it starts in a variable
  if($startNum = strrpos( $blogRecord['content'] , $filterOn)){
    
    //Display the first half of our content string before the placeholder
    echo substr($blogRecord['content'],0,$startNum);
    
    //Include our content from test.php
    include('test.php');

    //Display the second half of the content after the placeholder. Add the length of the placeholder to ensure the placeholder isn't displayed. 
    echo substr($blogRecord['content'],$startNum+(strlen($filterOn)));
  }else{
    
    //If the string doesn't contain our placeholder, display it like normal.
    echo $blogRecord['content'];
  }

So the above system first checks if the blog content contains the filter on variable, and number of digits into the string where it starts. It then displays the first half on the string before the placeholder. Then the content of test.php is included and then finally the second half of the content is displayed.

All of the above is just example code, so you might have to make a few changes to get it working with your pages.

Let me know if you have any questions. 

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By Toledoh - August 29, 2013

That works great Greg - and yes, it's dynamic content.

However, I've got multiple placeholders in a single section... ie.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

#content-1#

Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

#content-2#

Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Can I do that?

Cheers,

Tim (toledoh.com.au)

Hi Tim,

Here is a much simpler solution that allows you to insert several placeholders:

  // load records from 'blog'
  list($blogRecords, $blogMetaData) = getRecords(array(
    'tableName'   => 'blog',
    'where'       => "num = '1'",
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '1'
  ));
  $blogRecord = $blogRecords[0];


  //Create an array of placeholders to file names
  $insertArray = array(
    "#insert_content#"  => 'test.php',
    "#insert_content2#" => 'test2.php'
  );

  //Set content that will contains placeholders to a varaible
  $content = $blogRecord['content'];
  
  //Cycle through placeholders
  foreach($insertArray as $placeholder => $file){
    //If the content contains the placeholder....
    if($startNum = strrpos($content , $placeholder)){
      //Create and object and include the file to 'run' the code
      ob_start();
      include($file);

      //Add objects content to a placeholder content variable
      $placeholderContent = ob_get_clean();

      //Replace placeholder with replace holder content
      $content = str_replace($placeholder, $placeholderContent, $content);
    }
  }

  //Display completed content
  echo $content;

So the process is fairly similar to my first post, but instead we're running through an array of placeholders and files, and then using the output buffering feature to run the code in the include files and store it in a string, then replace that code with a placeholder.

Let me know if you have any questions. 

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By Toledoh - August 30, 2013

Wow - that's really cool.  It gives a whole new level of flexibility!

Cheers,

Tim (toledoh.com.au)