Jump to content

displaying left content


robgood

Recommended Posts

Hi, just started to try and get my head round oop and am getting a bit confused.  Have set up a display method which is basically a template for all my html and menus.  The display method, in turn, makes a call to DisplayLeftContent().  DisplayLeftContent() outputs a sub menu, some html etc and also $this->leftcontentforpage if it has been set.  This works fine until I tried to set the left content using another method from another class.

 

So i have my basic layout working and decide that i want to set the leftcontent to display a table of any new orders that have come in. (see class order below).  So i created a GetAllOrders() method below to pull the info from the database and used it in my script to set the left content.  Now the method does its job and outputs the data, but it does not add it to the corrrect position on the page.  It just ouputs the data at the top of the page, before all the other html, and pushes the rest of the site down.  Now if i return the data instead of echoing it inserts into the right place but only returns the first row of the table.  I'm guessing it's something to do with assinging multiple echo statments to the $leftcontentforpage attribute. Hope someone can set me on the right path on this one?     

 

When i want to

 

My page script

<?php
    session_start();
    require('lib/class_lib.php');
    
    $neworders = new order();
    
    $page = new page('Orders');
    $page->leftcontentforpage = $neworders->GetAllOrders(3);
    $page->Display();    
?>

 

I have only left in the relevant code to the problem.

 

My Class library

 

class page
{

var $leftcontentforpage;

public function DisplayLeftContent()
        {
            echo '<div id="leftnav">';
            echo '<img src="images/'.$this->currentpage.'header.jpg">';
            $this->DisplaySubMenu();
            echo "$this->leftcontentforpage"; 
            echo '</div>';
        }

public function DisplayLeftContent()
        {
            echo '<div id="leftnav">';
            echo '<img src="images/'.$this->currentpage.'header.jpg">';
            $this->DisplaySubMenu();
            echo "$this->leftcontentforpage"; 
            echo '</div>';
        }
}

class order
{
        var $orderstatus;       
        
        public function GetAllOrders($orderstatus)
        {
            $this->orderstatus = $orderstatus;
            
            $dbh = new PDO('mysql:host=111.111.111.111;dbname=mydbname', username, password);
            $sql = "SELECT id, surname, deliverydate FROM buyer, orders WHERE id=buyerid AND orderstatus=$this->orderstatus";
            
            foreach($dbh->query($sql) as $row)
            {
                $id = $row['id'];
          		$surname = $row['surname'];
          		$deliverydate = $row['deliverydate'];
          		$orderstatus = $row['orderstatus'];
          		
          		echo "<tr><td>$id</td><td>$surname</td><td>$deliverydate</td><td><a href='exploreorder.php?orderid=$id'><img src='images/anyclues.png'/></a></td></tr>";
          		
            }
                
        }
}

Link to comment
Share on other sites

If I recall correctly, this won't work:

$sql = "SELECT id, surname, deliverydate FROM buyer, orders WHERE id=buyerid AND orderstatus=$this->orderstatus";

 

You need to add curly braces around the variable when it becomes more than just the variable name and that is it. So, try changing it to

$sql = "SELECT id, surname, deliverydate FROM buyer, orders WHERE id=buyerid AND orderstatus={$this->orderstatus}";

 

Anyway, that really isn't what you are asking for. What you need to do is the following:

$result = '';
foreach($dbh->query($sql) as $row)
{
    $id = $row['id'];
    $surname = $row['surname'];
    $deliverydate = $row['deliverydate'];
    $orderstatus = $row['orderstatus'];
    
    $result .= "<tr><td>$id</td><td>$surname</td><td>$deliverydate</td><td><a href='exploreorder.php?orderid=$id'><img src='images/anyclues.png'/></a></td></tr>";
    
}

return $result
;

 

The reason that you were only getting one row is because you probably were using the return statement inside the foreach loop. Using return terminates the function. So store all the result to a variable then return that variable.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.