Dinamik iframe yüksekliği PHP içeriğine bağlı olarak?

2 Cevap php

Nasıl benim iframe yüksekliği onun içeriğine dinamik olarak ayarlayabilirsiniz.

Içeriği sadece PHP kodu, başka bir şey değildir.

Ben veritabanını sorgulamak için php kullanmak, ve sonra bazı reklamları gösterir. Daha fazla reklam, daha yüksek iframe olmalıdır.

Ben height = '100% 'bunu yapmak gerektiğini düşündüm, ama hayır ...

Ben diğer Q okudum ama bunların çözümleri benim için çalışmıyor.

Önerileriniz?

Teşekkürler

2 Cevap

Siz javascript iframe yüksekliğini ayarlamanız gerekir. JavaScript (iframe iframe ve sayfa sayfa farklı etki ise olmalıdır) iframe iç sayfa olmalıdır.

a.html:

<iframe src="b.html" id="myiframe"></iframe>

b.html:

<div style="height: 500px; background:magenta;"></div>
<script>
    parent.document.getElementById('myiframe').style.height = document.body.offsetHeight+'px';
</script>

Reklamlar sadece metin varsa, yükseklik gibi line height * number of lines kullanabilirsiniz. Ben yazı tipi boyutunu biliyor ve satır sayısı <br /> 's sayarak bulunabilir varsayalım:

<?php
// $content is your ads
// assuming ads.php returns an ad with an id, this example uses ad #10
// We are reading it with a full url because we don't want to read it as just
// a text file (we want it to be interpreted by php before we load it)
$content = file_get_contents('http://www.example.com/ads.php?id=10');
$line_height = 12; // line-height: 12px; is set in CSS
$number_of_lines = substr_count($content,'<br />'); // you can search for <br>
                                                    // and <br/> as well if you 
                                                    // need to
$height = $line_height * $number_of_lines; // You may also want to have padding

?>
<iframe src="ads.php?id=10" height="<?php echo $height; ?>" />

EDIT: $ line_height dolar font_size değiştirildi