TeddyKiller Posted April 12, 2010 Share Posted April 12, 2010 There are two methods I know of doing this. I have an array of titles used, and I'm wanting to add a span style onto them. - I can either add the span into the array with the titles, - I can put loads and loads of spans on the table the information is being displayed, - I can put the span styles into either the same array, or a seperate array. Method 1. $detail = array( '<span style="color:red;">Username</span>', '<span style="color:red;">Date of Birth</span>', '<span style="color:red;">Age</span>', '<span style="color:red;">Full Name</span>', '<span style="color:red;">Marital Status</span>', '<span style="color:red;">Interested In</span>', '<span style="color:red;">Looking for</span>', '<span style="color:red;">Orientation</span>', '<span style="color:red;">School</span>', '<span style="color:red;">College</span>', '<span style="color:red;">University</span>' ); Method 2- $detail = array('Username', 'Date of Birth', 'Age', 'Full Name', 'Marital Status', 'Interested In', 'Looking for', 'Orientation', 'School', 'College', 'University'); echo '<table><tr><td> <span style="color:red;">' . $detail[1] . '</span> </td><td> Information here</td></tr></table>'; Method 3- $detail = array('Username', 'Date of Birth', 'Age', 'Full Name', 'Marital Status', 'Interested In', 'Looking for', 'Orientation', 'School', 'College', 'University', '<spanstyle="color:red;">', '</span>'); echo '<table><tr><td>' . $detail[12] . $detail[1] . $detail[13] . '</td><td> Informationhere</td></tr></table>'; ///////// OR //////// $detail = array('Username', 'Date of Birth', 'Age', 'Full Name','Marital Status', 'Interested In', 'Looking for', 'Orientation','School', 'College', 'University'); $styles = array('<span style="color:red;">', '</span>'); echo '<table><tr><td>' . $styles[0] . $detail[1] . $styles[1] . '</td><td> Informationhere</td></tr></table>'; I'm wanting a simpler method to do these.. without doing something like method 1. Which is pretty messy. If there isn't a simpler/better method, which one of the above methods would you use? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/198281-how-to-add-a-style-onto-this-array-without-having-to-repeat/ Share on other sites More sharing options...
Ken2k7 Posted April 12, 2010 Share Posted April 12, 2010 Is the color always going to be red? If so, method 2 works perfectly. If not, then try this: <style type="text/css"> .css_class_1 { color: red; } .css_class_2 { color: green; } </style> <?php $detail = array( 'text1' => 'css_class_1', 'text2' => 'css_class_2' ); foreach ($detail as $key => $cssclass) { echo sprintf("<span class='%s'>%s</span>", $cssclass, $key); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/198281-how-to-add-a-style-onto-this-array-without-having-to-repeat/#findComment-1040366 Share on other sites More sharing options...
Psycho Posted April 12, 2010 Share Posted April 12, 2010 Are the spans stlye properties always going to be the same? If so, then the spans should be in the HTML templates (i.e. Option #2). However, if they will be different for different records, then simply create a function to format the arrays: function addSpans($array, $style) { foreach($array as $key => $value) { $array[$key] = "<span style=\"{$style}\">{$value}</span>"; } return $array; } $formattedArray = addSpans($originalArray, $style); Quote Link to comment https://forums.phpfreaks.com/topic/198281-how-to-add-a-style-onto-this-array-without-having-to-repeat/#findComment-1040374 Share on other sites More sharing options...
TeddyKiller Posted April 12, 2010 Author Share Posted April 12, 2010 Thanks for the replies but it can't be a loop, otherwise I would do that. Method two.. might look a bit messy 10 lines down. Here is the actual code, without the styles. function profile_details($id, $profile) { $user = user_get_details($id); $detail = array('Username', 'Date of Birth', 'Age', 'Full Name', 'Marital Status', 'Interested In', 'Looking for', 'Orientation', 'School', 'College', 'University'); $html_username = '<tr><td>' . $detail[0] . '</td><td>' . ucwords($user['username']) . '</td></tr>'; $html_dob = '<tr><td>' . $detail[1] . '</td><td>' . $user['dob'] . '</td></tr>'; $html_age = '<tr><td>' . $detail[2] . '</td><td>' . getAge($user['dob']) . '</td></tr>'; $html_real_name = '<tr><td>' . $detail[3] . '</td><td>' . ucwords($user['name']) . '</td></tr>'; $html_marital = '<tr><td>' . $detail[4] . '</td><td>' . ucwords($profile->rel_status) . '</td></tr>'; $html_interestedin = '<tr><td>' . $detail[5] . '</td><td>' . ucwords($profile->interested_in) . '</td></tr>'; $html_lookingfor = '<tr><td>' . $detail[6] . '</td><td>' . ucwords($profile->looking_for) . '</td></tr>'; $html_orien = '<tr><td>' . $detail[7] . '</td><td>' . ucwords($profile->orien_status) . '</td></tr>'; $html_school = '<tr><td>' . $detail[8] . '</td><td>' . ucwords($profile->school) . '</td></tr>'; $html_college = '<tr><td>' . $detail[9] . '</td><td>' . ucwords($profile->college) . '</td></tr>'; $html_uni = '<tr><td>' . $detail[10] . '</td><td>' . ucwords($profile->university) . '</td></tr>'; return '<fieldset><legend style="font-weight:bold;">Basic Information</legend>' . '<table id="details" width="60%">' . $html_username . $html_dob . $html_age . $html_real_name . $html_marital . $html_interestedin . $html_lookingfor . $html_orien . $html_school . $html_college . $html_uni . '</table>' . '</fieldset>'; } Note that- I will be adding in if statements. So that if user decides not to show a certain piece of info, it'll be hidden, or if information wasn't entered... to display as hidden etc. Quote Link to comment https://forums.phpfreaks.com/topic/198281-how-to-add-a-style-onto-this-array-without-having-to-repeat/#findComment-1040375 Share on other sites More sharing options...
Psycho Posted April 12, 2010 Share Posted April 12, 2010 Just modify your function to accept a style parameter and apply that parameter to the output. function profile_details($id, $profile, $style) { $user = user_get_details($id); $detail = array("Username", "Date of Birth", "Age", "Full Name", "Marital Status", "Interested In", "Looking for", "Orientation", "School", "College", "University"); $fields = array(); $fields[] = "<tr><td style=\"{$style}\">{$detail[0]}</td><td>" . ucwords($user["username"]) . "</td></tr>"; $fields[] = "<tr><td style=\"{$style}\">{$detail[1]}</td><td>{$user["dob"]}</td></tr>"; $fields[] = "<tr><td style=\"{$style}\">{$detail[2]}</td><td>" . getAge($user["dob"]) . "</td></tr>"; $fields[] = "<tr><td style=\"{$style}\">{$detail[3]}</td><td>" . ucwords($user["name"]) . "</td></tr>"; $fields[] = "<tr><td style=\"{$style}\">{$detail[4]}</td><td>" . ucwords($profile->rel_status) . "</td></tr>"; $fields[] = "<tr><td style=\"{$style}\">{$detail[5]}</td><td>" . ucwords($profile->interested_in) . "</td></tr>"; $fields[] = "<tr><td style=\"{$style}\">{$detail[6]}</td><td>" . ucwords($profile->looking_for) . "</td></tr>"; $fields[] = "<tr><td style=\"{$style}\">{$detail[7]}</td><td>" . ucwords($profile->orien_status) . "</td></tr>"; $fields[] = "<tr><td style=\"{$style}\">{$detail[8]}</td><td>" . ucwords($profile->school) . "</td></tr>"; $fields[] = "<tr><td style=\"{$style}\">{$detail[9]}</td><td>" . ucwords($profile->college) . "</td></tr>"; $fields[] = "<tr><td style=\"{$style}\">{$detail[10]}</td><td>" . ucwords($profile->university) . "</td></tr>"; $fieldList = implode("\n", $fields); $output = "<fieldset><legend style=\"font-weight:bold;\">Basic Information</legend>\n"; $output = "<table id=\"details\" width="60%">{$fieldList}</table>\n"; $output = "</fieldset>\n"; return $output; } Quote Link to comment https://forums.phpfreaks.com/topic/198281-how-to-add-a-style-onto-this-array-without-having-to-repeat/#findComment-1040383 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.