I have a database table with student room assignments. Each student has a specific hall, floor, and apartment. I need to display each student in a specific table so the results look like a floor layout. Below is an example. The student ID needs to be in the correct Apartment slot. There could be several ID's per apartment. Right now it just lists them down the page.
Apartment 102 Apartment 101
Apartment 104 Apartment 103
Apartment 106 Apartment 105
$query = "select res.ID_NUM as ID, res.APARTMENT
From Residents res
Where res.sess_cde = '$pulledsession'
and res.ROOM_ASSIGN_STS = 'A'
and res.BLDG_CDE = '$pulledhall'
and res.FLOOR = '$pulledfloor'";
$result = odbc_exec($connect, $query);
echo "<table style='padding:25;'>
<tr>
<th>Apartment</th>
<th>ID</th>
</tr>";
while(odbc_fetch_row($result)){
$ID = odbc_result($result,ID);
$APARTMENT = odbc_result($result,APARTMENT);
if ($APARTMENT == $pulledfloor.'01')
{
echo "<tr >";
echo "<td>" . $pulledfloor.'01' . "</td>";
echo "<td>" . $ID . "</td>";
echo "</tr>";
}
else if ($APARTMENT == $pulledfloor.'02')
{
echo "<tr>";
echo "<td>" . $pulledfloor.'02' . "</td>";
echo "<td>" . $ID . "</td>";
echo "</tr>";
}
else if ($APARTMENT == $pulledfloor.'03')
{
echo "<tr>";
echo "<td>" . $pulledfloor.'03' . "</td>";
echo "<td>" . $ID . "</td>";
echo "</tr>";
}
else if ($APARTMENT == $pulledfloor.'04')
{
echo "<tr>";
echo "<td>" . $pulledfloor.'04' . "</td>";
echo "<td>" . $ID . "</td>";
echo "</tr>";
}
else if ($APARTMENT == $pulledfloor.'05')
{
echo "<tr>";
echo "<td>" . $pulledfloor.'05' . "</td>";
echo "<td>" . $ID . "</td>";
echo "</tr>";
}
else if ($APARTMENT == $pulledfloor.'06')
{
echo "<tr>";
echo "<td>" . $pulledfloor.'06' . "</td>";
echo "<td>" . $ID . "</td>";
echo "</tr>";
}
}
echo "</table>";
You would need to retrieve the room number as well. After that, one way to do it would be to load the results into an associated array:
$rooms[$row['roomNumber']] = $resName; // Example
Once you have the array built, then you can echo it back out into a table, either manually (build the full table and echo each one with
echo "<tr><td>".$rooms['102']."</td><td>".$rooms['101']."</td></tr>";
or similar, or do it dynamically by incrementing two room numbers in a loop.
If you have multiple students in a room, then tack on more depth to the array:
$rooms[$row['roomNumber']][] = $resName;
Then use a loop in each cell to echo it back out.
Related
Thanks, friends, for the help and feedback. I know i am not good with PHP but still trying to learn and playing with it :D -- My table contains duplicate entries against evaid as Open Close or In Process --- with below code I get the last entered status from DB against each status by using query and if statement to show the data but i want to get the count of it as well. Anyone can help me out --- For example ---
$sql = "SELECT * FROM (SELECT * FROM disagreements ORDER BY addeddate DESC) disagreements GROUP BY evaid";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) { // Here with this query I got last entered status of each row against evaid – as 2 Open – 5 in Process and 10 Closed --- with below if statement – I can echo the rows with status but I want to have count of it that how many are open, in process or closed
if($row["status"]=='Open') { // I want to count this value as 2
echo "<tr>";
echo "<td>" . $row["evaid"]. "</td>";
echo "<td>" . $row["status"]. "</td>";
echo "</tr>";
}
}
} else {
echo "Nothing to Display";
}
mysqli_close($conn);
<?php
$count['open'] = 0;
$count['close'] = 0;
$count['process'] = 0;
while($row = mysqli_fetch_assoc($result)) {
if($row["status"]=='Open')
{
$count['open']++;
echo "<tr>";
echo "<td>" . $row["evaid"]. "</td>";
echo "<td>" . $row["status"]. "</td>";
echo "</tr>";
}
if($row["status"]=='Close')
{ // I want to count this value as 2
$count['close']++;
echo "<tr>";
echo "<td>" . $row["evaid"]. "</td>";
echo "<td>" . $row["status"]. "</td>";
echo "</tr>";
}
if($row["status"]=='Process')
{ // I want to count this value as 2
$count['process']++;
echo "<tr>";
echo "<td>" . $row["evaid"]. "</td>";
echo "<td>" . $row["status"]. "</td>";
echo "</tr>";
}
}
print_r($count);
?>
// Hii.. You can get a count from your SQL query itself, try this
$sql = "SELECT *, COUNT(ID) AS COUNT FROM (SELECT * FROM disagreements ORDER BY addeddate DESC) disagreements GROUP BY evaid";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
if($row["status"]=='Open')
{
echo "<tr>";
echo "<td>" . $row['count'] . "</td>"; // here you will get a count
echo "<td>" . $row["evaid"]. "</td>";
echo "<td>" . $row["status"]. "</td>";
echo "</tr>";
}
}
}
else
{
echo "Nothing to Display";
}
mysqli_close($conn);
<?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.
I want to display multiple records from single person in a table using PHP however the other records are outside the table and only one record is inside the table. It looks like this (view me).
Here is my code
$resultSet2 = $mysqli->query("SELECT class_subject, target_grade, current_grade , cl.classID FROM students AS stud INNER JOIN grade AS gr ON stud.studentID = gr.studentID INNER JOIN class cl ON gr.classID = cl.classID WHERE (cl.classID = '1' OR '2') and surname = '$search' ");
AND
while($row = $resultSet2->fetch_array()) {
echo "<tr>";
echo "<td>" . $row['class_subject'] . "</td>";
echo "<td>" . $row['target_grade'] . "</td>";
echo "<td>" . $row['current_grade'] . "</td>";
echo "</tr>";
echo "</table>";
How can i put them inside the table like the first one?
Move the closing table tag outside the while loop </table> also close your while loop.
echo "<table>";
while($row = $resultSet2->fetch_array()) {
echo "<tr>";
echo "<td>" . $row['class_subject'] . "</td>";
echo "<td>" . $row['target_grade'] . "</td>";
echo "<td>" . $row['current_grade'] . "</td>";
echo "</tr>";
}
echo "</table>";
So I am trying to do this..
$userID = $_SESSION['user_session'];
$stmt = $this->db->prepare("SELECT * FROM orrs");
$stmt->bindparam(":id", $userID);
$stmt->execute();
$count = $stmt->rowCount();
echo
"<div class='table-responsive'>
<table class='table' border='1'>
<tr class='head'>
<h3>Snapshot</h3>
<th>Se</th>
<th>#s</th>
<th>Ae</th>
<th>Prt</th>
<th>Pin</th>
</tr>";
while($userRows=$stmt->fetch(PDO::FETCH_ASSOC)) {
if($userRows['stage'] == '1')
{
echo "<tr>";
echo "<td>" . "Newn" . "</td>";
echo "<td>" . $count . "</td>";
echo "<td>" . $userRows['aow'] . "</td>";
echo "<td>" . $userRows['pit'] . "</td>";
echo "<td>" . $userRows['pgin'] . "</td>";
}
else if($userRows['stage'] == '2')
{
echo "<tr>";
echo "<td>" . "Pendinn" . "</td>";
echo "<td>" . $count . "</td>";
echo "<td>" . $userRows['gfh'] . "</td>";
echo "<td>" . $userRows['pt'] . "</td>";
echo "<td>" . $userRows[trin'] . "</td>";
}
}
Basically, If the value in the row STAGE = 1 I want it to count those rows and give me the number.. If the value of STAGE = 2 I want it to count those rows and give me the number.
Right now, It is just counting all of the rows.. So for both of the IF statment its count number is saying 2, When there is only 1 in each section..
I think you need to execute three different statements, one to get all the rows (for you to loop over and create your output) and one to get each of the counts
//The current one
$stmt = $this->db->prepare("SELECT * FROM orrs");
//The get the count for stage '1'
$stage_1_stmt = $this->db->prepare("SELECT * FROM orrs where STAGE = 1");
$stage_1_count = $stage_1_stmt->rowCount();
//The get the count for stage '2'
$stage_2_stmt = $this->db->prepare("SELECT * FROM orrs where STAGE = 2");
$stage_2_count = $stage_2_stmt->rowCount();
You can execute these others to get the counts which you should use in place of $count in your loop.
Your while loop then becomes
while($userRows=$stmt->fetch(PDO::FETCH_ASSOC)) {
if($userRows['stage'] == '1')
{
echo "<tr>";
echo "<td>" . "Newn" . "</td>";
echo "<td>" . $stage_1_count . "</td>";
echo "<td>" . $userRows['aow'] . "</td>";
echo "<td>" . $userRows['pit'] . "</td>";
echo "<td>" . $userRows['pgin'] . "</td>";
}
else if($userRows['stage'] == '2')
{
echo "<tr>";
echo "<td>" . "Pendinn" . "</td>";
echo "<td>" . $stage_2_count . "</td>";
echo "<td>" . $userRows['gfh'] . "</td>";
echo "<td>" . $userRows['pt'] . "</td>";
echo "<td>" . $userRows[trin'] . "</td>";
}
}
I have no idea how to explain myself which is why the question isn't even a question. I need to have a table dynamically created from data on mysql (which I've done) but I need to be able to have input in the cells under some of the headings (responsibility, organization, independent work...) When this data is submitted, I need it to be student specific. In other words, when I pull up Johnny Rotten's data, I need to be able to see all the comments under those headings that were submitted (yes this is for teaching). The number of students can vary which is why i need the whole thing to be dynamic. If this is not possible, please let me know. AND if you haven't figured it out already, I am brand new and self-taught!
Here's what I have...
<?php
include 'connect.php';
if ($db_found) {
$SQL = "SELECT * FROM studentlist WHERE teacher1='smith' OR teacher2 ='smith' OR
teacher3='smith' ORDER by homeroom";
$result = mysql_query($SQL);
echo "<table border='1'>
<tr>
<th>Student</th>
<th>Homeroom</th>
<th>Responsibility</th>
<th>Organization</th>
<th>Independent Work</th>
<th>Collaboration</th>
<th>Initiative</th>
<th>Self Regulation</th>
</tr>";
while ($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['student'] . "</td>";
echo "<td>" . $row['homeroom'] . "</td>";
echo "<td>" . "" . "</td>";
echo "<td>" . "" . "</td>";
echo "<td>" . "" . "</td>";
echo "<td>" . "" . "</td>";
echo "<td>" . "" . "</td>";
echo "<td>" . "" . "</td>";
echo "</tr>";
}
echo "</table>";
}
mysql_close($connect);
?>