How can I save the items of answers for a questions in an array so that I send them to the database using a controller and a model - mysql

The code below is the view page used to display the different kinds of questions and their choices.. Questions like Fill in the blanks and multiple answer consists of more than one answer element.
<?php echo form_open('courseware/saveanswer'); ?>
<?php if(count($test_questions)):?>
<?php $i = 1 ?>
<ol>
<?php foreach($test_questions as $test_question):?>
<li>
<?php if($test_question->question_type == 'Fill-blanks'):?>
<?php $test_question->question_text = str_replace('___',form_input(array('name'=>'answer[]','id'=>'answer_'.$i++,'type'=>'text','size'=>'8')),$test_question->question_text)?>
<?php endif ?>
<?php echo $test_question->question_text ?>
<?php if($test_question->question_type == 'Multi-choice'):?>
<?php $choices = explode("\n", $test_question->question_choices)?>
<?php if(count($choices)):?>
<ul>
<?php foreach($choices as $choice):?>
<li><?php echo form_radio('answer'.$test_question->question_id,$choice) ?> <?php echo $choice ?></li>
<?php endforeach ?>
</ul>
<?php echo form_submit('$save', 'Save') ?>
<?php endif ?>
<?php elseif($test_question->question_type == 'Multi-answer'):?>
<?php $choices = explode("\n", $test_question->question_choices)?>
<?php if(count($choices)):?>
<ul>
<?php foreach($choices as $choice):?>
<li><?php echo form_checkbox('answer',$choice) ?> <?php echo $choice ?></li>
<?php endforeach ?>
</ul>
<?php echo form_submit('$save', 'Save') ?>
<?php endif ?>
<?php elseif($test_question->question_type == 'True/False'):?>
<ul>
<li><?php echo form_radio('answer_'.$test_question->question_id,1) ?> True</li>
<li><?php echo form_radio('answer_'.$test_question->question_id,0) ?> False</li>
</ul>
<?php echo form_submit('$save', 'Save') ?>
<?php else:?>
<?php endif ?>
</li>
<?php endforeach?>
<?php echo form_close(); ?>
below is a screenshot.
The question page looks like this ..
I want an array that has all the answers selected and sends the data to the database
The database consists of the table tbl_response and has the columns
test_reponse_id,
test_reponse_text ,
question_id ,

there's a number of ways to store multiple elements in a single value. You can use an implode() function to save as a string with a separator, as you are already doing with the $test_question->question_choices , which is separated by a '\n'. The cleanest alternative to that is often using json_encode().
either way, as you're presumably going to be comparing the selected answer(s) to something else, i suggest being careful about keeping the format and order consistent.

Related

count up the rows in acf (repeater-field) and add html output for every row

I want to output a list element () for each row of an ACF repeater field. As soon as a new row is created, a new list element shoul be created with the value of the counter (cout up).
here is the code. the list elements should cout up according to the number of rows:
HTML:
<ul>
<li data-id="1" class="active">1</li>
<li data-id="2">2</li>
<li data-id="3">3</li>
<li data-id="4">4</li>
<li data-id="5">5</li>
<li data-id="6">6</li>
<li data-id="7">7</li>
<li data-id="...">...</li>
</ul>
PHP (ACF):
<?php
$i = 1;
if (have_rows('referenz-slide')) :
$counter = 0;
while (have_rows('referenz-slide')) : the_row();
$counter++;
// vars
$title = get_sub_field('title');
$link = get_sub_field('link');
$text = get_sub_field('text');
?>
<?php $state = "";
if ($i == 1) {
$state = "active";
} else {
$state = "hidden";
} ?>
<div class="referenz-content <?php echo $state; ?>" id="ref-<?php echo $i; ?>" data-referenz="<?php echo $i; ?>">
<h4 class="referenz-headline"><?php echo $title; ?></h4>
<p><?php echo $text; ?></p>
Jetzt mehr erfahren
</div>
<?php $i++;
endwhile; ?>
<?php else : ?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
I would like to increment the list items and the value of the data id (html output) according to the number of ACF rows. Can someone help?
So you pretty much have it all there already
<ul>
<?php while (have_rows('referenz-slide')) : the_row();?>
<li data-id="<?php echo $i;?>" class="<?php echo $state; ?>">
<?php echo $i;?>
</li>
<?php $i++; ?>
<?php endwhile; ?>
</ul>

ACF - repeater only showing 1 row on blog page

