PHP MySQL vertical table - php

I have a horizontal table using PHP and MySQL now
How can I make a vertical table from this code?
<div class="content-loader">
<table cellspacing="0" width="100%" id="rank2" class="table table-striped table-hover table-responsive">
<thead>
<tr>
<th>Nick</th>
<th>Kredity</th>
<th>Body1</th>
<th>Body2</th>
<th>Cas</th>
<th>online</th>
</tr>
</thead>
<tbody>
<?php
require_once 'dbconfig.php';
$stmt = $db_con->prepare("SELECT ranks.steamId, ranks.points, ranks.lastDisplayName, ranks.lastUpdated, ranksrussia2.points AS points2, uconomy.balance
FROM ranks
INNER JOIN ranksrussia2 ON ranks.steamId = ranksrussia2.steamId
LEFT JOIN uconomy ON ranks.steamId = uconomy.steamId
WHERE ranks.steamId = ?");
$stmt->execute(array($steamprofile['steamid']));
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
echo "<td>". $row['lastDisplayName']."</td><td>". $row['balance'] ."</td><td>". $row['points'] ."</td><td>". $row['points2'] ."</td><td>". $row['points2'] ."</td>";
}
?>
</tbody>
</table>
</div>

When generating tables, fetch() works on a row by row basis, works very well for horizontally printed tables. But in your case its better to fetchAll() the data before printing it out:
<?php
function unite(string $prefix, string $suffix, array $array){
$str = '';
foreach($array as $value){
$str.= $prefix . $value . $suffix;
}
return $str;
}
if($stmt->execute(array($steamprofile['steamid']))){
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
} else {
die('query failed');
}
?>
<table>
<tbody>
<tr>
<th>Nick</th><?php echo unite('<td>', '</td>', array_column($rows, 'lastDisplayName')) ?>
</tr>
<tr>
<th>Kredity</th><?php echo unite('<td>', '</td>', array_column($rows, 'balance')) ?>
</tr>
</tbody>
</table>
This way you can grab columns and print them out in 1 go. If you're not expecting any more columns than 1, you can also simply do the following:
<?php
if($stmt->execute(array($steamprofile['steamid']))){
if(!is_array($row = $stmt->fetch(PDO::FETCH_ASSOC))){
die('no results');
}
} else {
die('query failed');
}
?>
<tr>
<th>Nick</th><td><?php echo $row['lastDisplayName'] ?></td>
</tr>

You could try this one
Just copy and paste the prepare and change the variables, same goes to execute
<div class="content-loader">
<?php
require_once 'dbconfig.php';
$stmt1 = $db_con->prepare("SELECT ranks.steamId, ranks.points, ranks.lastDisplayName, ranks.lastUpdated, ranksrussia2.points AS points2, uconomy.balance FROM ranks INNER JOIN ranksrussia2 ON ranks.steamId = ranksrussia2.steamId LEFT JOIN uconomy ON ranks.steamId = uconomy.steamId WHERE ranks.steamId = ?");
$stmt2 = $db_con->prepare("SELECT ranks.steamId, ranks.points, ranks.lastDisplayName, ranks.lastUpdated, ranksrussia2.points AS points2, uconomy.balance FROM ranks INNER JOIN ranksrussia2 ON ranks.steamId = ranksrussia2.steamId LEFT JOIN uconomy ON ranks.steamId = uconomy.steamId WHERE ranks.steamId = ?");
$stmt3 = $db_con->prepare("SELECT ranks.steamId, ranks.points, ranks.lastDisplayName, ranks.lastUpdated, ranksrussia2.points AS points2, uconomy.balance FROM ranks INNER JOIN ranksrussia2 ON ranks.steamId = ranksrussia2.steamId LEFT JOIN uconomy ON ranks.steamId = uconomy.steamId WHERE ranks.steamId = ?");
$stmt4 = $db_con->prepare("SELECT ranks.steamId, ranks.points, ranks.lastDisplayName, ranks.lastUpdated, ranksrussia2.points AS points2, uconomy.balance FROM ranks INNER JOIN ranksrussia2 ON ranks.steamId = ranksrussia2.steamId LEFT JOIN uconomy ON ranks.steamId = uconomy.steamId WHERE ranks.steamId = ?");
$stmt5 = $db_con->prepare("SELECT ranks.steamId, ranks.points, ranks.lastDisplayName, ranks.lastUpdated, ranksrussia2.points AS points2, uconomy.balance FROM ranks INNER JOIN ranksrussia2 ON ranks.steamId = ranksrussia2.steamId LEFT JOIN uconomy ON ranks.steamId = uconomy.steamId WHERE ranks.steamId = ?");
$stmt1->execute(array($steamprofile['steamid']));
$stmt2->execute(array($steamprofile['steamid']));
$stmt3->execute(array($steamprofile['steamid']));
$stmt4->execute(array($steamprofile['steamid']));
$stmt5->execute(array($steamprofile['steamid']));
?>
<table cellspacing="0" width="100%" id="rank2" class="table table-striped table-hover table-responsive">
<thead>
<tr>
<td>Nick</td>
<?php
while($row = $stmt1->fetch(PDO::FETCH_ASSOC))
{
echo "<td>". $row['lastDisplayName']."</td>";
}
?>
</tr>
<tr>
<td>Kredity</td>
<?php
while($row = $stmt2->fetch(PDO::FETCH_ASSOC))
{
echo "<td>". $row['balance'] ."</td>";
}
?>
</tr>
<tr>
<td>Body1</td>
<?php
while($row = $stmt3->fetch(PDO::FETCH_ASSOC))
{
echo "<td>". $row['points'] ."</td>";
}
?>
</tr>
<tr>
<td>Body2</td>
<?php
while($row = $stmt4->fetch(PDO::FETCH_ASSOC))
{
echo "<td>". $row['points2'] ."</td>";
}
?>
</tr>
<tr>
<td>Cas</td>
<?php
while($row = $stmt5->fetch(PDO::FETCH_ASSOC))
{
echo "<td>". $row['points2'] ."</td>";
}
?>
</tr>
<tr>
<td>Online</td>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>

