Search code examples
phphtmlinclude

Using several included php pages, how do I make a layout?


I am trying to learn PHP (being a C# programmer) and so far it has not been difficult (getting db records, logging in...), but the thing I thought would be easy actually is not, for me - I learned to use the "include" keyword to inject the content of other files. However, I cannot figure out how to influence the design, where I would like to get the database records in the left sidebar (for that I wrote sidebar.php that does the querying, and page.php that is meant to show details, so that is the main content element):

<?php
   include('session.php');
?>
<html">
   
   <head>
      <title>My catalogue</title>
   </head>
   
   <body>
      <h2><a href = "logout.php">Sign Out</a></h2>
   </body>
   //I would need this to be always on the left
  <?php include("sidelist.php"); ?>
  //This should be the main content of the page
  <?php include("page.php"); ?>
   
</html>

I did search around but all advice just stated to use the include, which certainly is not all that is needed. Thanks a lot


Solution

  • When ever you are talking about layouts you will be dealing with HTML and CSS.

    wrap your two includes in divs and then create some css class to format the layout as you need e.g

    <?php
       include('session.php');
    ?>
    <html">
       
       <head>
          <title>My catalogue</title>
       </head>
       
       <body>
          <h2><a href = "logout.php">Sign Out</a></h2>
       </body>
       //I would need this to be always on the left
      <div id="sidelist">
      <?php include("sidelist.php"); ?>
      </div>
      //This should be the main content of the page
      <div id="main">
      <?php include("page.php"); ?>
      </div>
       
    </html>
    

    And then the CSS might look like

    #sidelist{
    float:left;
    width:75%;
    overflow:hidden;
    }
    #main{
    float:left;
    width:25%;
    overflow:hidden;
    }