I have created the following wordpress page template:
<?php
/* Template Name: Report Creator */
get_header();
wp_enqueue_style( 'wp-admin' );
?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
?>
<style>
.table td{
padding-left:0;
}
</style>
<!-- ############################### -->
<?php
if(isset($_POST['function-action'])){
$function_action = $_POST['function-action'];
switch($function_action){
case 'form-edit-profile':
break;
case 'form-delete-profile':
wp_delete_post(); //pass ID over here to delete the post
break;
default:
break;
}
}
?>
<table>
<tr>
<th>Titel</th>
<th>Creation Date</th>
<th>Next fetch time</th>
<th># of Recipience</th>
<th>Functions</th>
</tr>
<?php
if ( is_user_logged_in() ):
global $current_user;
$author_query = array('post_status' => array( 'draft' ),
'author' => $current_user->ID,
'meta_query' => array(
array(
'key' => 'wpgamail_options',
'key' => 'ganalytics_settings',
),
),);
$author_posts = new WP_Query($author_query);
while($author_posts->have_posts()) : $author_posts->the_post();
?>
<tr>
<td>
<?php the_title(); ?>
</td>
<td>
<?php $post_date = get_the_date(); echo $post_date; ?>
</td>
<td>
<?php $field = get_field('wpgamail_options', get_the_ID(), true); echo "Next report on " . date('l jS \of F Y h:i:s A', $field['nextfetchtime']); ?>
</td>
<td>
<?php echo count($field['recipients']) . " Recipient"; ?>
</td>
<td>
<form method="post">
<input type="hidden" name="function-action" value="form-edit-profile"/>
<input type="submit" value="Edit Profile" class="button button-primary" />
</form>
</td>
<td>
<form method="post">
<input type="hidden" name="function-action" value="form-delete-profile"/>
<input type="submit" value="Delete Profile" class="button button-primary" onclick="return confirm('Do you really want to delete the report-settings?')" />
</form>
</td>
</tr>
<?php
endwhile;
else :
echo "You are not logged in. Please log in!";
endif;
?>
</table>
<hr/>
</main>
<!-- .site-main -->
</div>
<!-- .content-area -->
<?php get_footer(); ?>
My problem is that I do not know how to pass the id of the currently selected post to the switch-caseconstruct in order to delete / edit the post.
Any suggestions how to do that?
I appreciate your replies!
Try this.
Your HTML
<form method="post">
<input type="hidden" name="function-action" value="form-delete-profile" />
<input type="hidden" name="this_id" value="<?php echo get_the_ID(); ?>" />
<input type="submit" value="Delete Profile" class="button button-primary" onclick="return confirm('Do you really want to delete the report-settings?')" />
</form>
Your PHP
<?php
if(isset($_POST['function-action'])){
$function_action = $_POST['function-action'];
$this_id = (int)$_POST['this_id'];
$post_author = get_post_field( 'post_author', $this_id ); //Get post's author
switch($function_action){
case 'form-edit-profile':
break;
case 'form-delete-profile':
if( $post_author == get_current_user_id() ) { //This condition is to check that currect is the author of this post
wp_delete_post( $this_id ); //pass ID over here to delete the post
} else{
echo "You don't have permission to delete it.!";
}
break;
default:
break;
}
}
?>
Good luck
Related
I am trying to create a Favourites Pages where the user selects items from the "menu" and then the item is placed in the Favourites Page. I am making use of a session variable
$_SESSION['favourites']
I am starting the session by using the
session_start('session.php');
In the session_start it includes
<?php
session_start();
?>
The favourite feature works when I combine the two files together, but it does not work when I display the favourites on a different page.
This is the menu page
$conn = mysqli_connect("localhost", "root", "", "restaurant");
$sql= "SELECT * FROM menu ORDER BY id ASC";
$result=mysqli_query($conn, $sql);
$resultcheck=mysqli_num_rows($result);
if(isset($_POST["add_to_favourite"]))
{
if(isset($_POST["favourites"]))
{
$food_item_id = array_column($_SESSION["favourites"], "item_id");
if(!in_array($_GET["id"], $food_item_id))
{
$count = count($_SESSION["favourites"]);
$item_array = array(
'item_id' => $_GET["id"],
'item_name' => $_POST["hidden_name"],
'item_price' => $_POST["hidden_price"],
'item_desc' => $_POST["hidden_desc"]
);
$_SESSION["favourites"][$count] = $item_array;
}else{
echo '<script>alert(You have already added this item to Favourites")</script>';
}
}
else{
$food_array = array(
'item_id' => $_GET["id"],
'item_name' => $_POST["hidden_name"],
'item_price' => $_POST["hidden_price"],
'item_desc' => $_POST["hidden_desc"]
);
$_SESSION["favourites"][0] = $food_array;
}
}
?>
<!-- Start of HTML -->
<section id="intro">
<div class="hero-global container">
<h1 class="global-header">Detailed Menu</h1>
</div>
</section>
<div class="container">
<ul class="food-cards">
<?php
if($resultcheck>0)
{
while($row=mysqli_fetch_assoc($result))
{
?>
<li class="card-info">
<form method="post" action="favourites.php?action=add&id=<?php echo $row["id"]; ?>">
<div class="food-card">
<div class="food-pic">
<img src="./assets/english-breakfast.png" alt="">
</div>
<div class="food-cont">
<h2 class="food-title">
<?php
echo $row['name']
?>
</h2>
<p class="food-desc">
<?php echo $row["description"]; ?>
</p>
<div class="price-fav-btn">
<div class="price">
<?php
echo "€".$row['price']
?>
</div>
<div class="atf-btn">
<input class="fav-btn" type="submit" name="add_to_favourite" value="Add to Favourites"/>
</div>
<input type="hidden" name="hidden_name" value="<?php echo $row["name"]; ?>" />
<input type="hidden" name="hidden_price" value="<?php echo $row["price"]; ?>" />
<input type="hidden" name="hidden_desc" value="<?php echo $row["description"]; ?>" />
</div>
</div>
</div>
</form>
</li>
<?php
}
}
?>
</ul> <!-- end of ul list -->
</div>
This is the favourites page, where the items are displayed.
<?php
if(isset($_GET["action"]))
{
if($_GET["action"] == "delete")
{
foreach($_SESSION["favourites"] as $keys => $values)
{
if($values["item_id"] == $_GET["id"])
{
unset($_SESSION["favourites"][$keys]);
echo '<script>alert("Item Removed")</script>';
echo '<script>window.location="menudetails.php"</script>';
}
}
}
}
?>
<div><h1>Favourites</h1></div>
<table>
<tr style="text-align: left">
<th width="30%">Name</th>
<th width="60%">Description</th>
<th width="5%">Price</th>
<th width="5%">Remove</th>
</tr>
<?php
if(!empty($_SESSION["favourites"]))
{
foreach($_SESSION["favourites"] as $key => $value)
{
?>
<tr>
<td width: 20%><?php echo $value["item_name"] ?></td>
<td style="text-align: left"><?php echo $value["item_desc"]?></td>
<td><?php echo '€'.$value["item_price"]?></td>
<td><span class="fav-btn">Remove</span></td>
</tr>
<tr>
</table>
<?php
}
}
?>
EDIT:
I am starting the session on both pages.
Also, the part that is not working is when it comes to seeing whats in the favourites page. There is not output, only the link (in search bar) with the respective id.
Output
I'm building a Wordpress plugin with a settings page in the admin dashboard.
I have several inputs including text fields, checkbox and select dropdowns.
I save the data with a <form> to options.php, as is recommended by Wordpress.
The checkbox and text field values successfully get saved to the database on submit, but the <select> element (the selected option) does not. I can't understand why.
This is my code:
<?php
//Create menu link
function awb_options_menu_link() {
$awb_options_page = add_options_page(
'Add WhatsApp Button Options', // title
'Add WhatsApp Button', // title of the menu link
'manage_options', // capabilities credentials, at least able to X
'awb-options', // menu URL slug
'awb_options_content' // name of the function that displays the option page content
);
}
function awb_validate_inputs( $input ) {
// Create our array for storing the validated options
$output = array();
// Loop through each of the incoming options
foreach( $input as $key => $value ) {
// Check to see if the current option has a value. If so, process it.
if( isset( $input[$key] ) ) {
// Strip all HTML and PHP tags and properly handle quoted strings
$output[$key] = strip_tags( stripslashes( $input[ $key ] ) );
} // end if
} // end foreach
// Return the array processing any additional functions filtered by this action
return apply_filters( 'awb_validate_inputs', $output, $input );
}
// Create a select dropdown form input for awb_settings[distance_from_bottom_mu]
function distance_from_bottom_mu_dropdown_fn() {
$items = array("px", "%");
$option_with_default = isset( $awb_settings['distance_from_bottom_mu'] ) ? $awb_settings['distance_from_bottom_mu'] : '%';
echo "<select id=\"awb_settings[distance_from_bottom_mu]\" name=\"awb_settings[distance_from_bottom_mu]\" style=\"vertical-align: baseline;\">";
foreach($items as $item) {
$selected = ($option_with_default == $item) ? 'selected="selected"' : '';
echo "<option value=\"$item\" $selected>$item</option>";
}
echo "</select>";
}
// Create options page content
function awb_options_content() {
// Init Options Global
global $awb_options;
// Create default button text
$button_text = isset( $awb_options['button_text'] ) ? sanitize_text_field( $awb_options['button_text'] ) : _e('Send us a WhatsApp', 'add-whatsapp-button');
$awb_location_values = array('right', 'left');
ob_start(); ?>
<div class="wrap">
<h2><?php _e('Add WhatsApp Button Settings', 'add-whatsapp-button') ?></h2>
<p><?php _e('Settings page for the Add WhatsApp Button plugin. Check out the preview screen below to see how your button would look on a smartphone before saving your settings to the database.', 'add-whatsapp-button') ?></p>
<form method="POST" action="options.php">
<?php settings_fields('awb_settings_group'); ?>
<table class="form-table">
<tbody>
<tr>
<th scope="row"><label for="awb_settings[enable]"><?php _e('Enable WhatsApp Button', 'add-whatsapp-button') ?></label></th>
<td><input name="awb_settings[enable]" type="checkbox" id="awb_settings[enable]" value="1" <?php checked('1', $awb_options['enable']); ?>></td>
</tr>
<tr>
<th scope="row"><label for="awb_settings[button_text]"><?php _e('Button Text', 'add-whatsapp-button') ?></label></th>
<td>
<input name="awb_settings[button_text]" type="text" id="awb_settings[button_text]" value="<?php echo $button_text; ?>" class="regular-text">
<p class="description"><?php _e('Enter the text you want the button to show. Recommended: up to 18 characters.', 'add-whatsapp-button'); ?></p>
</td>
</tr>
<tr>
<th scope="row"><label for="awb_settings[breakpoint]"><?php _e('Breakpoint', 'add-whatsapp-button') ?></label></th>
<td>
<input name="awb_settings[enable_breakpoint]" type="checkbox" id="awb_settings[enable_breakpoint]" value="1" <?php checked('1', $awb_options['enable_breakpoint']); ?>>
<p class="description"><?php _e('Check this box in order to only display the WhatsApp button up to a certain screen width.', 'add-whatsapp-button'); ?></p>
<div id="awb_breakpoint" class="bp-no-show">
<input name="awb_settings[breakpoint]" type="number" id="awb_settings[breakpoint]" value="<?php echo sanitize_text_field( $awb_options['breakpoint'] ); ?>" class="small-text"><?php _e('px', 'add-whatsapp-button'); ?>
<p class="description"><?php _e('Enter your desired screen width breakpoint here. Default is 600px.', 'add-whatsapp-button'); ?></p>
</div>
</td>
</tr>
<tr>
<th scope="row"><label for="awb_settings[button_bg_color]"><?php _e('Button Background Color', 'add-whatsapp-button') ?></label></th>
<td>
<!-- <input name="awb_settings[button_bg_color]" type="text" id="awb_settings[button_bg_color]" value="<?php //echo $awb_options['button_bg_color']; ?>" class="regular-text" /> -->
<input name="awb_settings[button_bg_color]" type="text" id="awb_settings[button_bg_color]" value="<?php echo sanitize_text_field( $awb_options['button_bg_color'] ); ?>" class="udi-color-picker" />
<p class="description"><?php _e('Choose a background color for your button. Default is green (#20B038)', 'add-whatsapp-button'); ?></p>
</td>
</tr>
<tr>
<th scope="row"><label for="awb_settings[distance_from_bottom]"><?php _e('Button Distance from Bottom', 'add-whatsapp-button') ?></label></th>
<td>
<input name="awb_settings[distance_from_bottom]" type="number" id="awb_settings[distance_from_bottom]" value="<?php echo sanitize_text_field( $awb_options['distance_from_bottom'] ); ?>" class="small-text" />
<?php distance_from_bottom_mu_dropdown_fn(); ?>
<p class="description"><?php _e('Choose your button\'s Distance from the bottom of the screen, in percentages or pixels. Default is 10%.', 'add-whatsapp-button'); ?></p>
</td>
</tr>
<th scope="row"><label for="awb_settings[button_location]"><?php _e('Button Location on Screen', 'add-whatsapp-button') ?></label></th>
<td>
<select id="awb_settings[button_location]" name="awb_settings[button_location]" style="vertical-align: baseline;">
<option value="right" <?php selected( $awb_settings['button_location'], 'right' ); ?>>right</option>
<option value="left" <?php selected( $awb_settings['button_location'], 'left' ); ?>>left</option>
</select>
<p class="description"><?php _e('Choose whether your button will appear on the left side or right side of the screen', 'add-whatsapp-button'); ?></p>
</td>
</tr>
</tbody>
</table>
<h2><?php _e('Button Preview', 'add-whatsapp-button'); ?></h2>
<div class="device-wrapper"> <!-- Mockup Container -->
<div class="device" data-device="iPhone7" data-orientation="portrait" data-color="black">
<div class="screen">
<div id="admin-wab-cont" class="wab-cont"> <!-- Button Preview HTML -->
<a id="whatsAppButton" href="https://api.whatsapp.com/send?phone=972544480070&text=%D7%94%D7%99%D7%99%2C%20%D7%90%D7%A4%D7%A9%D7%A8%20%D7%9C%D7%A9%D7%9E%D7%95%D7%A2%20%D7%A4%D7%A8%D7%98%D7%99%D7%9D%20%D7%A2%D7%9C%20%D7%91%D7%A8%D7%99%D7%AA%20%D7%9E%D7%99%D7%9C%D7%94%2F%D7%98%D7%99%D7%A4%D7%95%D7%9C%20%D7%91%D7%9C%D7%A9%D7%95%D7%9F%20%D7%A7%D7%A9%D7%95%D7%A8%D7%94%3F" target="_blank"><?php echo $button_text; ?> <!--<img src="<?php //echo plugins_url( '../img/wai.svg', __FILE__ ) ?>" style="max-width: 20px; display: inline;" />--></a>
</div>
</div> <!-- /screen -->
<div class="button">
<!-- You can hook the "home button" to some JavaScript events or just remove it -->
</div>
</div> <!-- /device -->
</div> <!-- /device-wrapper -->
<p class="submit"><input type="submit" name="submit" id="submit" class="button button-primary" value="<?php _e('Save Changes', 'add-whatsapp-button') ?>"></p>
</form>
</div>
<script>
console.log(<?php echo $awb_settings['button_location'] ?>);
</script>
<?php
echo ob_get_clean();
}
add_action('admin_menu', 'awb_options_menu_link');
//register plugin settings
function awb_register_settings() {
register_setting('awb_settings_group', 'awb_settings', 'awb_validate_inputs');
}
add_action('admin_init', 'awb_register_settings');
Anyone have any idea why the <select> option value does not save to the database?
I printed out the $awb_options array with print_r, and it turns out the value is indeed saved into the database correctly, but the saved value wouldn't display properly.
After re-examining my code I noticed I mixed up $awb_settings (which I used to post the settings to the DB) and $awb_options, which is the variable in the wp_options table that actually holds the array of saved settings. I accidentally called $awb_settings['button_location'] and not $awb_options['button_location'] in the input value. That was my mistake... A stupid one I would agree.
I have a cart plugin for Wordpress. It uses the_excerpt to show the product description. it works on all pages except the checkout page. On the checkout page it shows the same description for all the products. The description it shows is always from the first product in the database even if that product is not in the cart. I have tried many variations nothing fixes it. it pulls the correct id and price for the product but just the description is always the same for al products.
<?php the_excerpt(get_the_ID()); ?>
<input id="item-<?php echo $item->ID; ?>-price" name="item-<?php echo $item->ID; ?>-price" type="hidden" value="<?php echo intval($price); ?>"/>
In the code above, the price pulls correctly but the_excerpt is same for every product.
i have tried:
<?php the_excerpt(get_the_ID()); ?>
<?php the_excerpt($item->ID); ?>
<?php the_excerpt(get_the_excerpt); ?>
i have also tried if and while but then it just shows all 20 product descriptions for each product
this is all the code for the page:
<script>
window.onload = calculate_order(0,0);
function calculate_order(qty_item, qty_type) {
//qty_type could be add or substract
total = 0;
numberofitems = 0;
if (qty_type == "add"){
document.getElementById((qty_item+"-qty")).value++;
}
if (qty_type == "subtract") {
if (document.getElementById((qty_item+"-qty")).value > 0) {
document.getElementById((qty_item+"-qty")).value--;
}
}
//return;
$('.store-item').each(function(i, obj) {
price = document.getElementById(($(this).attr("id")+"-price")).value;
//price = price * 1 ;
qty = document.getElementById(($(this).attr("id")+"-qty")).value;
//qty = qty * 1;
numberofitems = numberofitems + (qty*1);
total = total + ((price*1)*(qty*1));
//alert (numberofitems);
});
document.getElementById("total_price").innerHTML = total ;
document.getElementById("total_qty").value = numberofitems ;
discount = 100*Math.floor(numberofitems/2);
document.getElementById("total_discount").innerHTML = discount ;
document.getElementById("final_price").innerHTML = (total - discount);
document.getElementById("tot_discount").value = discount ;
document.getElementById("tot_order").value = (total - discount);
document.getElementById("tot_price").value = total ;
if (numberofitems <2) {
$("#total-price").hide();
$("#total-discount").hide();
}
else {
$("#total-price").show();
$("#total-discount").show();
}
return;
}
</script>
<div id="shopping-cart">
<div>
<h2>Shopping Cart</h2>
</div>
<div id="section group">
<div class="col span_1_of_4"> Items in Cart</div>
<div class="col span_1_of_4">
Description
</div>
<div class="col span_1_of_4" style="text-align:center">
Price
</div>
<div class="col span_1_of_4" style="text-align:right">
Quantity
</div>
</div></div>
<div style="clear:both"></div>
<div class="cart-header">
<div id="checkout-form" class="cart-header" style="border:0px;">
<fieldset class="addressdetails">
<table width="330" border="0" cellspacing="0" cellpadding="0">
<tr><td style="width:120px;padding-bottom:5px">Your Name:</td><td style="padding-bottom:5px"><input id="yName" type="text" name="order_name2" style="background-color:#D3E3F8"/></td></tr>
<tr><td>Your Email</td><td style="padding-bottom:5px"><input id="Email" type="text" name="order_email" style="background-color:#D3E3F8"/> </td></tr>
<tr><td></br></td><td>
<tr><td style="width:120px;padding-bottom:5px">Shipping Name:</td><td style="padding-bottom:5px"><input id="Name" type="text" name="order_name" style="background-color:#D3E3F8"/></td></tr>
<tr><td>Shipping Address:</td><td style="padding-bottom:5px"><input id="Address" type="text" name="order_address" style="background-color:#D3E3F8"/></td></tr>
<tr><td>City: </td><td style="padding-bottom:5px"><input id="City" type="text" name="order_city" style="background-color:#D3E3F8"/></td></tr>
<tr><td>State:</td><td style="padding-bottom:5px"><input id="State" type="text" name="order_state" style="background-color:#D3E3F8"/></td></tr>
<tr><td>Zip Code:</td><td style="padding-bottom:5px"><input id="ZipCode" type="text" name="order_zipcode" style="background-color:#D3E3F8"/></td></tr>
<tr><td>Country:</td><td><input id="Country" type="text" name="order_country" style="background-color:#D3E3F8"/></td></tr>
<tr><td></br></td><td>
<tr><td>Comments</td><td style="padding-bottom:5px"><textarea id="Comments" name="order_comments" style="width:180px;background-color:#D3E3F8"></textarea></td></tr>
</table>
</br>
</fieldset>
</br>
<div><label></label><center><input type="submit" name="submit" value="Submit Order" class="submit_order_button"/></center></div>
</div>
</div>
<?php $total = 0; ?>
<?php $nitems = 0; ?>
<?php foreach ($_POST as $item => $quantity): ?>
<?php $matches = array(); ?>
<?php if (preg_match('/^item-([0-9]+)$/', $item, $matches) === 1 and intval($quantity) > 0): ?>
<?php $item = get_post(intval($matches[1])); ?>
<?php if ($item): ?>
<?php $nitems++; ?>
<?php $price = intval(get_post_meta($item->ID, 'price', true)); ?>
<?php $total += $price; ?>
<div class="section group" style="margin:10px 0 10px 0">
<input id="item-<?php echo $item->ID; ?>-ID" type="hidden" name="art_id[]]" value="<?php echo $item->ID; ?>"/>
<div class="col span_1_of_4" style="text-align:center">
<?php echo get_the_post_thumbnail($item->ID, 'checkout-thumbnail'); ?>
</div>
<div class="col span_1_of_4">
<h2><?php
$title = explode('(', $item->post_title, 2);
echo $title[0];
if (count($title) > 1) {
echo '<span class="parenthetical">(';
echo preg_replace('/ /', ' ', $title[1]);
echo '</span>';
}
?></h2>
<?php while ( have_posts() ) : the_post(); ?>
<?php
$my_excerpt = get_the_excerpt();
if ( $my_excerpt != '' ) {
// Some string manipulation performed
}
echo get_the_excerpt($item->ID); // Outputs the processed value to the page
?>
<?php endwhile; ?>
</div>
<div class="col span_1_of_4" style="text-align:center">
$ <span id="price"><?php echo $price; ?></span>
</div>
<input id="item-<?php echo $item->ID; ?>-price" name="item-<?php echo $item->ID; ?>-price" type="hidden" value="<?php echo intval($price); ?>"/>
<div class="col span_1_of_4" style="text-align:right">
<input type="text" class="store-item-quantity" style="width:20px" name="item-<?php echo $item->ID; ?>-qty" id="item-<?php echo $item->ID; ?>-qty" value="<?php echo intval($quantity); ?>" readonly />
<input type='button' class="qtybutton" name='add' onclick='javascript: calculate_order("item-<?php echo $item->ID; ?>", "add");' value='+'/>
<input type='button' class="qtybutton" name='subtract' onclick='javascript: calculate_order("item-<?php echo $item->ID; ?>", "subtract");' value='-'/>
</div></div>
<!--<input id="item-<?php echo $item->ID; ?>-qty" type="text" value="<?php echo intval($quantity); ?>"/>-->
</div></div>
<?php endif; ?>
<?php endif; ?>
<?php endforeach; ?>
<!-- MSR 11062013 display mode of discount -->
<?php $discount = 100*intval(floor($nitems/2)); ?>
<div id="price-totals" style="padding-right:20px">
Price includes international shipping<br />
<table id="price-table">
<input id="total_qty" type="hidden" value="<?php echo $nitems; ?>"/>
<tr id="total-price" <?php if ($nitems < 2) echo 'style="display:none"' ?>>
<td class="price-label">Total:</td>
<td class="price-value">$ <span id="total_price"><?php echo $total; ?></span></td>
</tr>
<tr id="total-discount" <?php if ($nitems < 2) echo 'style="display:none"' ?>>
<td class="price-label">Discount:</td>
<td class="price-value">$ <span id="total_discount"><?php echo $discount; ?></span> </td>
</tr>
<tr>
<td class="price-label"><strong>Final Price:</strong></td>
<td class="price-value"><strong>$ <span id="final_price"><?php echo $total - $discount; ?></strong></span></td>
</tr>
</table>
<input id="tot_price" type="hidden" name="tot_price" value="<?php echo $total; ?>"/>
<input id="tot_discount" type="hidden" name="tot_discount" value="<?php echo $discount; ?>"/>
<input id="tot_order" type="hidden" name="tot_order" value="<?php echo $total - $discount; ?>"/>
</div>
</div>
</div>
</div>
You can't pass parameters to the_excerpt, it always uses the "post in the loop", same goes for get_the_excerpt (I was wrong earlier). By looking at your code, you are looping over each item and getting its corresponding WP_Post object. Its excerpt should then be in $item->post_excerpt
A potentially cleaner way of doing this would be to create a new WP_Query that you can loop over. See http://codex.wordpress.org/The_Loop#Multiple_Loops
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 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.