Related

PHP MySQL join two tables query

I have two tables ie abstract table and author with one to many relation, for each iteration on while loop I want to display a html table of unique row of data from abstract table with corresponding rows from author table.
This what I did:
public function getAll() {
try {
$sql = " SELECT tbl_abstract.abstract_id, tbl_abstract.first_name,
tbl_abstract.last_name,tbl_abstract.content,
tbl_author.afirst_name, tbl_author.alast_name,
tbl_author.aaffilition
FROM tbl_abstract
INNER JOIN tbl_author ON tbl_abstract.abstract_id = tbl_author.abstract_id
GROUP BY tbl_abstract.abstract_id";
$stmt= $this->pdo->prepare($sql);
$stmt->execute();
$count = $stmt ->rowCount();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
?>
<table class="table" >
<tr>
<td align="center" >
//data from tbl_abstract
<?php echo $row["abstract_id"]; ?>. <?php print($row["abstract_title"]); ?>
<?php echo $row["first_name"].' '.$row["last_name"]; ?>,
//data from tbl_author
<?php echo $row["afirst_name"].' '.$row["alast_name"];?>
</td>
</tr>
<tr>
<td align="center" ">
//data from tbl_abstract
<?php print($row["content"]); ?>
</td>
</tr>
</table>
<?php
}
}catch(PDOException $e){
echo $e->getMessage();
return false;
}
}
There are three records from tbl_author associated with the abstract_id from tbl_abstract but i only get one record instead of 3 of them.Please help
Try this one :-
<?php
$sql = "SELECT *
FROM tbl_abstract
where abstract_id IN (SELECT distinct abstract_id
FROM tbl_abstract)";
$stmt= $this->pdo->prepare($sql);
$stmt->execute();
$count = $stmt ->rowCount();
?>
<table class="table" >
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
?>
<tr>
<td align="center" >
//data from tbl_abstract
<?php echo $row["abstract_id"]; ?> <?php print($row["abstract_title"]); ?>
<?php echo $row["first_name"].' '.$row["last_name"]; ?>,
<?php
$sql1 = "SELECT *
FROM tbl_author
WHERE abstract_id = '".$row["abstract_id"]."'" ;
$stmt1= $this->pdo->prepare($sql1);
$stmt1->execute();
while($row1 = $stmt1->fetch(PDO::FETCH_ASSOC)){
//data from tbl_author
echo $row1["afirst_name"].' '.$row1["alast_name"];
} ?>
</td>
<td align="center">
//data from tbl_abstract
<?php print($row["content"]); ?>
</td>
</tr>
<?php } ?>
</table>
Try removing the GROUP BY clause like this:
SELECT
tbl_abstract.abstract_id, tbl_abstract.first_name,
tbl_abstract.last_name,tbl_abstract.content, tbl_author.afirst_name,
tbl_author.alast_name, tbl_author.aaffilition
FROM
tbl_abstract
INNER JOIN
tbl_author ON tbl_abstract.abstract_id = tbl_author.abstract_id
Group by is grouping all the authors by the field abstract_id, which means, that it won't return all the authors, but only one for one abstract_id (based on the sorting field, which in this case is probably the primary key, because it is not explicitly defined)..

not able to get the data and display on web using php

$totDays = cal_days_in_month(CAL_GREGORIAN,$_REQUEST['month'],$_REQUEST['year']);
$attData = $commonObj->getAttendanceData($_REQUEST['month'],$_REQUEST['year']);
`
Student Name
<?php foreach($attData as $attk=>$attv){
$punchin = $commonObj->getTimeOfDate($attData[$attk]['punchin']);
$punchout = $commonObj->getTimeOfDate($attData[$attk]['punchout']);
$nam=$attv['name'];
?>
<tr>
<th class="danger">
<?php echo $nam;?>
</th>
<?php for($i=1;$i<=$totDays;$i++){?>
<?php if($commonObj->getDayOfDate($attData[$attk]['punch_time']) == $i){
echo "<td class='success' id='att_$i'>".$punchin.'-'.$punchout;?>
<table class="table table-responsive"style="display: none; position:relative;min-width:100px;max-width:200px; margin-top: -40px;" id="<?php echo "det_att_".$i;?>">
<tr>
<td>Time:</td>
<td><?php echo "p"?>
</td></tr>
<tr>
<td>UID:</td>
<td><?php echo $attData[$attk]['rfid_uid'];?></td>
</tr>
</table>
<?php
}else {echo "<td class='info'>#na";}?>
</td>
<?php }?>
</tr>
<?php }?>
</tr>
</table>`
this is the code for displaying on the web for viewing attendance ..
and here is the code for database part for getting attendance...
public function getAttendanceData($month,$year){
if(!is_numeric($month) || $month>12 || $month<1){
$this->setErrorMsg("Please Select valid month!");
header("location:viewAttendance.php");
exit;
}
$retData = array();
$fullDate = $year."-".$month;
//print_r($_SESSION);
if($_SESSION['user_type']==1){
$sql = "
SELECT t1.*
, t2.rfid_uid
, punch_time as punchin
, t2.name
FROM tbl_attendance as t1
right
join tbl_users as t2
on t1.rfid_uid = t2.rfid_uid
WHERE YEAR(punch_time) = $year
AND MONTH(punch_time) = $month
and t2.rfid_uid ='".$_SESSION['rfid_uid']."'
";
$retData = $this->getData($sql);
}else if($_SESSION['user_type'] == 2){
$sql = "SELECT t1.*,t2.rfid_uid,punch_time as punchin,t2.name FROM tbl_attendance as t1 right join tbl_users as t2 on t1.rfid_uid=t2.rfid_uid WHERE YEAR(punch_time) = $year AND MONTH(punch_time) = $month group by t2.rfid_uid";
$retData = $this->getData($sql);
}
return $retData;
}
web viewing imagedatabase table for attendanceuser
the attendance is recorded in the database but is not shown on the web..please help

Displaying data in tables depending on group

I have a question in relation to displaying PHP tables that should be straight forward but I cannot get my head around it at the moment so any help would be appreciated, basically what I want to do is display a team of players in a table, but display multiple tables of users with their team name display above it.
What I currently have : http://puu.sh/ilUJp/4a6ae5e47b.png
What I am looking to achieve : http://puu.sh/ilUJ8/7756033517.png
<div class="col-lg-6">
<h3>Team Name Goes Here </h3>
<?php
echo "<table class='table table-striped'>";
echo " <thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
";
while($row = mysqli_fetch_array($result)) {
$teamName = $row['teamName'];
$fName = $row['firstName'];
$surName = $row['surName'];
echo "
<tbody>
<tr>
<td>$teamName</td>
<td>$fName</td>
<td>$surName</td>
</tr>
</tbody>
";
}
echo "</table>";
?>
</div>
with my query :
$sql = "SELECT t.teamID,t.teamName,u.firstName,u.surName From users as u INNER JOIN team as t where u.teamID = t.teamID ";
I know the idea I need to do but cannot get it done, so any help would be appreciated.
Try this code
<?php $teemid=array();
while($row = mysqli_fetch_array($result)) {
if(!in_array($row['teamID'],$teemid)){
array_push($teemid,$row['teamID']);
if(!empty($teemid)){ ?>
</tbody>
</table>
</div>
<?php }
?>
<div class="col-lg-6">
<h3><?php echo $row['teamName']; ?></h3>
<table class='table table-striped'>
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<?php } ?>
<tr>
<td><?php echo $row['teamName']; ?></td>
<td><?php echo $row['firstName']; ?></td>
<td><?php echo $row['surName']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
SQL Query Change as below
$sql = "SELECT t.teamID,t.teamName,u.firstName,u.surName From users as u INNER JOIN team as t where u.teamID = t.teamID ORDER BY u.teamID";
You can do this logic
$teams = "get all teams sql query";
while ($row = mysqli_fetch_array($teams)) {
$teamid = $row['teamid'];
$teamname = $row['teamname'];
$teammemberquery = "select all member in the where team = $teamid sql query";
echo "<table>";
while ($r = mysqli_fetch_array($teammemberquery)) {
$teamName = $r['teamName'];
$fName = $r['firstName'];
$surName = $r['surName'];
echo "
<tbody>
<tr>
<td>$teamName</td>
<td>$fName</td>
<td>$surName</td>
</tr>
</tbody>
";
}
echo "</table>";
}
Try as below (Please replace table column name as yours and mysql to mysqli):
<?php
$link = mysql_connect('localhost', 'root', 'root');
$db_selected = mysql_select_db('test', $link);
$sql = "SELECT t.team_id,t.team,u.fname,u.lname,u.email From users as u INNER JOIN team as t where u.team_id = t.team_id order by t.team_id ";
$result = mysql_query($sql);
?>
<html><head><title>team</title></head><body><div class="col-lg-6">
<?php
echo "<table>";
$teamName = "";
$i=0;
while($row = mysql_fetch_array($result))
{
if($teamName == "" || $teamName != $row['team'])
{
if($i!=0)
echo "</table>";
echo "<tr><td colspan='3'><h3>".$row['team']."</h3></td></tr>";
$teamName = $row['team'];
$i=0;
}
$fName = $row['fname'];
$surName = $row['lname'];
$email = $row['email'];
if($i==0)
{
echo "<table class='table table-striped'><tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>";
}
echo "<tr>
<td>$fName</td>
<td>$surName</td>
<td>$email</td>
</tr>";
$i++;
}
echo "</table>";
?>
</div></body></html>

how to fetch colums data from two table using mysql with php using where

I have two Table ownership_profile and socity_unit.
Query For table1: select * from ownership_profile where SID='$id'
Query For Table2: select * from socity_unit where socity_id='$sid'
I have to join with one query, but i don't have idea how to do it.
This is my Php code but gives error:
<!-----------------Table For User Names-------------------------------------->
<table border="1" align="center">
<tr>
<th>Unit No</th>
<th>Member Name</th>
<th>Wing</th>
<th>Unit</th>
</tr>
<?php
if(isset($_GET['submit']))
{
$sql = "select * from ownership_profile o inner join society_unit s on o.sid = s.society_id where o.sid = '$sid' ";
$result = mysql_query($sql);
$i=1;
while($row=mysql_fetch_array($result)){
?>
<?php
$name = $row['NAME'];
$unitid = $row['UNIT_ID'];
$sid = $row['SID'];
$wings = $row['wings'];
$unit_no = $row['unit_no'];
{
?>
<!--User Submit Result-->
<tr>
<td><?php echo $unitid; ?></td>
<td><?php echo $name; ?></td>
<td><?php echo $wings; ?></td>
<td><?php echo $unit_no; ?></td>
</tr>
<?php }?>
<?php
//echo "<br>";
$i++;
}
}
?>
inner join is what you want.
select *
from ownership_profile o
inner join society_unit s
on o.sid = s.society_id
where o.sid = '$sid'
This assumes that 'sid' and 'society_id' are the relationship identifiers.
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name;
select * from ownership_profile o inner join socity_unit s on o.SID = s.socity_id where o.SID = '$SOCIETY_ID'
This query working fine but...... its generate dublicate entry from table

add list selector to mysql query

I have a php scrip that runs a MySQL query and returns some data. What I am wanting to do is control the returned results with a dropdown.
The dropdown would need to have the following info inside it for the student to select from.
SELECT courseid FROM course_students WHERE userid = $user
Below is the current code that I have..... I am trying to figure out how to incorporate the above with it:
<?php
defined('_JEXEC') or die;
$db = JFactory::getDBO();
$user = & JFactory::getUser();
$userid = $user->get('id');
$course = "SELECT
c.coursename
FROM
courses AS `c`
LEFT OUTER JOIN course_students AS s
ON s.courseid = c.id
WHERE s.userid = '1285'";
$courses= mysql_fetch_array($course);
?>
Please Select Course for Results:
<select>
<?php foreach($courses as $c){
echo "<option value='$c'>".$c."</option>";
} ?>
</select>
<?php
$sql =
"SELECT
as.name,
DATE_FORMAT(s.starttime, '%b %d, %Y %H:%i') AS TIME,
s.totalhours AS hrs
FROM
clinicalsites AS `as`
LEFT OUTER JOIN clinical_dashboard AS s
ON s.clinicalsite = as.id
WHERE s.userid = $userid
AND starttime > NOW()
AND courseid = $c";
$db->setQuery($sql);
$rows = $db->loadObjectList();
?>
<style>
table, td, th
{
border-bottom:1px solid black;
text-align:center;
}
th {
background-color:#000000;
color: #FFF;
}
.upcoming {
font-weight: bold;
}
.complete {
font-weight: bold;
color: #060;
}
.pending {
font-weight: bold;
color: #F00;
}
.approved {
color: #C60;
}
</style>
<table width="396">
<tr>
<th width="162">Type of Hours</th>
<th width="70">Pending</th>
<th width="74">Approved</th>
<th width="70">Complete</th>
</tr>
</table>
<table>
<tr>
<td width="162">Total Hospital Hours</td>
<td class="pending" width="70"><?php
$q2 = "SELECT
SUM(totalhours) AS sum2
FROM
clinical_dashboard
WHERE userid = $userid
AND clinicaltype = 'Hospital'
AND instructorapproval = '1'
AND courseid = $c";
$result2 = mysql_query($q2);
$row2 = mysql_fetch_assoc($result2);
echo $row2['sum2']
?></td>
<td class="approved" width="74">
<?php
$q3 = "SELECT
SUM(totalhours) AS sum3
FROM
clinical_dashboard
WHERE userid = $userid
AND clinicaltype = 'Hospital'
AND instructorapproval = '2'
AND courseid = $c";
$result = mysql_query($q);
$row3 = mysql_fetch_assoc($result);
echo $row3['sum2']
?>
</td>
<td class="complete" width="70">
<?php
$q4 = "SELECT
SUM(totalhours) AS sum4
FROM
clinical_dashboard
WHERE userid = $userid
AND clinicaltype = 'Hospital'
AND instructorapproval = '2'
AND STATUS = '2'
AND courseid = $c";
$result = mysql_query($q4);
$row4 = mysql_fetch_assoc($result);
echo $row4['sum4']
?>
</td>
</tr>
<tr>
<td>Total Pre-Hosptial Hours</td>
<td class="pending" ><?php
$q = "SELECT
SUM(totalhours) AS sum3
FROM
clinical_dashboard
WHERE userid = $userid
AND clinicaltype = 'Pre-Hospital'
AND instructorapproval = '1'
AND courseid = $c";
;
$result = mysql_query($q);
$row1 = mysql_fetch_assoc($result);
echo $row1['sum3']
?></td>
<td class="approved">
<?php
$q = "SELECT
SUM(totalhours) AS sum3
FROM
clinical_dashboard
WHERE userid = $userid
AND clinicaltype = 'Pre-Hospital'
AND instructorapproval = '2'
AND courseid = $c";
$result = mysql_query($q);
$row1 = mysql_fetch_assoc($result);
echo $row1['sum3']
?></td>
<td class="complete"><?php
$q = "SELECT
SUM(totalhours) AS sum3
FROM
clinical_dashboard
WHERE userid = $userid
AND clinicaltype = 'Pre-Hospital'
AND instructorapproval = '2'
AND STATUS = '2'
AND courseid = $c";
$result = mysql_query($q);
$row1 = mysql_fetch_assoc($result);
echo $row1['sum3']
?></td>
</tr>
<?PHP foreach ($rows as $row): ?>
<?php endforeach ?>
</table>
<p class="upcoming">Upcoming Clinical Schedule</p>
<table width="393" border="1">
<tr>
<th width="166">Clinical Site</th>
<th width="119">Date</th>
<th width="86">Total Hours</th>
</tr>
</table>
<table>
<?PHP foreach ($rows as $row): ?>
<tr>
<td width="170"><?php echo $row->name ?></td>
<td width="124"><?php echo $row->time ?></td>
<td width="84"><?php echo $row->hrs ?></td>
</tr>
<?php endforeach ?>
</table>
<p class="upcoming"></p>
Below is just the drop down queries with the select.
$course = "SELECT
c.coursename
FROM
courses AS `c`
LEFT OUTER JOIN course_students AS s
ON s.courseid = c.id
WHERE s.userid = '1285'";
$courses= mysql_fetch_array($course);
?>
Please Select Course for Results:
<select>
<?php foreach($courses as $c){
echo "<option value='$c'>".$c."</option>";
} ?>
</select>
I believe you need something like this
<select>
<? foreach($courses as $c){
echo "<option value=".$c['table_id'].">".$c['coursename']."</option>";
} ?>
</select>
Good Luck !
Also select the table id in the query, in order to use it as the value for "option"

Categories