PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
yanisdon
Forum Newbie
Posts: 18 Joined: Thu Jul 06, 2006 10:44 pm
Post
by yanisdon » Mon Oct 08, 2007 7:25 am
Hi there,
I want to create a function which can take two arrays and build a definition list from it.
Code: Select all
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:
Code: Select all
<?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
Zoxive
Forum Regular
Posts: 974 Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan
Post
by Zoxive » Mon Oct 08, 2007 8:08 am
From what i understand, you have two options. a For loop or use Index Arrays.
Code: Select all
$array_one = count($dt_data)-1;
$array_two = count($dd_data)-1;
if($array_one != $array_two) Die('Array count mismatch');
for($i=0;$i<=$array_one;$i++){
$output.="<dt>" . $dt_data[$i] . "</dt>\n";
$output.="<dd>" . $dd_data[$i] . "</dd>\n";
}
//Or Structure your Arrays like so
$Data = array(
'One' => 1,
'Two' => 2,
'Three'=>3,
);
foreach($Data as $Name=>$Val){
$output.="<dt>" . $Name . "</dt>\n";
$output.="<dd>" . $Val . "</dd>\n";
}
Stryks
Forum Regular
Posts: 746 Joined: Wed Jan 14, 2004 5:06 pm
Post
by Stryks » Mon Oct 08, 2007 8:29 am
Assuming you're constrained to two separate arrays (DT and DD), couldn't you just ...
Code: Select all
$a = array('one','two','three');
$b = array('1','2','3');
$dl_array = array_combine($a, $b);
foreach ($dl_array as $dt=>$dd) {
echo "$dt - $dd<br />";
}
You'd want to validate the number of elements as per Zoxive's post to avoid throwing errors.
If you aren't contrained to array format, Zoxive's suggested array format would be the way to go.
Cheers
### EDIT ###
Yes, I know that my code doesn't output as requested, it's just an example to show the loop mechanism I would probably use.
### EDIT ###
Last edited by
Stryks on Mon Oct 08, 2007 5:24 pm, edited 1 time in total.
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Mon Oct 08, 2007 11:32 am
In your code you are running two separate loops, with your DT loop running first then your DD loop running second. What you want is to loop your DT loop, then inside that loop loop the DD array and match whatever in the DD array would associate it with the DT loop position it is at.
So what you would need is something like this:
Code: Select all
<?php
function theme_definition_list($dt_data, $dd_data, $title = NULL) {
if (!empty($title)) {
$output .= '<h3>'. $title .'</h3>';
}
$output .='<dl>'."\n";
foreach($dt_data as $key => $dt_val) {
$output .= '<dt>'.$dt_val.'</dt>'."\n";
foreach($dd_data as $dd_val) {
if ($dd_val == $key) {
$output . '<dd>'.$dd_val.'</dd>'."\n";
}
}
}
$output .='</dl>'."\n";
return $output;
}
$a = array(1 => 'one','two','three');
$b = array('1','2','3');
$c = 'title';
$show_output = theme_definition_list($a, $b, $c);
print_r($show_output);
?>
I didn't test this, but it should work, or at the very least, get you pointed in the right direction.
yanisdon
Forum Newbie
Posts: 18 Joined: Thu Jul 06, 2006 10:44 pm
Post
by yanisdon » Mon Oct 08, 2007 8:01 pm
Thanks for the leverage. Here's the working function. Problem solved
!
Code: Select all
/**
* 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:
Code: Select all
$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);