Я перемещаю сайт из Wordpress в CodeIgniter. Все отлично работает, кроме калькулятора, который я не могу понять, как это правильно в CodeIgniter.
Мой код очень хорошо работает как плагин Wordpress, но у меня возникла проблема с его преобразованием в код CodeIgniter. Любая помощь здесь будет высоко оценена.
Примечание: я относительно новичок в Codeigniter.
Ниже мой код / плагин:
<?php function show_nice_quote(){
$ordersys= get_home_url()."example.com/orders";
$url = $orders.'example.com/api/rates';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_POSTFIELDS, null);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$settings = json_decode(curl_exec($curl));
ob_start();
<div class="main-cal">
<h8 class="text-center">Place a New Order</h8>
<br/>
<?php
$urls = 'example.com';
?>
<form class="form-horizontal" style="margin: 7px;" action="#" target="_blank">
<select required onchange="getOrderCost();" class="form-control" name="fieldOne_id">
<?php foreach($settings->fieldOnes as $fieldOne): ?>
<option value="<?php echo $fieldOne->id ?>"><?php echo $fieldOne->label ?></option>
<?php endforeach ?>
</select>
<br/>
<select onchange="setFieldTwo();" class="form-control" name="fieldTwo_id">
<?php foreach($settings->fieldTwos as $fieldTwo): ?>
<option value="<?php echo $fieldTwo->id ?>"><?php echo $fieldTwo->level ?></option>
<?php endforeach ?>
</select>
<br/>
<select style="display: none;" onchange="getOrderCost();" class="form-control" name="subject_id">
<?php foreach($settings->subjects as $subject): ?>
<option value="<?php echo $subject->id ?>"><?php echo $subject->label ?></option>
<?php endforeach ?>
</select>
<select onchange="getOrderCost();" class="form-control" name="rate_id">
<option value="">Select..</option>
</select>
<br/>
<select class="form-control" onchange="getOrderCost();" name="pages">
<?php
$end = 500;
for($i=1;$i<$end;$i++):
?>
<option value="<?php echo $i ?>"><?php echo $i.' Page(s) '.($i*275).' Words' ?></option>
<?php
endfor;
?>
</select>
<input type="hidden" name="partial" value="0">
<br>
<!--input type="text" name="" value="" placeholder="Your budget"-->
<br/>
<span id="total_price" style="text-align: center;"></span>
<button onclick="return setPartial(0);" type="submit" class="button_order">Order Now</button>
</form>
</div>
<?php
$home_urli = get_home_url();
$home_urli = str_replace('', '', $home_urli);
$home_urli = str_replace('http://', '', $home_urli);
$urli = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].'sss';
$urli = str_replace('/sss', 'yyyy', $urli);
if($urli == $home_urli){
?>
<script type="text/javascript">
// <![CDATA[
function setPartial(val){
jQuery("input[name='partial']").val(val);
}
<?php $replaced = str_replace("'","",json_encode($settings)); ?>
var settings = JSON.parse('<?php echo $replaced ?>');
var checked = jQuery("select[name='fieldTwo_id']").val();
var rate = null;
setfieldTwo();
function setfieldTwo(){
var id = jQuery("select[name='fieldTwo_id']").val();
jQuery("select[name='rate_id']").html('');
var fieldTwos = this.settings.fieldTwos;
var rates = this.settings.rates;
for(var i =0;i<rates.length;i++){
var rate = rates[i];
if(rate.fieldTwo_id==id && rate.deleted==0){
jQuery("select[name='rate_id']").append('<option value="'+rate.id+'">'+rate.label+'</option>');
}
}
getOrderCost();
}
function getRate(){
var rate_id = jQuery("select[name='rate_id']").val();
var rates = this.settings.rates;
var found = null;
for(var i=0;i<rates.length;i++){
var rate = rates[i];
if(rate.id==rate_id){
found = rate;
break;
}
}
// console.log(found);
return found;
}
function getOrderCost(){
var pages = jQuery("select[name='pages']").val();
var spacing = 1;
var cpp = getRate().cost;
var flat_rate = cpp*spacing;
var subject = getSubject();
var fieldOne = getfieldOne();
var subject_increase = 0;
var fieldOne_increase = 0;
/**
* Calculate increment by fieldOne type
*/
if(fieldOne.inc_type=='percent'){
console.log(flat_rate);
fieldOne_increase = flat_rate*((parseFloat(fieldOne.amount))/100);
}else if(fieldOne.inc_type=='money'){
fieldOne_increase = parseFloat(fieldOne.amount);
}
/**
* Calculate increment by subject
*/
if(subject.inc_type=='percent'){
subject_increase = flat_rate*((parseFloat(subject.amount))/100);
}else if(subject.inc_type=='money'){
subject_increase = parseFloat(subject.amount);
}
var total_cost = (flat_rate+subject_increase+fieldOne_increase)*pages;
jQuery("#total_price").html('Price: <strong style="font-size:larger;">$'+total_cost.toFixed(2)+'</strong>');
changeCurrency();
}
function getSubject(){
var subject_id = jQuery("select[name='subject_id']").val();
var subjects = this.settings.subjects;
var found = null;
for(var i=0;i<subjects.length;i++){
var subject = subjects[i];
if(subject.id==subject_id){
found = subject;
break;
}
}
return found;
}
function getWriter(){
var id = jQuery("input[name='writer_category_id']:checked").val();
var writer_categories = this.settings.writer_categories;
var writer = null;
for(var i =0; i<writer_categories.length;i++){
if(writer_categories[i].id==id){
writer = writer_categories[i];
}
}
return writer;
}
function getfieldOne(){
var fieldOne_id = jQuery("select[name='fieldOne_id']").val();
var fieldOnes = this.settings.fieldOnes;
var found = null;
for(var i=0;i<fieldOnes.length;i++){
var fieldOne = fieldOnes[i];
if(fieldOne.id==fieldOne_id){
found = fieldOne;
break;
}
}
return found;
}
function getLanguage(){
var language_id = jQuery("select[name='language_id']").val();
var languages = this.settings.languages;
var found = null;
for(var i=0;i<languages.length;i++){
var language = languages[i];
if(language.id==language_id){
found = language;
break;
}
}
return found;
}
function getStyle(){
var style_id = jQuery("select[name='style_id']").val();
var styles = this.settings.styles;
var found = null;
for(var i=0;i<styles.length;i++){
var style = styles[i];
if(style.id==style_id){
found = style;
break;
}
}
return found;
}
function changeCurrency(){
var selected = jQuery("#currency_select").val();
var original = jQuery("#total_price").text();
var original = original.replace('$','');
console.log(original,selected);
var currencies = this.settings.currencies;
for(i=0;i<currencies.length;i++){
var currency = currencies[i];
if(currency.id==selected){
var new_amt = parseFloat(original)*parseFloat(currency.usd_rate);
jQuery("#foreign_currency").val(new_amt.toFixed(2)+' '+currency.abbrev);
}
}
}
// ]]>
</script>
<?php
return ob_get_clean();
}
add_shortcode('show_nice_quote','show_nice_quote');
В Wordpress это работает как сон, но я не могу понять, как изменить это в плагин CodeIgniter MVC.