I have a repeater in my footer.php file and it works for the entire website, but whenever I go to the blog page (index.php), the repeater only displays one row of the entire repeater and I don't know why this is happening.
I've tried putting get_option('page_for_posts') as the repeater field's second parameter, but this didn't work.
This is how it looks on every page except the blog page.
and this is how it looks on the blog page itself:
It only shows one row on the blog page, which is the About Us column.
Here's the code:
<?php if ( have_rows('post_object_repeater') ) : ?>
<?php while( have_rows('post_object_repeater') ) : the_row(); ?>
<?php $header = get_sub_field( 'header' ); ?>
<div class="<?php echo $number_of_columns; ?> col-md-6 col-12 list-column">
<?php
$posts = get_sub_field('post_object_relationship');
if( $posts ): ?>
<ul class="list-unstyled <?php if ( ! $header ): echo 'no-header'; endif; ?>">
<?php if ( $header ): ?>
<li class="header mb-3 font-weight-bold text-uppercase"><?php echo $header; ?></li>
<?php endif; ?>
<?php foreach( $posts as $post): ?>
<?php setup_postdata($post); ?>
<li class="list-item">
<?php the_title(); ?>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
This repeater does have a relationship field inside to pull each of the individual pages or custom post types of the website. Would anyone have an idea why not all of the repeater rows display on a blog page, but work on all other pages?
Have you tried
if(have_rows('post_object_repeater', 'option')
while(have_rows('post_object_repeater', 'option'))
etc.
????

how to style a particular wordpress sidebar widget

I have sidebar widgets visible when I open a post in my website. But since the website is bilingual, some of them are supposed to be right aligned while others left aligned.
Is this possible? If I knew which function is generating the content for sidebar HTML, perhaps I could achieve this but I don't know where that function is, if it existed.
umm... kinda got it working.
I made the following changes to the file:
wp-includes\widgets\class-wp-widget-recent-posts.php
It had the following code:
<ul>
<?php while ( $r->have_posts() ) : $r->the_post(); ?>
<li>
<?php get_the_title() ? the_title() : the_ID(); ?>
<?php if ( $show_date ) : ?>
<span class="post-date"><?php echo get_the_date(); ?></span>
<?php endif; ?>
</li>
<?php endwhile; ?>
</ul>
I then changed it to:
<ul>
<?php while ( $r->have_posts() ) : $r->the_post(); ?>
<?php
if (strpos(get_the_title(), 'a') !== false) {
echo '<li class="engwidget">';
}else{
echo '<li class="localwidget">';
}
?>
<?php get_the_title() ? the_title() : the_ID(); ?>
<?php if ( $show_date ) : ?>
<span class="post-date"><?php echo get_the_date(); ?></span>
<?php endif; ?>
</li>
<?php endwhile; ?>
</ul>
I've achieved what I wanted but no idea if this is by any means a right way to do so.

Magento is not updating Shipping cost on changing the Shipping method radio button

I change the shipping method as you can see in the screenshot but magento does not refresh the inner page with the calculated total shipping cost.I think this is a bug in version 1.9. Here you can see the screenshot .Magento ver. 1.9.2.4
When I change the radio button from one shipping method the onestepcheckout does not refresh the inner page with calculated cost.
How can I refresh the inner page with javascript.
Thanks
Add the below code in available.phtml file
<?php if (!($_shippingRateGroups)): ?>
<strong><?php echo Mage::helper('oscheckout')->__('Sorry, no quotes are available for this order at this time.') ?></strong>
<?php else: ?>
<dl class="shipment-methods">
<?php $methodcount = count($_shippingRateGroups); $check_default = 1; foreach ($_shippingRateGroups as $code => $_rates): ?>
<dd><?php echo $this->getCarrierName($code) ?></dd>
<?php foreach ($_rates as $_rate): ?>
<dt style="margin-bottom: 5px;">
<?php if ($_rate->getErrorMessage()): ?>
<ul class="messages"><li class="error-msg"><ul><li><?php echo $_rate->getErrorMessage() ?></li></ul></li></ul>
<?php else: ?>
<input <?php echo $check_default == 1 ? 'checked="checked"':'' ?> name="shipping_method" type="radio" class="validate-one-required-by-name" value="<?php echo $_rate->getCode() ?>" id="s_method_<?php echo $_rate->getCode() ?>" />
<label for="s_method_<?php echo $_rate->getCode() ?>"> <?php echo $_rate->getMethodTitle() ?>
<strong>
<?php $_excl = $this->getShippingPrice($_rate->getPrice(), $this->helper('tax')->displayShippingPriceIncludingTax()); ?>
<?php $_incl = $this->getShippingPrice($_rate->getPrice(), true); ?>
<?php echo $_excl; ?>
<?php if ($this->helper('tax')->displayShippingBothPrices() && $_incl != $_excl): ?>
(<?php echo Mage::helper('oscheckout')->__('Incl. Tax'); ?> <?php echo $_incl; ?>)
<?php endif; ?>
</strong>
</label>
<?php endif ?>
</dt>
<?php endforeach; $check_default++; ?>
<?php endforeach; ?>
</dl>
<?php endif;
?>
Add this script in the same file
<script type="text/javascript">
<?php if($methodcount >= 1): ?>
document.observe('dom:loaded', function(){
shipping.loadReview();
var payment_methods = $('ajax-payment-methods');
//payment_methods.update('<div class="ajax-load"> </div>');
payment.reloadPaymentBlock();
// reloadPaymethod();
});
<?php endif; ?>
$$('dl.shipment-methods input').invoke('observe', 'click', function() {
shipping.loadReview();
var payment_methods = $('ajax-payment-methods');
//payment_methods.update('<div class="ajax-load"> </div>');
payment.reloadPaymentBlock();
// reloadPaymethod();
});
</script>

Nested IF statements in PHTML

Evening,
I tried to edit a simple code in Magento, yet im totaly new to PHTML and im having problems with basics...
I wanted to check the title to set a custom div for the translator to invoke...
This part of the edited PHTML code does not work...
<?php if( $this->getConfig('title') ): ?>
<?php if( $this->getConfig('title') == 'Categories' ?>
<div class="block-title"><strong><?php echo $this->__('Categories') ?></strong></div>
<?php else: ?>
<div class="block-title"><strong><?php echo $this->getConfig('title') ?></strong></div>
<?php endif ?>
<?php endif; ?>
While this original code works:
<?php if( $this->getConfig('title') ): ?>
<div class="block-title"><strong><?php echo $this->getConfig('title') ?></strong></div>
<?php endif; ?>
Can some expert please pinpoint an error so I wont spend whole bunch of time on this?
Thanks in advance.
<?php if( $this->getConfig('title') ): ?>
<?php if( $this->getConfig('title') == 'Categories'): ?>
---^
<div class="block-title"><strong><?php echo $this->__('Categories') ?></strong></div>
<?php else: ?>
<div class="block-title"><strong><?php echo $this->getConfig('title') ?></strong></div>
<?php endif; ?>
---^
<?php endif; ?>