i create two multi select list in my cakephp view .
in one of select box get all users and in anouther get all car list .
i want select users and car name and insert in user profile.but just select one and last item .
my table=>
user -->
-id
-username
-user_family
car-->
-id
-name
-model
my controller{
$users = $this->User->find('all', array(
'fields' => array('User.id', 'User.username', 'User.user_family')));
$this->set('users', $users);
$cars = $this->Car->find('all', array());
$this->set('cars', $cars);
}
and my view file {
foreach($users as $user){
?>
<option
value="<?php echo $user['User']['id'] ?>"><?php echo $user['User']['username'].' (' .$user['User']['user_family']. ' )' ?></option>
<?php
}
?></select>
<label>نام درس</label> </label>
<select name="car_id" id="car_id" multiple="true" style="width: 150px;height: 200px;">
<?php
foreach($cars as $car) {
?>
<option value="<?php echo $car['Car']['id'] ?>"><?php echo $course['Car']['name'] ?></option>
<?php
}
?>
</select></fieldset>
}
and my pr() result:
Array
(
[user_id] => 16
[car_id] => 8
)
but i select 10 user and 2 car .how can fix it?
Try in controller
$users = $this->User->find('list');
$cars = $this->Car->find('list');
$this->set(compact('users','cars'));
in your view use Form helper
echo $this->Form->input('User');
echo $this->Form->input('Car');
Related
I have this form with a select.
<form>[...]
<select name="quantity[]">
<?php
for ($i=1; $i <20; $i++) {
?>
<option value="<?php echo $i ?>"><?php echo $i ?></option>
<?php } ?>
</select>[...]
</form>
I'm trying to store the quantity data in the database using $wpdb.
global $wpdb;
$table_name=$wpdb->prefix.'prenotazione_eventi';
$wpdb_quantity = maybe_serialize( $_POST[ 'quantity'] );
$query = "INSERT INTO $table_name (quantity) VALUES (%s)";
$prepare_query = $wpdb->prepare( $query, $wpdb_quantity);
$result = $wpdb->query( $prepare_query );
When I submit the form the data is saved in the database, but in this sintax a:1:{i:0;s:2:"10";}. What I want is that the data have only the number that I choose I the form, "10". How do that?
Try this code.
use json_encode instead of maybe_serialize.
global $wpdb;
$table_name=$wpdb->prefix.'prenotazione_eventi';
$wpdb_quantity = json_encode( $_POST[ 'quantity'] );
$query = "INSERT INTO $table_name (quantity) VALUES (%s)";
$prepare_query = $wpdb->prepare( $query, $wpdb_quantity);
$result = $wpdb->query( $prepare_query );
I am working on CakePHP 2.7.8. I want to update related list with change in list using Ajax.
I have a customers table and customer_addresses table in the database and customers and customerAddress models in the project.
There is another controller serviceRequests where I have to select customer and address of the selected customer from a drop down list generated by CakePHP from database.
What I have done-
I have added a function getCustomerAddress in the serviceRequests controller
public function getCustomerAddress(){
$customer_id = $this->request->data['Post']['customer_id'];
$customer_address = $this->CustomerAddress->find('list',array(
'condition' => array('CustomerAddress.customer_id' => $customer_id),
'recursive' => -1
));
$this->set('customerAddresses', $customer_address);
$this->layout = 'ajax';
}
to display the retrieved data, I have a view get_customer_address.ctp
<?php
foreach ($customerAddresses as $key => $value): ?>
<option value="<?php echo $key;?>"><?php echo $value; ?></option>
<?php endforeach; ?>
In the add.ctp view of serviceRequests controller for add function, I have added following script at the last.
<div class="serviceRequests form">
<?php echo $this->Form->create('ServiceRequest'); ?>
<fieldset>
<legend><?php echo __('Add Service Request'); ?></legend>
<?php
echo $this->Form->input('customer_id');
echo $this->Form->input('customer_address_id');
echo $this->Form->input('status');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
<?php
$this->Js->get('#ServiceRequestCustomerId')->event('change',
$this->Js->request(array(
'controller' => 'serviceRequests',
'action' => 'getCustomerAddress'
), array(
'update' => '#ServiceRequestCustomerAddressId',
'async' => true,
'method' => 'post',
'dataExpression' => true,
'data' => $this->Js->serializeForm(array(
'isForm' => true,
'inline' => true
))
))
);
?>
and to render Js, I have added following code to to last of default.ctp
<!-- script for layout -->
<?php echo $scripts_for_layout; ?>
<!-- Js writeBuffer -->
<?php
if(class_exists('JsHelper') && method_exists($this->Js, 'writeBuffer')) echo $this->Js->writeBuffer ();
// writes cached scripts
?>
But on accessing localhost/serviceRequests/add the ajax call is not working and all customer's name and all customer's addresses are showing in the list.
This is an example of how to implement chained selects with cake http://sandbox.dereuromark.de/sandbox/ajax_examples/chained_dropdowns
- the related article for that example is here http://www.dereuromark.de/2014/01/09/ajax-and-cakephp/
I'm attempting to display one or more specific product(s) on my home page on Woocommerce, in a very simple fashion :
name of the product;
short description of the product;
price of the product;
quantity selector;
add to cart button;
Now, i've created a custom loop for doing so:
<?php
$args = array(
'post_type' => 'product',
'sku' => 'lundivegetarien',
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
echo the_title();
echo woocommerce_template_single_excerpt();
echo woocommerce_template_single_price();
echo woocommerce_template_single_add_to_cart();
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
Main problem here is that this loop displays all my products, irrespectively of the sku i'm trying to call. I would like to be more specific, and be able to choose to display one or several products i'd call by their specific sku.
What am i doing wrong?
Any pointers?
Help appreciated!
Sorted out the problem myself eventually, so dumb i was! Just turned to using categories and it works pretty fine!
Here's the updated code in cas anybody needs it!
<?php
$args = array( 'post_type' => 'product', 'product_cat' => 'name_of_the_category', 'posts_per_page' => 1 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product;
?>
<p>
<?php
the_title();
?>
</p>
<?php
echo woocommerce_template_single_excerpt();
echo $product->get_price_html();
?>
<div class="order_form close">
<p>
<?php
woocommerce_template_loop_add_to_cart( $loop->post, $product );
?>
</p>
</div>
<br>
<?php
endwhile;
?>
Hope this will help someone!
I am trying to add pagination in CakePHP 2.x with condition search in a form. However, in the View it doesn't give the next page with my condition. Here is my code:
Controller
public function result($palabraclave = null)
{
$this->layout = 'tempart';
$this->paginate = array(
'limit' => 5,
'order' => array(
'Articulo.created' => 'desc'),
'conditions'=>array("titulo like ?"=>"%".$this->data['Paginas']['palabraclave']."%")
)
$data = $this->paginate('Articulo');//Consulta a la base de datos
$this->set(compact('data'));
}
If I click "next" to see the next page result of the search, it doesn't pass on the pagination condition from page 1 to page 2. :(
View:
<div style="display: inline-block; width:70%;" id="test">
<?php foreach ($data as $da): ?>
<br />
<article>
<?php echo $this->Html->image('article_preview/mini_'.$da['Articulo']['imagen'], array('alt' => 'Hospital-Excel'))?>
<div style="text-align:left;">
<?php echo $this->Html->link("<h6 class='artnombre'>".$da['Articulo']['titulo']."<h6>", array('class'=>"artnombre",'action'=> 'articulo',$da['Articulo']['id']),array('escape' => false)); ?>
</div>
<br />
<p>
<?php echo $da['Articulo']['descripcion']; ?> <?php echo $this->Html->link('Read more ....', array('action'=> 'articulo',$da['Articulo']['id'])); ?>
<h5></h5>
</p>
<div class="puntoshorizontal" style="clear"></div>
</article>
<?php endforeach; ?>
<div style="text-align:center">
<?php
// Shows the page numbers
// Shows the next and previous links
echo $this->Paginator->prev('« Back', null, null, array('class' => 'disabled')) . " |";
echo $this->Paginator->numbers(). "| ";
echo $this->Paginator->next('Next »', null, null, array('class' => 'disabled'));
echo "<br />";
echo $this->Paginator->counter();
// prints X of Y, where X is current page and Y is number of pages
?>
</div>
</div>
a way to persist your search params on each page you should do the following:
After you post data, in your method you have $this->data['Paginas']['palabraClave'] then save it the the session. So in each subsequent call to /resultados/pag1,2,..n you get the value of palabra clave from the session.
Finally you would get something like:
public function resultados(){
$conditions = array();
if($this->Session->check('conditions')){
$conditions = $this->Session->read('conditions');
}
if(isset($this->data)){
$conditions = array("titulo LIKE" => "%".this->data['Paginas']'palabraclave']"%",));
//save to the session
$this->Session->write('conditions',$conditions);
}
$data = $this->painate('Articulo',$conditions);
$this->set('data');
}
This may need some adjust but the idea I think is clear.
Set your paginate conditions when calling the paginate method, like this:
$data = $this->paginate('Articulo', array(
"titulo LIKE" => "%" . $this->data['Paginas']['palabraclave'] . "%",
));
as opposed to in the paginate property.
this is example:
1: pass all URL arguments to paginator functions
$this->Paginator->options(array('url' => $this->passedArgs));
2: Common mistake: not keep condition search with paginate
if($this->data){
$this->passedArgs = array(
'name' => $this->data[‘User’]['name']
);
}else{
if(isset($this->passedArgs['name'])){
$this->data[‘User’]['name'] = $this->passedArgs['name'];
}else{
$this->data[‘User’]['name'] = ;
}
}
when i need to use select form i see the first value is empty..but i dont need this empty value option ..how to do this..thanks
<?php
$options = array('M' => 'Male', 'F' => 'Female');
echo $this->Form->select('gender', $options)
?>
Will output:
<select name="data[User][gender]" id="UserGender">
<option value=""></option>
<option value="M">Male</option>
<option value="F">Female</option>
</select>
In Cake 2.x, you can just add the 'empty'=>false like this (tested and works):
<?php
$options = array('M' => 'Male', 'F' => 'Female');
echo $this->Form->select('gender', $options, array('empty'=>false));
?>
In CakePHP 1.3.x (per this page in the book) you might have to add an additional null like this:
<?php
$options = array('M' => 'Male', 'F' => 'Female');
echo $this->Form->select('gender', $options, null, array('empty'=>false));
?>