2013-09-27 74 views
0

我正在使用基于wordpress的优惠券网站。我必须创建一个适用于所有个人类别页面的金额计算器。我会有一个数额滑块,这将有$值。显示wordpress中的所有文章中的所有帖子的计算值

一旦选定了一个值并点击了提交按钮,我希望百分比交易(在相应的类别下)计算相对于使用滑块选择的$金额的金额。然后显示在他们各自的交易。 我希望这个想法很清楚。直到现在,我已经设法将当前类别页面的所有帖子标题放入一个数组中,然后使用preg_match功能,我已经设法提取出'%'交易金额。 我还创建了一个简单的滑块,用户需要输入他们的$数量。

<?php 
$array = array(); 
global $post; 
$category = get_the_category($post->ID); 
$category = $category[0]->cat_ID; 
$myposts = get_posts(array('numberposts' => 1, 'offset' => 0, 'category__in' => array($category), 'post_status'=>'publish')); 
foreach($myposts as $post) : 
setup_postdata($post); 

$title = get_the_title(); 
array_push($array,$title); 

endforeach; ?> 
<?php wp_reset_query(); ?> 

    <?php 

foreach($array as $str) { 
if(preg_match('/(\d+)\%/i' ,$str,$m)) { 
      echo $m[1],"\n"; ?> 

    <input type="text" name="a" value="<?php echo $m[1]; ?>" size=5> 

<?php } 
} ?> 

上述代码用于获取当前类别下的所有帖子,并从相应的帖子标题中提取%值。提取的数字是'$ m [1]',我想通过这个帖子。

我无法定义相应的帖子并传递该'%'金额,并且将发送计算的金额并将其保存回该特定帖子。也就是说,点击提交按钮后,我希望每个具有百分比值的帖子都能得到计算,并针对该帖子进行显示。对不起,这样一个巨大的解释。我不想要任何细节错过。任何帮助,将不胜感激。

编辑代码 - 此代码负责显示一笔交易。我已经把上面提到的主题的边栏文件中。我想在相应的百分比交易中显示节省额。

<div style="float:left; <?php if($GLOBALS['themename']['display_previewimage'] =="yes"){ ?>width:357px;<?php }else{ ?>width:477px;margin-left:10px;<?php } ?>"> 

    <h2 class="coupontitle"> 

     <a href="<?php echo $link; ?>" title="<?php the_title_attribute(); ?>" <?php if($GLOBALS['premiumpress']['analytics_tracking'] =="yes"){ ?>onclick="pageTracker._trackEvent('COUPON CLICK', 'TITLE CLICK', '<?php the_title(); ?>');"<?php } ?> <?php if(is_single()){ ?> target="_blank"<?php } ?>> 

      <?php the_title(); ?> 

     </a> 

    </h2> 

    <p><?php echo $post->post_content; ?></p> 

    <?php if($code != "" && $GLOBALS['themename']['system'] =="link"){ ?>   
    </div> 
+0

它有点不清楚,你要找什么。纠正我,如果我错了,但你有一个类别索引页面列出帖子。在这些职位中有一个百分比。在类别索引页面上,您有一个代表美元数量的滑块。这是我感到困惑的地方,用户将滑块滑动到一美元点击提交,你想发生什么? – Chausser

+0

您好Chausser,用户选择金额并点击提交按钮后,我想收集每个帖子的所有%交易,并计算用户输入(滑块)的金额w.r.t并显示相应帖子的金额。 –

+0

所以EX:post 1%是10%post 2%是25%用户滑动滑块以指示$ 100,您想要post 1显示保存$ 10 a post 2显示保存$ 25? – Chausser

回答

0

好的我创建了一个JSFIDDLE,它可以让你知道该怎么做。 http://jsfiddle.net/Q5sAK/1/

为此,我使用了jQuery UI滑块,但这应该适用于您自己的滑块,只需在准备好时调用$ .each()函数即可。

因此,我们知道您的百分比位于带有您所有产品的类coupontitle的h2标签中。

首先我们要通过修改H2开始有在,我们将用它来保存计算的储蓄结束的跨度标签:然后我们需要补充的是将计算的JavaScript

<h2 class="coupontitle"> 
    <a href="<?php echo $link; ?>" title="<?php the_title_attribute(); ?>" <?php if($GLOBALS['premiumpress']['analytics_tracking'] =="yes"){ ?>onclick="pageTracker._trackEvent('COUPON CLICK', 'TITLE CLICK', '<?php the_title(); ?>');"<?php } ?> <?php if(is_single()){ ?> target="_blank"<?php } ?>> 
     <?php the_title(); ?> 
    </a> 
    <span><span> 
</h2> 

储蓄。

function updateSlider(){ 
    //Get the value of the slider. 
    var curentSliderAmount = $('#sliderId').val(); 

    //Loop over all of the titles 
    $.each($('.coupontitle'),function(index,coupObj){ 
     //This will create the variable from the regex search that will have all of the parts for the percent we need 
     var percent = $(coupObj).text().match(/(\d+)\%/i); 
     //We will then take the 2nd part which is just the number without the % sign and make it a percent then multiply that by the slider value and then fix it to a 2 decimal value so it can be used a curency. 
     var savings = ((percent[1]*.01) * curentSliderAmount).toFixed(2); 
     //We then set the span html content = to our newly calculated value. 
     $('span',coupObj).html('Save: $'+savings); 
    }); 
} 
//Run this when the page starts 
$(document).ready(function(){ updateSlider() }); 

然后,只需在更新滑块时调用updateSlider()。

+0

非常感谢Chausser。该代码适用于所有百分比交易。现在我想为'$'交易和交易创造一个条件,它没有任何金额。 –

+0

任何百分比的数额?所以没有保存%的帖子只显示输入值? – Chausser

+0

的意思是,没有25%或任何金额的交易。其中一些交易的价格为20美元,其中一些交易没有任何数字。此外,给定的代码仅适用于第一笔交易,可能会因为其他交易不是折扣优惠而中断。 –

相关问题