Skip to content
Advertisement

HTML DOM: remove specific div based on data attribute value using php

I am new in codeigniter.I get the html string form database as shown below

<div class="rockfm-form-container uiform-wrap">
<div class="rockfm-alert-container"></div>
<form class="rockfm-form" action="" name="" method="post" data-zgfm-type="1" enctype="multipart/form-data" id="rockfm_form_7">
<input type="hidden" value="7" class="_rockfm_form_id" name="_rockfm_form_id"> 
<input type="hidden" value="0" class="_rockfm_wizard_st" > 
<input type="hidden" value="PGRpdiBjbGFzcz0icm9ja2ZtLWFsZXJ0IHJvY2tmbS1hbGVydC1zdWNjZXNzIj5TdWNjZXNzISBGb3JtIHdhcyBzZW50IHN1Y2Nlc3NmdWxseS48L2Rpdj4=" name="_rockfm_onsubm_smsg" class="_rockfm_onsubm_smsg" > <!--- ajax or post ---> 
<input type="hidden" value="1" class="_rockfm_type_submit" name="_rockfm_type_submit"> 
<input type="hidden" value="rocket_front_submitajaxmode" name="action"> 
<div class="uiform-main-form">
 <div class="uiform-step-content" >
    <div id="rockfm_uiibpy0eewu" data-idfield="uiibpy0eewu" data-typefield="33" class="rockfm-heading rockfm-field " ></div>
    <div id="rockfm_uiya0wevyae" data-idfield="uiya0wevyae" data-typefield="28" class="rockfm-preptext rockfm-field rockfm-required " data-val-type="4"></div>
    <div id="rockfm_ui04fxilqp6" data-idfield="ui04fxilqp6" data-typefield="7" class="rockfm-textarea rockfm-field " ></div>
    <div id="rockfm_uiwdtl19w05" data-idfield="uiwdtl19w05" data-typefield="10" class="rockfm-select rockfm-field " ></div>
    <div id="rockfm_uiksx46ggj9" data-idfield="uiksx46ggj9" data-typefield="12" class="rockfm-fileupload rockfm-field " ></div>
    <div id="rockfm_ui2jrdo8mcp" data-idfield="ui2jrdo8mcp" data-typefield="20" class="rockfm-submitbtn rockfm-field " ></div>
 </div>
</div>
</form>
</div>

I have another table of typefields as shown in image

enter image description here

html string is convert into DOM and add check condition of if typefieldid = 10 is flag_status = 0 then remove this div only <div id="rockfm_uiwdtl19w05" data-idfield="uiwdtl19w05" data-typefield="10" class="rockfm-select rockfm-field " ></div> but this gave me code like this

<div id="rockfm_uiibpy0eewu" data-idfield="uiibpy0eewu" data-typefield="33" class="rockfm-heading rockfm-field "></div>
<div id="rockfm_uiya0wevyae" data-idfield="uiya0wevyae" data-typefield="28" class="rockfm-preptext rockfm-field rockfm-required "></div>
<div id="rockfm_ui04fxilqp6" data-idfield="ui04fxilqp6" data-typefield="7" class="rockfm-textarea rockfm-field "></div>
<div id="rockfm_uiksx46ggj9" data-idfield="uiksx46ggj9" data-typefield="12" class="rockfm-fileupload rockfm-field "></div>
<div id="rockfm_ui2jrdo8mcp" data-idfield="ui2jrdo8mcp" data-typefield="20" class="rockfm-submitbtn rockfm-field "></div>

Issue is that when typefield = 10 is flag_status = 0 its remove <div id="rockfm_uiwdtl19w05" data-idfield="uiwdtl19w05" data-typefield="10" class="rockfm-select rockfm-field " ></div> but other information in html string is also remove. I show code of controller function where I html string converted into DOM documents, get the record of fby_id and flag_status form typefields table and making an array of this record. Then make a foreach loop of data-typefield using DOM query for getting attribute value of data-typefield then add check condition for if 0 = 1 then remove specific div

