add list selector to mysql query - php

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"

Related

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

Will a LIKE function give me better results when Match Against function does not give any results in Mysql

Currently, When I type 'M' in the search engine I do not get any results inspite of having a 'Math' value in my database. (I am using Match Against function of mysql on my website). So I decided to make a code that first runs a Match Against function and if no output is obtained, it again runs the search but this time using 'LIKE' function of my sql and if still no result is obtained it shows alert 'no values found'.
the gist of code is somewhat like this..
mysql(Match against function)
If (mysqli_num_rows > 0)
{output}
elseif (mysqli_num_rows < 1)
{ mysqli (LIKE function)
then show output}
else {show alert'no results'}
Will this code give the result 'Math' when I enter 'M' in the search engine.
The actual code.
<?php
if (isset($_POST['go']))
{ $search = $_POST['search'];
$college = $_POST['colleges'];
if (!empty($search))
{
if(isset($_POST['colleges']) )
{
$query1 = "Select filename, description, groupid, emailid, college, upload_date FROM images WHERE MATCH(description) AGAINST('$search') AND college = '$college' group by groupid order by MATCH(description) AGAINST('$search') DESC";
$query_run1 = mysqli_query($con, $query1) ;
}
else
{
$query1 = "Select filename, description, groupid, emailid, college,upload_date FROM images WHERE MATCH(description) AGAINST('$search') group by groupid order by MATCH(description) AGAINST('$search') DESC";
$query_run1 = mysqli_query($con, $query1) ;
$searchresults = mysqli_num_rows($query_run1);
?><p class = "totalresults"> <br />
<br /> &nbsp &nbsp <font size= "4" >Total Results :
<?php
echo $searchresults ;
?>
</font> </p>
<?php
if(mysqli_num_rows($query_run1)>0)
{
?>
<table class="searchenginelist" style="color: black;">
<tr>
<th> Uploader </th>
<th> Title </th>
<th> Date </th>
<th> Upvotes </th>
<th> Downvotes </th>
<th> College </th>
</tr>
<?php
while( $rows = mysqli_fetch_assoc($query_run1))
{
//$imagedisplay = $rows['file'];
$imagename = $rows['filename'];
// mysqli_real_escape_string($con,$imagedisplay);
$descrip = $rows['description'];
$groupid = $rows['groupid'];
//$groupid = $_SESSION['groupid'] ;
$uploader_emailid = $rows['emailid'];
$college = $rows['college'];
?>
<tr>
<td width="70px"> <?php
$query = " SELECT username from userinfo WHERE emailid = '$uploader_emailid' " ;
$query_run = mysqli_query($con,$query );
$raw = mysqli_fetch_assoc($query_run);
echo $raw['username']; ?>
</td>
<td width="450px"> <a href="imagespace.php?groupid=<?php echo $groupid ;?>">
<?php echo $descrip ; ?>
</a>
</td>
<td style="font-size:15px;" width="85px">
<?php echo $rows['upload_date'] ; ?>
</td>
<td align="center" style="color:green;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '1' ";
$query_run= mysqli_query($con, $query);
$upvote=mysqli_num_rows($query_run) ;
echo $upvote;
?>
</td>
<td align="center" style="color:red;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '0' ";
$query_run= mysqli_query($con, $query);
$downvote=mysqli_num_rows($query_run) ;
echo $downvote;
?>
</td>
<td style="font-size:13px;" width="130px">
<?php echo $college; ?>
</td>
</tr>
<?php
}
?>
</table>
<?php
}
elseif(mysqli_num_rows($query_run1) < 1)
{
$query12 = "Select * FROM images WHERE description LIKE '%".mysqli_real_escape_string($con,$search)."%' group by groupid order by groupid DESC";
$query_run12 = mysqli_query($con, $query12) ;
?> <table class="searchenginelist" style="color: black;">
<tr>
<th> Uploader </th>
<th> Title </th>
<th> Date </th>
<th> Upvotes </th>
<th> Downvotes </th>
<th> College </th>
</tr>
<?php
while( $rows = mysqli_fetch_assoc($query_run12))
{
//$imagedisplay = $rows['file'];
$imagename = $rows['filename'];
// mysqli_real_escape_string($con,$imagedisplay);
$descrip = $rows['description'];
$groupid = $rows['groupid'];
//$groupid = $_SESSION['groupid'] ;
$uploader_emailid = $rows['emailid'];
$college = $rows['college'];
?>
<tr>
<td width="70px"> <?php
$query = " SELECT username from userinfo WHERE emailid = '$uploader_emailid' " ;
$query_run = mysqli_query($con,$query );
$raw = mysqli_fetch_assoc($query_run);
echo $raw['username']; ?>
</td>
<td width="450px"> <a href="imagespace.php?groupid=<?php echo $groupid ;?>">
<?php echo $descrip ; ?>
</a>
</td>
<td style="font-size:15px;" width="85px">
<?php echo $rows['upload_date'] ; ?>
</td>
<td align="center" style="color:green;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '1' ";
$query_run= mysqli_query($con, $query);
$upvote=mysqli_num_rows($query_run) ;
echo $upvote;
?>
</td>
<td align="center" style="color:red;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '0' ";
$query_run= mysqli_query($con, $query);
$downvote=mysqli_num_rows($query_run) ;
echo $downvote;
?>
</td>
<td style="font-size:13px;" width="130px">
<?php echo $college; ?>
</td>
</tr>
}
?>
</table>
<?php
}
else
{
echo '<script type="text/javascript"> alert ("No results found") </script>' ;
}
}
else
{ echo '<script type="text/javascript"> alert ("No data entered") </script>';
}
}
}
You have some syntax issues in your code, your else statements were not lined up properly, so your page was not working. (For example, you had an } else { lined up with another } else {).
I went through all of the code and formatted it so I could see what the issue was, remember, Some sensible code indentation would be a good idea.
It helps us read the code and more importantly it will help you debug your code.
Take a quick look at a coding standard for your own benefit.
You may be asked to amend this code in a few weeks/months and you will thank me in the end.
<?php
if (isset($_POST['go'])) {
$search = $_POST['search'];
$college = $_POST['colleges'];
if (!empty($search)) {
if(isset($_POST['colleges']) ) {
$query1 = "Select filename, description, groupid, emailid, college, upload_date FROM images WHERE MATCH(description) AGAINST('$search') AND college = '$college' group by groupid order by MATCH(description) AGAINST('$search') DESC";
$query_run1 = mysqli_query($con, $query1);
} else {
$query1 = "Select filename, description, groupid, emailid, college,upload_date FROM images WHERE MATCH(description) AGAINST('$search') group by groupid order by MATCH(description) AGAINST('$search') DESC";
$query_run1 = mysqli_query($con, $query1);
$searchresults = mysqli_num_rows($query_run1);
?>
<p class = "totalresults">
<br />
<br />
&nbsp &nbsp <font size= "4" >Total Results: <?php echo $searchresults; ?></font>
</p>
<?php
if(mysqli_num_rows($query_run1)>0) {
?>
<table class="searchenginelist" style="color: black;">
<tr>
<th> Uploader </th>
<th> Title </th>
<th> Date </th>
<th> Upvotes </th>
<th> Downvotes </th>
<th> College </th>
</tr>
<?php
while( $rows = mysqli_fetch_assoc($query_run1)) {
//$imagedisplay = $rows['file'];
$imagename = $rows['filename'];
// mysqli_real_escape_string($con,$imagedisplay);
$descrip = $rows['description'];
$groupid = $rows['groupid'];
//$groupid = $_SESSION['groupid'] ;
$uploader_emailid = $rows['emailid'];
$college = $rows['college'];
?>
<tr>
<td width="70px">
<?php
$query = " SELECT username from userinfo WHERE emailid = '$uploader_emailid' " ;
$query_run = mysqli_query($con,$query );
$raw = mysqli_fetch_assoc($query_run);
echo $raw['username'];
?>
</td>
<td width="450px">
<a href="imagespace.php?groupid=<?php echo $groupid ;?>">
<?php
echo $descrip;
?>
</a>
</td>
<td style="font-size:15px;" width="85px">
<?php
echo $rows['upload_date'] ;
?>
</td>
<td align="center" style="color:green;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '1' ";
$query_run= mysqli_query($con, $query);
$upvote=mysqli_num_rows($query_run) ;
echo $upvote;
?>
</td>
<td align="center" style="color:red;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '0' ";
$query_run= mysqli_query($con, $query);
$downvote=mysqli_num_rows($query_run) ;
echo $downvote;
?>
</td>
<td style="font-size:13px;" width="130px">
<?php
echo $college;
?>
</td>
</tr>
<?php
} //end while loop
?>
</table>
<?php
} else if(mysqli_num_rows($query_run1) < 1) {
$query12 = "Select * FROM images WHERE description LIKE '%".mysqli_real_escape_string($con,$search)."%' group by groupid order by groupid DESC";
$query_run12 = mysqli_query($con, $query12) ;
?>
<table class="searchenginelist" style="color: black;">
<tr>
<th> Uploader </th>
<th> Title </th>
<th> Date </th>
<th> Upvotes </th>
<th> Downvotes </th>
<th> College </th>
</tr>
<?php
while( $rows = mysqli_fetch_assoc($query_run12)) {
//$imagedisplay = $rows['file'];
$imagename = $rows['filename'];
// mysqli_real_escape_string($con,$imagedisplay);
$descrip = $rows['description'];
$groupid = $rows['groupid'];
//$groupid = $_SESSION['groupid'] ;
$uploader_emailid = $rows['emailid'];
$college = $rows['college'];
?>
<tr>
<td width="70px">
<?php
$query = " SELECT username from userinfo WHERE emailid = '$uploader_emailid' " ;
$query_run = mysqli_query($con,$query );
$raw = mysqli_fetch_assoc($query_run);
echo $raw['username'];
?>
</td>
<td width="450px">
<a href="imagespace.php?groupid=<?php echo $groupid ;?>">
<?php
echo $descrip ;
?>
</a>
</td>
<td style="font-size:15px;" width="85px">
<?php
echo $rows['upload_date'] ;
?>
</td>
<td align="center" style="color:green;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '1' ";
$query_run= mysqli_query($con, $query);
$upvote=mysqli_num_rows($query_run) ;
echo $upvote;
?>
</td>
<td align="center" style="color:red;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '0' ";
$query_run= mysqli_query($con, $query);
$downvote=mysqli_num_rows($query_run) ;
echo $downvote;
?>
</td>
<td style="font-size:13px;" width="130px">
<?php
echo $college;
?>
</td>
</tr>
</table>
<?php
} //end while loop
} else { //num rows < 1
echo '<script type="text/javascript"> alert ("No results found") </script>' ;
}
}
} else { //no searches returned
echo '<script type="text/javascript"> alert ("No data entered") </script>';
}
}
I also suggest, for really complex, nested structures, to comment what each ending bracket goes to like I did in your code. I put comments so that I could see exactly what the } was ending.
WARNING: This code may be vulnerable to SQL Injection Attacks. You should Learn about Prepared Statements for MySQLi or PDO. I recommend PDO, which I wrote a function for to make it extremely easy, very clean, and way more secure than using non-parameterized queries.
If you could access the mysql.ini of your host, you could simply change the min word length for full text search.
[mysqld]
ft_min_word_len=N
Otherwise you should decide which search function you want to use. It's not very intuitive for users if one search is a full text search and the other is a simple LIKE.
One way around could also be to only use full text, if the user types more than 2 chars.
// ...
if (strlen($query) < 4) {
$where = "MATCH (...) AGAINST ...";
} else {
$where = "...LIKE ... ";
}
$sql .= $where;
Btw. MATCH AGAINST should normaly return way more results than like, so doing a LIKE search after MATCH AGAINST returns 0 rows, is kinda useless.

