Unable to get partial data from database using Codeigniter - php

I am very new in codeigniter. This is my first app. In my database I insert empid and deptid. When both are inserted in table all data show. But I want when both data not present in table then also all data show with blank that column. If any data not present in table then a add button display in place of delete url.
controller
public function index()
{
$rows = $this->EmpDept_model->get_empdept();
$data['recdept'] = $this->EmpDept_model->get_empdept();
$this->load->helper('url');
$this->load->view('empdept/EmpDept_list',$data);
}
view
<table class="table table-striped table-bordered" id="dataTables-example">
<thead>
<tr style="background-color: #d7ccc8;">
<th>Employee Name</th>
<th>Department Name</th>
<th>Option</th>
</tr>
</thead>
<tbody>
<?php
foreach($recdept as $r)
{
echo "<tr class='odd gradeX'>";
echo "<td>".$r->empname."</td>";
echo "<td>".$r->deptfname."</td>";
echo " <td> <a data-toggle='tooltip' data-placement='bottom' title='Delete' style='color: #000000;' href = '".base_url()."index.php/empdept/delete/".$r->empdeptid."'>Delete</a></td>";
/*echo "<td><a data-toggle='tooltip' data-placement='bottom' title='Edit' style='color: #000000;' href = '".base_url()."index.php/emp/edit/".$r->empid."'><span class='glyphicon glyphicon-pencil'></span></a>";
echo " || <a data-toggle='tooltip' data-placement='bottom' title='Delete' style='color: #000000;' href = '".base_url()."index.php/emp/delete/".$r->empid."'><span class='fa fa-ban fa-1x'></span></a></td>";*/
echo "</tr>";
}
?>
</tbody>
</table>
model
public function get_empdept()
{
$this->db->select('ed.empdeptid,e.empname,d.deptfname');
$this->db->from('empdept ed,empinfo e,deptinfo d');
$this->db->where('e.empid =ed.empid and d.deptid = ed.deptid');
$query = $this->db->get();
return $query->result();
}

Try this code:
public function get_empdept()
{
$this->db->select('ed.empdeptid,e.empname,d.deptfname');
$this->db->from('empdept ed');
$this->db->join('empinfo e','e.empid = ed.empid', 'left');
$this->db->join('deptinfo d','d.deptid = ed.deptid', 'left');
$query = $this->db->get();
return $query->result();
}
Check if column is blank than set add button otherwise delete button:
<table class="table table-striped table-bordered" id="dataTables-example">
<thead>
<tr style="background-color: #d7ccc8;">
<th>Employee Name</th>
<th>Department Name</th>
<th>Option</th>
</tr>
</thead>
<tbody>
<?php
foreach($recdept as $r)
{
echo "<tr class='odd gradeX'>";
echo "<td>".$r->empname."</td>";
echo "<td>".$r->deptfname."</td>";
if(!empty($r->empdeptid)){
echo " <td> <a data-toggle='tooltip' data-placement='bottom' title='Delete' style='color: #000000;' href = '".base_url()."index.php/empdept/delete/".$r->empdeptid."'>Delete</a></td>";
} else {
echo " <td> <a data-toggle='tooltip' data-placement='bottom' title='Add New' style='color: #000000;' href = '".base_url()."index.php/empdept/add/'>Add</a></td>";
}
echo "</tr>";
}
?>
</tbody>
</table>

You have to use union to achieve this. But code-igniter doesn't support union
So you can try like following . Change the query in model
$this->db->query("Select empdeptid from empdept union Select empname from empinfo union Select deptfname from deptinfo where empinfo.empid = empdept.empid and deptinfo.deptid = empdept.deptid");

Related

Images Not Showing in a table when PHP echo but will show if just in html

