Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Build a Simple PHP Application Listing Inventory Items Displaying All Products

can u help me with my code

class function that is retrieving values from db

public function retrieveFun() { include 'inc/main.php';//connecting to db.

    $result = mysqli_query($con,"SELECT * FROM db  order by name DESC");


    while($row = mysqli_fetch_array($result))
      {
        $var = array('name' =>$row['name'] ,
                      'owner'=>$row['owner'],
                      'date'=>$row['date'] );
        // echo $var["name"]."<br/>";
        // echo $var["owner"]."<br/>";
        // echo $var["date"]."<br/><br/>";
        return array($var["name"],$var["owner"],$var["date"]);

}}

and code where i want to display all the rows retrieved in desired format

$obj = new classname(); $id= $obj->retrieveFun();

echo //here i want to display all the rows retrieved in table format. Please help me out as soon as possible

1 Answer

I don't know if I understand you question, or if you have even asked one, but try this:

  1. Make sure you're connected to your database. Then you can do this.
$result = mysqli_query($con,"SELECT * FROM db ORDER BY name DESC");
while($row = mysqli_fetch_array($result)){

$name = $row['name']; //name is a column in your db
$owner = $row['owner']; //owner is a column in your db
$date = $row['date']; //date is a column in your db
}

$printoutput .= "<table width='500' border='0' cellspacing='0' cellpadding='0'>
             <tr><td> ". $name . "</td></tr>
             <tr><td>$" . $owner . "</td></tr>
             <tr><td>". $date . "</td></tr></table>

echo $printoutput;

I'm not sure what this is, $obj = new classname(); $id= $obj->retrieveFun(); Or what you meant by this, but I hope this helps.