darbeey Posted December 17, 2013 Share Posted December 17, 2013 (edited) Hello, I am trying to wrap a span html element around $bi_i in the following snippet of code: echo str_ireplace(';', ', ', substr($i->fields[$diddy_all[$bi_i]],1,-1)); Here is what i have tried to do with no success: echo str_ireplace(';', ', ', substr($i->fields[$diddy_all['<span class='.$bi_i.'"></span>]],1,-1)); Thanks for looking. Edited December 17, 2013 by darbeey Quote Link to comment https://forums.phpfreaks.com/topic/284819-how-can-i-add-a-span-element-to-this-snippet-of-php/ Share on other sites More sharing options...
requinix Posted December 17, 2013 Share Posted December 17, 2013 $bi_i is probably a number... What output do you get now and what output are you trying to get? Quote Link to comment https://forums.phpfreaks.com/topic/284819-how-can-i-add-a-span-element-to-this-snippet-of-php/#findComment-1462576 Share on other sites More sharing options...
Ch0cu3r Posted December 17, 2013 Share Posted December 17, 2013 (edited) $b_i is a variable which is being used as an array key for $diddy_all. Maybe you mean to add the span to the echo? echo "<span class=\"$b_i\">" . str_ireplace(';', ', ', substr($i->fields[$diddy_all[$bi_i]],1,-1)) . "</span>"; Edited December 17, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/284819-how-can-i-add-a-span-element-to-this-snippet-of-php/#findComment-1462577 Share on other sites More sharing options...
darbeey Posted December 18, 2013 Author Share Posted December 18, 2013 Hello, Thanks for the replies and help. The code is an output of extra fields on a component i am using, the code is used to output the value of the checked checkboxes, here is a more fuller snippet with the original variables intact: if($this->custom_fields[$cf_active_all[$cf_i]]->type=='checkbox'){ echo str_ireplace(';', ', ', substr($i->fields[$cf_active_all[$cf_i></span>']],1,-1)); The code above prints out one or multiple values depending on how many checked boxes are checked. Your help is good Ch0cu3r but it wraps all the values in the span element, what i am trying to do is wrap each individual value in its own span element so i can customize each individually. Quote Link to comment https://forums.phpfreaks.com/topic/284819-how-can-i-add-a-span-element-to-this-snippet-of-php/#findComment-1462649 Share on other sites More sharing options...
Solution Ch0cu3r Posted December 18, 2013 Solution Share Posted December 18, 2013 (edited) Not sure but you could try echo "<span class=\"$cf_i\">" . str_ireplace(';', "</span>, <span class=\"$cf_i\">", substr($i->fields[$cf_active_all[$cf_i]],1,-1) . '</span>'); If not you'll need to post more code Edited December 18, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/284819-how-can-i-add-a-span-element-to-this-snippet-of-php/#findComment-1462652 Share on other sites More sharing options...
darbeey Posted December 18, 2013 Author Share Posted December 18, 2013 Thanks that is great. It added a span wrapped around each value, but the class="0" like this: <span class="0">Motorbike</span> I will PM more code for you to look at if you have the time Ch0cu3r. Quote Link to comment https://forums.phpfreaks.com/topic/284819-how-can-i-add-a-span-element-to-this-snippet-of-php/#findComment-1462657 Share on other sites More sharing options...
Ch0cu3r Posted December 18, 2013 Share Posted December 18, 2013 If all fields are going to have the same class then replace $cf_i with the name of the class you want to apply. If you want separate classes for each field then you will need to post more code, and what fields require which class. Quote Link to comment https://forums.phpfreaks.com/topic/284819-how-can-i-add-a-span-element-to-this-snippet-of-php/#findComment-1462659 Share on other sites More sharing options...
darbeey Posted December 18, 2013 Author Share Posted December 18, 2013 (edited) Hello, I am trying to individually style each one because i will be adding a background image for each value via css, here is more code: if(count($cf_active_all)){ for($cf_i=0;$cf_i<count($cf_active_all);$cf_i++){ if(isset($i->fields[$cf_active_all[$cf_i]])){ echo '<p class="howdcustomf">'; /*echo '<span class="label_title">'.$this->custom_fields[$cf_active_all[$cf_i]]->label.' : </span>';*/ if($this->custom_fields[$cf_active_all[$cf_i]]->type=='checkbox'){ echo "<span class=\"$cf_i\">" . str_ireplace(';', "</span>, <span class=\"$cf_i\">", substr($i->fields[$cf_active_all[$cf_i]],1,-1) . '</span>'); }else if($this->custom_fields[$cf_active_all[$cf_i]]->type=='link'){ if($i->fields[$cf_active_all[$cf_i]]==''){echo '---'; } else{ if(strstr($i->fields[$cf_active_all[$cf_i]], 'http://') || strstr($i->fields[$cf_active_all[$cf_i]], 'https://')){ echo '<a '.$this->custom_fields[$cf_active_all[$cf_i]]->params.' href="'.$i->fields[$cf_active_all[$cf_i]].'">'.str_ireplace(array("http://","https://"), array('',''), $i->fields[$cf_active_all[$cf_i]]).'</a>';; }else{ echo '<a '.$this->custom_fields[$cf_active_all[$cf_i]]->params.' href="http://'.$i->fields[$cf_active_all[$cf_i]].'">'.$i->fields[$cf_active_all[$cf_i]].'</a>';; } } }else{ echo $i->fields[$cf_active_all[$cf_i]]; } echo '</p>'; } } } Edited December 18, 2013 by darbeey Quote Link to comment https://forums.phpfreaks.com/topic/284819-how-can-i-add-a-span-element-to-this-snippet-of-php/#findComment-1462662 Share on other sites More sharing options...
Ch0cu3r Posted December 18, 2013 Share Posted December 18, 2013 (edited) What is the output of these statements printf('<pre>%s</pre>', print_r($i->fields, true)); printf('<pre>%s</pre>', print_r($cf_active_all, true)); I am trying to individually style each one What classes are you wanting to apply and to which fields? Edited December 18, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/284819-how-can-i-add-a-span-element-to-this-snippet-of-php/#findComment-1462664 Share on other sites More sharing options...
darbeey Posted December 18, 2013 Author Share Posted December 18, 2013 Hi, Here is the output of the code you gave: Array ( [2] => ;Motorbike;Car;Minibus;Large Goods Vehicles;Truck; ) Array ( [0] => 2 ) I have added these check boxe values to the extra field: Motorbike, Car, Minibus, Large Goods Vehicles, Truck The css classes i would like to apply would be something like: Motorbike = odmb Car = odcr Minibus = odmb Large Goods Vehicles = odlgv Truck = odtr Quote Link to comment https://forums.phpfreaks.com/topic/284819-how-can-i-add-a-span-element-to-this-snippet-of-php/#findComment-1462669 Share on other sites More sharing options...
Ch0cu3r Posted December 18, 2013 Share Posted December 18, 2013 Change echo "<span class=\"$cf_i\">" . str_ireplace(';', "</span>, <span class=\"$cf_i\">", substr($i->fields[$cf_active_all[$cf_i]],1,-1) . '</span>'); to $fields = explode(';', substr($ifields[$cf_active_all[$cf_i]],1,-1)); $classes = array('odmb', 'odcr', 'odmb', 'odlgv', 'odtr'); foreach($fields as $key => &$field) { $field = '<span class="'.$classes[$key].'">'.$field.'</span>'; } echo implode(', ', $fields); Quote Link to comment https://forums.phpfreaks.com/topic/284819-how-can-i-add-a-span-element-to-this-snippet-of-php/#findComment-1462671 Share on other sites More sharing options...
darbeey Posted December 20, 2013 Author Share Posted December 20, 2013 (edited) Hi again, sorry for the slow response. Thanks be to God i took another look at the component and It turns out it allowed me to make separate check box options and i didn't have to put them all together as i was doing above. So now i have separate check box options and i can easily style each one using your help and example Ch0cu3r: echo "<span class=\"$cf_i\">" . str_ireplace(';', "</span>, <span class=\"$cf_i\">", substr($i->fields[$cf_active_all[$cf_i]],1,-1) . '</span>'); I just changed: $cf_i to $this->custom_fields[$cf_active_all[$cf_i]]->label Thanks allot for your time and help, Happy Christmas Edited December 20, 2013 by darbeey Quote Link to comment https://forums.phpfreaks.com/topic/284819-how-can-i-add-a-span-element-to-this-snippet-of-php/#findComment-1462855 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.