MongoDB Collection data to Php table issue - php

I have php code for displaying MongoDB collection data to php tablle, when i run it it doesn't show any output,
please be kind enough to sort this
Here my code
<?php
$m = new MongoClient();
$db = $m->selectDB('MapData');
$collection = new MongoCollection($db,'ETom4');
$cursor = $collection->find();
//echo "<html><head><body>";
echo "<table>";
foreach($cursor as $doc) {
echo "<tr>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Marks'] . "</td>";
echo "<td>" . $row['value'] . "</td>";
echo "</tr>";
}
echo "<table>";
//echo "</html></head></body>";
?>

You're using as $doc instead of as $row here.
foreach($cursor as $doc) {
echo "<tr>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Marks'] . "</td>";
echo "<td>" . $row['value'] . "</td>";
Change it to the variable you need to use, being $doc and not $row:
$m = new MongoClient();
$db = $m->selectDB('MapData');
$collection = new MongoCollection($db,'ETom4');
$cursor = $collection->find();
//echo "<html><head><body>";
echo "<table>";
foreach($cursor as $doc) {
echo "<tr>";
echo "<td>" . $doc['Name'] . "</td>";
echo "<td>" . $doc['Marks'] . "</td>";
echo "<td>" . $doc['value'] . "</td>";
echo "</tr>";
}
echo "<table>";
//echo "</html></head></body>";
HTML stickler:
You also commented out 2 lines which you're including your markup inside <head></head> and have placed </html> before </body>.
$m = new MongoClient();
$db = $m->selectDB('MapData');
$collection = new MongoCollection($db,'ETom4');
$cursor = $collection->find();
//echo "<html><head></head><body>";
echo "<table>";
foreach($cursor as $doc) {
echo "<tr>";
echo "<td>" . $doc['Name'] . "</td>";
echo "<td>" . $doc['Marks'] . "</td>";
echo "<td>" . $doc['value'] . "</td>";
echo "</tr>";
}
echo "<table>";
//echo "</body></html>";
The syntax/structure is:
<html>
<head>
...
</head>
<body>
...
</body>
</html>
You can also add <!DOCTYPE html> as the first line.
(Modern-day) Web browsers require a valid doc type.
Consult: https://www.w3.org/QA/2002/04/valid-dtd-list.html
Footnotes:
You also need to make sure of the columns' letter-case.
They are case-sensitive when iterating over rows in a loop.
Therefore Name and name would be considered different.
If (any or all of) the above still doesn't work for you, then you may have errors somewhere.
So, check for errors.

Related

How do I grab all rows from a single column from multiple tables, based on user input?

I am building a Process selection app and I'm not even sure if my approach is correct (I'm new, sorry!). I have a form with 5 input parameters using radio buttons (choose 1-10).
I have 5 tables setup for each of those parameters that stores the name of the process and fields set as 1 - 10 to correspond to the user input...One of them looks like this:
enter image description here
I am looking to store each parameters user input (1-10) as a session variable to output to an array for further calculation.
I have the following code that sets the session variables from the form and returns only those records that match user input from only one parameter:
<?php
session_start();
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "selectionapp";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if (isset($_POST['Submit'])) {
$_SESSION['temperature'] = $_POST['temperature'];
$_SESSION['partSize'] = $_POST['partSize'];
$_SESSION['volume'] = $_POST['volume'];
$_SESSION['stiffness'] = $_POST['stiffness'];
$_SESSION['weight'] = $_POST['weight'];
}
echo $_SESSION['temperature'];
echo $_SESSION['partSize'];
echo $_SESSION['volume'];
echo $_SESSION['stiffness'];
echo $_SESSION['weight'];
$result = mysqli_query($conn, "SELECT * FROM processes WHERE temperature LIKE '%{$_SESSION['temperature']}%'");
$row = mysqli_fetch_assoc($result);
echo "<table border='1'>";
while ($row = mysqli_fetch_assoc($result)) {
echo '<tr>';
echo '<td>' . $row['score'] .'</td>';
echo "<td>" . $row['type'] . "</td>";
echo "<td>" . $row['temperature'] . "</td>";
echo "<td>" . $row['partSize'] . "</td>";
echo "<td>" . $row['volume'] . "</td>";
echo "<td>" . $row['stiffness'] . "</td>";
echo "<td>" . $row['weight'] . "</td>";
echo "<td>" . $row['tool'] . "</td>";
echo "<td>" . $row['paint'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo '<tr/>';
}
echo '</table>';
?>
<?php
// close connection
mysqli_close($conn);
?>
Except, what I think I really need is to grab the one column that matches the user input (1-10) from each of the 5 tables, to create anew array so that I can use those values in a calculation that will score/rank that process type.
Appreciate any help. Thanks!
Im not entirely sure if this is what you need:
// You can start to create an array:
$array = [];
// Next is to set the $array with data
$array = [
'temperature' => (int) $_POST['temperature'],
'partSize' => (int) $_POST['partSize'],
];
// In case you want to echo a piece of the array simply do this:
echo $array['temperature'];
// If you want to 'add' a number to the current one you can do this:
$array['partSize'] = ($array['partSize'] + (int) $_POST['partSize']);
// Do a check if $_POST data actually exist and/or is valid:
$array = [
'temperature' => (isset($_POST['temperature'])) ? (int) $_POST['temperature'] : 0,
'partSize' => (isset($_POST['partSize'])) ? (int) $_POST['partSize'] : 0,
];
To be honest, I would personally not store this within a session everytime you get new data. Simply do one query and get all data into one array, and calculate these.
For example:
// Set $temperature
$temperature = [];
// Query to get all temperature's and put into an array:
$temperature = [1, 6, 3, 8, 2, 9, 10, 3, 7, 9];
// Count how many items you've got
$count_temperature = count($temperature); // = 10
// Now calculate and get an average
$sum_temperature = 0;
foreach ($temperature as $temp)
{
$sum_temperature = ($sum_temperature + $temp);
}
echo $sum_temperature; // 58
$avg_temperature = ($sum_temperature / $count_temperature);
echo $avg_temperature; // 5.8
I hope you get the idea behind it and this answers your question. You are not forced to use a database connection, it could be stored into a $_SESSION. But please let me reminder you once the session is destroyed, the input will be lost. If this DOES NOT answer your question, please let me know and I will help you find for a solution!
I did it! It is NOT pretty nor concise nor elegant...but it works. Thank you #RonnieOosting! While I did not go the array route, you helped point me in the right direction.
<?php
session_start();
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "selectionapp";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if (isset($_POST['Submit'])) {
$_SESSION['temperature'] = $_POST['temperature'];
$_SESSION['partSize'] = $_POST['partSize'];
$_SESSION['volume'] = $_POST['volume'];
$_SESSION['stiffness'] = $_POST['stiffness'];
$_SESSION['weight'] = $_POST['weight'];
}
echo $_SESSION['temperature'];
echo $_SESSION['partSize'];
echo $_SESSION['volume'];
echo $_SESSION['stiffness'];
echo $_SESSION['weight'];
if ($result_temp = mysqli_query($conn, "SELECT * FROM temperature WHERE '%{$_POST['temperature']}%' LIKE '%{$_SESSION['temperature']}%'"));
echo "<table border='1'>";
echo '<tr>';
echo "<td>" . 'Process Type' . "</td>";
echo "<td>" . 'Temperature Score' . "</td>";
echo '<tr/>';
while ($row = mysqli_fetch_assoc($result_temp)) {
echo '<tr>';
echo "<td>" . $row['type'] . "</td>";
echo "<td>" . $row[$_SESSION['temperature']] * 10 . "</td>";
echo '<tr/>';
}
echo '</table>';
$result_size = mysqli_query($conn, "SELECT * FROM partSize WHERE '%{$_POST['partSize']}%' LIKE '%{$_SESSION['partSize']}%'");
//$row = mysqli_fetch_assoc($result);
echo "<table border='1'>";
echo '<tr>';
echo "<td>" . 'Process Type' . "</td>";
echo "<td>" . 'Part Size Score' . "</td>";
echo '<tr/>';
while ($row = mysqli_fetch_assoc($result_size)) {
echo '<tr>';
echo "<td>" . $row['type'] . "</td>";
echo "<td>" . $row[$_SESSION['partSize']] . "</td>";
echo '<tr/>';
}
echo '</table>';
$result_vol = mysqli_query($conn, "SELECT * FROM volume WHERE '%{$_POST['volume']}%' LIKE '%{$_SESSION['volume']}%'");
//$row = mysqli_fetch_assoc($result);
echo "<table border='1'>";
echo '<tr>';
echo "<td>" . 'Process Type' . "</td>";
echo "<td>" . 'Volume Score' . "</td>";
echo '<tr/>';
while ($row = mysqli_fetch_assoc($result_vol)) {
echo '<tr>';
echo "<td>" . $row['type'] . "</td>";
echo "<td>" . $row[$_SESSION['volume']] . "</td>";
echo '<tr/>';
}
echo '</table>';
$result_stiff = mysqli_query($conn, "SELECT * FROM stiffness WHERE '%{$_POST['stiffness']}%' LIKE '%{$_SESSION['stiffness']}%'");
//$row = mysqli_fetch_assoc($result);
echo "<table border='1'>";
echo '<tr>';
echo "<td>" . 'Process Type' . "</td>";
echo "<td>" . 'Stiffness Score' . "</td>";
echo '<tr/>';
while ($row = mysqli_fetch_assoc($result_stiff)) {
echo '<tr>';
echo "<td>" . $row['type'] . "</td>";
echo "<td>" . $row[$_SESSION['stiffness']] . "</td>";
echo '<tr/>';
}
echo '</table>';
$result_wght = mysqli_query($conn, "SELECT * FROM weight WHERE '%{$_POST['weight']}%' LIKE '%{$_SESSION['weight']}%'");
//$row = mysqli_fetch_assoc($result);
echo "<table border='1'>";
echo '<tr>';
echo "<td>" . 'Process Type' . "</td>";
echo "<td>" . 'Weight Score' . "</td>";
echo '<tr/>';
while ($row = mysqli_fetch_assoc($result_wght)) {
echo '<tr>';
echo "<td>" . $row['type'] . "</td>";
echo "<td>" . $row[$_SESSION['weight']] . "</td>";
echo '<tr/>';
}
echo '</table>';
echo "<table border='1'>";
echo '<tr>';
echo "<td>" . 'LFI Results' . "</td>";
echo "<td>" . 'Score' . "</td>";
echo '<tr/>';
echo '<tr>';
echo "<td>" . $row['type'] . "</td>";
echo "<td>" . $row[$_SESSION['weight']] . "</td>";
echo '<tr/>';
echo '</table>';
?>
<?php
// close connection
mysqli_close($conn);
?>
This outputs:
Process Type Temperature Score
LFI 5
DCPD 5
SMC 10
Process Type Part Size Score
LFI 0.0
DCPD 0.0
SMC 1.0
Process Type Volume Score
LFI 0.5
DCPD 0.5
SMC 1.0
Process Type Stiffness Score
LFI 1.0
DCPD 1.0
SMC 1.0
Process Type Weight Score
LFI 1.0
DCPD 1.0
SMC 1.0

Get data from SQL database of multiple columns stored in explode function using PHP

I am using this code to get that data from SQL Server database using PHP
<?php
foreach ($dbDB->query($query) as $row) {
echo "<tr>";
echo "<td>" . $row['Country'] . "</td>";
echo "<td>" . $row['OrderNumber'] . "</td>";
echo "<td>" . $row['Region'] . "</td>";
echo "<td>" . $row['ShipDate'] . "</td>";
echo "<td>" . $row['ProducedDate'] . "</td>";
echo "</tr>"; }
?>
I am trying to replace these multiple lines but storing the columns' names in a string for example $_POST['SelectedColumns'].
The values coming into post as comma separated string, For example : Country,OrderNumber,Region,ShipDate,ProducedDate
I have tried this solution but still not working for me.
<?php
$ser="********";
$db="******";
$user="******";
$pass="******";
$query = 'SELECT '.$_POST['SelectedColumns'].' FROM reporting.REPORT_ALL';
$dbDB = new PDO("odbc:Driver=ODBC Driver 13 for SQL Server;Server=*******;Database=******;Port=1456", $user, $pass);
$row = $_POST["SelectedColumns"];
$rows = explode(",",$row);
/*Here I have the another html code independent of this part */
foreach ($dbDB->query($query) as $dataRow) {
echo "<table>";
echo "<tr>";
foreach ($rows as $r ) {
echo "<td>" . $dataRow[$r] . "</td>"; }
echo "</tr>";
echo "</table>"; }
?>
Any suggestions please ?

How to deal with arrays in PHP language

<?php
$con = mysqli_connect('localhost', 'root', '');
if(!$con)
{
die("not ok");
}
mysqli_select_db($con,"uoh");
$q1 = "SELECT * FROM student_record INNER JOIN degree_plan ON
student_record.course_number = degree_plan.course_number
INNER JOIN courses ON student_record.course_number =
courses.course_number where student_record.id = 201102887 AND degree_plan.major='COE'";
$result = mysqli_query($con , $q1 ) ;
$data = array();
while($row = mysqli_fetch_array($result))
{
$data[$row["term_no"]][] = array(
'code' => $row["code"],
'grade' => $row["grade"]
);
}
echo '<table width="200" border="1">';
echo "<tr>";
echo "<th>courses</th>";
echo "<th>terms</th>";
echo "<th>grades</th>";
echo "</tr>";
foreach($data as $term=>$otherrow) {
$count = 0;
foreach ($otherrow as $data) {
if($count == 0) {
echo "<tr>";
echo "<td>" . $data["code"]. "</td>";
echo '<td rowspan="'.count($otherrow).'">' . $term. '</td>';
echo "<td>" . $data["grade"]. "</td>";
echo "</tr>";
}
else
{
echo "<tr>";
echo "<td>" . $data["code"]. "</td>";
echo "<td>" . $data["grade"]. "</td>";
echo "</tr>";
}
$count++;
}
}
echo "</table>";
?>
I have this code and it work very well but I faced problem when I want to add more column .
I tried to add fourth column(echo "<td>" . $row["crd"]. "</td>"; ) but there is no result .It give me empty cells. how I can do that?
I want add add this echo "<td>" . $row["crd"]. "</td>"; column to my code.
As mentioned in the comments, there are two errors that have been noticed.
You are re-declaring $data in your second foreach loop
You don't have $row initiated anywhere, and atempting to echo $row["crd"] will result in an empty cell.
Proposed Solution:
Change the name of the $data value in the foreach loop to $row and hence solve both problems at the same time:
foreach($data as $term=>$otherrow) {
$count = 0;
foreach ($otherrow as $row) {
if($count == 0) {
echo "<tr>";
echo "<td>" . $row["code"]. "</td>";
echo '<td rowspan="'.count($otherrow).'">' . $term. '</td>';
echo "<td>" . $row["grade"]. "</td>";
echo "</tr>";
}
else
{
echo "<tr>";
echo "<td>" . $row["code"]. "</td>";
echo "<td>" . $row["grade"]. "</td>";
echo "</tr>";
}
$count++;
}
}
And when you add echo "<td>" . $row["crd"]. "</td>"; now it should echo the value stored in the $row array (as long as the value was extracted from the table in the database in the first place of course).
Let me know if this worked for you.

PHP - SQL select Data display

<html>
<head>
<meta http-equiv = "content-type" content = "text/html; charset = utf-8" />
<title>Using file functions PHP</title>
</head>
<body>
<h1>Web Development - Lab05</h1>
<?php
require_once("settings.php");
$dbconnect = #mysqli_connect($host, $user, $pswd, $dbnm);
if($dbconnect->connect_errno >0)
{
die('Unable to connecto to database [' . $db->connect_error . ']');
}
$queryResult = "SELECT car_id, make, model, price FROM cars";
echo "<table width='100%' border='1'>";
echo "<tr><th>ID</th><th>Make</th><th>Model</th><th>Price</th></tr>";
//initiate array
$displayrow= mysqli_fetch_array($queryResult);
//initiate while loop to iterate through table
while($displayrow)
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['Make'] . "</td>";
echo "<td>" . $row['Model'] . "</td>";
echo "<td>" . $row['Price'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($dbconnect);
?>
</body>
</html>
This is doing my head in, I cannot figure out why it will not display the actual data apart from the Table header. No matter what I used.
I have tried mysqli_fetch_array, mysqli_fetch_row, mysqli_fetch_assoc but nothing works.
Help and explanation why it was not displaying the data would be much appreciated :)
First: You aren't running a query, you are only putting the query text in a variable. You need to use mysqli_query.
Second: You should add mysqli_fetch_array to the loop.
For example:
while($displayrow = mysqli_fetch_array($queryResult))
{
}
Otherwise you are only getting the first row.
Third: Array keys are case sensitive. There is no $row['ID'], as Jeribo pointed out, it is $row['car_id'] as referenced in your query. $row['Make'] is not the same as $row['make'].
Please Precision to names of field in Query ( car_id,make,...)
while($displayrow= mysql_fetch_assoc($queryResult) )
{
echo "<tr>";
echo "<td>" . $displayrow['car_id'] . "</td>";
echo "<td>" . $displayrow['make'] . "</td>";
echo "<td>" . $displayrow['model'] . "</td>";
echo "<td>" . $displayrow['price'] . "</td>";
echo "</tr>";
}
If you want to query outside you still have to set it in the loop:
$result = $db->query($queryResult)
while($row = $result ->fetch_assoc()){
...
}
a Good Tutorial is shown here: http://codular.com/php-mysqli
$row needs to be initialized so why don't you try:
while($row = mysqli_fetch_array($queryResult))
{
....
}
You have to get the result set first and then try fetching array from result set
<?php
require_once("settings.php");
$dbconnect = #mysqli_connect($host, $user, $pswd, $dbnm);
if($dbconnect->connect_errno >0)
{
die('Unable to connecto to database [' . $db->connect_error . ']');
}
$query = "SELECT car_id, make, model, price FROM cars";
$resultSet=mysqli_query($dbconnect,$query)
echo "<table width='100%' border='1'>";
echo "<tr><th>ID</th><th>Make</th><th>Model</th><th>Price</th></tr>";
//initiate array
$displayrow= mysqli_fetch_array( $resultSet);
//initiate while loop to iterate through table
while($displayrow)
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['Make'] . "</td>";
echo "<td>" . $row['Model'] . "</td>";
echo "<td>" . $row['Price'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($dbconnect);
?>
http://www.w3schools.com/php/func_mysqli_fetch_array.asp

PHP - How do I get this to print out in a table from an reference number?

echo '<td><input type="checkbox" name="items[]" value="' . $row['0'] . '" /></td>';
Hi, I'm trying to get a reference number which comes from a an array called items from another page as shown above, and to find it in the table and print out the reference row, like "Title,Platform...." into another table, but I can't seem to get it working...any help be appreciated
if (isset($_POST['items'])) {
$n = count($_POST['items']);
for($i=0; $i < $n; $i++){
// echo $_POST['items'][$i];
}
$items = array();
foreach ($_POST['items'] as $item) {
$items[] = pg_escape_string($con, $item);
}
if (!$_SESSION["selectingrows"]) {
$item_string = "'" . implode("','", $items) . "'";
$result = pg_query ($con, "SELECT title, platform, description, price FROM CSGames WHERE 'refnumber' IN ($item_string)");
while($rows = pg_fetch_assoc($result))
{
echo "<tr>";
echo "<td>" . $rows['1'] . "</td>";
echo "<td>" . $rows['2'] . "</td>";
echo "<td>" . $rows['3'] . "</td>";
echo "<td>" . $rows['4'] . "</td>";
echo "</tr>";
}
}
}
One thing, you need to put {} braces after your while loop.
Here is what you are doing:
while($rows = pg_fetch_assoc($result))
echo"<tr>"; echo "<td>" . $rows['1'] . "</td>"; echo "<td>" . $rows['2'] . "</td>"; echo "<td>" . $rows['3'] . "</td>"; echo "<td>" . $rows['4'] . "</td>";
echo"</tr>";
By not putting braces around the code after the while statement, here is what your code really does:
while($rows = pg_fetch_assoc($result))
{
echo"<tr>";
}
echo "<td>" . $rows['1'] . "</td>"; echo "<td>" . $rows['2'] . "</td>"; echo "<td>" . $rows['3'] . "</td>"; echo "<td>" . $rows['4'] . "</td>";
echo"</tr>";
You should always put braces in to define what code is in the while loop.
You want your code to be something like this:
while($rows = pg_fetch_assoc($result))
{
echo "<tr>";
echo "<td>" . $rows['1'] . "</td>";
echo "<td>" . $rows['2'] . "</td>";
echo "<td>" . $rows['3'] . "</td>";
echo "<td>" . $rows['4'] . "</td>";
echo "</tr>";
}
Format your code neatly and properly. By doing this your code is clearer and it is much easier to notice possible mistakes like the above. Always use braces for if, while, for statements. When putting an end line semicolon ; put in a new line break. Indent your code correctly. It's little things like formatting that make coding easier.
Now the next problem I can see is the values you are getting from the $rows array:
$rows['1'];
$rows['2'];
$rows['3'];
$rows['4'];
This is trying to get something from the $rows array which has the key of string '1'.
Usually you access array values by index, which uses an integer beggining from 0. Or you access it by a key.
Either you can try this:
$rows[0];
$rows[1];
$rows[2];
$rows[3];
Or this:
$rows['title'];
$rows['platform'];
$rows['description'];
$rows['price'];

Categories