2

i am trying to display data from mongoDB with HTNL but the code that i wrote doesn't do it and i have no clue what i should change so it will work.

<body>
<?php
    $server = "mongodb://localhost:27017/test";
   // connect to mongodb
   $m = new MongoClient($server);
   // select a database
   $db = $m->test;
?>

    try{
        // Connecting to server
    $c = new MongoClient( $server );
    }catch(MongoConnectionException $connectionException){
        print $connectionException;
        exit;
    }

    $data  = "<table style='border:1px solid red;";
    $data .= "border-collapse:collapse' border='1px'>";
    $data .= "<thead>";
    $data .= "<tr>";
    $data .= "<th>Name</th>";
    $data .= "<th>UserID</th>";
    $data .= "<th>Email</th>";
    $data .= "<th>Sites Links</th>";
    $data .= "<th>Imgaes</th>"
    $data .= "</tr>";
    $data .= "</thead>";
    $data .= "<tbody>";

    try{
        $collection = $db->links;
        $people = $collection->find();
        foreach($people as $document){
            $data .= "<tr>";
            $data .= "<td>" . $document["Name_of_the_person"] . "</td>"; // Name
            $data .= "<td>" . $document["userID"]."</td>"; // UserID
            $data .= "<td>" . $document["email"]."</td>"; // Email
            $data .= "<td>" . $document["links"]."</td>"; // Sites
            $data .= "<td>" . $document["imagesurl"]."</td>"; // Images
            $data .= "</tr>";
        }
        $data .= "</tbody>";
        $data .= "</table>";
        echo $data;

    }catch(MongoException $mongoException){
        print $mongoException;
        exit;
    }

</body>
</html>

i would really want to know what is wrong with the code.

Thanks

5
  • Because the ?> tag is in the wrong position? Commented Nov 3, 2014 at 14:17
  • So where should i put it? Commented Nov 3, 2014 at 14:20
  • Where your php ends? Commented Nov 3, 2014 at 14:22
  • After I connect to the db I think. I need to display the data that is saved inside this data base but I don't know how.. Commented Nov 3, 2014 at 14:27
  • You need not have to write code to do this. Start mongoDB with --rest , you can query data from the REST API(though not in the format mentioned by you) Commented Nov 4, 2014 at 12:46

1 Answer 1

3

I agree with vmr's comment. But if you still want to use the code:

<?php
$server = "mongodb://localhost:27017/test";
// connect to mongodb
$m = new MongoClient($server);
// select a database
$db = $m->test;

?>

<table style='border:1px solid red border-collapse:collapse' border='1px'>";
    <thead>
        <tr>
            <th>Name</th>
            <th>UserID</th>
            <th>Email</th>
            <th>Sites Links</th>
            <th>Images</th>
        </tr>
    </thead>
<tbody>


<?php
try{
    $collection = $db->links;
    $people = $collection->find();
    foreach($people as $document){
       echo "<tr>";
       echo "<td>".$document["Name_of_the_person"]."</td>"; // Name
       echo "<td>".$document["userID"]."</td>"; // UserID
       echo "<td>".$document["email"]."</td>"; // Email
       echo "<td>".$document["links"]."</td>"; // Sites
       echo "<td>".$document["imagesurl"]."</td>"; // Images
       echo "</tr>";
   }
    echo "</tbody>";
    echo "</table>";
}catch(MongoException $mongoException){
   print $mongoException;
   exit;
}
?>

I know the post is old. But if you are still looking for the solution it might help.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.