Kod
<?php
$str = preg_replace_callback('#\[rozwin=(.*?){1}\](.*?){2}\[\/rozwin\]#i', 'rozwin', $str);
?>
$str = preg_replace_callback('#\[rozwin=(.*?){1}\](.*?){2}\[\/rozwin\]#i', 'rozwin', $str);
?>
Kod
<?php
function rozwin ($txt) {
$expand_id = uniqid('');
return '<div class="expand_wrapper"><a href="javascript:;" onclick="expand(\''.$expand_id.'\')" id="expand-link-'.$expand_id.'" class="expand">'.strip_tags($txt[1]).'</a><div id="expand-'.$expand_id.'" class="expand_hidden">'.$txt[2].'</div></div>';
}
?>
function rozwin ($txt) {
$expand_id = uniqid('');
return '<div class="expand_wrapper"><a href="javascript:;" onclick="expand(\''.$expand_id.'\')" id="expand-link-'.$expand_id.'" class="expand">'.strip_tags($txt[1]).'</a><div id="expand-'.$expand_id.'" class="expand_hidden">'.$txt[2].'</div></div>';
}
?>
Jeśli wyświetlam np. 5 newsów na stronie i w każdym jest jedno rozwijanie to wszystko działa, bo funkcja rozwin() wywoływana jest 5 raz. Natomiast jeśli wyświetlam jednego newsa i w nim jest 5 rozwijań to funkcja rozwin() wywoływana jest chyba tylko jeden raz, ponieważ wszystkim elementom rozwijanym nadaje ten sam numer $expand_id.
Próbowałem sam to naprawić oraz poszukać tu, na webhelpie i na google, ale bez skutku.
Jak mogę to obejść?
Jeśli to kogoś obchodzi, nie mogąc znaleźć lepszego rozwiązania, poradziłem sobie przy pomocy jQuery (polecam).
Kod
<html>
<head>
<script type="text/javascript" src="../jquery.js"></script>
<script type="text/javascript">
$(document).ready(
function() {
$("div.expand_wrapper").children("a.expand").click( function() {
$(this).next("div.expand_text").toggle();
$(this).toggleClass('contract', 'expand');
});
});
</script>
<style type="text/css">
(...)
.expand_wrapper .expand_text {
display: none;
}
</style>
</head>
<body>
<div class="expand_wrapper">
<a href="javascript:;" class="expand">Rozwiń treść</a>
<div class="expand_text">
adsf
adsf
</div>
</div>
<div class="expand_wrapper">
<a href="javascript:;" class="expand">Rozwiń treść</a>
<div class="expand_text">
adsf
adsf
adsf
</div>
</div>
</form>
</body>
</html>
<head>
<script type="text/javascript" src="../jquery.js"></script>
<script type="text/javascript">
$(document).ready(
function() {
$("div.expand_wrapper").children("a.expand").click( function() {
$(this).next("div.expand_text").toggle();
$(this).toggleClass('contract', 'expand');
});
});
</script>
<style type="text/css">
(...)
.expand_wrapper .expand_text {
display: none;
}
</style>
</head>
<body>
<div class="expand_wrapper">
<a href="javascript:;" class="expand">Rozwiń treść</a>
<div class="expand_text">
adsf
adsf
</div>
</div>
<div class="expand_wrapper">
<a href="javascript:;" class="expand">Rozwiń treść</a>
<div class="expand_text">
adsf
adsf
adsf
</div>
</div>
</form>
</body>
</html>