I am using the following code on a PHP page. When I load the page, the data shows up but the tag will not show anything other than a dot. If I add style="height:11px; width:16px", it shows a very very thin line (same goes if I just add height="11px" width="16px"
I can view the rendered html code and the img src is correct, it will display no problems. I can add, outside of my PHP script, an
as an example, and it displays just fine. So I am missing something within this snippet of code which is causing my grief?
I've looked in the Inspector tool in Chrome and I don't have any obvious errors other than a javascript error but I don't think that should be causing the issue.
The test page witho nly the two css. It works when I only have bootstrap.css but goes away when I add on the style.css
http://cronkflies.com/test5.php
<!-- Top Routes -->
<div class="col-lg-3" style="background:white;">
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col" colspan="2"><span style="font-size:18px; font-weight:bold; text-transform:uppercase"><?php echo $lang_top_routes ?></span></th>
</tr>
</thead>
<tbody>
<tr>
<td>
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/includes/connections/mysqli.php";
require($path);
$sql = "SELECT vertrekluchthaven, lh.luchthavencode AS vertrek, lh.luchthavennaam AS vnaam, lh.countryflag AS vflag, aankomstluchthaven, lh2.luchthavennaam AS anaam, lh2.luchthavencode AS aankomst, lh2.countryflag AS aflag, COUNT(*) AS count
FROM tbl_vluchtgegevens vg
INNER JOIN tbl_luchthaven lh
ON vg.vertrekluchthaven = lh.luchthavenID
INNER JOIN tbl_luchthaven lh2
ON vg.aankomstluchthaven = lh2.luchthavenID
WHERE vg.vertrekdatum2 <=Now()
GROUP BY vertrekluchthaven, aankomstluchthaven
ORDER BY count DESC
LIMIT 10;";
$result = $result = $link->query($sql);
if ($result->num_rows > 0) {
echo" <table class='table'>";
echo " <tbody>";
while($row = $result->fetch_assoc()) {
$vflag = $row['vflag'];
$aflag = $row['aflag'];
echo " <tr>";
echo " <td align='center'><img src='http://cronkflies.com/img/flags/" . $vflag . "'></td>";
echo " <td ><div class='top10_luchthaven' data-tooltip=\"".htmlspecialchars($row['vnaam'])."\" >".$row['vertrek']."</div></td>";
echo " <td ><strong class='home_statistieks_label'> -</strong></td>";
echo " <td ><img src='http://cronkflies.com/img/flags/".$aflag."'></td>";
echo " <td ><div class='top10_luchthaven' data-tooltip=\"".htmlspecialchars($row['anaam'])."\" >".$row['aankomst']."</div></td>";
echo " <td ><strong class='home_statistieks_label'><div align='center'>".$row['count']."</div></strong></td>";
echo " </tr>";
}
echo " </tbody>";
echo "</table>";
}
$link->close();
?>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
It seems that you should have the same amount of columns in the header <th></th> as in the row <td></td>

Bootstrap 4 table td width size customize

I need to increase the rating column width but my w-9 doesn't work.. I want to expand the rating column.
Here's my table code:
<table class="table table-bordered stick-top">
<thead class="text-center" id="data">
<tr>
<th></th>
<th>Questions</th>
<th id="try">Rating</th>
</tr>
</thead>
<tbody class="body_data">
<?php
$jio=mysqli_query($conn,"SELECT * FROM question")or die ("error");
while(list($Question_ID,$Question_Description)=mysqli_fetch_array($jio))
{ ?>
<tr>
<td><?php echo $Question_ID; ?></td>
<td><?php echo $Question_Description; ?> </td>
<?php
$q1 = "SELECT * FROM rating ORDER BY Rating_ID ASC";
$r1 = $conn -> query($q1);
echo "<td class='w-9'";
while($row = $r1 -> fetch_object()){
echo "<input id='radio' type='radio' name='radio$Question_ID' value='" . $row -> Rating_ID . "' required>". $row -> Rating_ID;"";
}
echo "</td>";
?>
</tr>
<?php } ?>
</tbody>
</table>
if you check Bootstrap Doc at Sizing doc you will they have included support for 25%, 50%, 75%, 100% and auto so basically when you are using w-9 the class is empty
so if you can't modify css file you need to place the following attribute inside your td tag :
<td style="width:9%">

Unable to submit my form with Javascript Link

I have tried to submit my form which is having two different links leading to the delete and edit php files but i noticed that the links do not submit the value of the radio button to php files especially the delete php file. But when i changed the links to buttons the value was submitted. Please i do not want to use the button tag, how can i make the value get across to the php files with with my links?
Here is the form:
<form action="del_cn_rec.php" method="POST" id="consignments" >
<table id="example1" class="table table-hover table-bordered table-striped">
<thead>
<tr>
<th>ID</th>
<th>Consignment No</th>
<th>Sender</th>
<th>Reciever</th>
<th>Pickup Date/Time</th>
<th>Msg_Status</th>
<th>Status</th>
<th>Action</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$sqlc="SELECT * FROM consignments";
$resultc= mysql_query($sqlc) or die(mysql_error());
while($rwsc= mysql_fetch_array($resultc)){
echo "<tr>";
echo "<td><input type='radio' class='flat-red' name='cn_id' value=".$rwsc[0];
echo " /></td>";
echo "<td>".$rwsc[1]."</td>";
echo "<td>".$rwsc[16]." ".$rwsc[17]."</td>";
echo "<td>".$rwsc[21]." ".$rwsc[22]."</td>";
echo "<td>".$rwsc[6]." ".$rwsc[7]."</td>";
$sqli='SELECT * FROM admin_inbox WHERE cn="'.$rwsc[1].'"';
$resulti= mysql_query($sqli);
$rwsi= mysql_fetch_array($resulti);
$mstatus= $rwsi[4];
if("$mstatus" === "Unreplied"){
$moutput = "<span class='label label-danger'><i class='fa fa-circle'></i> Unreplied</span>";
}
if("$mstatus" === "Replied"){
$moutput = "<span class='label label-success'><i class='fa fa-check-circle'></i> Replied</span>";
}
if("$mstatus" === ""){
$moutput = "<span class='label label-warning'><i class='fa fa-circle-o'></i> No Message</span>";
}
echo "<td>".$moutput."</td>";
$status= $rwsc[9];
if("$status" === "Delivered"){
$output = "<span class='label label-success'><i class='fa fa-check-square-o'></i> Delivered</span>";
}
if("$status" === "On Hold"){
$output = "<span class='label label-danger'><i class='fa fa-hand-stop-o'></i> On Hold</span>";
}
if("$status" === "Arrived"){
$output = "<span class='label label-primary'><i class='fa fa-motorcycle'></i> Arrived</span>";
}
if("$status" === "In Transit"){
$output = "<span class='label label-warning'><i class='fa fa-truck'></i> In Transit</span>";
}
echo "<td>".$output."</td>";
echo "<td><span class=\"label label-primary\"><i class=\"fa fa-edit\"></i> Edit Rec.</span></td>";
echo "<td><span class=\"label label-danger\"><i class=\"fa fa-times\"></i> Delete Rec.</span></td>";
echo "</tr>";}
?>
</tbody>
<tfoot>
<tr>
<th>ID</th>
<th>Consignment No</th>
<th>Sender</th>
<th>Reciever</th>
<th>Pickup Date/Time</th>
<th>Msg_Status</th>
<th>Status</th>
<th>Action</th>
<th>Action</th>
</tr>
</tfoot>
</table>
</form>
</div>
<script>
form=document.getElementById("consignments");
function askForEdit_Rec() {
form.action = <?php echo json_encode($urlc); ?>;
form.submit();
}
</script><script>
form=document.getElementById("consignments");
function askForDelete_Rec() {
form.action="del_cn_rec.php";
form.submit();
}
</script>
Here is the Delete php file: del_cn_rec.php
<?php
session_start();
include '_inc/dbconn.php';
$sql="SELECT * FROM admin WHERE id='1'";
$result= mysql_query($sql);
$rws= mysql_fetch_array($result);
$email_1= $rws[3];
$email_2= $rws[4];
$phone= $rws[5];
$address= $rws[6];
$url= $rws[7];
if(!isset($_SESSION['admin_login']))
header("location:$url/server-side");
?>
<?php
$id= mysql_real_escape_string($_REQUEST['cn_id']);
$sql1="SELECT * FROM consignments WHERE id='$id'";
$result1= mysql_query($sql1);
$rwsn= mysql_fetch_array($result1);
$cn= $rwsn[1];
$sql2="DROP TABLE IF EXISTS ".$cn."status ";
mysql_query($sql2) or die(mysql_error());
$sql3="DROP TABLE IF EXISTS ".$cn."msg ";
mysql_query($sql3) or die(mysql_error());
$sql6 = mysql_query("SELECT * FROM admin_inbox WHERE cn='$cn'");
if (mysql_fetch_row($sql6)){
$sql4="DELETE FROM admin_inbox WHERE cn='$cn'";
mysql_query($sql4) or die(mysql_error());
}
$sql5="DELETE FROM consignments WHERE id='$id' AND cn='$cn'";
mysql_query($sql5) or die(mysql_error());
//Consignment Remove Success Msg
$msg = "<i class=\"fa fa-check\"></i> Consignment: $cn Has Been Successfully Deleted!";
header("Location:$url/server-side/consignments?msg=$msg");
?>
this should generally work, just ensure you don't have any errors or notices in your php log, or in the generated html, or any javascript errors in the browser console. Ensure that the value for the radio button is filled in the html (maybe you are getting wrong result from the sql).
Also I hope you know you have to first select the radio button and only after that click the "Delete" or "Edit" link - it is a little confusing here, since you put the link on every row. Radio buttons send values only when they are selected, otherwise they will not even appear in the POST data.
If you wanted to use the links on every row without first selecting the radio button, you have to pass the cn_id as a parameter to the askForDelete_Rec() function and then use it there. An input type="hidden" might be usefull for that, or just append it to the action as a GET parameter.

retrieve id of table rows?

i have this script bellow to open table2 when clicking on the buttom 'more details'
<script>
$(document).ready(function() {
$('#Table1 tr').click(function(event){
$('#Table2').show();
alert($(this).attr('id'));
});
});
</script>
and this my code
<table id= "Table1" width='100%' border='1' cellspacing='0' cellpadding='0'>
$sql2=..
$sql3 = blabla ;
while($row3 =mysql_fetch_array($sql3)){
$sql4 = mysql_query (" SELECT $ww as place FROM data WHERE $ww =".$row3[$ww]." and id_user = ".$userid." ");
$row4 = mysql_fetch_array($sql4) ;
$string = "<blink > here </blink>" ;
$wnumber = $row3[$ww] ;
echo "<tr id= '".$wnumber."'><td style= 'text-align : center ;'>Week ".$row3[$ww]."
</td>" ;
echo "<td >".(int) $row3["percent"] ."% </td>";
echo "<td > "?><?php if($row4['place'] ==
$row3[$ww] and $row2['id'] == $userid ){ echo $string ; } else { echo "";} ;?><?php
"</td>";
echo "<td ><button class='showr'>More Details</button> </td></tr>";
//More Details when clicking on this buttom it open the table2
}
</tr>
</table>
this is second table
<?php
echo "<div id= 'Table2' style= 'display:none;'>";
echo "<table width='100%' border='1' cellspacing='0' cellpadding='0'>";
echo "<th> Week ".$wnumber."</th>";
echo "<th>try2</th>";
echo "<tr ><td>day</td>";
echo "<td>fff</td></tr>";
echo "</table></div>";
?>
*what i have now 5 rows with 5 buttoms .
*what it happen now is when clicking on every bottom it echo same '$wnumber' lets say 6.
however it defers from row to row ,
- script works good with alert of the id of which row is clicked.
- only the last buttom who works with the last id of row.
*what i want is every bottom works with its row id which echo the right '$wnumber'
* what i have tried is (make variable in the div)
echo "<div id= '".$wnumber."' style= 'display:none;'>";
instead of
echo "<div id= 'Table2' style= 'display:none;'>";
but didnt work.
hope its clear and there is solution of it.
EDIT : this source code
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function() {
$('#Table1 tr').click(function(event){
$('#Table2').show();
alert($(this).attr('id'));
});
});
</script>
<br />
<table id= "Table1" width='100%' border='1' cellspacing='0' cellpadding='0'>
<th >Weeks</th>
<th ><p></p></th>
<th > Your place</th>
<th > More Details</th>
<tr>
<tr id= '1'><td style= 'text-align : center ;'>Week 1</td><td style= 'text-align :
center ;'>33% </td><td style= 'text-align : center ;'> <td style= 'text-align :
center ;'><button class='showr'>More Details</button></td></tr><tr id= '6'><td
style= 'text-align : center ;'>Week 6</td><td style= 'text-align : center ;'>33%
</td><td style= 'text-align : center ;'> <td style= 'text-align : center
;'><button
class='showr'>More Details</button></td></tr><tr id= '13'><td style= 'text-align:
center ;'>Week 13</td><td style= 'text-align : center ;'>33% </td><td style=
'text-align : center ;'> <blink style= 'color:#990000 ;font-weight: bolder;' > 69
here </blink><td style= 'text-align : center ;'><button class='showr'>More
Details</button></td></tr></tr>
</table>
<br />
<div id= 'Table2' style= 'display:none;'><table width='100%' border='1'
cellspacing='0' cellpadding='0'><th> Week 13</th><th>try2</th><tr ><td>day</td>
<td>fff</td></tr></table></div>
<br /><br /> <br />
I tried your html code after correcting the missing td, tr.
And then clicking on each row / button displays the div.
Ensure you form proper html code in your php echo
Try something like this:
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function() {
$('#Table1 tr').click(function(event){
$('#details').find('#week' + $(this).attr('id')).show();
// alert($(this).attr('id'));
});
});
</script>
<?php
$result = mysql_query($sql);
// save the results to an array so you can loop them twice
while($row = mysql_fetch_array($result)) :
$rows[] = $row;
endwhile;
?>
<table id="table1">
<tr>
<th>heading</th>
<th>heading</th>
<th>heading</th>
<th>heading</th>
</tr>
<?php foreach ($rows as $row) : // loop #1 to output the main table ?>
<tr id="<?php echo $row['ww'] ?>">
<td>value</td>
<td>value</td>
<td>value</td>
<td><button type="button">More details</button></td>
</tr>
<?php endforeach; ?>
</table> <!-- end table 1 -->
<div id="details">
<?php foreach ($rows as $row) : // loop #2 to output a table for each set of details ?>
<table id="week<?php echo $row['ww'] ?>" style="display: none">
<tr>
<th>Week <?php echo $row['ww'] ?></th>
<th>try2</th>
</tr>
<tr>
<td>value</td>
<td>value</td>
</tr>
</table>
<?php endforeach; ?>
</div> <!-- end details -->
I copy / pasted your code into a jsFiddle and it seems to work as expected. Clicking on the row sends an alert with the correct ID.
http://jsfiddle.net/JeeNN/
Is there something I'm missing in your intent here?
Note : Best practice would be to skip the inline CSS and add external styling for your tables. Also, valid HTML is a must.
Update:
I went ahead and formatted the php code you posted so it's a bit easier to read. There's some variables that aren't defined and a couple other issues, but I'm sure you have it correct in your php file.
I think that you're going to want to run the loop a second time to create the second table. In this second loop, echo out the second table once per loop - you'll end up with a bunch of tables (one per row plus the first table). Then simply swap the visibility of the tables as user's click each id.
Is that what you're looking for? If not, perhaps try to rephrase your question.
Here's that formatted code:
<table id="Table1" width='100%' border='1' cellspacing='0' cellpadding='0'>
<?php
$sql2 = [..];
$sql3 = [..];
$ww = ?;
while($row3 = mysql_fetch_array($sql3)){
$sql4 = mysql_query ("SELECT $ww as place FROM data WHERE $ww =".$row3[$ww]." and id_user = ".$userid." ");
$row4 = mysql_fetch_array($sql4) ;
$string = "<blink> here </blink>" ;
$wnumber = $row3[$ww];
echo "<tr id='".$wnumber."'>";
echo "<td style='text-align:center;'>Week ".$row3[$ww]."</td>";
echo "<td>".(int) $row3["percent"] ."% </td>";
echo "<td>":
if($row4['place'] == $row3[$ww] and $row2['id'] == $userid ){
echo $string;
} else {
echo " ";
}
echo "</td>";
//More Details when clicking on this buttom it open the table2
echo "<td ><button class='showr'>More Details</button></td>";
echo "</tr>";
} ?>
</table>
<div id= 'Table2' style= 'display:none;'>
<table width='100%' border='1' cellspacing='0' cellpadding='0'>
<tr>
<th> Week <?php echo $wnumber; ?></th>
<th>try2</th>
</tr>
<tr>
<td>day</td>
<td>fff</td>
</tr>
</table>
</div>

