v2.5.2
Giriş yap

PHP Simple Html Dom ile tablodan veri çekmek

siyahkalem
604 defa görüntülendi ve 2 kişi tarafından değerlendirildi

PHP Simple html dom kütüphanesini kullanarak ilgili tablodan veri çekiyorum. Ancak bir sorum var.

Tablolarda sınıf olmadığı için indeks numarasına göre veri alıyorum. Yılların günlerini almak istediğimde, "exaple: By 2021" td etiketi değil de th etiketi geldiğinde kaymalar oluyor.

Bu durumda hem yıllar hem de yıllar için gün sayısını nasıl alabilirim?

    // kodlarınız buraya eklenecek
    
    $total = 0;
 for($i = 7; $i <= 455; $i += 15){

        $year       = $html_base->find('td',$i)->plaintext;
        $days       = $html_base->find('td',$i + 2)->plaintext;

        $total += $html_base->find('td',$i + 3)->plaintext;
            if($year == "2020/11"){ break; } 

     }


<table class="resultTable table table-striped table-bordered">

    <thead style="font-size: 12px">
        <tr>
            <th scope="col">aaa</th>
            <th scope="col">aaa</th>
            <th scope="col">aaa</th>
            <th scope="col">aaa</th>
            <th scope="col">aaa</th>
            <th scope="col">aaa</th>
            <th scope="col">aaa</th>
            <th scope="col">Year / Month</th>
            <th scope="col">aaa</th>
            <th scope="col">aaa</th>
            <th scope="col">Days</th>
            <th scope="col">aaa</th>
            <th scope="col">aaa</th>
            <th scope="col">aaa</th>
            <th scope="col">aaa</th>        
        </tr>
    </thead>
    <tbody style="font-size: 12px">
        <tr>
            <td>xxx</td>
            <td>xxx</td>
            <td>xxxx</td>
            <td>xxx</td>
            <td>xxx</td>
            <td>xxxx</td>
            <td>xxx</td>
            <td>2022/01</td>
            <td>xxx</td>
            <td>xxx</td>
            <td>30</td>
            <td>xxxx</td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
        <tr>
            <th colspan="10">2022 Year</th>
            <td>yyyy</td>
            <td colspan="5">yyy</td>
        </tr>

        <tr>
            <td>xxx</td>
            <td>xxx</td>
            <td>xxxx</td>
            <td>xxx</td>
            <td>xxx</td>
            <td>xxxx</td>
            <td>xxx</td>
            <td>2021/12</td>
            <td>xxx</td>
            <td>xxx</td>
            <td>25</td>
            <td>xxxx</td>
            <td></td>
            <td></td>
            <td></td>
        </tr>

        <tr>
            <td>xxx</td>
            <td>xxx</td>
            <td>xxxx</td>
            <td>xxx</td>
            <td>xxx</td>
            <td>xxxx</td>
            <td>xxx</td>
            <td>2021/11</td>
            <td>xxx</td>
            <td>xxx</td>
            <td>25</td>
            <td>xxxx</td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
        <tr>
            <td>xxx</td>
            <td>xxx</td>
            <td>xxxx</td>
            <td>xxx</td>
            <td>xxx</td>
            <td>xxxx</td>
            <td>xxx</td>
            <td>2021/10</td>
            <td>xxx</td>
            <td>xxx</td>
            <td>25</td>
            <td>xxxx</td>
            <td></td>
            <td></td>
            <td></td>
        </tr>

        <tr>
            <td>xxx</td>
            <td>xxx</td>
            <td>xxxx</td>
            <td>xxx</td>
            <td>xxx</td>
            <td>xxxx</td>
            <td>xxx</td>
            <td>2022/01</td>
            <td>xxx</td>
            <td>xxx</td>
            <td>30</td>
            <td>xxxx</td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
        <tr>
            <th colspan="10">2021 Year</th>
            <td>yyyy</td>
            <td colspan="5">yyy</td>
        </tr>
        <tr>
            <td>xxx</td>
            <td>xxx</td>
            <td>xxxx</td>
            <td>xxx</td>
            <td>xxx</td>
            <td>xxxx</td>
            <td>xxx</td>
            <td>2020/12</td>
            <td>xxx</td>
            <td>xxx</td>
            <td>25</td>
            <td>xxxx</td>
            <td></td>
            <td></td>
            <td></td>
        </tr>           

        <tr>
            <td>xxx</td>
            <td>xxx</td>
            <td>xxxx</td>
            <td>xxx</td>
            <td>xxx</td>
            <td>xxxx</td>
            <td>xxx</td>
            <td>2020/11</td>
            <td>xxx</td>
            <td>xxx</td>
            <td>25</td>
            <td>xxxx</td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
        <tr>
            <th colspan="10">2020 Year</th>
            <td>yyyy</td>
            <td colspan="5">yyy</td>
        </tr>
    </tbody>
</table>



makifgokce
1013 gün önce

tbody içerisindeki tüm tr etiketlerini foreach döngüsüyle dönerek
içindeki ilk elementin tag ı th ise farklı değil ise farklı işlem yapabilirsin.
->children() ile elementin içindeki elementlere erişebilirsin.

foreach($html_base->find('tbody tr') as $elem){
	if($elem->children(0)->tag == "th"){
		echo $elem->children(0)."<br>";
	} else {
		$year = $elem->children(7);
		$days = $elem->children(9);
		echo $year." - ".$days."<br>";
	}
}