Access variable outside foreach loop - php

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.

Related

Post the id on delete in codeigniter

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;
}

How to get same row data when click on button using PHP?

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>

PHP, checkboxes

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 ?>

WP Alchemy: Attempting to echo index

I'm attempting to show users the current index of the slide they are on. Using WP Alchemy, I can get all the labels and inputs to display their names and values correctly. However, when trying to use the_index(), I can't get the correct index to echo after adding more than 2 slides.
I've removed all my javascript to ensure it wasn't something that was conflicting with jQuery's .sortable(). After the second slide, all names and indexes outside a label or input is: _homepage_slider[slides_2][1][handle] or 1. Anything I need to do, or will it only work in inputs and labels?
<?php global $wpalchemy_media_access; ?>
<div class="my_meta_control">
<?php while($mb->have_fields_and_multi('slides_2')): ?>
<?php $mb->the_group_open(); ?>
<table class="meta-field sortable table" cellspacing="0">
<tr>
<td width="30" class="handle"><?php $mb->the_index() //Only echoes '1' after the second iteration ?></td>
<td width="280" class="imagefield">
<?php $mb->the_field('imgurl'); ?>
<?php $wpalchemy_media_access->setGroupName('img-n'. $mb->get_the_index())->setInsertButtonLabel('Insert'); ?>
<img data-image="mediafield-img-n<?php $mb->the_index() ?>" src="<?php bloginfo('template_url') ?>/framework/images/cm-no-image.jpg" />
<div class="media-field-input hide">
<?php echo $wpalchemy_media_access->getField(array('name' => $mb->get_the_name(), 'value' => $mb->get_the_value())); ?>
</div>
<div class="add-media-btn">
<?php echo $wpalchemy_media_access->getButton(); ?>
</div>
</td>
<td class="last-col">
<?php $mb->the_field('title'); ?>
<label for="<?php $mb->the_name(); ?>">Title</label>
<div><input type="text" id="<?php $mb->the_name(); ?>" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>"/></div>
<?php $mb->the_field('handle'); ?>
<p>Name: <?php $mb->the_name(); //Only echoes _homepage_slider[slides_2][1][handle] after second iteration ?></p>
<p>Index: <?php $mb->the_index(); //Only echoes '1' after second iteration ?></p>
Remove Slide
</td>
</tr>
</table><!-- /.meta-field -->
<?php $mb->the_group_close(); ?>
<?php endwhile; ?>
<p style="margin-bottom:15px; padding-top:5px;">Add</p>
</div>
For any of you wondering, WP Alchemy doesn't work this way. You can add field identifiers to the name, class, id, or value attributes of an element. When echoing the index, it needs a "n" in front of it.
For example:
<?php $mb->the_field('handle'); ?>
<input type="hidden" class="anything-i-want-n<?php $mb->the_index() //This will work ?>" name="<?php $name = $mb->get_the_name() ?>" />
<?php echo $mb->the_index() //This will not ?>

Using a PHP foreach - how to create individual divs per each row?

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.

Categories