1. Halo Guest, pastikan Anda selalu menaati peraturan forum sebelum mengirimkan post atau thread baru.

Simple-HTML-Dom Foreach Loop, ada yang bisa bantu?

Discussion in 'Pemrograman Web' started by Hornett, Sep 26, 2019.

  1. Hornett

    Hornett New Comer

    Joined:
    Apr 14, 2016
    Messages:
    11
    Likes Received:
    2
    Permisi suhu dan master PHP di forum, saya mohon bantuannya, dengan koding ini
    saya bingung dimana peletakan kode if ($i > 5) {break;} yang benar supaya bisa melakukan loop sebanyak 5x, karena saya coba disini cuma bisa loop 1x lalu berhenti.

    Code:
    $html = file_get_html($url);
    $thedata = [];
    $i = 0;
    foreach ($html->find('div.gallery') as $data)
    {
        if ($i > 5) {break;}
      
            foreach ($data->find('tr.*[id]') as $row)
            {
                $item['title'] = $row->find('td a.gallery_title',0)->plaintext;
                $item['url'] = $row->find('td a.gallery_title',0)->href;
             }  
          
            foreach ($data->find('td[colspan=3]') as $img) {
                $item['image'] = $img->find('img.gallery_preview',0)->src;
        }
    
    $thedata[] = $item;
    $i++;
          
    }
    
    Tolong diberi petunjuk ya suhu, terima kasih.
     
  2. reival

    reival AdSense Fan

    Joined:
    Aug 26, 2011
    Messages:
    118
    Likes Received:
    18
    Coba:


    Code:
    foreach ($html->find('div.gallery') as $data)
    {
       if ($i > 5) {break;}
     
    Ganti:

    Code:
    foreach ($html->find('div.gallery') as $k=>$data)
    {
       if ($k > 5)break;
     
    
     
  3. Hornett

    Hornett New Comer

    Joined:
    Apr 14, 2016
    Messages:
    11
    Likes Received:
    2
    Sama saja suhu, tetep 1x loop
     
  4. kimbut

    kimbut AdSense Pro

    Joined:
    Jan 11, 2010
    Messages:
    449
    Likes Received:
    22
    Location:
    Localhost
    kalau tanpa if itu bisa looping banyak gan?
     
  5. xrvel

    xrvel Super Hero

    Joined:
    Oct 21, 2007
    Messages:
    2,850
    Likes Received:
    930
    Location:
    Di sini
    PHP:
    $html file_get_html($url);
    $thedata = [];
    $i 0;
    foreach (
    $html->find('div.gallery') as $data)
    {
        if (
    $i 5)
        {
            foreach (
    $data->find('tr.*[id]') as $row)
            {
                
    $item['title'] = $row->find('td a.gallery_title',0)->plaintext;
                
    $item['url'] = $row->find('td a.gallery_title',0)->href;
            }

            foreach (
    $data->find('td[colspan=3]') as $img) {
                
    $item['image'] = $img->find('img.gallery_preview',0)->src;
            }

            
    $thedata[] = $item;
        }
        
    $i++;

    }
     

Share This Page