One more issue is that how to pass and show final data in view file after removing specific div based on condition. Here is code of controller function

public function getform() {

$form_id = ($this->input->post('id')) ? Uiform_Form_Helper::sanitizeInput($this->input->post('id')) : 0;

$data = array();

if (intval($form_id) === 0) {
    return;
} else {


    $rdata = $this->model_forms->getFormById($form_id);
}


$response = array();
if (!empty($rdata)) {
    $response['html_content'] = Uiform_Form_Helper::encodeHex($rdata->fmb_html);
}

$html = $rdata->fmb_html;
$doc = new DOMDocument();
$doc->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXPath($doc);

$fields = $this->model_fields->getAllFieldsType();

$field_array =array();
 foreach ($fields as $value) {

    $field_array[$value->fby_id] = $value->flag_status;

}

foreach ($xpath->query("//div[@data-typefield]") as $item) 
{

    $typefield = $item->getAttribute('data-typefield');

    if($field_array[$typefield] == 1)
    {
        $item->outertext = '';

        $doc->saveHTML($item);
    }


}

$data = array();

$json = $response;
$data['json'] = $response;

$this->load->view('html_view', $data);
}

and please guide me is there any option to remove only specific div from HTML DOM. I want my code output like this way

<div class="rockfm-form-container uiform-wrap">
<div class="rockfm-alert-container"></div>
<form class="rockfm-form" action="" name="" method="post" data-zgfm-type="1" enctype="multipart/form-data" id="rockfm_form_7">
<input type="hidden" value="7" class="_rockfm_form_id" name="_rockfm_form_id"> 
<input type="hidden" value="0" class="_rockfm_wizard_st" > 
<input type="hidden" value="PGRpdiBjbGFzcz0icm9ja2ZtLWFsZXJ0IHJvY2tmbS1hbGVydC1zdWNjZXNzIj5TdWNjZXNzISBGb3JtIHdhcyBzZW50IHN1Y2Nlc3NmdWxseS48L2Rpdj4=" name="_rockfm_onsubm_smsg" class="_rockfm_onsubm_smsg" > <!--- ajax or post ---> 
<input type="hidden" value="1" class="_rockfm_type_submit" name="_rockfm_type_submit"> 
<input type="hidden" value="rocket_front_submitajaxmode" name="action"> 
<div class="uiform-main-form">
  <div class="uiform-step-content" >
     <div id="rockfm_uiibpy0eewu" data-idfield="uiibpy0eewu" data-typefield="33" class="rockfm-heading rockfm-field " ></div>
     <div id="rockfm_uiya0wevyae" data-idfield="uiya0wevyae" data-typefield="28" class="rockfm-preptext rockfm-field rockfm-required " data-val-type="4"></div>
     <div id="rockfm_ui04fxilqp6" data-idfield="ui04fxilqp6" data-typefield="7" class="rockfm-textarea rockfm-field " ></div>
     <div id="rockfm_uiksx46ggj9" data-idfield="uiksx46ggj9" data-typefield="12" class="rockfm-fileupload rockfm-field " ></div>
     <div id="rockfm_ui2jrdo8mcp" data-idfield="ui2jrdo8mcp" data-typefield="20" class="rockfm-submitbtn rockfm-field " ></div>
   </div>
 </div>
 </form>
</div>

Advertisement

Answer

Your code is mostly there, but the main part is removing the node, I’ve changed the test to say if the status = 0, then use removeChild() to get rid of the element. Then the document should only contain the <div> elements you want at the end…

foreach ($xpath->query("//div[@data-typefield]") as $item)
{
    $typefield = $item->getAttribute('data-typefield');
    if($field_array[$typefield] == 0)
    {
        $item->parentNode->removeChild($item);
    }
}

echo $doc->saveHTML();
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement