0

I am trying to print random slections from an array. However array contains full html and it causes problems, it does not echo the whole html. It just displays the texts within the HTML. I have tried singles quotes but also did not work.

Here is the the thing what i have done;

$items = Array('<div class="custom-top-tags">
<div class="sm-top-tags-title">Öne Çıkanlar:</div>
<div class="sm-top-tags-keywords">
<ul class="sm-top-tags-keywords-list">
<li><a href="makina-grubu?keyword=Matkap">Matkaplar</a></li>
<li><a href="eldiven-grubu">İş Eldivenleri</a></li>
<li><a href="shop?keyword=Karıştırıcı">Mikser & Karıştırıcı</a></li>
<li><a href="shop?keyword=Silikon">Silikon</a></li>
<li><a href="shop?keyword=Tangit">Tangitt</a></li>
<li><a href="shop?keyword=Lokma+Takımı">Lokma Takımı</a></li>
<li><a href="shop?keyword=Plastik+Boru+Makası">Plastik Boru Makası</a></li>
<li><a href="shop?keyword=El+Arabası">El Arabası</a></li>
<li><a href="shop?keyword=Vana">Vanalar</a></li>
<li><a href="boya-grubu">Boya</a></li>
<li><a href="makina-grubu?keyword=Kompresör">Kompresör</a></li>
</ul>
</div>
</div>',
'<div class="custom-top-tags">
<div class="sm-top-tags-title">Öne Çıkanlar:</div>
<div class="sm-top-tags-keywords">
<ul class="sm-top-tags-keywords-list">
<li><a href="makina-grubu?keyword=Matkap">Matkaplar</a></li>
<li><a href="eldiven-grubu">İş Eldivenleri</a></li>
<li><a href="shop?keyword=Karıştırıcı">Mikser & Karıştırıcı</a></li>
<li><a href="shop?keyword=Silikon">Silikon</a></li>
<li><a href="shop?keyword=Tangit">Tangitt</a></li>
<li><a href="shop?keyword=Lokma+Takımı">Lokma Takımı</a></li>
<li><a href="shop?keyword=Plastik+Boru+Makası">Plastik Boru Makası</a></li>
<li><a href="shop?keyword=El+Arabası">El Arabası</a></li>
<li><a href="shop?keyword=Vana">Vanalar</a></li>
<li><a href="boya-grubu">Boya</a></li>
<li><a href="makina-grubu?keyword=Kompresör">Kompresör</a></li>
</ul>
</div>
</div>');


echo $items[array_rand($items)];

And my expectaion is displaying randomly selected HTML code.

Meaning, i need to choose different links randomly from a list and i need to display them in html with codes as seen below.

<div class="custom-top-tags">
    <div class="sm-top-tags-title">Öne Çıkanlar:</div>
    <div class="sm-top-tags-keywords">
    <ul class="sm-top-tags-keywords-list">
    <li><a href="makina-grubu?keyword=Matkap">Matkaplar</a></li>
    <li><a href="eldiven-grubu">İş Eldivenleri</a></li>
    <li><a href="shop?keyword=Karıştırıcı">Mikser & Karıştırıcı</a></li>
    <li><a href="shop?keyword=Silikon">Silikon</a></li>
    <li><a href="shop?keyword=Tangit">Tangitt</a></li>
    <li><a href="shop?keyword=Lokma+Takımı">Lokma Takımı</a></li>
    <li><a href="shop?keyword=Plastik+Boru+Makası">Plastik Boru Makası</a></li>
    <li><a href="shop?keyword=El+Arabası">El Arabası</a></li>
    <li><a href="shop?keyword=Vana">Vanalar</a></li>
    <li><a href="boya-grubu">Boya</a></li>
    <li><a href="makina-grubu?keyword=Kompresör">Kompresör</a></li>
    </ul>
    </div>
    </div>

Waiting for the suggestions and helps.

4
  • However array contains full html and it causes problems - what problems? Commented Oct 6, 2018 at 19:21
  • it cannot fully print the html content, it just displays Text Commented Oct 6, 2018 at 19:24
  • 1
    This array contains only one entry! Commented Oct 6, 2018 at 20:46
  • I know that copied mistakenly the same content Commented Oct 6, 2018 at 20:48

2 Answers 2

1

The line breaks in you code won't work... I would suggest to split the code into a temp variable and then array allocation And use the proper multi lkne syntax

1st of all for styling purposes, and also to prevent this error

You should use $tempvar and the code I wrote for each array insertion

$items  = array();
$tempvar = <<<TEMP
    <div class="custom-top-tags">
 <div class="sm-top-tags-title">Öne Çıkanlar:</div> 
<div class="sm-top-tags-keywords">
 <ul class="sm-top-tags-keywords-list"> <li><a href="makina-grubu?keyword=Matkap">Matkaplar</a></li> <li><a href="eldiven-grubu">İş Eldivenleri</a></li> 
<li><a href="shop?keyword=Karıştırıcı">Mikser & Karıştırıcı</a></li> 
<li><a href="shop?keyword=Silikon">Silikon</a></li> 
<li><a href="shop?keyword=Tangit">Tangitt</a></li> <li><a href="shop?keyword=Lokma+Takımı">Lokma Takımı</a></li> 
<li><a href="shop?keyword=Plastik+Boru+Makası">Plastik Boru Makası</a></li>
 <li><a href="shop?keyword=El+Arabası">El Arabası</a></li> <li><a href="shop?keyword=Vana">Vanalar</a></li> 
<li><a href="boya-grubu">Boya</a></li> <li><a href="makina-grubu?keyword=Kompresör">Kompresör</a></li> </ul> </div> </div>
TEMP;

    $items[] = $tempvar;

    ... 
Sign up to request clarification or add additional context in comments.

10 Comments

Thank you but how can i randomly select them? should i create two differen tempvar?
no no, I used the 3 dots to save the reptitiveness of the solution... you should use tempvar 3 times and use array_rand as you did
Oohh ok, i will try it now
I could not make it work, would you help me with that?
@orko sure, can you elaborate on what exactly broke? try to print the 1st tempvar right after the allocation. what do you get?
|
0

that is one possible solution

echo htmlspecialchars($items[array_rand($items)]);

1 Comment

for custom output echo "<pre>".htmlspecialchars($items[array_rand($items)])."</pre>";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.