I have two tables users and posts where is the primary key is id. When i click on delete button, i want to post the id.
Here is my view:
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">Title</th><th scope="col">Hit</th>
<th scope="col">Edit</th><th scope="col">Delete</th><th scope="col">Read More</th>
</tr>
</thead>
<tbody>
<?php foreach($posts as $post) : ?>
<?php if($this->session->userdata('username') == $_SESSION["username"]): ?>
<tr>
<td><?php echo $post['title']; ?> </td>
<td><?php echo $post['post_views']; ?></td>
<td><a class="btn btn-default" href="<?php echo base_url(); ?>posts/edit/<?php echo $post['slug']; ?>">Edit</a></td>
<td>
<?php echo form_open('/posts/delete/'.$post['id']); ?>
<input type="submit" value="Delete" class="btn btn-danger">
<input type="hidden" name="id" value="<?php echo $post['id'] ?>" />
</form>
</td>
<td><p><a class="btn btn-default" href="<?php echo site_url('/posts/'.$post['slug']); ?>">Read More</a></p></td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
</tbody>
</table>
There are so many ways to do this, the best most common one is to get it from the url like this:
$id = #end($this->uri->segment_array());
Or this:
$id = $this->uri->segment(3);
Or you can do this by passing it in a hidden input like this:
<?php echo form_open('/posts/delete/'.$post['id']); ?>
<input type="submit" value="Delete" class="btn btn-danger">
<input type="hidden" name="id" value="<?php echo $post['id'] ?>" />
</form>
Or using ajax.
delete link with confimation
Delete
call delete method in js and then show confimation msg.
Why you are using form tag to call a function? You can do this with anchor tag easily and can pass id. This one line code will work perfectly
<td><a class="btn btn-danger" href="<?= site_url('posts/delete/'.$post['id']) ?>">Delete</a></td>
In controller delete function will be like this
public function delete($id){
echo $id;
}
Related
I explain briefly I created a table that shows all my products on video, where inside the container that contains it are two select, enclosed in a form, which pass values in order to populate the table in my db, and everything works perfectly. The only thing though that when I select more than one product the db correctly registers only the last one selected from the checkbox,place my code in order to reach my goal, that of being able to insert more products(impianto_id_campagna),for the same customer id (cliente_id_campagna), and event id (id_campagna_cliente):
code:
<?php
$messaggio = "";
if (isset($_POST['submit'])) {
include '../connessione.php';
$id_campagna_cliente = $connessione->real_escape_string($_POST['id_campagna_cliente']);
$cliente_id_campagna = $connessione->real_escape_string($_POST['cliente_id_campagna']);
$impianto_id_campagna = $connessione->real_escape_string($_POST['impianto_id_campagna']);
$connessione->query("INSERT INTO campagne_cliente (
id_campagna_cliente,
cliente_id_campagna,
impianto_id_campagna)
VALUES (
'$id_campagna_cliente',
'$cliente_id_campagna',
'$impianto_id_campagna')");
$messaggio = "Registrazione Completata!";
}
?>
<main>
<?php
include '../connessione.php';
$query_string = "SELECT * FROM store_locator WHERE store_locator.id NOT IN (SELECT impianto_id_campagna FROM campagne_cliente)";
$query = mysqli_query($connessione, $query_string);
?>
<?php
include '../connessione.php';
$query_string = "SELECT * FROM clienti";
$clienti = mysqli_query($connessione, $query_string);
?>
<?php
include '../connessione.php';
$query_string = "SELECT * FROM campagne_cliente
INNER JOIN clienti
ON clienti.cliente_id = campagne_cliente.cliente_id_campagna
INNER JOIN campagne
ON campagne.id_campagna = campagne_cliente.id_campagna_cliente GROUP BY cognome";
$campagne = mysqli_query($connessione, $query_string);
?>
<!-- Datatables initialization -->
<script>
// Basic example
$(document).ready(function () {
$('#dtBasicExample').DataTable();
$('.dataTables_length').addClass('bs-select');
});
</script>
<!-- Structured data: Breadcrumbs -->
<form method="post" action="index.php">
<div class="container-fluid text-center">
<div class="row">
<div class="col-md-6">
<select name="cliente_id_campagna" class="ciao colorful-select dropdown-primary" multiple searchable="Cerca il Cliente">
<option value="" disabled selected>Cliente</option>
<?php
while($row = mysqli_fetch_assoc($clienti)){ ?>
<option value="<?php echo $row['cliente_id'] ;?>"><?php echo $row['nome'].' '.$row['cognome'] ;?></option>
<?php } ?>
</select>
<script type="text/javascript">
// Material Select Initialization
$(document).ready(function() {
$('.ciao').material_select();
});
</script>
</div>
<div class="col-md-6">
<select name="id_campagna_cliente" class="ok colorful-select dropdown-primary" multiple searchable="Cerca la campagna">
<option value="" disabled selected>Cliente</option>
<?php
while($row = mysqli_fetch_assoc($campagne)){ ?>
<option value="<?php echo $row['id_campagna_cliente'] ;?>"><?php echo $row['nome'].' '.$row['cognome'].' INIZIO['.$row['data_inizio'].'] FINE['.$row['data_fine'].']' ;?></option>
<?php } ?>
</select>
<script type="text/javascript">
// Material Select Initialization
$(document).ready(function() {
$('.ok').material_select();
});
</script>
</div>
</div>
<div class="col-md-12">
<?php if ($messaggio != "") echo $messaggio . "<br><br>"; ?>
<table id="dtBasicExample" class="table table-striped table-bordered table-sm" cellspacing="0" width="100%">
<thead>
<tr>
<th class="th-sm">ID
<i class="fa fa-sort float-right" aria-hidden="true"></i>
</th>
<th class="th-sm">Cimasa
<i class="fa fa-sort float-right" aria-hidden="true"></i>
</th>
<th class="th-sm">Proprietaria
<i class="fa fa-sort float-right" aria-hidden="true"></i>
</th>
<th class="th-sm">Concessionaria
<i class="fa fa-sort float-right" aria-hidden="true"></i>
</th>
<th class="th-sm">City
<i class="fa fa-sort float-right" aria-hidden="true"></i>
</th>
<th class="th-sm">Latitudine
<i class="fa fa-sort float-right" aria-hidden="true"></i>
</th>
<th class="th-sm">Longitudine
<i class="fa fa-sort float-right" aria-hidden="true"></i>
</th>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_assoc($query)){ ?>
<tr>
<td>
<!-- Material unchecked -->
<div class="form-check">
<input type="checkbox" name="impianto_id_campagna" class="form-check-input" value="<?php echo $row['id'] ;?>" id="<?php echo $row['id'] ;?>">
<label class="form-check-label" for="<?php echo $row['id'] ;?>"></label>
</div>
</td>
<td><?php echo $row['cimasa'] ;?></td>
<td><?php echo $row['proprietaria'] ;?></td>
<td><?php echo $row['concessionaria'] ;?></td>
<td><?php echo $row['city'] ;?></td>
<td><?php echo $row['lat'] ;?></td>
<td><?php echo $row['lng'] ;?></td>
</tr>
<?php } ?>
</tfoot>
</table>
<input class="btn btn-primary" name="submit" type="submit" value="Register..."><br>
</form>
</div>
</div>
It register only the last one selected product because you have the same name attribute for every checkbox. Set the name to impianto_id_campagna[] in order to return an array in your $_POST variable.
<input type="checkbox" name="impianto_id_campagna[]" class="form-check-input" value="<?php echo $row['id'] ;?>" id="<?php echo $row['id'] ;?>">
Then you loop all your checkbox values inserting one product at a time:
<?php
$id_campagna_cliente = $connessione->real_escape_string($_POST['id_campagna_cliente']);
$cliente_id_campagna = $connessione->real_escape_string($_POST['cliente_id_campagna']);
foreach ($_POST['impianto_id_campagna'] as $value)
{
$impianto_id_campagna = $connessione->real_escape_string($value);
$connessione->query("INSERT INTO campagne_cliente (
id_campagna_cliente,
cliente_id_campagna,
impianto_id_campagna)
VALUES (
'$id_campagna_cliente',
'$cliente_id_campagna',
'$impianto_id_campagna')");
$messaggio = "Registrazione Completata!";
}
Checkbox items are passed as an array. In order to get them in to the database, you have to loop through the array and act accordingly.
foreach($impianto_id_campagna as $row){
//Iterate through and do what you need to do with the data.
}
EDIT: Solved by adding modal inside loop
Can someone explain to me how to access the foreach loop variable outside foreach?
<?php foreach($sup as $data){ ?>
<tr>
<td><i class="circular blue lock icon"></i><?php echo $data['id']; ?></td>
<td><div class="ui large green label">
Solved!
</div></td>
<td><?php echo $data['subject']; ?></td>
<td><?php echo $data['text']; ?></td>
<td><?php echo $data['date']; ?></td>
<td>
<div class="ui buttons">
<input type="hidden" name="id" value="<?php echo $data['id']; ?>" />
<input type="submit" name="cancel" value="Lock" class="ui button" tabindex="5">
<div class="or"></div>
<button id="sup" class="ui right labeled icon positive button">
Reply
</button>
</div>
</td>
</tr>
<?php } ?>
For example, I want to use $data['text'] in my modal.
Add the data attribute to your button:
<button data-text="<?php echo $data['text']; ?>" class="ui right labeled icon positive button">
Then, in your modal you can access it with Javascript, like so:
var text = document.getElementById('sup').dataset.text
Also, you can't use an unique id inside a foreach loop, otherwise it will be replicated along with the rest of the code. Remove the "id" field from your button.
I need to fetch one row data from array using PHP. Here is my code:
<?php
if($bstatus==1) {
$bikeArr=$data['data'];
$count=1;
foreach ($bikeArr as $v) {
?>
<tr>
<td><?php echo $count++; ?></td>
<td><?php echo $v['book_date']; ?>, <?php echo $v['book_time']; ?></td>
<td><?php echo $v['book_id']; ?></td>
<td><?php echo $v['start_date']; ?>, <?php echo $v['start_time']; ?> /<br /> <?php echo $v['end_date']; ?>, <?php echo $v['end_time']; ?></td>
<td><?php echo $v['service_name']; ?> / <br /> <?php echo $v['model_name']; ?></td>
<td><?php echo $v['booking_status_str']; ?></td>
<td><a class="btn btn-xs btn-success" data-toggle="modal" data-target="#takemebookingsec">View Booking <i class="fa fa-arrow-circle-right"></i></a></td>
</tr>
<?php
}
}
?>
Here I need while user will click on View Booking button the exact same row index data will fetch from array and push into other variable.
You can parse the value of the primary key on the view booking button as in
<?php
echo $v['whatever_your_primary_key_is'];
?>
And then on the modal, get that id and do a query to get the current details You will need to parse these details as values of course on your html form in the modal view
To avoid using the database again, just echo the value of primary. But make sure you are still in the same loop to be able to get the current values. SO your modal must also be in the same loop
Try:
<a class="btn btn-xs btn-success" data-toggle="modal" data-target="#takemebookingsec<?= $v['your_primary_key_goes_here']; ?>">View Booking <i class="fa fa-arrow-circle-right"></i></a>
On your modal, still in the same loop,
<div class="modal fade" id="takemebookingsec<?=$v['your_primary_key_goes_here']; ?>">
<form>
<input type="text" name="name" value="<?= $v['what_ever_name']; ?>">
<!--Add other input here-->
</form>
</div>
I have a gallery where there are images and next to the images are checkboxes. When for exmaple user click 3 of checkboxex and a submit button i would like to save these 3 images in session to show them in the other gallery (of chose images). Now my code looks like this:
<?php if ($images->count()): ?>
<?php foreach ($images as $image): ?>
<tr>
<td><?= $image['title'] ?></td>
<td><?= $image['author'] ?></td>
<td class="image">
<a href="static/images/<?= $image['file_name'] ?>">
<img class="gallery" src='static/images/<?= $image['file_name']?>'>
</a>
</td>
<td>
<form action="gallery" method="post" class="wide"/>
<input type="hidden" name="id" value="<?= $image['_id'] ?>"/>
<input type="checkbox" name="ckeckbox"/>
<input type="submit" name="gallery" value="Zapamiętaj"/>
</form>
</td>
</tr>
<?php endforeach ?>
<?php endif ?>
It works but the problem is, that all images have own buttons and I would like to have one universal button to accept all clicked checkboxes. How can I do this?
You have to move your form outside of the foreach loop. I also suggest you rename your checkbox input (hidden input is not usefull in this case but you can keep it if you want):
<?php if ($images->count()): ?>
<form action="gallery" method="post" class="wide"/>
<?php foreach ($images as $image): ?>
<tr>
<td><?= $image['title'] ?></td>
<td><?= $image['author'] ?></td>
<td class="image">
<a href="static/images/<?= $image['file_name'] ?>">
<img class="gallery" src='static/images/<?= $image['file_name']?>'>
</a>
</td>
<td>
<input type="checkbox" name="<?= $image['_id'] ?>"/>
</td>
</tr>
<?php endforeach ?>
<input type="submit" name="gallery" value="Zapamiętaj"/>
</form>
<?php endif ?>
<input type="hidden" name="id**[]**" value="<?= $image['_id'] ?>"/>
If you change name of the input to "id[]", it means it will be an array named "id". In the PHP script gallery (form action), where you add it to session, you can do it like this:
<?php
foreach($_POST["id"] as $id){
$_SESSION["selectedphotos"][] = $id;
}
?>
EDIT: And you have to move form before foreach...
<?php if ($images->count()): ?>
<form action="gallery" method="post" class="wide"/>
<?php foreach ($images as $image): ?>
<tr>
<td><?= $image['title'] ?></td>
<td><?= $image['author'] ?></td>
<td class="image">
<a href="static/images/<?= $image['file_name'] ?>">
<img class="gallery" src='static/images/<?= $image['file_name']?>'>
</a>
</td>
<td>
<input type="hidden" name="id" value="<?= $image['_id'] ?>"/>
<input type="checkbox" name="ckeckbox"/>
<input type="submit" name="gallery" value="Zapamiętaj"/>
</td>
</tr>
<?php endforeach ?>
</form>
<?php endif ?>
I have a query that selects all the information from a database table and puts it into an array. I then use a PHP foreach statement to display all that in a uniform manner. It's the left table here to get a sense of what I'm talking about.
What I want to do is to make one of the divs (it normally just appears repeatedly under the same name) to have a unique name for each sumbission row. For example, instead of the "response" divs all just being called response, they are "response1", "response2", and so on. Is there any way to do this? (code below)
Any help would be greatly appreciated.
Here's where I call the info from the query:
<?php foreach($images as $image) { ?>
<table id="front_pgs">
<tr>
<td id="front_text">
<div id="imagetitle">
<?php echo $image['name'];?>
</div>
<div id="submission_info">
submitted by <?php echo $image['submitter'];?>
</div>
<div id="ratingcontainer">
<form id="ratingform">
<input name="vote" type="button" onclick="getVote('<?php echo $image['filename'];?>')" value='Like' id="likebutton"/>
<input name="dislike" type="button" value='Disike' id="dislikebutton"/>
</form>
<div id="rate_count">
<div id="response">
<?php echo $image['rating'];?>
</div>
</div>
</div>
</td>
<td id="front_pg_img" valign="center" align="center">
<a onClick="switchImageUrl('<?php echo $image['filename']; ?>', '<?php echo $image['width']; ?>', '<?php echo $image['height']; ?>')"><img src="<?php echo $image['filename'];?>" id="front_pg_thumbnail"/></a>
</td>
</tr>
</table>
<?php } ?>
You can do this by two ways I will show you now
1- add the row id if exists to the id value or any unique column
<div id="response<?php echo $image['id']; ?>">
<?php echo $image['rating'];?>
</div>
2- make a counter
<?php
$i= 1;
foreach($images as $image) { ?>
<table id="front_pgs">
<tr>
<td id="front_text">
<div id="imagetitle">
<?php echo $image['name'];?>
</div>
<div id="submission_info">
submitted by <?php echo $image['submitter'];?>
</div>
<div id="ratingcontainer">
<form id="ratingform">
<input name="vote" type="button" onclick="getVote('<?php echo $image['filename'];?>')" value='Like' id="likebutton"/>
<input name="dislike" type="button" value='Disike' id="dislikebutton"/>
</form>
<div id="rate_count">
<div id="response<?php echo $i; ?>">
<?php echo $image['rating'];?>
</div>
</div>
</div>
</td>
<td id="front_pg_img" valign="center" align="center">
<a onClick="switchImageUrl('<?php echo $image['filename']; ?>', '<?php echo $image['width']; ?>', '<?php echo $image['height']; ?>')"><img src="<?php echo $image['filename'];?>" id="front_pg_thumbnail"/></a>
</td>
</tr>
</table>
<?php
$i++; //increment the $i each iteration
} ?>
<?php $i = 1; foreach($images as $image) { ?>
<table id="front_pgs">
<tr>
<td id="front_text">
<div id="imagetitle">
<?php echo $image['name'];?>
</div>
<div id="submission_info">
submitted by <?php echo $image['submitter'];?>
</div>
<div id="ratingcontainer">
<form id="ratingform">
<input name="vote" type="button" onclick="getVote('<?php echo $image['filename'];?>')" value='Like' id="likebutton"/>
<input name="dislike" type="button" value='Disike' id="dislikebutton"/>
</form>
<div id="rate_count">
<div id="response<?php echo $i; ?>">
<?php echo $image['rating'];?>
</div>
</div>
</div>
</td>
<td id="front_pg_img" valign="center" align="center">
<a onClick="switchImageUrl('<?php echo $image['filename']; ?>', '<?php echo $image['width']; ?>', '<?php echo $image['height']; ?>')"><img src="<?php echo $image['filename'];?>" id="front_pg_thumbnail"/></a>
</td>
</tr>
</table>
<?php $i ++; } ?>
Notice the $i = 1 before the foreach as well as the $i ++ before the closing }. Also, echo $i in the response div id.