PHP:如何从数组中动态输入选项卡中的值?
发布时间:2020-12-13 16:09:26 所属栏目:PHP教程 来源:网络整理
导读:我有一个 PHP数组,其中我有一些相关的信息来回应这样. ?php $options = array( array("name" = "Title","desc" = "Description","type" = '_one',"tab-name" = "Tab 1" ),array("name" = "Title2","desc" = "Description2","type" = '_two',"tab-name" = "Ta
我有一个
PHP数组,其中我有一些相关的信息来回应这样.
<?php $options = array( array("name" => "Title","desc" => "Description","type" => '_one',"tab-name" => "Tab 1" ),array("name" => "Title2","desc" => "Description2","type" => '_two',"tab-name" => "Tab 2" ),array("name" => "Title3","desc" => "Description3","type" => '_three',"tab-name" => "Tab 3" ) ) ?> 我已经做了一个函数来回显出名称,desc,这是完美的 这里是: <?php function create_heading($value) { echo '<h2>'.$value['name'].'</h2>'; } function create_description($value){ echo '<p>'.$value['desc'].'</p>' } function wrapper1($value) { echo '<h1>This is the wrapper 1</h1>' create_heading($value); create_description($value); } function wrapper2($value) { echo '<h1>This is the wrapper 2</h1>' create_heading($value); create_description($value); } function wrapper2($value) { echo '<h1>This is the wrapper 3</h1>' create_heading($value); create_description($value); } function render_things($options) { foreach ($options as $value) { switch($value['type']) { case "one": wrapper1($value); break; case "two": wrapper2($value); case "three": wrapper3($value); } } } render_things(); ?> 所以有什么问题: 问题是如何根据数组中的tab-name动态回显选项卡中的这些值意味着我已经在tab-name中给出了第二个数组的值为2,所以如果我改变它应该在第二个选项卡中打印它应该分别将值打印到一个或三个. 我希望你们明白. body {font-family: "Lato",sans-serif;} /* Style the tab */ div.tab { overflow: hidden; border: 1px solid #ccc; background-color: #f1f1f1; } /* Style the buttons inside the tab */ div.tab button { background-color: inherit; float: left; border: none; outline: none; cursor: pointer; padding: 14px 16px; transition: 0.3s; font-size: 17px; } /* Change background color of buttons on hover */ div.tab button:hover { background-color: #ddd; } /* Create an active/current tablink class */ div.tab button.active { background-color: #ccc; } /* Style the tab content */ .tabcontent { display: none; padding: 6px 12px; border: 1px solid #ccc; border-top: none; } <div class="tab"> <button class="tablinks" onclick="openCity(event,'tab1')" id="defaultOpen">tab 1</button> <button class="tablinks" onclick="openCity(event,'tab2')">tab 2</button> <button class="tablinks" onclick="openCity(event,'tab3')">Tab 3</button> </div> <div id="tab1" class="tabcontent"> <h1>This is tab 1</h1> </div> <div id="tab2" class="tabcontent"> <h1>This is tab 2</h1> </div> <div id="tab3" class="tabcontent"> <h1>This is tab 3</h1> </div> <script> function openCity(evt,cityName) { var i,tabcontent,tablinks; tabcontent = document.getElementsByClassName("tabcontent"); for (i = 0; i < tabcontent.length; i++) { tabcontent[i].style.display = "none"; } tablinks = document.getElementsByClassName("tablinks"); for (i = 0; i < tablinks.length; i++) { tablinks[i].className = tablinks[i].className.replace(" active",""); } document.getElementById(cityName).style.display = "block"; evt.currentTarget.className += " active"; } // Get the element with id="defaultOpen" and click on it document.getElementById("defaultOpen").click(); </script> 注意:我不想使用js来实现这些信息,所以如果可以使用PHP,请尽量避免使用js答案. 解决方法
你的php页面必须是这样的: –
<style> body {font-family: "Lato",sans-serif;} /* Style the tab */ div.tab { overflow: hidden; border: 1px solid #ccc; background-color: #f1f1f1; } /* Style the buttons inside the tab */ div.tab button { background-color: inherit; float: left; border: none; outline: none; cursor: pointer; padding: 14px 16px; transition: 0.3s; font-size: 17px; } /* Change background color of buttons on hover */ div.tab button:hover { background-color: #ddd; } /* Create an active/current tablink class */ div.tab button.active { background-color: #ccc; } .tabcontent { display: none; padding: 6px 12px; border: 1px solid #ccc; border-top: none; } </style> <?php // i assume that your page have this array available $options = array( array("name" => "Title","tab-name" => "Tab 3" ) ); ?> <div class="tab"> <?php foreach($options as $option){?> <button class="tablinks" onclick='openCity(event,"<?php echo str_replace(" ","",$option["tab-name"]);?>")' id="defaultOpen"><?php echo $option['tab-name'];?></button> <?php } ?> </div> <?php foreach ($options as $opti){?> <div id="<?php echo str_replace(' ','',$opti['tab-name']);?>" class="tabcontent"> <?php foreach($opti as $key=>$val){?> <h1><?php echo $key;?> : <?php echo $val;?></h1> <?php } ?> </div> <?php } ?> <script> function openCity(evt,""); } document.getElementById(cityName).style.display = "block"; evt.currentTarget.className += " active"; } // Get the element with id="defaultOpen" and click on it document.getElementById("defaultOpen").click(); </script> 输出: – http://prntscr.com/fydwix和http://prntscr.com/fydwnx和http://prntscr.com/fydwsg (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |