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

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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); 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.