PHP MySQL vertical table

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>

Unorganized table

I need help with the table. They are not organized/neat and very confusing ->
The table has to look neat, understandable and not confusing like this -> (this is how the table should look like)
This is what I have achieved so far (See: OrderID 2) -> http://i.imgur.com/fj06EGB.png
Below is the code
<table>
<tr>
<th>Customer Name</th>
<th>Customer Contact</th>
<th>Customer Email</th>
<th>Order ID</th>
<th>Order Date</th>
<th>Menu Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Total Amount</th>
<th>Action</th>
</tr>
<?php
$result = $mysqli->query("SELECT * FROM `order`");
while($obj = mysqli_fetch_assoc($result)) {
$orderDate = $obj['OrderDate'];
$orderId = $obj['OrderID'];
$totalAmount = $obj['OrderTotal'];
$paymentStatus = $obj['PaymentStatus'];
$customerId = $obj['CustomerID'];
$res = $mysqli->query("SELECT CustomerName, CustomerContactNo, CustomerEmail FROM customer WHERE CustomerID=$customerId");
if($row = mysqli_fetch_assoc($res)) {
$customerName = $row['CustomerName'];
$customerContactNo = $row['CustomerContactNo'];
$email = $row['CustomerEmail'];
}
$result1 = $mysqli->query("SELECT * FROM ordermenu WHERE OrderID = $orderId");
while($obj1 = mysqli_fetch_assoc($result1)) {
$menuId = $obj1['MenuID'];
$menuQty = $obj1['menuQty'];
$result2 = $mysqli->query("SELECT * FROM menu WHERE MenuID = $menuId");
$obj2 = mysqli_fetch_assoc($result2);
$name = $obj2['MenuName'];
$price = $obj2['MenuPrice'];
?>
<tr>
<td><?php echo $customerName;?></td>
<td><?php echo $customerContactNo;?></td>
<td><?php echo $email;?></td>
<td><?php echo $orderId;?></td>
<td><?php echo $orderDate;?></td>
<td><?php echo $name;?></td>
<td>$<?php echo $price;?></td>
<td><?php echo $menuQty;?></td>
<td>$<?php echo $totalAmount;?></td>
<td>Update</td>
</tr>
<?php } ?>
<?php } ?>
</table>
Anyone help please.
First get Customers From db
Foreach customer get Orders by customerID joined with ordermenu (on ordermenu.OrderID = order.OrderID) and joined with menu (on ordermenu.MenuID = menu.MenuID)
<?php
$customerQuery = $mysqli->query("SELECT CustomerID, CustomerName, CustomerContactNo, CustomerEmail FROM customer;");
while($customer = mysqli_fetch_assoc($customerQuery)) {
$customerId = $customer['CustomerID'];
$orderQuery = $mysqli->query("SELECT * FROM `order` o LEFT JOIN ordermenu om ON o.OrderID = om.OrderID LEFT JOIN menu m ON m.MenuID = om.MenuID WHERE o.CustomerID = $customerId");
while($order = mysqli_fetch_assoc($orderQuery)) {
// do something with your customer and order record
// show in table for example
}
}

