So, I'm connected to a database and I'm pulling data from two different tables within same database. I'm running foreach to generate rows, but the table renders not the way I want. I want the 2nd foreach to create a 2nd column to the right of the first one and all those dynamic rows to go downward.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/db.css" rel="stylesheet">
<script src="js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row text-center">
<h3>Schedule of Ms. Kim & Ms. Morly</h3>
</div>
<?php
include 'database.php';
$pdo = Database::connect();
//Start of table Kim - ID=10
$sql = 'SELECT * FROM kim ORDER BY id';
$q = $pdo->prepare($sql);
$q->execute(array($schd_tut_id));
$data = $q->fetch(PDO::FETCH_ASSOC);
$sql2 = 'SELECT tut_img FROM tbl_tutors_profile WHERE tut_profile_id=10 ';
$q2 = $pdo->prepare($sql2);
$q2->execute(array($tut_profile_id));
$data2 = $q2->fetch(PDO::FETCH_ASSOC);
$imgkim = $data2['tut_img'];
//End of Table Kim
//Start of table Morly - ID=16
$sql3 = 'SELECT * FROM morly ORDER BY id';
$q3 = $pdo->prepare($sql3);
$q3->execute(array($schd_tut_id));
$data3 = $q3->fetch(PDO::FETCH_ASSOC);
$sql4 = 'SELECT tut_img FROM tbl_tutors_profile WHERE tut_profile_id=16 ';
$q4 = $pdo->prepare($sql4);
$q4->execute(array($tut_profile_id));
$data4 = $q4->fetch(PDO::FETCH_ASSOC);
$imgmorly = $data4['tut_img'];
//End of table Morly
?>
<table class="table table-striped table-bordered">
<thead>
<th style="width:20px;">Time</th>
<th><img class="img-responsive" src="<?php echo $imgkim ?>"</th>
<th><img class="img-responsive" src="<?php echo $imgmorly ?>"</th>
<th><img class="img-responsive" src="<?php echo $imgmorly ?>"</th>
</thead>
<tbody>
<?php
//tutor kim
foreach ($pdo->query($sql) as $row ) {
echo '<tr>';
//echo '<td hidden>'. $row['id'] . '</td>';
//echo '<td hidden>'. $row['schd_tut_id'] . '</td>';
echo '<td>'. $row['time'] . '</td>';
if($row['status']=='available')
{ echo '<td>'. 'available';}
else {echo '<td>'. $row['status'] . '</td>';}
}
//echo '</tr>';
//tutor morly
foreach ($pdo->query($sql3) as $row ) {
//echo '<tr>';
//echo '<td hidden>'. $row['id'] . '</td>';
//echo '<td hidden>'. $row['schd_tut_id'] . '</td>';
echo '<td>'. $row['time'] . '</td>';
if($row['status']=='available')
{ echo '<td>'. 'available';}
else {echo '<td>'. $row['status'] . '</td>';}
echo '</tr>';
}
Database::disconnect();
?>
</tbody>
</table>
</div>
</div> <!-- /container -->
</body>
</html>
Something like this should work, might need a bit of fiddeling.
Basically just store the values in an array and then print the array.
<?php
$row1 = [];
$row2 = [];
//table 1
foreach ($pdo->query($sql) as $row ) {
$row1[] = $row['status'];
}
//table 2
foreach ($pdo->query($sql2) as $row ) {
$row2[] = $row['status'];
}
echo "<table>";
foreach ($row1 as $key => $row) {
echo "<tr><td>" . $row[$key] . "</td><td>" . $row2[$key] . "</td></tr>";
}
echo "</table>";
Database::disconnect();
?>
Related
I'm trying to open a new php page from the sNumber and display the data from the student table on student profile page from the sNumber. But I can't retrieve the data, it goes right to the error. Any help will be appreciated. Thanks
studentlist.php
<div class="memtable">
<?php
$reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages;
echo '<div class="pagination"><ul>';
if ($total_pages > 1) {
echo paginate($reload, $show_page, $total_pages);
}
echo "</ul></div>";
// display data in table
echo "<table class='table table-bordered'>";
echo "<thead><tr><th>Last Name</th> <th>First Name</th> <th>School</th> <th>Snumber</th></tr></thead>";
// loop through results of database query, displaying them in the table
for ($i = $start; $i < $end; $i++) {
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) {
break;
}
// echo out the contents of each row into a table
$lastName = "<a href = 'studentprofile.php?id= " .mysql_result($result, $i, 'sNumber'). "'>" . mysql_result($result, $i, 'lastName') . "</a>";
echo "<tr " . $cls . ">";
echo '<td>' . $lastName . '</td>';
echo '<td>' . mysql_result($result, $i, 'firstName') . '</td>';
echo '<td>' . mysql_result($result, $i, 'school') . '</td>';
echo '<td>' . mysql_result($result, $i, 'sNumber') . '</td>';
echo "</tr>";
}
// close table>
echo "</table>";
// pagination
?>
</div>
studentprofile.php
<?php
include('phpdocs/connect.inc.php');
include('header.php');
if ( isset( $_GET[ "sNumber" ] ) )
$student_sNumber = $_GET['sNumber'];
$getStudentInfo = " SELECT sNumber FROM student WHERE student.sNumber = " . $student_sNumber;
?>
<!DOCTYPE html>
<html>
<head>
<title>Student</title>
<link href="css/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div class="transoverlay">
<?php
if ($result = mysql_query($getStudentInfo)) {
/* fetch associative array */
while ($row = mysql_fetch_assoc($result)) {
echo "<h1 class='tv'>" . $row["sNumber"]. ", ". $row['firstName']."</h1>";
}
mysql_free_result($result);
}else{
echo "<div class='tv'>Student Data could not be listed. </div>";
}
?>
<hr color="#1a1a1a">
</div>
</body>
<?php include('footer.php');?>
</html>
the url uses id but you checking for sNumber change one of those
you need to quote student number in the query as its a string
$getStudentInfo = "SELECT sNumber FROM student WHERE student.sNumber ='". $student_sNumber."'";
I am creating a table that echo out replied messages and not replied messages.
My script for not replied messages worked well,only those not replied appeared.
But my other script for replied messages echo all data, both replied and not replied.
my script in one page,
<div class="container">
<div class="row">
<?php
require_once 'dbfunction.php';
$con = getDbConnect();
if (!mysqli_connect_errno($con)) {
$sqlQueryStr = "SELECT * FROM feedback WHERE status = 0";
$result = mysqli_query($con, $sqlQueryStr);
while ($row = mysqli_fetch_array($result)) { // fetch the record
$feedback[$row['record']] = $row;
}
mysqli_close($con);
} else {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
<div class="container">
<h2>Customer Feedback - await for reply</h2>
<table class="table table-hover">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
<th>Relpy</th>
</tr>
</thead>
<?php
foreach ($feedback as $id => $info) {
echo '<tbody>';
echo '<tr>';
echo '<td>' . $info['name'] . '</td>';
echo '<td>' . $info['email'] . '</td>';
echo '<td>' . $info['message'] . '</td>';
echo '<td><button class="btn btn-info">' . 'Reply' . '</button></td>';
echo '</tr>';
echo '</tbody>';
}
?>
</table>
</div>
</div>
</div>
<div class="container">
<div class="row">
<?php
require_once 'dbfunction.php';
$con = getDbConnect();
if (!mysqli_connect_errno($con)) {
$sqlQueryStr = "SELECT * FROM feedback WHERE status != 0";
$result = mysqli_query($con, $sqlQueryStr);
while ($row = mysqli_fetch_array($result)) { // fetch the record
$feedback[$row['record']] = $row;
}
mysqli_close($con);
} else {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
<div class="container">
<h2>Customer Feedback - Replied</h2>
<table class="table table-hover">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
<th>Relpy</th>
</tr>
</thead>
<?php
foreach ($feedback as $id => $info) {
echo '<tbody>';
echo '<tr>';
echo '<td>' . $info['name'] . '</td>';
echo '<td>' . $info['email'] . '</td>';
echo '<td>' . $info['message'] . '</td>';
echo '<td>' . 'Replied' . '</td>';
echo '</tr>';
echo '</tbody>';
}
?>
</table>
</div>
</div>
</div>
How can i change it?
Solve it by changing some of the codes in the second table
<?php
require_once 'dbfunction.php';
$con = getDbConnect();
if (!mysqli_connect_errno($con)) {
$sqlQueryStr = "SELECT * FROM feedback WHERE status != 0";
$result2 = mysqli_query($con, $sqlQueryStr);
while ($row2 = mysqli_fetch_array($result2)) { // fetch the record
$feedback2[$row2['record']] = $row2;
}
mysqli_close($con);
} else {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
<div class="container">
<h2>Customer Feedback - Replied</h2>
<table class="table table-hover">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
<th>Relpy</th>
</tr>
</thead>
<?php
foreach ($feedback2 as $id2 => $info2) {
echo '<tbody>';
echo '<tr>';
echo '<td>' . $info2['name'] . '</td>';
echo '<td>' . $info2['email'] . '</td>';
echo '<td>' . $info2['message'] . '</td>';
echo '<td>' . 'Replied' . '</td>';
echo '</tr>';
echo '</tbody>';
}
?>
</table>
</div>
Hello i am able to use a drop down list to show rows from a table but what i am wanting to is to populate the drop down list with the 2 tables i have in my database.
Example: i am currently listing members from the table members
bob
jake
chris
but what i am wanting to do is to list the tables
members
cars
<?php
// check for errors
ini_set('display_errors', 1);
//calls connection
require_once('connection.php');
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<h1> View Table(s) </h1>
<?php
// calls get results method to list members
$ResultSet = getResults("members");
echo "<table border='1' cellpadding='6'>";
echo "<tr> <th>Id</th> <th>First Name</th> <th>Second Name</th> <th>Age</th> <th>Email</th>";
foreach ($ResultSet as $row) {
echo "<tr>";
echo "<td>" . $row ['id'] . "</td>";
echo "<td>" . $row['first_name'] . "</td>";
echo "<td>" . $row['second_name'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "</tr>";
}
echo "<table>";
?>
<br/>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method='post'>
<p>Choose a member to view:</p>
<select name='members' id="ddmembers">
<?php
$results = getResults('members');
if ($results) {
foreach ($results as $row) {
echo '<option value="' . $row['id'] . '">' . $row['first_name'] . '</option>';
}
}
else
echo '<option value="0"0"> No Data</option>';
?>
</select>
<input type="submit" id="submit" value="Submit"/>
<br/>
<br/>
</form>
<?php
//Message for what has been selected
if (isset($_POST['members'])) {
echo 'You have selected Member ' . $_POST['members'];
}
?>
</body>
</html>
Try using information_schema database
See the following post for more information
Get table names using SELECT statement in MySQL
EDIT :
Without using information_schema, you can do something like
<?php
$dbname = "DATABASE_NAME";
$sql = "SHOW TABLES FROM $dbname";
$result = mysql_query($sql);
$tableNames= array();
while ($row = mysql_fetch_row($result)) {
$tableNames[] = $row[0];
}
echo '<select name="tables" id="tables">';
foreach ($tableNames as $name){
echo '<option value="' . $name . '">' . $name . '</option>';
}
echo '</select>';
?>
2 times action :
Save all names into array
Iterate this array and print options
Of course, if you prefer, you can do this with 1 time action
I'm having some display problems here.
I have a "backend.php" file where I ask for two inputs.
These inputs are processed by "Addproducts.php" file and this file redirects to backend.php.
Backend.php also shows the current records in the database.
Here's the code for backend.php
<html>
<head>
<title>Inventory - Backend</title>
</head>
<body>
<form action="addproducts.php" method="post">
<table>
<tr>
<td>Product Name : </td>
<td><input type="text" name="pname"/></td>
</tr>
<tr>
<td>Product Quantity : </td>
<td><input type="text" name="productq"/></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td><input type="submit" name="Add Product"/></td>
</tr>
</table>
</form>
<h2>Current Products</h2>
<?php
$db = mysql_connect('127.0.0.1', 'root', '') or die ('Unable to Connect.Check your connection parameters');
mysql_select_db('stock_inventory', $db) or die (mysql_error($db));
$query = 'SELECT * FROM PRODUCTS';
$result = mysql_query($query, $db) or die (mysql_error($db));
echo '<table>';
echo '<tr>';
echo '<th>Product ID </th>';
echo '<th>Producr Name </th>';
echo '<th>Product Stock </th>';
echo '</tr>';
while($row = mysql_fetch_assoc($result))
{
if(mysql_num_rows($result) > 0)
{
echo '<tr>';
echo '<td>' . $row['product_id'] . '</td>';
echo '<td>' . $row['product_name'] . '</td>';
echo '<td>' . $row['product_stock'] . '</td>';
echo '</tr>';
echo '<br/>';
echo '</table>';
}
else
{
echo "No products in the database";
}
}
?>
</body>
</html>
It displays something like this :-
Product ID Producr Name Product Stock
1 NewProduct 1
2HTC One5
3Samsung10
4Sony10
You see?
Only the first product is aligned, the rest are not.
How do I make them all align ?
Thanks.
The reason is you are closing your table tag within the loop, move it outside the loop like follows:
while($row = mysql_fetch_assoc($result))
{
if(mysql_num_rows($result) > 0)
{
echo '<tr>';
echo '<td>' . $row['product_id'] . '</td>';
echo '<td>' . $row['product_name'] . '</td>';
echo '<td>' . $row['product_stock'] . '</td>';
echo '</tr>';
}
else
{
echo "No products in the database";
}
}
echo '<br/>';
echo '</table>';
Update: A better fix (see Barmar's comment below):
if (empty(mysql_num_row($result))) {
echo "<tr><td colspan='3'>No products in the database</td></tr>";
} else {
while($row = mysql_fetch_assoc($result)) {
echo '<tr>';
echo '<td>' . $row['product_id'] . '</td>';
echo '<td>' . $row['product_name'] . '</td>';
echo '<td>' . $row['product_stock'] . '</td>';
echo '</tr>';
}
}
echo '</table>';
Also start looking into using mysqli(http://php.net/manual/en/book.mysqli.php) or PDO (http://php.net/manual/en/book.pdo.php), mysql_ is deprecated!
I would suggest removing the
<br/>
from within your table. I believe that putting markup like line breaks inside of table markup will break the layout.
I am trying to find a way to display a table from SQL in HTML in this way as I have shown below in the html part:
Html
<table>
<tr>
<td><a><img src="" width=135 height=100/></a></td>
<td><a><h2>Title</h2></a><p>DDMMYY</p><p>Description</p></td>
</tr>
</table>
My current script only outputs the result in one line, like this:
PHP
<?php
$stmt = $db->prepare('SELECT title, tag, desc FROM Tbl1;');
$stmt->execute();
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo "<table>";
foreach($res AS $val) {
echo "<tr>";
foreach($val AS $val1) {
echo "<td>$val1</td>";
}
echo "</tr>";
}
echo "</table>";
$db = null;
?>
$stmt = $db->prepare('SELECT title, tag, `desc` FROM Tbl1;');
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC));
echo '<table>';
for($results as $row) {
echo '<tr>';
echo '<td>' . $row['imagefield'] . '</td>';
echo '<td style="text-align: right">' . $row['description'] . '<br />' . $text . '</td>';
// etc...
echo '</tr>';
}
echo '</table>';
There is no reason why this code wouldn't show a valid table.