depending on select box value need to display view table in codeigniter - mysql

i have one view selection table in my application..in that i display all the view selection details..but what i want to do is i want to display the view selection depending on the role name..i have role_id in my selection table..i get the role names in dropdown..
this is my Controller:
public function vendor_view_selection()
{
$data['rolename']=$this->SelectionModel->getrolename();
$data['view_selection'] = $this->SelectionModel->getselection();
$this->load->view('selection/vendor_view_selection',$data);
}
This is my Model:
function getselection()
{
$this->db->select('*');
$this->db->from('selection');
$this->db->join('role','role.role_id= selection.role_id');
$this->db->join('candidates_details','candidates_details.candidate_id=selection.candidate_id');
$query = $this->db->get();
return $query->result();
}
select box for role name:(depending on selected role name need to display selection details)
<div class="form-group">
<label>Choose RoleName</label>
<select class="form-control" multiple class="form-control" data-placeholder="Role name" name="role_id" >
<option value="0"></option>
<?php foreach($rolename as $rows) { ?>
<option value="<?php echo $rows->role_id?>"><?php echo ucfirst($rows->role_name)?></option>
<?php } ?>
</select>
</div>
view:(view_selection)table
<div class="box-body">
<table id="" class="table table-bordered table-hover">
<thead>
<tr>
<th>selection </th>
<th>comments</th>
<th>results</th>
<th>candidate</th>
<th>role</th>
</tr>
</thead>
<?php
foreach ($view_selection as $idata) {
?>
<tbody>
<tr id="domain<?php echo $idata->selection_id;?>">
<td><?php echo $idata->selection;?></td>
<td><?php echo $idata->comments;?></td>
<td><?php echo $idata->results;?></td>
<td><?php echo $idata->candidate_id;?></td>
<td><?php echo $idata->role_id;?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
<!-- /.box-body -->
</div>
<!-- /.box -->
</div>
Please anyone help me how to do that..
Thanks in advance..

Send list id by $.ajax to the special function in controller.
Then
echo $this->load->view('view_selection',array(),true);//return string of view
exit();
i hope so worked for you,

Related

how to show datas in database in a table using modal

I want the data in my table to be clickable and show in a modal.
then get the id of that data(from the table) and show the rest of the data in the database in that modal.
I tried following this but it doesn't work, I dunno why
<table class="table table-hover table-bordered results" style="color:white;">
<thead>
<tr>
<th>Contact #</th>
<th>Picture</th>
<th >Name / Surname</th>
<th >Address</th>
</tr>
<tr class="warning no-result">
<td colspan="4"><i class="fa fa-warning"></i> No result</td>
</tr>
</thead>
<tbody>
<?php
$sqlb = "select * from debtors ";
$resultb = $conn->query($sqlb);
while($rowb = $resultb->fetch_assoc()){
$id=$rowb['contact #'];
?>
<tr a class="dropdown-item" data-toggle="modal" data-target="#logoutModal" style="color:white;">
<td><?php echo $rowb['contact #']; ?></td>
<td><?php echo $rowb['lname']; ?></td>
<td><?php echo $rowb['fname']; ?></td>
<td><?php echo $rowb['address']; ?></td></a>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<!-- Logout Modal-->
<span>
<div class="modal fade" id="logoutModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel"></h5>
<button class="close" type="button" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">x</span>
</button>
</div>
<div class="modal-body"><?php echo $rowb['lname']; ?>, <?php echo $rowb['fname']; ?><BR>
<?php echo $rowb['balance']; ?>
</div>
<div class="modal-footer">
<button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
<a class="btn btn-info" href="admin_logout.php">Yes</a>
</div>
</div>
</div>
</div>
</span>
I want my table row to show modal when clicked, then show data relating to the clicked row from the database in that modal
First of all the modal is not in the loop so you can't access $rowb in modal and secondly this is not the right way because after putting the modal in the loop it will create so many (hundreds or thousands) modals which is not good performance wise.
The right way is, create a modal and assign a class to the button and put the numeric id in the id attribute and on the click of that button make an ajax call and get the data and show it in the modal.
In this way the single modal will show you the right data dynamically.
You can use instead the try catch for the tbody of your table.
try {
$pdo = new PDO('mysql:host=localhost:3306;dbname=databaseName;', 'username', 'password' );
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$stmt = $pdo->prepare(
"SELECT * from debtors "
);
$flag = $stmt->execute();
if ( !$flag ) {
$info = $stmt->errorInfo();
exit( $info[2] );
}
while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ) {
$num = $num +1;
#$tbody .='<tr>';
//$tbody .='<td>'.$row["id"].'</td>
$tbody .='<td>'.$num.'</td
<input type="hidden" id="id'.$row["id"].'" value="'.$row["id"].'"/>
<input type="hidden" id="contact #'.$row["contact #"].'" value="'.$row["id"].'"/>
<input type="hidden" id="lname'.$row["id"].'" value="'.$row["lname"].'"/>
<input type="hidden" id="fname'.$row["id"].'" value="'.$row["fname"].'"/>
<input type="hidden" id="address'.$row["id"].'" value="'.$row["address"].'" />
';
$tbody .='<td>'.$row["contact #"].'</td>';
$tbody .='<td>'.$row["lname"].'</td>';
$tbody .='<td>'.$row["fname"].'</td>';
$tbody .='<td>P'.$row["address"].'</td>';
$tbody .='<td><input type ="button" id = "btnshow" value = "show" data-id="'.$row["id"].'"></input></td></a>';
}
}
catch ( PDOException $e ) {
echo $e->getMessage();
$pdo = null;
}
just make sure data in database has a unique ID's.
for the script :
$(document).on('click','#btnshow',function(){
var id_show = $(this).data('id');
var contact= document.getElementById('contact #'+id_edit).value;
var lname= document.getElementById('lname'+id_edit).value;
var fname= document.getElementById('fname'+id_edit).value;
var address= document.getElementById('address'+id_edit).value;
$(".modal-heading").html('')
$(".modal-body").html(
'<div class = "container-fluid" style="width:89%;" >' +
'<input type="hidden" class="form-control" id="id_row" name="id_row" value="'+ id_edit +'"></input>'+ <input type="text" id="contact" name= "contact" readonly="readonly" class="form-control" value="'+contact #+'"></input>'+ <input type="text" id="lname" name= "lname" readonly="readonly" class="form-control" value="'+lname+'"></input>'+ <input type="text" id="fname" name= "fname" readonly="readonly" class="form-control" value="'+fname+'"></input>'+<input type="text" id="address" name= "address" readonly="readonly" class="form-control" value="'+address+'"></input>'+
'</form></div>'
)
})
your html :
<table id ="products">
<col width="130">
<col width="80">
<thead>
<th>ID</th>
<th>CONTACT</th>
<th>LNAME</th>
<th>FNAME</th>
<th>ADDRESS</th>
</thead>
<tbody>
<?php echo $tbody;?>
</tbody>
</table>

I am unable to find if the row already exists in mysql db using time function in php mysql query?

i am currently working on a module where faculty can post attendance to the students by selecting a dropdown(select box) which is generated dynamically by the information given by the faculty.When faculty selects a particular year and section respected student list is retrived and displayed in a table.But the requirement is once the attendance is posted to a particular class/section on a particular it cannot be opened again by the faculty
I have tried using mysql_num_rows() function to check if any rows are already present in the db or not on that particular date.But its not working the way i wanted
here is my entire code of the module excluding db file
<form action="take.php" method="Post">
<br>
<table class="table table-bordered table-hover ">
<tr>
<th>S.no</th>
<th>Student Name</th>
<th>Roll Number</th>
<th>Present</th>
<th>Absent</th>
</tr>
<?php
if (isset($_POST['search']))
{
$stu="Student";
$yr=$_POST['year'];
$se=$_POST['section'];
$subdr=mysql_query("SELECT subject FROM schedule WHERE id='$cuid' AND day='$d' AND class='$yr' AND section='$se'");
$subj=mysql_fetch_assoc($subdr);
$dis_date=date("Y-m-d H:i:s");
$subj_d=$subj['subject'];
$display=mysql_query("select * from attendance_records where id='$cuid' AND ondate='".$dis_date."' And subject='$subj_d'");
$rec=mysql_num_rows($display);
if($rec){
echo "Records posted";
}
else{
$display=mysql_query("select name,id from login where role='$stu' AND academic='$yr' AND section='$se'");
$sno=0;
$count=0;
while ($row=mysql_fetch_array($display)) {
$sno++;
?>
<tr>
<td><?php echo $sno ?></td>
<td>
<?php echo $row['name'] ?>
<input type="hidden" name="name[]" value="<?php echo $row['name'] ?>">
</td>
<td>
<?php echo $row['id'] ?>
<input type="hidden" name="id[]" value="<?php echo $row['id'] ?>">
</td>
<td>
<input type="radio" name="attendance_status[<?php echo $count ?>]" value="Present" required>
</td>
<td>
<input type="radio" name="attendance_status[<?php echo $count ?>]" value="Absent" required>
</td>
</tr>
<?php
$count++;
}
}
?>
<tr>
<td colspan=5>
<center><label><?php echo "Subject : ".$subj['subject']; ?></label></center>
</td>
</tr>
<input type="hidden" name="yr" value="<?php echo $_POST['year']; ?>">
<input type="hidden" name="set" value="<?php echo $_POST['section']; ?>">
<?php
} ?>
</table>
<center><input type="submit" name="submit" value="Submit" class="btn btn-primary" >
</center>
</div>
the expected output should display a message saying "Records posted" based on query like :
$dis_date=date("Y-m-d H:i:s");
$subj_d=$subj['subject'];
$display=mysql_query("select * from attendance_records where id='$cuid'
AND ondate='".$dis_date."' And subject='$subj_d'");
$rec=mysql_num_rows($display);
if($rec){
echo "Records posted";
}
else{
#display the student list
}
Your assignment is:
$dis_date=date("Y-m-d H:i:s");
so $dis_date contains both a date and a time of day. The query will only match if the records in the table have the exact same time of day, not just the same date.
You should leave the time out of the variable:
$dis_date=date("Y-m-d");
If the datatype of the column in the table is DATETIME, you also need to filter out the time from that, with:
AND DATE(ondate)='$dis_date' And subject='$subj_d'"
You don't need to do this if the datatype is DATE.

$_POST array in wrong order?

like to submit the array via $_POST, it works only with one foreach, how can i combine two foreach-arrays without getting everything doubled?
i tried it with "keTitle" and and "keSoll", but only once at a time works..
html:
<div class="table-responsive">
<table class="table table-striped">
<thead>
<td>Kühlgerät</td>
<td>Soll</td>
<td>Ist</td>
</thead>
<tbody>
<?php
$todo_query = mysqli_query($db, "SELECT * FROM temp_ke WHERE ListID = '1'");
while($row2 = mysqli_fetch_object($todo_query))
{
?>
<tr>
<th scope="row">
<?php echo "$row2->keTitle"; ?>
<input name="Title[<? echo "$row2->keTitle";?>]" value="<? echo "$row2->keTitle";?>" type="hidden">
</th>
<td><?php echo "$row2->keSoll"; ?> °C<br>
<input name="SollWert[<? echo "$row2->keSoll";?>]" value="<? echo "$row2->keSoll";?>" type="hidden">
</td>
<td>
<input class="shortinput" name="IstWert[]" value="" type="text"> °C<br>
</td>
</tr>
<?php }} ?>
</tbody>
</table>
<div class="tablefooter">Ausgefüllt von: <input name="Mitarbeiter" value="" type="text"></div>
<br>
<input type="submit" class="btn btn-default" value="Temperaturen eintragen">
</form>
</div>
next page to $_POST to:
<?php
foreach ($_POST['Title'] as $keTitle) {
foreach ($_POST['SollWert'] as $keSoll) {
echo "$keSoll<br>";
echo "$keTitle<br><br>";
}
}
?>
form value
$_POST['Title'] = 'Mein Titel';
$_POST['SollWert'] = '123';
thats the array:
$ksTitle = $_POST['Title'];
$keSoll = $_POST['SollWert'];
echo "$keSoll<br/>";
echo "$keTitle<br/>";
or in foreach:
foreach( $_POST AS $value){
echo $value . "<br/>";
}
output:
Mein Titel
123

Can't Update My Database

I can't update my database.. I wonder why it's not working while it worked on my other pages. I'm receiving this error:
You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version
for the right syntax to use near 'WHERE id =1' at line 4
This is the code:
<?php require_once("include/connect.php"); ?>
<?php require("include/bp_function.php"); ?>
<?php url(); ?>
<?php
$title = mysql_prep($_POST['title']);
$content = mysql_prep($_POST['content']);
$id = mysql_prep($_GET['aboutusid']);
$query = "UPDATE aboutus SET
title='{$title}',
content='{$content}',
WHERE id ={$id}";
mysql_query($query);
if(mysql_affected_rows() == 1) {
echo "Succesfully Updated {$title}
<br/>Go back to Backpanel";
} else {
echo "failed {$id}<br />".mysql_error()."<p> </p>";
}
?>
<?php require_once("include/footer.php"); ?>`
This is the form:
<?php require("include/connect.php"); ?>
<?php require("include/bp_function.php"); ?>
<?php url(); ?>
<?php include("include/bp_header.php"); ?>
<div id="bgcontainer">
<!-- NEWS CONTAINER -->
<div id="bodycont">
<div id="left_page">
<h2>About Us Menu</h2>
<?php list_of_aboutus(); ?>
<br />+ Add Menu
<hr />
</div>
<div id="right_page">
<h2>Edit: <?php echo $s_aboutus['title']; ?> </h2>
<br /><br />
<form action="query_editaboutus.php?aboutusid=<?php echo urlencode($s_aboutus['id']); ?>" method="post" enctype="multipart/form-data">
<table>
<tr valign="top"><td width="100px">Title:</td> <td><input name="title" type="name" size="45" value="<?php echo $s_aboutus['title']; ?>" /></td></tr>
<tr valign="top"><td width="100px">Content:</td> <td>
<textarea name="content" cols="45" rows="20" value="" > <?php echo $s_aboutus['content']; ?> </textarea>
<tr valign="top"><td width="100px">Update:</td><td><input type="submit" id="submit" value="Update" /></td></tr>
</table>
</form>
</div>
<!-- MEDIA CONTAINER -->
<?php include("include/footer.php"); ?>
Remove the comma after setting the content field.
$query = "UPDATE aboutus SET title='{$title}', content='{$content}' WHERE id ={$id}";
Original for comparison
$query = "UPDATE aboutus SET title='{$title}', content='{$content}', WHERE id ={$id}";
Check your update statement
$query = "UPDATE aboutus SET
title='{$title}',
content='{$content}'
WHERE id ={$id}";

form text-input unwanted redirection on click

Please help, I can't understand why the code below keeps bringing me to viewcart.php when I click on the textbox that is used to input qty to buy, I made sure to close all the anchor tags in the page. I even removed all the links to viewcart.php but no luck:
<form name="cx" method="get" action="viewcart.php?action=add&id=<?php $pid; ?>">
<?php
while($row=mysql_fetch_assoc($result)){
$pid=$row['PID'];
?>
<tr>
<td><?php echo $row['PID']; ?></td>
<td> <?php echo $row['PRODUCT']; ?></td>
<td><?php echo $row['CATEGORY']; ?></td>
<td><?php echo $row['P_DESC']; ?></td>
<td><?php echo $row['QTYHAND']; ?></td>
<td><?php echo $row['S_PRICE']; ?></td>
<input type="hidden" value="<?php echo $row['QTYHAND']; ?>" name="qoh[]"/>
<input type="hidden" value="<?php echo $row['S_PRICE']; ?>" name="sprice[]"/>
<?php echo "<td><img src=\"../img/system/add-icon.png\"></td>"; ?>
<td><input type="checkbox" name="sc[]" id="<?php echo $row['PID'];?>" value="<?php echo $row['PID']; ?>"></input></td>
<td><input type="text" name="qbuys[]" value="" id="qb"></input></td> <!--when I click on this, it seems like I'm clicking on a link to viewcart.php -->
</table>
<input type="submit" value="submit"></input>
</form>
I removed some of the code which I don't think is useful in solving this problem. Please help. Thanks.
Your code has two issues. YOu're not echoing the $pid and you have a scoping error.
<form name="cx" method="get" action="viewcart.php?action=add&id=<?php $pid; ?>">
You need to add an echo:
<form name="cx" method="get" action="viewcart.php?action=add&id=<?php echo $pid; ?>">
Also, $pid doesn't exists until you're inside the while loop so you'll always be echoing '':
// $pid == null;
<form name="cx" method="get" action="viewcart.php?action=add&id=<?php $pid; ?>">
<?php
while($row=mysql_fetch_assoc($result)){
$pid=$row['PID']; // NOW $pid has a value
?>
If you view the source of this page you should see:
<form name="cx" method="get" action="viewcart.php?action=add&id=">
Notice the empty id. This would keep sending you back to viewcart.php
You are not echoing your $pid into the form action.
This means that when you submit your form it is not going where you expect.
action="viewcart.php?action=add&id=<?php echo $pid; ?>"
or
action="viewcart.php?action=add&id=<?=$pid?>"
This second examle will only work if your server has short tags enabled.
Rather than looking at the PHP code that generates the page, i would suggest loading the page in your browser, and then clicking "view source". That way you can see exactly what code is being generated, then work back from there to identify which statement is going wrong.
However, I would suggest looking at the following line:
<td> <?php echo $row['PRODUCT']; ?></td>
There is no echo on the 'Product' property.