Hello guys I just want to ask in my project there are three tables product, color and product_color.I insert database using insert_batch then it work fine, when i update product_color table using update_batch then face some problems.Here's my sample code:
Database:
product:id,name,sku...
color:id,color_name
product_color:id,pro_id,color_id
Input Form:
<?php foreach($colors as $color): ?>
<input type="checkbox" class="form-check-input" name="color[]" value="<?php echo $color->color_id; ?>" <?php foreach ($productcolor as $key => $value){ $array[] = $value->color_id;} if(in_array($color->color_id,$array)) echo 'checked'; else ''; ?>>
<label class="form-check-label">
<?php echo $color->color_name; ?>
</label>
<?php endforeach; ?>
Actually i want to pass primary id from product_color table.Here i pass color_id.Have any way to pass primary id from input form;
Here Is my Controller:
$colorBatch = array();
foreach ($color as $colorvalue) {
$colorBatch[] = array(
'id'=>$id
'pro_id' =>$pid,
'color_id' => $colorvalue
);
}
$this->db->update_batch('product_color', $colorBatch,'pro_id');
Where $pid contains product_id;
Is it possible to pass product_color table primary id from input form or Have any better solution to solve this.Sorry for bad english.
Thanks
Please check below code as your array structure is wrong;
$colorBatch = array();
foreach ($color as $key => $colorvalue) {
$colorBatch[$key] = array(
'id'=>$id
'pro_id' =>$pid,
'color_id' => $colorvalue
);
}
$this->db->update_batch('product_color', $colorBatch,'pro_id');
Related
i am learning php nad html and mssql
Hello i dont know anything wrong can someone explain me?
+(everything configured righ in db i guess)
<?php
include('Database\DBconnect.php');
?><?php
$procs = "{call p_bas_sel_DataTest()}";
$paramss = array();
while($getquestionsfromphp=sqlsrv_fetch_object($results)){ ?>
Now HTML
<label class="questlabel">1.blah blah?</label>
<div class="answers" id="ans1" style="margin-left: 2em">
<button ><label ><span ><?php echo $getquestionsfromphp['D_Value'];?></span></label></button>
</div><?php } ?>
<?php
$sql = "SELECT * FROM Data_Test WHERE lang='M'" ;
$query = sqlsrv_query ($conn,$sql);
$arr = array();
while ($row = sqlsrv_fetch_array($query)){
$array[] = $row;
}
?>
I fixed my error coding like this i put data into array from database then used on some text as index numbers
I've tried GROUP BY with my data below but it only brings back one subtopic. How can I return all the subtopics and organise them under each topic without the topic_name appearing with each subtopic_name.
Edit: Included a screenshot of the page and here is the PHP used:
<ul class="topics-list">
<?php
foreach ($data as $key){
foreach ($key as $item){
$topic_name = $item['topic_name'];
$subtopic_name = ucwords($item['subtopic_name']);
?>
<div class="the_topic">
<h2 class="topic_change"><?php echo $topic_name; ?></h2>
<ul><li class="subtopic_name"><h3><?php echo $subtopic_name; ?></h3></li></ul>
<hr />
</div>
<?php } ?>
<?php } ?>
</ul>
You could use GROUP_CONCAT() to concatenate all subtopics into one string per topic, and then parse the string in your application code.
SELECT topic_name, GROUP_CONCAT(subtopic_name DELIMITER 'ยงยงยง') as subtopic_names
FROM questions2
GROUP BY topic_name
But i do not recommend that, because you will get in troubles, if a subtopic contains your delimiter. I would just use your second query and group the result in the application code.
PHP code would look something like:
// group the data
$groupedData = array();
foreach ($data as $item) {
$topic_name = $item['topic_name'];
$subtopic_name = ucwords($item['subtopic_name']);
$groupedData[$topic_name][] = $subtopic_name;
}
// grouped output
foreach ($groupedData as $topic_name => $subtopic_names) {
echo '<div class="the_topic">';
echo '<h2 class="topic_change">' . $topic_name . '</h2><ul>';
foreach ($subtopic_names as $subtopic_name) {
echo '<li class="subtopic_name"><a href="#" data-toggle="modal" data-target="#lvlModal"><h3>';
echo $subtopic_name;
echo '</h3></a></li>';
}
echo '</ul><hr /></div>';
}
I have a woocommerce site that groups products within categories and displays them on the initial shop page.
I want to add custom fields to the categories.
Theory: On the home page is a search form, the user types in their postcode and the categories with the matching postcode details (which will be specified within the custom fields) are displayed.
I don't need the custom fields to show on the shop page, I just need to be able to apply the matching postcodes in the backend.
Please does anyone know how I can do this?
I've been working on it for ages and can't seem to find a solution.
A huge thank you any answers or suggestions in advance.
P.S: I don't want to use a plugin, just php/html/javascript (& other code) please.
Add below code in your themes function.php to create custom field on your product categories taxonomy.
// Add term page
function custom_product_taxonomy_add_new_meta_field() {
// this will add the custom meta field to the add new term page
?>
<div class="form-field">
<label for="term_meta[custom_term_meta]"><?php _e( 'Postalcode', 'my-text-domain' ); ?></label>
<input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="">
<p class="description"><?php _e( 'Enter a value for this field','my-text-domain' ); ?></p>
</div>
<?php
}
add_action( 'product_cat_add_form_fields', 'custom_product_taxonomy_add_new_meta_field', 10, 2 );
// Edit term page
function custom_product_taxonomy_edit_meta_field($term) {
// put the term ID into a variable
$t_id = $term->term_id;
// retrieve the existing value(s) for this meta field. This returns an array
$term_meta = get_option( "taxonomy_$t_id" ); ?>
<tr class="form-field">
<th scope="row" valign="top"><label for="term_meta[custom_term_meta]"><?php _e( 'Postalcode', 'my-text-domain' ); ?></label></th>
<td>
<input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="<?php echo esc_attr( $term_meta['custom_term_meta'] ) ? esc_attr( $term_meta['custom_term_meta'] ) : ''; ?>">
<p class="description"><?php _e( 'Enter a value for this field','my-text-domain' ); ?></p>
</td>
</tr>
<?php
}
add_action( 'product_cat_edit_form_fields', 'custom_product_taxonomy_edit_meta_field', 10, 2 );
// Save extra taxonomy fields callback function.
function save_taxonomy_custom_meta( $term_id ) {
if ( isset( $_POST['term_meta'] ) ) {
$t_id = $term_id;
$term_meta = get_option( "taxonomy_$t_id" );
$cat_keys = array_keys( $_POST['term_meta'] );
foreach ( $cat_keys as $key ) {
if ( isset ( $_POST['term_meta'][$key] ) ) {
$term_meta[$key] = $_POST['term_meta'][$key];
}
}
// Save the option array.
update_option( "taxonomy_$t_id", $term_meta );
}
}
add_action( 'edited_product_cat', 'save_taxonomy_custom_meta', 10, 2 );
add_action( 'create_product_cat', 'save_taxonomy_custom_meta', 10, 2 );
you can save this custom field along with categories. you can used this custom field for your filtration.
I know I can use
<?= $form ->field($model, 'subject')
->textInput(array('value' => 'VALUE'))
->label('Titel'); ?>
to prefill a Textfield, but how can I do it for a radioList?
<?= $form ->field($model, 'locations')
->radioList($regionen)
->label('Regionen');
I could use ->textInput again but this transforms the whole List into a single Textfield
Alternativly: Is there a better way to modify a database record? Currently I'm trying to set all values into a new form.
Put the value that you want selected in the locations attribute of your $model and after rendering, it will be pre-selected in the radio list. That is:
$model->locations = ....
I assume that locations is a foreign key to some other table (or maybe a fixed list of strings).
Referring the documentation :
Pass the second parameter, options[] to radioList()
$options = [
'item' => function($index, $label, $name, $checked, $value) {
// check if the radio button is already selected
$checked = ($checked) ? 'checked' : '';
$return = '<label class="radio-inline">';
$return .= '<input type="radio" name="' . $name . '" value="' . $value . '" ' . $checked . '>';
$return .= $label;
$return .= '</label>';
return $return;
}
]
<?= $form->field($model, 'locations')
->radioList($regionen, $options)
->label('Regionen');
Hope this helps..
I have post about this before, but I didn't get any conclusive answer but I'm really hoping someone can help me. I have setup some custom post types, and with them, some custom fields using Wordpress 3's UI.
One of the fields I have set up is called banner_image, but in the loop, it doesn't output the image.
<?php echo get_post_meta($post->ID, 'banner_image', true); ?>
This simply outputs the ID number of the post. If I set the function to false, I get an array with this ID in and nothing else. How do I get the path to the image? I can't work this out and Googling reveals a sea of content not related to my problem, it's a real difficult one to search for so you're my only hope!
Many thanks,
Michael.
<?php
global $post;
$tmp_post = $post;
$args = array(
'post_status' => 'publish',
'post_type' => 'work',
'order' => 'DESC'
);
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<?php if( get_post_meta($post->ID, 'show_in_home_banner', true) == "yes" ) { ?>
<li class="slide">
<div class="slide-image">
<a href="<?php echo get_page_link($post->ID) ?>">
<?php echo get_post_meta($post->ID, 'banner_image', true); ?>
</a>
</div>
<div class="slide-content">
<h3 class="slide-header"><?php echo get_post_meta($post->ID, 'sub_title', true); ?></h3>
<p class="slide-title"><strong><?php echo the_title(); ?></strong></p>
</div>
</li>
<?php } ?>
<?php endforeach; ?>
Try this
<?php echo get_post_meta($post->ID, 'banner_image', $single); ?>
Apparently, the custom field 'banner_image' doesn't have the right value. I guess it doesn't save the correct value first. You may want to install the Simple WP FirePHP plugin (http://wordpress.org/extend/plugins/simple-wp-firephp/) and check the value with the fb() function.