yanisdon Posted October 8, 2007 Share Posted October 8, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/72306-solved-generating-a-definition-list-from-2-arrays/ Share on other sites More sharing options...
sasa Posted October 8, 2007 Share Posted October 8, 2007 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/72306-solved-generating-a-definition-list-from-2-arrays/#findComment-364603 Share on other sites More sharing options...
wildteen88 Posted October 8, 2007 Share Posted October 8, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/72306-solved-generating-a-definition-list-from-2-arrays/#findComment-364786 Share on other sites More sharing options...
yanisdon Posted October 9, 2007 Author Share Posted October 9, 2007 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); Quote Link to comment https://forums.phpfreaks.com/topic/72306-solved-generating-a-definition-list-from-2-arrays/#findComment-365227 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.