Jump to content

[SOLVED] Generating a definition list from 2 arrays


yanisdon

Recommended Posts

Hi there,

 

I want to create a function which can take two arrays and build a definition list from it.

 

<?php
But instead of :

/* 
* title
*
* one 
*		1
* two
*		2
* three
*		3
*/ 

I get this:

/* 
* title
*
* one 
*		
* two
*		
* three
*
*         1
*
*         2
*
*         3
*		
*/ 
?>

 

Here's what I got so far:

 


<?php


function theme_definition_list($dt_data, $dd_data, $title = NULL) {
  
  if (isset($title)) {
    $output .= '<h3>'. $title .'</h3>';
  }

    $output .='<dl>'."\n";
    
       foreach($dt_data as $dt_val){
                $output .= '<dt>'.$dt_val.'</dt>'."\n";
       }          

       foreach($dd_data as $dd_val){
                $output . '<dd>'.$dd_val.'</dd>'."\n";
       }                               

    $output .='</dl>'."\n";

  return $output;
  
}

$a = array('one','two','three');
$b = array('1','2','3');
$c = 'title';

$show_output = theme_definition_list($a, $b, $c);
print_r($show_output);

/* 
* This is what I like to get:
* title
*
* one 
*		1
* two
*		2
* three
*		3
*/ 
?>

 

Any suggetsions?

 

Cheers

try

<?php
function theme_definition_list($dt_data, $dd_data, $title = NULL) {
  
  if (isset($title)) {
    $output .= '<h3>'. $title .'</h3>';
  }
$dt_data = array_values($dt_data);
$dd_data = array_values($dd_data);
    $output .='<dl>'."\n";
    for ($i = 0; $i < count($dt_data); $i++){
                $output .= '<dt>'.$dt_data[$i].'</dt>'."\n";
                $output .= '<dd>'.$dd_data[$i].'</dd>'."\n";
    }
    $output .='</dl>'."\n";
  return $output;
}

$a = array('one','two','three');
$b = array('1','2','3');
$c = 'title';

$show_output = theme_definition_list($a, $b, $c);
print_r($show_output);
?>

You could of just changed the first foreach loop to this:

foreach($dt_data as $k => $dt_val)
    {
        $output .= '<dt>'.$dt_val."</dt>\n<dd>" . $dd_data[$k] . "</dd>\n";
    }

 

And remove the secound foreach. Rather than rewriting the whole thing.

Thanks for the leverage. Here's the working function:

 

 

/**
* Customize a definition list.
*
* @param dt_data
*   An array of values to be placed within the <dt> part
*
* @param ddt_data
*   An array of values to be placed within the <dd> part
*
* @param title
*   Optional parameter. If set a title will be displayed
*
* @param cls
*   optional parameter. If set a css class will be added
*   to the definition list
*/
function theme_definition_list($dt_data, $dd_data, $title = NULL, $cls = NULL) {
  if (!empty($title)) {
    $output .= '<h3>'. $title .'</h3>';
  }

  $output .='<dl'.(!empty($cls)?' class="'.$cls.'"':'').'>'."\n";
    $dl_array = array_combine($dt_data, $dd_data);
  		foreach ($dl_array as $dt=>$dd) {        	
        	$output .= '<dt>'.$dt.'</dt>'."\n";
		$output .= '<dd>'.$dd.'</dd>'."\n";
	} 
    $output .='</dl>'."\n";
  return $output;
} 

 

Here's how to call the function:


$invite_header = array('Accepted', 'Pending', 'Expired');
    $invite_row  = array(invites_status('accepted'), invites_status('pending'), invites_status('expired'));
	$invite_title = 'test';
$invite_class = 't-display';
$content = theme_definition_list($invite_header, $invite_row, $invite_title, $invite_class); 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.