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 ?>
Related
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;
}
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 am translating a review form. There are some phrases inside form which are predefined in Mage_Review.csv.
No problem so far. But there are two words Overall and Quality inside review form that I can't find their English source inside Mage_Review.csv. I even searched inside Mage_Rating.csv.
This is the form.phtml content. Please take a look:
<div class="form-add">
<h2><?php echo $this->__('Write Your Own Review') ?></h2>
<?php if ($this->getAllowWriteReviewFlag()): ?>
<form action="<?php echo $this->getAction() ?>" method="post" id="review-form">
<fieldset>
<?php echo $this->getChildHtml('form_fields_before')?>
<h3><?php echo $this->__("You're reviewing:"); ?> <span><?php echo $this->htmlEscape($this->getProductInfo()->getName()) ?></span></h3>
<ul class="form-list">
<li>
<label for="nickname_field" class="required"><em>*</em><?php echo $this->__('Nickname') ?></label>
<div class="input-box">
<input type="text" name="nickname" id="nickname_field" class="input-text required-entry" value="<?php echo $this->htmlEscape($data->getNickname()) ?>" />
</div>
</li>
<li>
<label for="summary_field" class="required"><em>*</em><?php echo $this->__('Summary of Your Review') ?></label>
<div class="input-box">
<input type="text" name="title" id="summary_field" class="input-text required-entry" value="<?php echo $this->htmlEscape($data->getTitle()) ?>" />
</div>
</li>
<li>
<label for="review_field" class="required"><em>*</em><?php echo $this->__('Review') ?></label>
<div class="input-box">
<textarea name="detail" id="review_field" cols="5" rows="3" class="required-entry"><?php echo $this->htmlEscape($data->getDetail()) ?></textarea>
</div>
</li>
</ul>
<?php if( $this->getRatings() && $this->getRatings()->getSize()): ?>
<h4><?php echo $this->__('How do you rate this product?') ?> <em class="required">*</em></h4>
<span id="input-message-box"></span>
<table class="data-table" id="product-review-table">
<col />
<col width="1" />
<col width="1" />
<col width="1" />
<col width="1" />
<col width="1" />
<thead>
<tr>
<th> </th>
<th><span class="nobr"><?php echo $this->__('1 star') ?></span></th>
<th><span class="nobr"><?php echo $this->__('2 stars') ?></span></th>
<th><span class="nobr"><?php echo $this->__('3 stars') ?></span></th>
<th><span class="nobr"><?php echo $this->__('4 stars') ?></span></th>
<th><span class="nobr"><?php echo $this->__('5 stars') ?></span></th>
</tr>
</thead>
<tbody>
<?php foreach ($this->getRatings() as $_rating): ?>
<tr>
<th><?php echo $this->escapeHtml($_rating->getRatingCode()) ?></th>
<?php foreach ($_rating->getOptions() as $_option): ?>
<td class="value"><input type="radio" name="ratings[<?php echo $_rating->getId() ?>]" id="<?php echo $this->escapeHtml($_rating->getRatingCode()) ?>_<?php echo $_option->getValue() ?>" value="<?php echo $_option->getId() ?>" class="radio" /></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<input type="hidden" name="validate_rating" class="validate-rating" value="" />
<script type="text/javascript">decorateTable('product-review-table')</script>
<?php endif; ?>
</fieldset>
<div class="buttons-set">
<button type="submit" title="<?php echo $this->__('Submit Review') ?>" class="button"><span><span><?php echo $this->__('Submit Review') ?></span></span></button>
</div>
</form>
<script type="text/javascript">
//<![CDATA[
var dataForm = new VarienForm('review-form');
Validation.addAllThese(
[
['validate-rating', '<?php echo $this->__('Please select one of each of the ratings above') ?>', function(v) {
var trs = $('product-review-table').select('tr');
var inputs;
var error = 1;
for( var j=0; j < trs.length; j++ ) {
var tr = trs[j];
if( j > 0 ) {
inputs = tr.select('input');
for( i in inputs ) {
if( inputs[i].checked == true ) {
error = 0;
}
}
if( error == 1 ) {
return false;
} else {
error = 1;
}
}
}
return true;
}]
]
);
//]]>
</script>
<?php else: ?>
<p class="review-nologged" id="review-form">
<?php echo $this->__('Only registered users can write reviews. Please, log in or register', $this->getLoginLink(), Mage::helper('customer')->getRegisterUrl()) ?>
</p>
<?php endif ?>
You can translate Rating labels in Catalog > Reviews & Ratings > Manage ratings
What Sergey says is correct. You can translate it there, but if you want to do it via the translation files you will have to fix a minor bug in Magento's review from template. The labels in the review form template aren't passed trough the translation module
find this template:
app/design/frontend/[yourtheme]/[yourskin]/template/review/form.phtml
At line 58 (Magento 1.9) you will find this code:
<th><?php echo $this->escapeHtml($_rating->getRatingCode()) ?></th>
You will have to add the translation parser ($this->__()) to the code like so:
<th><?php echo $this->__($this->escapeHtml($_rating->getRatingCode())) ?></th>
After you do this you will be able to translate the labels via the translation files
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 ?>
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.