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
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 am trying to display a table by school year using PHP. I it seems not working very well because the school year displays redundantly.
How to simply display it like this?
No | SY | Student ID | Student Name | Gender | Section Code | Subject
-----------------------------------------------------------------------------
2015-2016 SEMESTER 1
-----------------------------------------------------------------------------
1 | 2 | 0011941 | Cocos, Scrappy S. | Male | IT15B | IT15
-----------------------------------------------------------------------------
1 | 2 | 1212211 | asasas, sasas a. | Male | IT15B | IT15
-----------------------------------------------------------------------------
2015-2016 SEMESTER 2
-----------------------------------------------------------------------------
1 | 2 | 0011941 | Cocos, Scrappy S. | Male | IT15B | IT15
-----------------------------------------------------------------------------
1 | 2 | aSAsaSA | dsdsadsad.ssasasas | Male | IT15B | IT15
-----------------------------------------------------------------------------
2016-2017 SEMESTER 1
-----------------------------------------------------------------------------
1 | 2 | 0011941 | Doo, Scooby D. | Male | IT15B | IT15
-----------------------------------------------------------------------------
2016-2017 SEMESTER 2
-----------------------------------------------------------------------------
1 | 2 | 0011941 | Doo, Scooby D. | Male | IT15B | IT15
here's my code:
<tr>
<th>No</th>
<th>SY</th>
<th>Student ID</th>
<th>Student Name</th>
<th>Gender</th>
<th>Section Code</th>
<th>Subject</th>
<th colspan="2">Update</th>
</tr>
$sql= "SELECT sts.stud_id, sts.fname, sts.lname, sts.mi, sts.gender, sub.section_code, sub.subject_name, ets.sem, ets.enroll_num, ets.sy
FROM students sts
JOIN enrollments ets ON(sts.stud_id = ets.stud_id)
JOIN subjects sub ON (sub.section_code = ets.section_code)
GROUP BY sts.stud_id, sub.section_code ORDER BY ets.stud_id ASC";
$sql_sel=mysql_query($sql);
$num_rows = mysql_num_rows($sql_sel);
if($num_rows==0)
{
echo "No results found. Please try again";
}
$i=0;
while($row=mysql_fetch_array($sql_sel)) //for the first query
{
$i++;
$color=($i%2==0)?"lightblue":"white";
?>
<th colspan="8"><?php echo $row['sy'];?></th> //this thing here
<tr bgcolor="<?php echo $color?>">
<td><?php echo $i;?></td>
<td><?php echo $row['sem'];?></td>
<td><?php echo $row['stud_id'];?></td>
<td width="200"><?php echo $row['lname'].", ".$row['fname']." ".$row['mi'].".";?></td>
<td><?php echo $row['gender'];?></td>
<td><?php echo $row['section_code'];?></td>
<td><?php echo $row['subject_name'];?></td>
<td align="center" width="82"><img src="picture/update.png" /></td>
<!----<td align="center"><img src="picture/delete.png" /></td>-------->
</tr>
<?php
}
?>
First you need to order by year and semester in your query:
$sql= "SELECT sts.stud_id, sts.fname, sts.lname, sts.mi, sts.gender, sub.section_code, sub.subject_name, ets.sem, ets.enroll_num, ets.sy
FROM students sts
JOIN enrollments ets ON(sts.stud_id = ets.stud_id)
JOIN subjects sub ON (sub.section_code = ets.section_code)
GROUP BY sts.stud_id, sub.section_code ORDER BY ets.sy ASC, ets.sem ASC, ets.stud_id ASC";
Then you only write the year row when you detect a change of year:
$i=0;
$yr=null;
while($row=mysql_fetch_array($sql_sel)) //for the first query
{
if ($yr != ($row['sy'] . ' SEMESTER ' . $row['sem'])) {
$yr = $row['sy'] . ' SEMESTER ' . $row['sem'];
print ('<th colspan="8">' . $yr . '</th>');
}
$i++;
$color=($i%2==0)?"lightblue":"white";
?>
<tr bgcolor="<?php echo $color?>">
<td><?php echo $i;?></td>
<td><?php echo $row['sem'];?></td>
<td><?php echo $row['stud_id'];?></td>
<td width="200"><?php echo $row['lname'].", ".$row['fname']." ".$row['mi'].".";?></td>
<td><?php echo $row['gender'];?></td>
<td><?php echo $row['section_code'];?></td>
<td><?php echo $row['subject_name'];?></td>
<td align="center" width="82"><img src="picture/update.png" /></td>
<!----<td align="center"><img src="picture/delete.png" /></td>-------->
</tr>
<?php
}
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;
}
}
?>
I have problems when numbering the tables using CodeIgniter framework with libraary pagination. below are the table..
+------------------------------------------+
| num| NIP | Name | Category | Type |
+------------------------------------------+
| 1 | 12345 | udin | A | A1 |
+------------------------------------------+
| | | | B | B1 |
+------------------------------------------+
| | | | C | C1 |
+------------------------------------------+
| 2 | 54321 | JHON | B | B2 |
+------------------------------------------+
| | | | C | C1 |
+------------------------------------------+
the issue is on the next page in the table below..
+------------------------------------------+
| num| NIP | Name | Category | Type |
+------------------------------------------+
| 1 | 54321 | JHON | F | F2 |
+------------------------------------------+
| | | | G | G3 |
+------------------------------------------+
| | | | G | G4 |
+------------------------------------------+
| | | | I | I3 |
+------------------------------------------+
| | | | J | J1 |
+------------------------------------------+
I want the next page numbering sequential numbering starting from the end of the previous value. Supposedly in figure 2 numbering number "2" instead of "1".
code in the controller:
$this->load->library('pagination');
$data['judul'] = 'Report Data Attachment';
$start_row = $this->uri->segment(3);
$per_page = 10;
if (trim($this->uri->segment(3)) == '')
{
$start_row = '0';
}
$config['base_url'] = base_url().'report/index/';
$config['total_rows'] = $this->lampiran_m->lampiran_karyawan_semua_page_conter()->num_rows();
//$config['uri_segment'] = '3';
$config['per_page'] = $per_page;
$config['full_tag_open'] = '<p class="page">';
$config['full_tag_close'] = '</p>';
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$data['lampiran'] = $this->lampiran_m->lampiran_karyawan_semua_page($start_row,','.$per_page);
$data['no'] = '';
$data['last'] = '';
$this->template->display('tabel_report',$data);
and code in my Model :
public function lampiran_karyawan_semua_page($offset='',$limit='')
{
$qry = "select distinct lampiran.id_category,name_category,tipe,lampiran.nip from lampiran
left join employee on (lampiran.nip = employee.nip)
left join category on (lampiran.id_category=category.id_category) where lampiran.id_category = category.id_category and lampiran.nip=employee.nip order by lampiran.nip desc limit $offset $limit";
return $this->db->query($qry);
}
and this code in view :
<table border="1" width="100%" cellpadding="0" cellspacing="0" id="product-table" >
<tr>
<th class="table-header-repeat line-left minwidth-1"> No</th>
<th class="table-header-repeat line-left minwidth-1"> NIP</th>
<th class="table-header-repeat line-left minwidth-1"> Name</th>
<th class="table-header-repeat line-left minwidth-1"> Category Attachment</th>
<th class="table-header-repeat line-left minwidth-1">Type Attachment </th>
<th class="table-header-repeat line-left minwidth-1">Page </th>
</tr>
<?php
foreach($lampiran->result_array() as $k) {
$now=$k['nip'];
if($last!=$now ) {
$no++;
echo "<tr><td>$no</td><td>$k[nip]</td><td>".$this->employee_m->get($k['nip'])->nama."</td><td>$k[name_category]</td><td>$k[tipe]</td><td>"; ?>
<?php foreach($this->lampiran_m->conter_tabel_semua($k['id_category'],$k['tipe'],$k['nip'])->result() as $row) :?>
<?php echo $row->nilai;?>
<?php endforeach;
echo "</td></tr>";
}else{
echo "<tr><td> </td><td> </td><td> </td><td>$k[name_category]</td><td>$k[tipe]</td><td>"; ?>
<?php foreach($this->lampiran_m->conter_tabel_semua($k['id_category'],$k['tipe'],$k['nip'])->result() as $row) :?>
<?php echo $row->nilai;?>
<?php endforeach;
echo "</td></tr>";
}
$last = $k['nip'];
}
?>
<tr><td colspan="5" style="text-align: center;"><b>Jumlah Total</b></td><td>
<?php foreach($this->lampiran_m->jumlah_semua_lampiran()->result() as $row) :?>
<?php echo "<b>$row->jumlah</b>";?>
<?php endforeach; ?>
</td></tr>
</table>
I hope someone can help me on this issue .. thanks
before foreach you have to add
$no = $this->uri->segment(3)+1;
No
NIP
Name
Category Attachment
Type Attachment
Page
</tr>
<?php
//before foreach you have to add
$no = $this->uri->segment(3)+1;
foreach($lampiran->result_array() as $k) {
$now=$k['nip'];
if($last!=$now ) {
$no++;
echo "<tr><td>$no</td><td>$k[nip]</td><td>".$this->employee_m->get($k['nip'])->nama."</td><td>$k[name_category]</td><td>$k[tipe]</td><td>"; ?>
<?php foreach($this->lampiran_m->conter_tabel_semua($k['id_category'],$k['tipe'],$k['nip'])->result() as $row) :?>
<?php echo $row->nilai;?>
<?php endforeach;
echo "</td></tr>";
}else{
echo "<tr><td> </td><td> </td><td> </td><td>$k[name_category]</td><td>$k[tipe]</td><td>"; ?>
<?php foreach($this->lampiran_m->conter_tabel_semua($k['id_category'],$k['tipe'],$k['nip'])->result() as $row) :?>
<?php echo $row->nilai;?>
<?php endforeach;
echo "</td></tr>";
}
$last = $k['nip'];
}
?>
<tr><td colspan="5" style="text-align: center;"><b>Jumlah Total</b></td><td>
<?php foreach($this->lampiran_m->jumlah_semua_lampiran()->result() as $row) :?>
<?php echo "<b>$row->jumlah</b>";?>
<?php endforeach; ?>
</td></tr>
</table>
please put below line before foreach statement
$currentffset = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$slno_start = (($currentffset / $per_page) * per_page) + 1;
after that use $slno_start as serial number for each item inside foreach at last line of foreach just increment it
$slno_start++;
Minor improvement from above answers.Thanks both of u guys
$count =$this->uri->segment(3)+1;
foreach($records as $rec){echo $count; $count++;}