2013-09-27 61 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美元,其中一些交易沒有任何數字。此外,給定的代碼僅適用於第一筆交易,可能會因爲其他交易不是折扣優惠而中斷。 –

相關問題