No display of results in HEREDOC php functions

I am currently using heredocs to call one function in another function.
I am getting the results correctly but the main problem is that the results are not organized in the manner i desire my application to work. The main purpose of my application is to show subcategories under the categories main title.
I currently have two tables in my database, which one is the category and the other is the sub category table.

Below is a picture of my two tables and their structure and data

This is the category table in my database.category table in my database

This is the subcategories table in my database.
subcategories table in my database.

The following is the code(mainly fucntions) which is used to display my table with the results using heredocs format

The first function is used to display categories.

function  dispcategories(){

  global $conn;

  $select= mysqli_query($conn," SELECT * FROM category");

  while ($row=mysqli_fetch_assoc($select)) {


$cat =<<<DELIMETER
<table class="table firstTable">
<tr class="me">

<td class="title" >{$row['name']}</td>

<?php $sub_category ?>   

echo $cat;



The last function is used to display sub categories and get a result on sub categories.

function dispsubcategories($parent_id){

  global $conn;
  $select = mysqli_query($conn, "SELECT category_id, sub_cat_id, subcategory_title, subcategory_desc FROM category, subcategories 
                    WHERE ($parent_id = category.category_id) AND ($parent_id = subcategories.parent_id)");
  if (!$select) {
    printf("Error: %s\n", mysqli_error($conn));

  while ($row=mysqli_fetch_assoc($select)) {

 echo $subs = <<<DELIMETER
<td class="results" >



The main problem comes when displaying the results.In my dispcategories() function it clearly shows that the result of the dispsubcategories($parent_id) should be below but all the results are showing on top of the title when i am using this heredoc format.Here is a picture of results showing on top instead of bottom.
a picture of results showing on top instead of bottom.

When i use return instead of echo in my dispsubcategories($parent_id) function. It only returns one result. As seen in the picture below. One result when return is used instead of echo

Please help me solve this problem using heredocs. I am making my application and using a lot of functions and i am using heredocs.
I will really apprecaite your help.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s