tablesorter.pager plugin

The number of returned results is 2 which is correct and it comes up with 2 for the number of pages which is also correct but instead of showing 1 on the first page it shows both records. I'm not sure hwy the tablesorter.pager plugin is doing that.
<?php
session_start();
require("../inc/dbconfig.php");
require("../inc/global_functions.php");
require("../inc/variables.php");
// find out how many rows are in the table
$query = "SELECT CONCAT_WS(' ',firstName,lastName) AS name, username, emailAddress, userID FROM manager_users WHERE statusID != 4";
$result = mysqli_query($dbc,$query);
$rows = mysqli_num_rows($result);
$itemsPerPage = 1;
$totalPages = ceil($rows/$itemsPerPage);
$fileName = basename($_SERVER[PHP_SELF]);
$pageName = "User Accounts";
$userData = $_SESSION['user_data'];
$userID = $userData['userID'];
?>
<script type="text/javascript">
$(document).ready(function() {
$('#usersPageList').tablesorter().tablesorterPager({sortlist: [0,0], container:$('#usersPageList .pagination'),cssPageLinks:'a.pageLink'});
$('a.bt_green').click(function(e) {
e.preventDefault();
$('div.right_content').load('forms/addnew/' + $(this).attr('id'));
});
$('.ask').jConfirmAction();
});
</script>
<h2>User Accounts</h2>
<table id="usersPageList" class="rounded-corner">
<thead>
<tr>
<th scope="col" class="rounded-first"></th>
<th scope="col" class="rounded">Name</th>
<th scope="col" class="rounded">Email Address</th>
<th scope="col" class="rounded">Username</th>
<th scope="col" class="rounded">Edit</th>
<th scope="col" class="rounded-last">Delete</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="5" class="rounded-foot-left"><em>Displays all of the registered and verified users!</em></td>
<td class="rounded-foot-right"> </td>
</tr>
</tfoot>
<tbody>
<?php
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo "<tr>";
echo "<td><input type=\"checkbox\" name=\"\" /></td>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['emailAddress']."</td>";
echo "<td>".$row['username']."</td>";
echo "<td><img src=\"images/user_edit.png\" alt=\"\" title=\"\" border=\"0\" /></td>";
echo "<td>";
if (($row['userID'] !== '10000') && ($row['userID'] !== $userID)){
echo "<img src=\"images/trash.png\" class=\"delete\" alt=\"\" title=\"\" border=\"0\" id=\"".$row['userID']."\" />";
}
echo "</td>";
echo "</tr>";
}
?>
</tbody>
</table>
<?php
addRemove($fileName,$pageName);
pagination($totalPages);
?>
<input type="hidden" name="myhiddenPageToken" id="myhiddenPageToken" value="useraccounts" />
Maybe I'm misunderstanding what you want, but I think you want just one record at a time to show on the page? I have no idea why you would do that, but you can use the pager's size option:
$("table").tablesorter({
widthFixed: true,
widgets: ['zebra']
}).tablesorterPager({
container: $("#pager"),
size: 1 // only show one record
});
Also, if you are using the select to change the number of records showing, you'll need to include a "1" (demo).

Categories