I want to sort a table into the right section. I have a database with users and they all have a rank assigned to each. In each section there should be a table displaying all the users in that rank. Example:
Manager
ID | rank | username | email | password
---------------------------------------------------
70 | Manager | MyUser | email#mail.com | pswrd
---------------------------------------------------
70 | Manager | MyUser | email#mail.com | pswrd
Admin
ID | rank | username | email | password
---------------------------------------------------
9 | Admin | userName | email#email.com | pswrd22
I'd like to make it using a while loop. What I have so far:
<html>
<head>
</head>
<?php
$tUsers_Select = "SELECT * FROM users LEFT JOIN ranks ON ranks.rankName = users.rank";
$tRanks_Select = "SELECT * FROM ranks";
$tUsers_Select_Query = mysqli_query($dbConnect, $tUsers_Select);
$tRanks_Select_Query = mysqli_query($dbConnect, $tRanks_Select);
?>
<body>
<?php
while ($users_item = mysqli_fetch_array($tUsers_Select_Query, MYSQL_ASSOC)) {
$ID = $users_item['ID'];
$rank = $users_item['rank'];
$username = $users_item['username'];
$email = $users_item['email'];
$password = $users_item['password'];
}
while ($ranks_item = mysqli_fetch_array($tRanks_Select_Query, MYSQL_ASSOC)) {
$rankName = $ranks_item['rankName'];
$value = $ranks_item['value'];
echo '<h2>'.$rankName.'</h2>';
}
?>
<table border="1">
<tr>
<td>ID</td>
<td>rank</td>
<td>username</td>
<td>email</td>
<td>password</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
</body>
If you need any more details, please comment.
This would be better done with prepared statements. But here you go
<?php
$tRanks_Select = "SELECT `rankName` FROM `ranks`";
$tRanks_Select_Query = mysqli_query($dbConnect, $tRanks_Select);
while ($ranks_item = mysqli_fetch_array($tRanks_Select_Query, MYSQL_ASSOC)) {
$rankName = $ranks_item['rankName'];
echo '<h2>'. $rankName .'</h2>';
while ($users_item = mysqli_fetch_array($tUsers_Select_Query, MYSQL_ASSOC)) {
$tUsers_Select = "SELECT `ID`, `rank`, `username`, `email`, `password` FROM `users` WHERE `rank` = '{$rankName}'";
$tUsers_Select_Query = mysqli_query($dbConnect, $tUsers_Select);
$ID = $users_item['ID'];
$rank = $users_item['rank'];
$username = $users_item['username'];
$email = $users_item['email'];
$password = $users_item['password'];
echo <<<EOD
<table border="1">
<tr>
<td>ID</td>
<td>rank</td>
<td>username</td>
<td>email</td>
<td>password</td>
</tr>
<tr>
<td>$ID</td>
<td>$rank</td>
<td>$username</td>
<td>$email</td>
<td>$password</td>
</tr>
</table>
EOD;
}
}
?>
Related
I am trying to group my data with country and year together and display it in a table but it is currently only grouped by year and not country.
As below -
USA 2020
| Reise | Land | Reisedatnum | Dauer | Reise |
| ------- | ------ | ------------- | ------- | ------- |
|12324 | Ak | 23-10-22 | AF |intensive|
USA 2020
|Reise |Land |Reisedatnum |Dauer |Reise|
|-|-|-|-|-|
|12323 |AG |3-10-22 |AF| intensive|
But it should be shown in one table only as Country and year are the same, currently is showing in a different table. I also grouped by keyregion
In database
keyregion = countryname
Year = $count=$row->jahr;
MySQL Query
function starttable($year,$country)
{
;
?>
<table class="termine" >
<caption class="date"><b><?php echo $country;?> <?php echo $year; ?> </b></caption>
<thead>
<tr>
<th scope="col" class="reisenr">Reise Nr.</th>
<th scope="col" class="land">Land</th>
<th scope="col" class="datum">Reisedatum</th>
<th scope="col" class="dauer">Dauer</th>
<th scope="col" class="reise">Reise</th>
<th scope="col" class="preis">Reisepreis</th>
</tr>
</thead>
<tbody>
<?
}
function closetable()
{
echo "
</tbody>
</table>
";
}
function ausgabe_einfach($zeile)
{
global $status;
$anfang = htmlentities($zeile->beginn_f,ENT_COMPAT,'UTF-8',0);
$ende = htmlentities($zeile->ende_f ,ENT_COMPAT,'UTF-8',0);
$commen = ($zeile->comment_de) ? "<div class=\"comment\">". nl2br(htmlentities($zeile->comment_de,ENT_COMPAT,'UTF-8',0)) ."</div>" : "";
?>
<tr>
<td><?php echo $zeile->reisenr ?></td>
<td><?php echo $zeile->keycountry .''.'<br>' . $zeile->Shortlink . ''?></td>
<td class="commdate"><?php echo $anfang ?> – <?php echo $ende ?></td>
<td><?php echo $zeile->tage+1 ?> T</td>
<td><?php echo $zeile->tourname ?></td>
<td><?php echo ($zeile->price) ? $zeile->price." Euro" : "-" ?> </td>
</tr>
<?
}
// Datenbankverbindung
$connection = #mysqli_connect("localhost","username","password","DB");
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
if ($connection)
{
mysqli_set_charset($connection, "UTF8");
mysqli_query($connection, "SET lc_time_names = 'de_DE'");
$keywords = "Afrika";
$zeig = 1;
if ($keysuffix == "")
$where = "WHERE keyregion LIKE \"".$keywords."\" AND zeig='$zeig'" ;
else
$where = "WHERE ( keyregion LIKE \"".$keywords."\" OR keycountry LIKE \"".$keywords.$keysuffix."\" )";
$abfrage = "SELECT reisenr,beginn,ende,airline,status,price,keywords,keyregion,Shortlink,keycountry,tourname, YEAR(beginn) AS jahr, MONTH(beginn) AS month, DATE_FORMAT(beginn,\"%a, %e. %b\") AS beginn_f,DATE_FORMAT(ende,\"%a, %e. %b %Y\") AS ende_f,comment_de, DATEDIFF(ende,beginn) as tage FROM $tabelle $where ORDER BY jahr, keyregion";
$ergebnis = mysqli_query($connection, $abfrage);
$opentab = false; // Tabelle offen
$p_month=0;
// Aktueller Jahresdurchlauf
while($row = mysqli_fetch_object($ergebnis))
{
if ($row->month != $p_month)
{
$p_month=$row->month;
$datee= htmlentities($row->beginn,ENT_COMPAT,'UTF-8',0);
$count=$row->jahr;
$country= $row->keyregion;
$bigin =htmlentities($row->beginn,ENT_COMPAT,'UTF-8',0);
if ($opentab)
closetable();
starttable($count,$country);
$opentab = true;
}
ausgabe_einfach($row);
}
if ($opentab)
closetable();
}
else
{ ?>
Daten können nicht geladen werden.
<?
}
if the date is same with database date just display one time only.Example:database date 16/2 then all 16/2 must display in a box, but other date also need to display. who help me to solve this problem.
<h1>Payment Record</h1>
<?php
$user_check=$_SESSION['login_user'];
$query = "SELECT * FROM `confirm_order` where customer = '$user_check'";
$result=mysqli_query($mysqli,$query);
while($row=mysqli_fetch_array($result))
{
?>
<table style="width:100%;border:1px solid black;margin-bottom:20px;">
<tr>
<td>Product Name: <?php echo $row['product_name'];?></td>
</tr>
<tr>
<td>Quantity: <?php echo $row['quantity']?></td>
</tr>
<tr>
<td>Receiver Name: <?php echo $row['receiver_name']?></td>
</tr>
<tr>
<td>Receiver Address: <?php echo $row['receiver_address']?></td>
</tr>
<tr>
<td>Receiver Contact: <?php echo $row['receiver_contact']?></td>
</tr>
<tr>
<td style="float:right">Date: <?php echo $row['date']?></td>
</tr>
</table>
<?php
}
?>
Example
id | product | date |
---|---------|------|
1 | book | 16/2 |
2 | pencil | 16/2 |
3 | shoe | 18/2 |
Result
-------
book |
pencil |
-------
shoe |
-------
Here is the complete solution:
$user_check=$_SESSION['login_user'];
$query = "SELECT * FROM `confirm_order` where customer = '$user_check'";
$result=mysqli_query($mysqli,$query);
$current_date=NULL;
$previous_date=NULL;
$table_open="<table border='1'>";
$table_close="</table><hr>";
$output=NULL;
while($row=mysqli_fetch_array($result))
{
$current_date=$row['date'];
if ($current_date==$previous_date) {
// SAME DATE
$output.= "<tr><td>SAME DATE</td><td>".$current_date."</td></tr>";
} else {
// NEW DATE
if ($previous_date<>NULL) {
$output.= $table_close;
}
$output.= $table_open."<tr><td>NEW DATE</td><td>".$current_date."</td></tr>";
}
$previous_date=$row['date'];
}
echo $output;
I'm facing an odd situation with something extremely simple.
The information is not being populated from the DB to the HTML table. The DB is a MySQL and I'm using PHP to connect and get the information.
The connection is answer as "Success!" so I can not see a reason for this issue.
MY CODE:
connector1.php:
<?php
$host = "localhost";
$user = "root";
$pass = "xxxxxxxxxxx";
$database = "PERFBASE";
$mysqli = new mysqli($host, $user, $pass, $database);
if($mysqli->connect_errno)
echo "Connection to DB Failed: (".$mysqli->connect_errno.") ".$mysqli->connect_error;
else echo "Success!";
-------------------------------------------------------------------------
index.php
<?php
include("connector1.php");
$consulta = sprintf("SELECT * FROM PERFTRIG");
$con = $mysqli -> query($consulta) or die($mysqli -> error);
$SYSNAME = "SYSNAME";
?>
<html>
<body>
<table style="width:100%">
<tr>
<th>System</th>
<th>Client</th>
<th>Date</th>
</tr>
<tr>
<?php while($dado = $con->fetch_array())?>
<td><?php echo $dado[$SYSNAME]; ?></td>
<td>Ozzy</td>
<td>17/09/22</td>
</tr>
</table>
</body>
</html>
This is a query directly in the MYSQL:
MariaDB [PERFBASE]> describe PERFTRIG
-> ;
+--------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+----------------+
| SYSID | int(10) | NO | PRI | NULL | auto_increment |
| DATETIME | date | YES | | NULL | |
| SYSNAME | varchar(10) | YES | | NULL | |
| ACCOUNTL | varchar(10) | YES | | NULL | |
Do you have any idea about what is happening?
Replace this part:
<?php while($dado = $con->fetch_array())?>
<td><?php echo $dado[$SYSNAME]; ?></td>
with that:
<?php while($dado = $con->fetch_array()) { ?> // loop start here
<td><?php echo $dado[$SYSNAME]; ?></td>
<?php } ?>
Its hard logic for php and you need to be correct with sintax.
And I think that you don't need next one (whats the point of this?):
$consulta = sprintf("SELECT * FROM PERFTRIG");
Just use in you code:
$consulta = "SELECT * FROM PERFTRIG";
Actually you doing mistake with while loop you missing curly braces
Try this way.
<tr>
<?php while($dado = $con->fetch_array()): ?> // dont forget colon
<td><?php echo $dado[$SYSNAME]; ?></td>
<td>Ozzy</td>
<td>17/09/22</td>
<?php endwhile; ?>
</tr>
William Perron, ficuscr, RamaKrishna - Thank you very much dudes!!!
Final Answer after fixing according to your clarification:
<body>
<table style="width:100%">
<tr>
<th>System</th>
<th>Client</th>
<th>Date</th>
</tr>
<?php while($dado = $conn->fetch_array()): ?>
<tr>
<td><?php echo $dado[$SYSNAME]; ?></td>
<td>Ozzy</td>
<td>17/09/22</td>
</tr>
<?php endwhile; ?>
</table>
</body>
Its working just fine now!
i am fetching data from db in datatable , but my while loop is only fetching first row results . i can guess that i am wrong somewhere but not exactly know where . find below the details
Tables :
user :
user_id | user_role_id | user_name | fname | lname | profile
:-----: | :----------: | :-------: | :---: | :---: | :-----:
1 | 2 | schin | sam | chin | t1.png
2 | 2 | mlouis | mark | louis| t2.png
teachers :
t_id | classes | subjects | pri_classes | primary_subjects | email
:-----: | :----------: | :-------: | :---------: | :--------------: | :-----:
1 | eight,nine | math,phy | nine | maths | 1#gmail.com
2 | two | science | two | science | 2#gmail.com
Php Query :
<?php
$sql3 = "select * from user where user_role_id = 2";
$result3 = $dbh->query($sql3);
$row3 = mysqli_fetch_assoc($result3);
$user_id = $row3['user_id'];
$sql4 = "select * from teachers where t_id = '$user_id'";
$result4 = $dbh->query($sql4);
?>
Table head :
<table id="datatable-table" class="table table-striped table-hover">
<thead>
<tr>
<th>S.no</th>
<th class="no-sort hidden-sm-down">Image</th>
<th >Name</th>
<th >User Name</th>
<th >Classes Handled</th>
<th >Subjects Handled</th>
<th >Primary Class</th>
<th >Primary Subjects</th>
<th >Email</th>
</tr>
</thead>
<tbody>
While loop :
<?php
while(($row4 = mysqli_fetch_assoc($result4))&&($row3)){
?>
<tr align="center">
<td>
<?php
$i = 1;
echo $i;
$i++;
?>
</td>
<td>
<span >
<img src="../img/<?php echo $row3['profile'];?>" style="width:40px; height:40px;">
</span>
</td>
<td>
<span class="fw-semi-bold">
<?php echo $row3['fname'].' '.$row3['lname']; ?>
</span>
</td>
<td>
<span class="fw-semi-bold">
<?php echo $row3['user_name']; ?>
</span>
</td>
<td><span class="fw-semi-bold">
<?php
$classes=explode(',', $row4['classes']);
$prefix = '';
foreach($classes as $cout)
{
echo $prefix . '' . wordsToNumber($cout);
$prefix = ', ';
}
?>
</span>
</td>
<td><span class="fw-semi-bold">
<?php
$subjects=explode(',', $row4['subjects']);
$prefix = '';
foreach($subjects as $sout) {
echo $prefix . '' . str_replace('\' ', '\'', ucwords(str_replace('\'', '\' ', strtolower($sout))));
$prefix = ', ';
}
?>
</span>
</td>
<td>
<span class="fw-semi-bold">
<?php
echo wordsToNumber($row4['pri_classes']);
?>
</span></td>
<td><span class="fw-semi-bold">
<?php
echo str_replace('\' ', '\'', ucwords(str_replace('\'', '\' ', strtolower($row4['primary_subjects']))));
?>
</span></td>
<td><span class="fw-semi-bold"><?php echo wordwrap($row4['email'],10, "<br>\n"); ?></span></td>
</tr>
<?php
}
?>
</tbody>
</table>
For wordsToNumber i use this function
Your first query is only retrieving a single user. You need nested loops.
while ($row3 = mysqli_fetch_assoc($result3)) {
$user_id = $row3['user_id'];
$result4 = $dbh->query("select * from teachers where t_id = '$user_id'");
while($row4 = mysqli_fetch_assoc($result4)){
// display etc
}
}
You may want to consider some joins in MySQL to avoid so many queries http://dev.mysql.com/doc/refman/5.7/en/join.html
How can I read the latest number from the following code
<table width="100" border="1" >
<tr align="center" bgcolor="#999999" >
<td >NO</td>
<td >Name</td>
<td >PBSID</td>
</tr>
$query ="select * from stock ";
$hasil = mysql_query($query);
$no=0;
while ($row = mysql_fetch_array($hasil))
{
$no++;
if($x != $row[pbsid] )
{
$no=1;
echo "<tr bgcolor=#CCCCCC>
<td colspan=4 ><b> GROUP $row[pbsid]</b></td>
</tr>";
}
$mo=count($no);
echo "<tr><td>$no </td><td> $row[bnama]</td><td>$row[pbsid] </td></tr>";
$x = $row["pbsid"];
}
The result:
===========
NO | NAME | GROUP
---------------------------------------
===========
GROUP 1
---------------------------------------
===========
1 | A | 1
2 | B | 1
3 | C | 1
4 | D | 1
---------------------------------------
===========
GROUP 2
---------------------------------------
===========
1 | A | 2
2 | B | 2
3 | C | 2
I want to show the latest GROUP number, Group 1=4 and Group 2=3, can anyone help me with this?
Try this query:
SELECT s.*, c.count
FROM stock AS s
LEFT JOIN ( SELECT pbsid, COUNT(1) AS `count` FROM stock GROUP BY pbsid ) AS c
ON s.pbsid = c.pbsid
Now you will have the count for each group, which is what it seems you really want.
If you'd prefer to do all the work in PHP for some reason, you could do it like this:
<table width="100" border="1" >
<tr align="center" bgcolor="#999999" >
<td >NO</td>
<td >Name</td>
<td >PBSID</td>
</tr>
$query ="select * from stock ";
$hasil = mysql_query($query);
$no=0;
$counts = array(); // make an array of counts for each group, keyed by pbsid
$rows = array(); // put all the data from SQL into an array
while ($row = mysql_fetch_array($hasil))
{
$rows[] = $row;
if (!isset($counts[$row[pbsid]]))
{
$counts[$row[pbsid]] = 0; // initialize it to 0
}
$counts[$row[pbsid]]++; // increment for each occurrence of a pbsid
}
foreach ($rows as $row)
{
$no++;
if($x != $row[pbsid] )
{
$no=1;
echo "<tr bgcolor=#CCCCCC>
<td colspan=4 ><b> GROUP $row[pbsid]</b> Count = " . $counts[$row[pbsid]] . " </td>
</tr>";
}
$mo=count($no);
echo "<tr><td>$no </td><td> $row[bnama]</td><td>$row[pbsid] </td></tr>";
$x = $row["pbsid"];
}
Although, I think it's much simpler to just have this information calculated in your query.
This would work
$query ="select * from stock ";
$hasil = mysql_query($query);
$no=0;
$latest = array();
while ($row = mysql_fetch_array($hasil))
{
$no++;
if($x != $row[pbsid] ){
$no=1;
echo "<tr bgcolor=#CCCCCC>
<td colspan=4 ><b> GROUP $row[pbsid]</b></td>
</tr>";
}
$mo=count($no);
echo "<tr><td>$no </td><td> $row[bnama]</td><td>$row[pbsid] </td></tr>";
$x = $row["pbsid"];
$key = 'Group'.$row['pbsid'];
$latest[$key] = $no;
}
print_r($latest);
<table width="100" border="1" >
<tr align="center" bgcolor="#999999" >
<td >NO</td>
<td >Name</td>
<td >PBSID</td>
</tr>
$last_no = array();
$query ="select * from stock ";
$hasil = mysql_query($query);
$no=0;
while ($row = mysql_fetch_array($hasil))
{
$no++;
if($x != $row[pbsid] ){
$no=1;
echo "<tr bgcolor=#CCCCCC>
<td colspan=4 ><b> GROUP $row[pbsid]</b></td>
</tr>";
}
$mo=count($no);
echo "<tr><td>$no </td><td> $row[bnama]</td><td>$row[pbsid] </td></tr>";
$x = $row["pbsid"];
$last_no[] = $no;
}
print_r($last_no);
Change your query $query ="select MAX(no),bnama, pbsid from yourtablename GROUP BY pbsid ";