2017-03-22 26 views
-3

this siteからPHPでデータを取得して表示したいと考えています。PHPでテーブルにデータ(ウェブサイトから取得)を表示

PHPのpregmatch関数を使用して、3つの異なるテーブルで目的の値(記事名、価格、およびその他の値)を取得しました。まだ残っているのは、2次元の表にそれらを表示することです。

テーブルの最初の行に商品名と価格が必要です。行の残りの部分には、その値の後ろに続くタイトルが含まれていなければなりません。

これは私の現在のPHPコードです:

<?php 

$debut="https://www.agriconomie.com"; 
$txt = file_get_contents('https://www.agriconomie.com/pieces-agricoles/tracteur/attelage---relevage/pc2902'); /*ici c'est pour Lire la page html*/ 

$results = array(); 
// $test = preg_match_all('#<a href="(.*?)">#', $txt, $names_array); 

$test = preg_match_all('#<a href="(.+)" class="(.+)" title="(.+)"#', $txt, $names_array); 

/*recupéré les liens du site en particuliers le text qui se situe entre griffe "" du href*/ 

for($i = 0; $i < count($names_array[1]); $i++) 
{ 
    $j=$i; 

    $debut="https://www.agriconomie.com".$names_array[1][$i]; 

    $adresse =$debut; 
    /* echo $adresse ; ?> <br /> <?php */ 

    $page = file_get_contents ($adresse); 

    /* preg_match_all ('#<h3 class="product-name">(.+)</h3>#', $page, $names_array5); */      
    preg_match_all ('#(<dd>(.+)</dd>)#', $page, $names_array2); 
    preg_match_all ('#<span><i class="icon-chevron-right"></i>(.*?)</span>#', $page, $names_array3); 
    preg_match_all ('#<p class="price" itemprop="price" content="(.*?)">#', $page, $names_array4); 

    echo "<center>"; 

    echo "<table class='table table-bordered table-striped table-condensed'>"; 

    /* 
    for($j = 0; $j < count($names_array5[1]); $j++) 
    { 
     $NOM = $names_array5[1][$j]; 

     echo 'Nom ='.$NOM ; 
    } 
    */ 

    for($j = 0; $j < count($names_array4[1]); $j++) 
    { 
     $price = $names_array4[1][$j]; 
     echo  'Prix ='.$price.'$' ; 
    } 


    for($i = 0; $i < count($names_array3[1]); $i++) 
    { 
     for($j= 0; $j < count($names_array2[1]); $j++){ 
      $descriptif = $names_array2[1][$i]; 
     } 

     $intitule = $names_array3[1][$i]; 
     echo "<tr><td>".$intitule." </td> <td> ".$descriptif." </td> </tr> "; 
    } 
} 

echo "</table>"; 
echo "</center>"; 

?> 
+0

質問を投稿する前にコードの書式を設定してください。 –

+0

コードをフォーマットするにはどうすればよいですか?ここに私のソースコードです!私を助けてください 。 –

+0

あなたのコードは@Qirelによってフォーマットおよび編集されました –

答えて

0

私は、多くの物事が整理整頓/まで訂正することが分かったので、私はほぼ完全な書き直しをしました。

$debut="https://www.agriconomie.com"; 
$txt = file_get_contents('https://www.agriconomie.com/pieces-agricoles/tracteur/attelage---relevage/pc2902'); 

if(!preg_match_all('#<a href="([^"]*?)".*?title="([^"]*?)"#',$txt,$desarticles)){exit("Failure @ desarticles");} 
foreach($desarticles[1] as $i=>$url_ext){ 
    $page=file_get_contents("https://www.agriconomie.com{$url_ext}"); // https://www.agriconomie.com/clips-ordinaire-de-9x45-le-cent/p207990 

    if(!preg_match_all('#<p class="price" itemprop="price" content="(.*?)">#',$page,$desprix)){exit("Failure @ desprix ($i)");} 
    if(!preg_match_all('#<i class="icon-chevron-right"><\/i>(.*?)<\/span>.*?<dd>(.+)<\/dd>#s',$page,$information)){exit("Failure @ information ($i)");} 

    echo "<center>"; 
     echo "<table class='table table-bordered table-striped table-condensed'>"; 
      echo "<tr>"; 
       echo "<td>{$desarticles[2][$i]}</td>"; // borrow $i from iteration of $desarticles[1] 
       echo "<td>Prix ={$desprix[1][0]}$</td>"; // Price (only one per loop) 
      echo "</tr>"; 
      foreach($information[1] as $k=>$info){ 
       echo "<tr>"; 
        echo "<td>{$info}</td>"; 
        echo "<td>{$information[2][$k]}</td>"; // borrow $k from iteration of $information[1] 
       echo "</tr>"; 
      } 
     echo "</table>"; 
    echo "</center>"; 
} 

細かい点のいくつかは:

  • 私は$ desarticlesに正規表現をスピードアップし、中央のキャプチャグループを省略しました。
  • 不要な変数を削除しました。
  • 私は、ループのfor iterate/countをforeachループで置き換えました(カウント条件を避けるために)。
  • 2つのpreg_match_all行を$ informationという名前の行にマージしました。
  • $情報の正規表現の終了タグをエスケープしました。
  • 私は、要求どおりにきれいな基本的な2列テーブル構造を作成しました。
+0

@RubenLamboniBinban Voila、私はあなたの要求に応え、プロセスを改善するために最善を尽くしました。私はこのコードをテストしなかったので、何かが間違っていたら私の答えを修正できるように私にコメントを残してください。私はこの質問に多くの時間を費やしました。それが満足すれば、あなたはグリーンティックと一緒にアップコートを提供するように頼みます。自分のコードがどのように機能するのか楽しみにお待ちしています。 – mickmackusa

関連する問題