0

I am a newbie.

I have a JSON file that contains names and urls. All i want to do is get randomly selected 11 items from the JSON object and display them in HTML.

I can parse and randomly dump the content of the JSON but i don't know what to do more. I am thinking to use foreach for printing html but i don't know how to get the items from JSON object.

Here is the project in my mind;

<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">
 <?php foreach($array as $key=>$value){ ?>
<li><a href="<?php echo $key[link]; ?>"><?php echo $key[urun]; ?></a></li>
 <?php } ?>
</ul>
</div>
</div>

But unfortunately, i don't know how to fill $key[link] and $key[name] variables by randomly selected 11 items from JSON file.

Here is current code that i have;

$json = file_get_contents('./includes/one-cikanlar.json');
$json_data = json_decode($json,true);

shuffle($json_data);

echo $json_data;

Here is the JSON structure;

 {
   "urunler": [
     {
       "urun":"Matkap",
       "link":"makina-grubu?keyword=Matkap"
     },
     {
       "urun":"İş Eldivenleri",
       "link":"eldiven-grubu"      
     },
     {
       "urun":"Mikser & Karıştırıcı",
       "link":"shop?keyword=Karıştırıcı"
       },
     {
       "urun":"Silikon",
       "link":"shop?keyword=Silikon"
       },
     {
       "urun":"Tangitt",
       "link":"shop?keyword=Tangit"
       },
     {
       "urun":"Lokma Takımı",
       "link":"shop?keyword=Lokma+Takımı"
       },
     ...                                              
   ]
 }

To summarize, i need to get randomly selected 11 items from urunler object within JSON And print these selected items into HTML.

I am waiting for your suggestions and help. Thanks in advance

orko
  • 117
  • 2
  • 11

1 Answers1

2

The array with the items is in $json_data['urunler']; instead of $json_data so you could shuffle that and use for example a for loop to take 11 items.

<?php
$json_data = json_decode($json, true);
shuffle($json_data['urunler']);
?>
<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">
            <?php for ($i = 0; $i < 11; $i++) { ?>
                <li><a href="<?php echo $json_data['urunler'][$i]['link']; ?>"><?php echo $json_data['urunler'][$i]['urun']; ?></a></li>
            <?php } ?>
        </ul>
    </div>
</div>

Demo php

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
  • I think we can go somewhere from here. But current code does not run. This is the output:
    Öne Çıkanlar:
    – orko Oct 07 '18 at 11:18
  • @orko I have added a demo. Wat does not run? Do you get an error? – The fourth bird Oct 07 '18 at 11:19
  • It just prints the title "Öne Çıkanlar" and there is nothing more. There is no error or anything But this is just the output that i am getting:
    Öne Çıkanlar:
    – orko Oct 07 '18 at 11:21
  • What do you see when you run `var_dump($json_data);` or `var_dump($json_data['urunler']);` – The fourth bird Oct 07 '18 at 11:23
  • Sorry, i forgot to add JSON file: $json = file_get_contents('./includes/one-cikanlar.json'); Now it is working but styling is wrong. – orko Oct 07 '18 at 11:23
  • Öne Çıkanlar: Akü Matkap Ucu İş Eldivenleri Çizme Boya Testere Plastik Boru Makası Yapıştırıcı Kompresör Tank Ölçü Aletleri It does not print html as it should. – orko Oct 07 '18 at 11:24
  • And one more thing, how we can prevent getting duplicated items? – orko Oct 07 '18 at 11:26
  • Glad it works. For the styling, I have used the html you added to the question. For no duplicate items you might read [this page](https://stackoverflow.com/questions/307674/how-to-remove-duplicate-values-from-a-multi-dimensional-array-in-php). Try `$json_data['urunler'] = array_unique($json_data['urunler'], SORT_REGULAR);` – The fourth bird Oct 07 '18 at 11:31
  • `
    Öne Çıkanlar:
      Vidalama Plastik Boru Makası Silikon Matkap Klepe Testere Matkap Ucu Kilit Yapıştırıcı Anahtar Sprey Boya
    ` This is the output but i dould not make this line work `
  • ` It just dumps the name of the product – orko Oct 07 '18 at 11:35
  • The `'
  • ` should be inside the `
      ` and you did not add the for loop to loop the items. Check this demo with unique items and your `
    • ` in a loop. https://3v4l.org/HpSCm
  • – The fourth bird Oct 07 '18 at 11:42
  • Actually, i was trying this code inside Joomla with sourcerer pluign. The problem was caused by sourcerer algorithm, i have sourcerer's own IDE to paste the code, and it worked. Now i fixed that problem. Thank you so much. I will just have a look the the remove duplicated items. If i could not do it can i ask for your help? – orko Oct 07 '18 at 11:49
  • 1
    See my previous comment about the duplicates. It is also in the last demo https://3v4l.org/HpSCm – The fourth bird Oct 07 '18 at 11:51
  • 1
    you are the man. thanks a lot for helping. It a lot helped me as a newbie to understand and improve my way of thinking. – orko Oct 07 '18 at 13:11