Support

Account

Home Forums Front-end Issues HowTo: build tables with acf and merged cells (colspan) Reply To: HowTo: build tables with acf and merged cells (colspan)

  • updated (better) version with prepend and append elements and support for regular text input fields and without extra function:

    function make_cells($field){
    	global $post, $posts;
    	$i = 1; // variable counter, may reset during foreach
    	$b = 1; // global counter, never resets
    	$end = count($posts);
    	echo "<td";
    	foreach($posts as $key => $post) {
    		$value = get_field($field);
    		$valueobj = get_field_object($field);
    		$nextvalue = "";
    		if(isset($posts[$key+1]))
    			$nextvalue = get_field($field,$posts[$key+1]->ID);
    		if($nextvalue != $value){ 
    			echo " colspan='".$i."'>";
    			if(isset($valueobj["prepend"])) echo $valueobj["prepend"]." ";
    			if(isset($valueobj["choices"])){
    				if(is_array($value)){
    					$newarray = [];
    					foreach($value as $val){
    						$newarray[] = $valueobj["choices"][$val];
    					}
    					echo implode(", ",$newarray);
    				}else{
    					echo $valueobj["choices"][$value];
    				}
    			}else{
    				echo $value;
    			}	
    			if(isset($valueobj["append"])) echo " ".$valueobj["append"];
    			$i=0; 
    			if($b != $end) echo "</td><td";
    		} 
    		if($b == $end) echo "</td>";
    		$i++;
    		$b++;
    	}
    }