Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
197 views
in Technique[技术] by (71.8m points)

How can I re-formating PHP code, returning duplicate attribute class?

Dears, I would like ask you for help with formating PHP code. Because W3C validator, return me this as error, "Error: Duplicate attribute class." I have now next PHP code:

$html .= '<a href="'.$href.'"';
if($this->hasChilds($item, $nazev)) $html .= ' class="qmparent"';
if ((empty($view) && $item->view == 'main') || (empty($display) && $view == $item->view) || (!empty($display) && $display == $item->display)) $html .= ' class="active"';
$html .= '>';

This giving me html
code: <a href="link.html" class="qmparent" class="active">LINK</a> And I would like, can will be generate, a different html
code: <a href="link.html" class="qmparent active" >LINK</a>
Can I ask you for help, how to rewrite? I don? know if it is enough for correct sense, if not I will add much of code. Thanx!

question from:https://stackoverflow.com/questions/65832596/how-can-i-re-formating-php-code-returning-duplicate-attribute-class

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The error is HTML error because you have duplicated class. In HTML it is ok to have empty class attribute. So you can append classes then add them to the HTML class attribute. See the code:

$classes = '';
if($this->hasChilds($item, $nazev)) $classes .= ' qmparent';
if ((empty($view) && $item->view == 'main') || (empty($display) && $view == $item->view) || (!empty($display) && $display == $item->display)) $classes .= ' active';
$html .= '<a href="'.$href.'"class ="'.$classes.'"/>';

To have better structure, it is common to separate the view from model. So you can create a data array that contains or your HTML elements with final classes, type and id. Then pass this array to view to construct the view correctly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...