retrieve and add the data into database

I need to retrieve the relevant data from database, and add to the attendance table again. this code shows many error. actually i dunno how to use the array to add the data. can anyone help?? thankssssssssssss.
<?php
$sql = "select p_module_ID, student_ID,
p_attendance_Date, p_attendance_Time,
p_attendance_Status,p_attendance_reason
from attendance";
$result = mysqli_query($con, $sql);
if(!$result)
{
echo mysqli_error($con);
exit();
}
while($rows = mysqli_fetch_array($result))
{
$attendance_list[] = array('p_module_ID' => $rows['p_module_ID'],
'student_ID' => $rows['student_ID'],
'p_attendance_Date' => $rows['p_attendance_Date'],
'p_attendance_Time' => $rows['p_attendance_Time'],
'p_attendance_Status' => $rows['p_attendance_Status'],
'p_attendance_reason' => $rows['p_attendance_reason']);
}
?>
<html>
<body>
<form action="attendance.php" method="post" accept-charset='UTF-8'>
<table border="0" cellspacing="20" >
<tr>
<td>
<select name="p_module_ID">
<?php
$sql = "SELECT p_module_ID FROM schedule";
$result = $con->query($sql);
while($row = mysqli_fetch_assoc($result)) {
echo "<option>".$row['p_module_ID']."</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td>
<select name="p_attendance_Date" >
<?php
$sql = "SELECT p_StartDate FROM schedule";
$result = $con->query($sql);
while($row = mysqli_fetch_assoc($result)) {
echo "<option>".$row['p_StartDate']."</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td>
<select name="p_attendance_Time">
<?php
$sql = "SELECT p_Time FROM schedule";
$result = $con->query($sql);
while($row = mysqli_fetch_assoc($result)) {
echo "<option>".$row['p_Time']."</option>";
}
?>
</select>
</td>
</tr>
<table id="t01">
<tr>
<th> Student ID </th>
<th> Name </th>
<th> Attendance </th>
<th> Reason </th>
<?php foreach($attendance_list as $attend) : ?>
<tr>
<td>
<?php
$sql = "SELECT p.student_ID,CONCAT(s.student_fname, ' ', s.student_lname) AS fullname FROM Pals p JOIN student s ON p.student_ID = s.student_ID WHERE student_role = 'Student' GROUP BY student_ID";
$result = $con->query($sql);
while($row = mysqli_fetch_assoc($result)) {
echo $attend[$row['student_id']];
}
?>
</td>
<td>
<?php
$sql = "SELECT p.student_ID,CONCAT(s.student_fname, ' ', s.student_lname) AS Fullname FROM Pals p JOIN student s ON p.student_ID = s.student_ID WHERE student_role = 'Student' GROUP BY student_ID";
$result = $con->query($sql);
while($row = mysqli_fetch_assoc($result)) {
echo $attend['Fullname'];
}
?>
</td>
<td>
<?php echo $attend["p_attendance_Status"]; ?>
</td>
<td>
<?php echo $attend["p_attendance_reason"]; ?>
</td>
</tr>
</tr>
<?php endforeach; ?>
</table>
</table>
</form>
</body>
</html>
Use $row['student_Id'] instead of $row['student_id']. Same as for Fullname. You selecting AS fullname not AS Fullname. Listen to the case sensitiveness.

Categories