2015-12-18 105 views
4

增加值我有一個WordPress循環retriving表是這樣的:PHP的表,如果單元格重複

enter image description here

我要檢查,如果名稱重複它自己,如果這樣做的部分值的總和顯示總數。 在我的屏幕截圖上,第1行和第3行具有相同的「nome」,所以全局值應該是85,任何一個都可以告訴我該怎麼做?

我的PHP:

<?php while(have_rows('wallet_repeater')): the_row(); 

echo '<div class="promotoras_item_wallet"> '; 
echo '<div class="promnames_wallet"> '; 
// vars 

$adicionados = get_sub_field('wallet_promotora'); 

    foreach($adicionados as $post) : 

$nome = simple_fields_values("pname1"); 
$im = simple_fields_values("ftotop"); 
$cp=$adicionados ; 
$imatop = $im; 
$data=get_sub_field('wallet_data'); 
$evento=get_sub_field('wallet_evento'); 
$obs=get_sub_field("wallet_obs"); 
$numeros_horas = get_sub_field("Wallet_n_horas"); 
$valor_horas = get_sub_field("wallet_valorh"); 
$evento = get_sub_field("wallet_evento"); 
$horarios = get_sub_field("wallet_horario"); 

$total_parcial = $valor_horas * $numeros_horas."€"; 
$ii = wp_get_attachment_image($imatop[0]); 


?> 

<table id="wallet_table1" width="900px" border="0" cellspacing="2" cellpadding="0"> 
    <tbody> 
    <tr> 
     <tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';"> 

     <td class="wallet_data_img" width="5"><div class="w_promotora_images"><?php echo wp_get_attachment_image($imatop[0]); ?></td> 
     <td class="wallet_data_data" width="20"><?php echo the_sub_field('wallet_data'); ?> </td> 
     <td class="wallet_data_nome" width="200"><?php echo $nome[0];$nomes[]=$nome[0];?></td> 
     <td class="wallet_data_evento" width="10"> <?php echo the_sub_field("wallet_evento"); ?></td> 
     <td class="wallet_data_horarios" width="10"><?php echo the_sub_field("wallet_horario"); ?></td> 
     <td class="wallet_data_obs" width="10"><?php echo the_sub_field("wallet_obs"); ?></td> 
     <td class="wallet_data_horas" width="10"><?php echo the_sub_field("Wallet_n_horas")."h"; ?></td> 
     <td class="wallet_data_valorh" width="5"><?php echo the_sub_field("wallet_valorh")."€"; ?></td> 
     <td class="wallet_data_props" width="5"><?php echo the_sub_field("wallet_props"); ?></td> 
     <td class="wallet_data_total" width="5">Parcial: <?php echo $total_parcial; ?> global: 

</td> </tr></tbody></table> 


<?php 
+0

你嘗試了什麼? – utkbansal

回答

1

東西查詢數據到一個關聯陣列和輸出它之後。 你可以這樣做:

$qry = new WP_Query('post_type' => 'your_post_type', 'posts_per_page' => -1) 
$arr = array(); 
while($qry->have_posts()) { 
    $qry->the_post() 
    $nam = //Get the name data; 
    $val = //Get the «global» data; 
    if($qry[$nam]) { 
     $qry[$nam]['global'] += $val; 
    } 
    else { 
     $qry[$nam] = array(
      'foto' => //Photo data of the current post, 
      'name' => //Photo data of the current post, 
      //and so on 
      'global' => $val, 
     ); 
    } 
} 
foreach($qry as $key => $data) { 
    //Output your table here 
} 
+0

我收到我的信息: <?php while(have_rows('wallet_repeater')):the_row(); <?php echo $ nome [0]; $ nomes [] = $ nome [0];?> –

+0

只需在while循環中獲取數據,它在正確的地方(評論是) – Codeartist

+0

foreach-loop $ key - 是名字,所以你可以在數組中使用它或者複製它,並像'$ data ['name']'' – Codeartist