我正试图为wordpress站点创建一个表。如果条目相同,我希望表可以跨列。例如,如果我在while语句中有4个电压条目,它们是12、13、14、15,那么电压行中将有4列。如果条目是12,12,12,12,12,12,12,12,12,12,24行,如果是12,12,24,24行,则有2列,每列2列,如果是12,24,12,24,则每列4列。
我以为我已经弄明白了,但我只是做了太多的检查,我想,我不能把我的头绕在简化这个东西上。
下面是我现在的代码:
if( have_rows('product_variations') ):
//if these later come true it prints the colum with a heading
$pname = '';
$pnumber ='';
$pcolor ='';
$pvoltage ='';
$pcurrent = '';
$ppower = '';
$pwigwag ='';
$psync = '';
$plumens = '';
$pcandela = '';
$pbeamprofile = '';
$plength = '';
$pwidth = '';
$pdepth = '';
$pheight = '';
$pbeamprofile = '';
//trial for comparing depth
$comparedepth = '';
$depthcount = '1';
//trial for comparing votage
$comparevoltage = '';
$voltagecount = '1';
$testvoltage = '';
$testcolor = '';
$testwig = '';
$count = count(get_field('product_variations'));
while( have_rows('product_variations') ) : the_row();
$pname .= '<th>'.get_sub_field('name').'</th>';
$pnumber .='<td>'.get_sub_field('part_number').'</td>';
$pcolor .='<td class="'.get_sub_field('color').'">'.get_sub_field('color').'</td>';
if (get_sub_field('color')) $testcolor = true;
if ( get_sub_field('voltage') && $comparevoltage == get_sub_field('voltage') ) {
$voltagecount++;
$testvoltage = true;
$pvoltage .='<td align="center" colspan="'.$voltagecount.'">'.get_sub_field('voltage').'</td>';
$comparevoltage = get_sub_field('voltage');
} elseif ( get_sub_field('voltage') ) {
$testvoltage = true;
$voltagecount = '1';
$pvoltage .='<td align="center" colspan="'.$voltagecount.'">'.get_sub_field('voltage').'</td>';
$comparevoltage = get_sub_field('voltage');
}
$pcurrent .='<td>'.get_sub_field('current').'</td>';
if (get_sub_field('current')) $testcurrent = true;
$ppower .='<td>'.get_sub_field('wattage').'</td>';
if (get_sub_field('wattage')) $testpower = true;
if (get_sub_field('wigwag')) {
$pwigwag .='<td>Yes</td>';
$testwig = true;
} else {
$pwigwag .='<td>No</td>';
}
if (get_sub_field('syncronization')) {
$psync .='<td>Yes</td>';
$testsync = true;
} else {
$psync .='<td>No</td>';
}
$plumens .='<td>'.get_sub_field('lumens').'</td>';
if (get_sub_field('lumens')) $testlumens = true;
$pcandela .='<td>'.get_sub_field('candella').'</td>';
if (get_sub_field('candella')) $testcandela = true;
$pbeamprofile .='<td>'.get_sub_field('beam_profile').'</td>';
if (get_sub_field('beam_profile')) $testbeamprofile = true;
$plength .='<td>'.get_sub_field('length').'</td>';
if (get_sub_field('length')) $testlength = true;
$pwidth .='<td>'.get_sub_field('width').'</td>';
if (get_sub_field('width')) $testwidth = true;
if ( get_sub_field('depth') && $comparedepth == get_sub_field('depth') ) {
$depthcount++;
$testdepth = true;
$pdepth ='<td align="center" colspan="'.$depthcount.'">'.get_sub_field('depth').'</td>';
$comparedepth = get_sub_field('depth');
} elseif ( get_sub_field('depth') ) {
$testdepth = true;
$pdepth .='<td align="center" colspan="'.$depthcount.'">'.get_sub_field('depth').'</td>';
$comparedepth = get_sub_field('depth');
}
$pweight .='<td>'.get_sub_field('weight').'</td>';
if ( get_sub_field('weight') ) {
$testweight = true;
}
endwhile;
$output .= '<tr><th></th>'.$pname.'</tr>';
$output .= '<tr><th>Part Number</th>'.$pnumber.'</tr>';
if( $testcolor ) $output .= '<tr><th>Color</th>'.$pcolor.'</tr>';
if( $testvoltage )$output .= '<tr><th>Voltage</th>'.$pvoltage.'</tr>';
//if( $testcurrent ) $output .= '<tr><th>Current</th>'.$pcurrent.'</tr>';
if( $testpower ) $output .= '<tr><th>Power</th>'.$ppower.'</tr>';
if( $testwig ) $output .= '<tr><th>Wigwag</th>'.$pwigwag.'</tr>';
if( $testsync ) $output .= '<tr><th>Synchronization</th>'.$psync.'</tr>';
if( $testlumens ) $output .= '<tr><th>Lumens</th>'.$plumens.'</tr>';
if( $testcandela ) $output .= '<tr><th>Candela</th>'.$pcandela.'</tr>';
if( $testbeamprofile ) $output .= '<tr><th>Beam Profile</th>'.$pbeamprofile.'</tr>';
if( $testlength ) $output .= '<tr><th>Length</th>'.$plength.'</tr>';
if( $testwidth ) $output .= '<tr><th>Width</th>'.$pwidth.'</tr>';
if( $testdepth ) $output .= '<tr><th>Depth</th>'.$pdepth.'</tr>';
if( $testweight ) $output .= '<tr><th>Weight</th>'.$pweight.'</tr>';
endif;电压是我现在有问题的那个。参赛人数是12,12,24,24。因此,我希望此行输出有html,如下所示:
<tr>
<th>Voltage</th>
<td colspan="2" align="center">12</td>
<td colspan="2" align="center">24</td>
</tr>然而,它实际上是这样出现的:
<tr>
<th>Voltage</th>
<td colspan="1" align="center">12</td>
<td colspan="2" align="center">12</td>
<td colspan="1" align="center">24</td>
<td colspan="2" align="center">24</td>
</tr>我知道这是因为当$pvoltage是真的时候,我不会覆盖我的if ( get_sub_field('voltage') && $comparevoltage == get_sub_field('voltage') )变量,但是我不知道如何添加一个条件,说明这是第二次,并且已经有了一些相同的条件。
发布于 2022-01-31 17:18:58
为了能够对每一件事情的发生分别跟踪$voltagecount,您可以使用关联数组分别迭代计数器。
因此,不要使用一个普通的变量$voltagecount,而是使用一个关联数组,其中每个键都是变量或值的组合,您需要单独计算。因此,数组的结构可能如下所示:
// just an example to get an idea about the array structure
$voltagecount['voltage_12'] = 2;
$voltagecount['voltage_24'] = 1;
$voltagecount['depth_12'] = 1;
$voltagecount['depth_24'] = 2;这样,您就可以单独计算每个计数器。
下面是实际编码的想法:
<?php
// instead of declaring variable as string (why string, you use it as integer anyway???)
$voltagecount = '1';
// declare it as array
$voltagecount = array();
// later on in your conditional part initiate the array key with default value 1 just increase by one...
if( !isset($voltagecount['voltage_'.get_sub_field('voltage')] ){
$voltagecount['voltage_'.get_sub_field('voltage')] = 1;
}
// later on when counter needed increase that by one
$voltagecount['voltage_'.get_sub_field('voltage')]++;
// printing the colspan value
$pvoltage .='<td align="center" colspan="'. $voltagecount['voltage_'.get_sub_field('voltage')] .'">'.get_sub_field('voltage').'</td>';
// and finally resetting the counter to init value
$voltagecount['voltage_'.get_sub_field('voltage')] = 1;注意,由于您的脚本是相当复杂的,没有现场演示,我提供公正和想法。请根据您的需要采用这种想法,特别是数组键和位置,以实现脚本的逻辑。
https://stackoverflow.com/questions/70912226
复制相似问题