Mooticus Mooticus - 1 month ago 5
PHP Question

Mixing PHP and HTML for Search Results Page

i'm working on setting up a page that uses PHP to layout the page, but i also need this PHP HTML code inserted into the PHP page for a search function. The search is done on a different page, and then the action is sent to the results page. I'm trying to get the PHP and HTML to mix together. I've tried using echo to no success. Basically i need the PHP HTML code to put the results into the $layout->content("");

<?php
require_once($_SERVER["DOCUMENT_ROOT"].'/layout/layout.inc.php');
require_once($_SERVER["DOCUMENT_ROOT"].'/functions/general.inc.php');

$layout = new default_layout();

$layout->title('IT KB Search');

$layout->content("<div class='border'>");
$layout->content('<h1>IT Support Knowledge Base - Search Results</h1>');

if (isset($_GET['q'])) {

$query = rawurlencode( strip_tags($_GET['q']));
$timestamp = time();
$baseUrl = 'https://oursite.atlassian.net/wiki';
$url = $baseUrl.'/rest/api/content/search?cql=space=KB%20AND%20type=page%20AND%20title~'.$query;

// To enable authenticated search:
// $url .= "&os_username=$username&os_password=$password";

$response = file_get_contents($url);
$response = json_decode($response);

$results = $response->results;

Echo '<div>';
Echo ' <ol>';
foreach($results as $item) {
Echo ' <li><strong><a href="';
$baseUrl. $item-> _links-> webui
Echo ' " target='_blank'>';
$item->title
Echo ' </a></strong></li>';
}
Echo ' </ol></div><hr>';

}

$layout->content("</div>");

$layout->render();
?>

Answer

You can to store the HTML to a string and then pass it to the $layout->content() function, like this...

<?php
require_once($_SERVER["DOCUMENT_ROOT"].'/layout/layout.inc.php');
require_once($_SERVER["DOCUMENT_ROOT"].'/functions/general.inc.php');

$layout = new default_layout();

$layout->title('IT KB Search');

$layout->content("<div class='border'>");
$layout->content('<h1>IT Support Knowledge Base - Search Results</h1>');

if (isset($_GET['q'])) {
    $query = rawurlencode( strip_tags($_GET['q']));
    $timestamp = time();
    $baseUrl = 'https://oursite.atlassian.net/wiki';
    $url = $baseUrl.'/rest/api/content/search?cql=space=KB%20AND%20type=page%20AND%20title~'.$query; 

    // To enable authenticated search: 
    // $url .= "&os_username=$username&os_password=$password";

    $response = file_get_contents($url);
    $response = json_decode($response);
    $results = $response->results;

    # Change Starts
    $html = '<div>';
    $html .= '<ol>';
    foreach($results as $item) {
       $html .= '<li><strong><a href="';
       $html .= $baseUrl. $item-> _links-> webui;
       $html .= '" target="_blank">';
       $html .= $item->title;
       $html .= '</a></strong></li>';
    }
    $html .= '</ol></div><hr>';
    $html .= '</div>';

    $layout->content($html);
    # Change Ends
} 

$layout->content('</div>');
$layout->render();
Comments