I have a function inside my controller that outputs html elements to a view. This is how it looks like.
function show_res() {
$output = '';
foreach($data->result() as $row) {
if($row->user_agency == $user_agency) {
$output .= '
<tr>
<td>'.$row->user_name.' '.$row->user_lname.'</td>
<td>'.$row->user_type.'</td>
<td>
<button>Edit</button>
<button>Delete</button>
</td>
</tr>
';
}
}
echo $output;
}
My task is that I need to include a php function for when the delete button is clicked, so far this is what I've tried:
if($row->user_agency == $user_agency) {
$output .= '
<tr>
<td>'.$row->user_name.' '.$row->user_lname.'</td>
<td>'.$row->user_type.'</td>
<td>
<button>Edit</button>
<a href="<?php echo base_url().delete_c/delete_user/.$row->user_id; ?>">
<button>Delete</button>
</a>
</td>
</tr>
';
}
and
if($row->user_agency == $user_agency) {
$output .= '
<tr>
<td>'.$row->user_name.' '.$row->user_lname.'</td>
<td>'.$row->user_type.'</td>
<td>
<button>Edit</button>
<a href="'.echo base_url().'delete_c/delete_user/'.$row->user_id'">
<button>Delete</button>
</a>
</td>
</tr>
';
}
The problem with the first one is that the whole
<?php echo base_url().delete_c/delete_user/.$row->user_id; ?>
is added as a string instead of a php script so when I click on the button it would redirect to this URL
https://localhost/test/<?php%20echo%20base_url().delete_c/delete_user/.$row->user_id;%20?>
As for the second one, it causes an internal error resulting to the status code 500
Use this. There is no need to write again PHP start because you already started and you forgot to mention . operator.
<a href='.base_url().'delete_c/delete_user/'.$row->user_id.'> <button>Delete</button>
</a>
The following code should work.
<a class="btn" href='.base_url().'delete_c/delete_user/'.$row->user_id.'>
Delete
</a>
500 error code may be cause of you forgot to put . in string concatenation or may be undefined variable used in function.
Related
<?php
$pros_array_id= array_column($_SESSION['cart'], 'paint_id');
$productee = $productn->getData('paint');
foreach($productee as $pro):
if($pro['paint_id'] == $pros_array_id):
?>
<table>
<tr>
<td class="imgTag">
<img class="img-fluid" src="<?php echo $pro['paint_image'] ?? 1; ?>" >
</td>
</tr>
</table>
<?php
endif;
endforeach;
?>
Am trying to display session cart items and its not showing anything. When I print_r($productee) and print_r($pros_array_id) after the foreach statement both display the accurate data, yet nothing displat in the <tr> tag.
The is to display the result
When I implode $pros_array_id like this "$imp = implode(" ",$pros_array_id);" and put the variable in the if-statement, it works fine if only one product is in the session, but the moment I add more than one products in the session, nothing is display again.
Please can someone point to me what I should do?
Thanks
Try it like this:
<table>
<?php
$pros_array_id = array_column($_SESSION['cart'], 'paint_id');
$productee = $productn->getData('paint');
foreach($productee as $pro) {
//Also see and try to disable this check to see if it works then!
if($pro['paint_id'] == $pros_array_id) { ?>
<tr>
<td class="imgTag">
<img class="img-fluid" src="<?php echo $pro['paint_image'] ?? 1; ?>">
</td>
</tr>
<?php
}
}
?>
</table>
Always make sure while debugging this kind of code to eliminate any checks that could result in false, so try disabling the "if clause" as well, to see if it displays something then.
I am making a table from a database and want to add an Edit/delete button to each row update or delete a specific row from the database. I have successfully added working "delete" button but I have no idea how could I update data in table <td> in view and send it to controller.
Here is my code:
view file
<table class="table table-striped">
<tr>
<td>name</td>
<td>age</td>
<td>gender</td>
<td>class</td>
<td>roll no.</td>
</tr>
<?php foreach($record as $r): ?>
<tr>
<td><?php echo $r->name; ?></td>
<td><?php echo $r->age; ?></td>
<td><?php echo $r->gender; ?></td>
<td><?php echo $r->class; ?></td>
<td><?php echo $r->roll no; ?></td>
<td><a href="" >Edit</a>
<a href="<?php echo base_url()."student/deleteRow" ?id="$r->name">"
onclick="return confirm
('Are you sure to Delete?')"><i class="icon-trash"></a></td>
</tr>
<?php endforeach; ?>
</table>
Controller Function
public function deleteRow(){
if(isset($_GET['id'])){
$id=$this->input->get('id');
$this->student_model->rowDelete($id);
redirect($_SERVER['HTTP_REFERER']);
}
}
I don't know how can I now insert an input field to update table row without effecting previous view. Any suggestion would be helpful.
To Edit the Studen data you need to pass an id or uniue column name to the data base to get that student data.
First Set the student id in <a href=""> tag.
<td><a href="<?= base_url('student/edit_student') ?>/$r->id" >Edit</a>
Then When you click on the edit it will take you to the controller. You can get the third url parameter direct in as show in the controler code:
You can also use get as shon
Your Controller should be:
public function edit_student($id){
$student_data = $this->student_model->get_student_data($id);
$this->load->view('your_view',['student_data'=>$student_data)]);
}
Here is you model which get the id form controllr and find the student data and passit to back to the controller:
Your Model should be:
public function get_student_data($id){
$this->db->select('*');
$this->db->from('your_table_name');
$this->db->where('id',$id);
$query = $this->db->get();
$student_data = $query->$row_array();
if(isset($student_data) && !empty($student_data)){
return student_data;
} else {
return FALSE;
}
}
Form controller you pass the data to the view.
On View Side:
<?php
// Just to ensure the data. Comment it after testing
echo "<pre>";
print_r($student_data);
echo "</pre>";
?>
<form action="<?= base_url('student/update_student') ?>/<?= $student_data['id'] ?>">
<input name="your_column_name" value="<?= $student_data['your_column_name'] ?>">
// Try to do the same for all you column.
<input type="submit" value="updata">
</form>
Here is the controller for update the data
public function update_student($id){
$student_data = $this->input->post();
$status = $this->student_model->update_student($id,$student_data);
if($status == TRUE){
$this->load->view('your_view');
// Your Success view
} else {
// Your view if fail to update
$this->load->view('your_view');
}
}
Here is the model for update the data
public function get_student_data($id,$student_data){
$this->db->where('id',$id);
$this->db->update('your_table_name',$student_data);
if($this->db->affected_rows() == 1){
return TRUE;
} else {
return FALSE;
}
}
Very similar to what you have done for delete. Something like this:
<td>
<a href="" >Edit/Delete</a>
<!-- This should be another method in Student controller -->
<i class="icon-trash"><!-- I changed order of edit and delete -->
</td>
I need to warn you for CSRF. If you don't implement better security here, anyone pointing to that link would be able to edit or delete data.
Check Security class in documentation and how to set hidden value so that way you would ensure that only one who has already requested that page was able to edit/delete rows.
<td><a href="<?php echo base_url();?>controller_name/function_name/<?php echo $edit_id;?>" >Edit</a></td>
another way
<td><a href="<?php echo base_url();?>controller_name/function_name?id=<?php echo $edit_id;?>" >Edit</a></td>
You can use any one according to your requirement.
hye, i'm trying to do 'delete function' for my modul. i've tried so many different ways for this. but it come out no result, some come with error.
this is my controller (named expert.php) :
public function remove() {
$siri = $this->uri->segment(3);
$this->Kepakaran_m->delete($siri);
$this->view();
}
siri is the a field name (column name) in my table. it it primary key.
this is my model (named Kepakaran_m.php):
function delete($siri) {
$this->dbsmk->where('siri', $siri);
$this->dbsmk->delete('kexpt003pakar');
if ($this->dbsmk->affected_rows() == 1) {
return TRUE;
}
return FALSE;
}
kexpt003pakar is the table name.
so my view is this (named kepakaran.php):
<tbody>
<?php if(empty($kepakaran)) { ?>
<tr>
<td colspan="8">Pengguna tidak mempunyai rekod kepakaran!</td>
</tr>
<?php } else {
$num = 0;
foreach ($kepakaran as $list_kepakaran) {
$num++;
?>
<tr>
<td><?php echo $num; ?></td>
<td><?php echo $list_kepakaran->kategori; ?></td>
<td><?php echo $list_kepakaran->bidang; ?></td>
<td><?php echo $list_kepakaran->spesifik; ?></td>
<!-- untuk keluarkan tahap -->
<td><?php
if($list_kepakaran->tahap=='1'){
echo "Sederhana";
}elseif ($list_kepakaran->tahap=='2') {
echo "Tinggi";
}elseif ($list_kepakaran->tahap=='3') {
echo "Sangat Tinggi";
}
?>
</td>
<!-- done untuk keluarkan tahap -->
<td><?php echo $list_kepakaran->biltahun; ?></td>
<td>
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#tambahkepakaran"><i class="glyphicon glyphicon-pencil"></i></button>
<i class="glyphicon glyphicon-trash"></i>
</td>
</tr>
<?php }} ?>
</tbody>
so the delete button is in this area >>siri; ?>" type="button".....
i hope your answer will help me to discover this up. thank you :)
What's the error? I think you just need to modify your codes becomes:
<i class="glyphicon glyphicon-trash"></i>
Then it should be ok.
In order to use uri segment..do not forget to call url helper in autoload.php
english is not my native language.
For solve your problem, you can check a lot of things.
Did you forget to load model in your controller ?
Try to dump value of $siri in your remove function and show response on your network tab in chrome devTools for example.
If you don't call your controller, look at side of your href or open Application/config/routes.php for use routing and add this $route['expert/remove/(:num)']['GET'] = "expert/remove/$1";
If $siri is empty look at the side of $this->uri->segment(3);
Verify if you load correctly the helper.
For that, go to Application/config/autoload.php and see if url is in helper $autoload['helper'] = array('url','security','language', 'form', 'text');
trying to get my head around REST, I am following/copying a tutorial. The "$_get" is blank, I noticed that the URL that is being called is blank here is a copy,
http://localhost/RestClient/index.php?action=get_user&id=
but the href I am clicking looks ok to me.
<a href='http://localhost/RestClient/index.php?action=get_user&id='3' alt=user_'3'>Carbonnel</a>
here is my code I am new to PHP so figuring it all as I go!!!!
<?php
/*** this is the client ***/
if (isset($_GET["action"]) && isset($_GET["id"]) && $_GET["action"] == "get_user") // if the get parameter action is get_user and if the id is set, call the api to get the user information
{
$user_info = file_get_contents('http://localhost/RestServer/api.php?action=get_user&id=' . $_GET ["id"]);
$user_info = json_decode($user_info, true);
// THAT IS VERY QUICK AND DIRTY !!!!!
?>
<table>
<tr>
<td>Name: </td><td> <?php echo $user_info["last_name"] ?></td>
</tr>
<tr>
<td>First Name: </td><td> <?php echo $user_info["first_name"] ?></td>
</tr>
<tr>
<td>Age: </td><td> <?php echo $user_info["age"] ?></td>
</tr>
</table>
<a href="http://localhost/RestClient/index.php?action=get_userlist" >Return to the user list</a>
<?php
}
else // else take the user list
{
$user_list = file_get_contents('http://localhost/RestServer/api.php?action=get_user_list');
$user_list = json_decode($user_list, true);
// THAT IS VERY QUICK AND DIRTY !!!!!
?>
<ul>
<?php foreach ($user_list as $user): ?>
<li>
<?php echo "<a href='http://localhost/RestClient/index.php?action=get_user&id='".$user ['id']."' alt=user_'".$user['id']."'>"; ?><?php echo $user["name"] . "</a>"; ?>
</li>
<?php endforeach; ?>
</ul>
<?php
}
?>
The Link
<a href='http://localhost/RestClient/index.php?action=get_user&id='3' alt=user_'3'>Carbonnel</a>
is incorrect, it must be:
<a href='http://localhost/RestClient/index.php?action=get_user&id=3' alt='user_3'>Carbonnel</a>
Watch the changes in ' signs.
In you example $_GET['id'] must have been always null.
There is definitely something wrong with you <a>.
You are using single quotes for the tag attribute and then for query string parameters too.
Any program having to interpret that will have no idea where the href= actually ends.
One solution would be to use double quotes (") for the attribute and single quotes for the value (if you need those at all).
Change
<?php echo "<a href='http://localhost/RestClient/index.php?action=get_user&id='".$user ['id']."' alt=user_'".$user['id']."'>"; ?>
to
<?php echo "<a href='http://localhost/RestClient/index.php?action=get_user&id=".$user ['id']." alt=user_".$user['id']."'>"; ?>
I have a foreach loop in php. Now I need to print the content of a div for each record separately what I have now is the following:
<script type="text/javascript">
function printContent(div_id) {
var DocumentContainer = document.getElementById("<?php echo $route->flightnum;?>");
var html = '<html><head>' +
'</head><body style="background:#ffffff;">' + DocumentContainer.innerHTML +
'</body></html>';
var WindowObject = window.open("", "PrintWindow", "width=1000,height=1000,top=0,left=0,toolbars=no,scrollbars=yes,status=no,resizable=yes");
WindowObject.document.writeln(html);
WindowObject.document.close();
WindowObject.focus();
WindowObject.print();
//WindowObject.close();
document.getElementById('print_link').style.display = 'block';
}
</script>
Then I have the following div:
<div id="brief_print" style="margin-left: 15px">
.
.
.
.
.
</div>
And perhaps a link like this:
<a class="glow" style="text-decoration: underline;" href="javascript:printContent('<?php echo $route->flightnum;?>')"id='print_link'>Open in new window for printing</a>
My problem is that when I hit the link I don't get anything in return. Please tell me where I'm wrong.
Thanks
The entire codes:
<?php
foreach($allroutes as $route)
{
?>
<tr>
<td onclick="$('#details_<?php echo $route->flightnum;?>').toggle('slow')">P</td>
<td style="padding:5px 10px" ALIGN="center"><?php echo $route->depicao;?></td>
<td style="padding:5px 10px" ALIGN="center"><?php echo $route->arricao;?></td>
<td style="padding:5px 10px" ALIGN="center"><?php echo $route->aircraft; ?></td>
<td style="padding:5px 10px"ALIGN="center"><?php echo $route->distance;?></td>
</tr>
<tr >
<td colspan="14"><div align="center">
<table cellspacing="0" width="100%" id="details_<?php echo $route->flightnum;?>" style="display:none">
<tr><th style="padding:5px 10px"><div align="center">Flight Details</div></th></tr>
<tr><td>
<section>
<?php
if(!isset($_GET['newwindow']))
{
?>
<a class="glow" style="text-decoration: underline;" href="javascript:printContent('<?php echo $route->flightnum;?>')"id='print_link'>Open in new window for printing</a>
<script type="text/javascript">
function printContent(div_id)
{
var DocumentContainer = document.getElementById("<?php echo $route->flightnum;?>");
var html = '<html><head>'+'</head><body style="background:#ffffff;">'+ DocumentContainer.innerHTML+ '</body></html>';
var WindowObject = window.open("", "PrintWindow","width=1000,height=1000,top=0,left=0,toolbars=no,scrollbars=yes,status=no,resizable=yes");
WindowObject.document.writeln(html);
WindowObject.document.close();
WindowObject.focus();
WindowObject.print();
//WindowObject.close();
document.getElementById('print_link').style.display='block';
}
</script>
<?php
}
?>
<div id="brief_print" style="margin-left: 15px">
<pre>
<b>Departing</b> : <?php echo $schedule->depname ?>
<b>Arriving</b> : <?php echo $schedule->arrname ?>
<b>Equipment</b> : <?php echo $schedule->fullname;?>
<b>Alternate</b> : <?php echo $alternate1; ?>
<b>Alternate</b> : <?php echo $alternate2; ?>
<b>Alternate</b> : <?php echo $alternate3; ?>
</pre>
</div>
</section>
</td></tr>
</table>
</div>
<?php
}
?>
There are some big problems with your code. You don't seem to understand what is happening when and where. Let me try to explain.
When a client goes to a website, their browser sends a request for the page to your hosting server. Your host server executes the PHP code, producing the source code for the page to be sent to the client's browser. So the page is not sent back to the client's browser until after PHP has completely finished executing. No JavaScript code has executed at this point.
Once the page is sent to the browser (and PHP is done), that's when JavaScript takes over. JavaScript executes on the client-side.
So, once you understand this, you'll realize that you obviously cannot call PHP from JavaScript without making a new request to the server.
Try opening your page in a browser and view the source. You will see that you are defining the JS function printContent() once for every loop iteration. You are also missing closing tags for your second <tr> and <td> tags (add an extra </td></tr> after your last </div>). Perhaps you'll realize all the other problems when you view the source to see what your PHP code is producing.