Jump to content

How can i add a span element to this snippet of php


Go to solution Solved by Ch0cu3r,

Recommended Posts

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&gt]],1,-1));

 

 

Thanks for looking.

Edited by darbeey

$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 by Ch0cu3r

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.

  • Solution

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 by Ch0cu3r

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.

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 by darbeey

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 by Ch0cu3r

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

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

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 :DO0

Edited by darbeey
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.