为何在数据库中插入一些不需要的值

时间:2017-08-17 22:07:37

标签: php mysql

我写了下面的php脚本后:

<?php

include_once ('C:\Users\fabio.gatta\OneDrive - Accenture\MySite\moduli\simple_html_dom.php');

$url = 'https://www.sports-reference.com/olympics/summer/1896/ATH/';
$tagname_h1 = 'h1';
$tagname_tr = 'tr  class=""';


    $olympiad = array();
    $html = file_get_html($url,true);
    foreach($html->find($tagname_h1) as $tag) {
        $olympiad[] = trim($tag->innertext);
    }


    $result_oly=explode(" ",$olympiad[0]);

    $year = $result_oly[3];
    $city = $result_oly[4];
    $season = $result_oly[5];
    $sport = $result_oly[0];

//Prendo le informazioni dei vincitori

    $athletes = array();
    $country = array();

    foreach($html->find($tagname_tr) as $tag)
    {
        foreach($tag->find('td') as $td)
        {
            $a_tags = $td->find('a');
           $img_tags = $td->find('img');

            if(count($a_tags) ==0)
            {
                $ath ="";
            }
            elseif(count($a_tags)==1)
            {               
                $ath = $a_tags[0]->innertext;


            }
            else
            {
                $ath = array();
                foreach($a_tags as $a)
                {
                    $ath[] = $a->innertext;

                }
            }
            $athletes[] = $ath;

            if(count($img_tags) ==0)
            {
                $coun ="";
            }
            elseif(count($img_tags)==1)
            {               
                $coun = $img_tags[0]->alt;

            }
            else
            {
                $coun = array();
                foreach($img_tags as $alt)
                {
                    $coun[] = $alt->alt;

                }
            }

            $country[] = $coun;         
        }

        $medals = ["gold", "silver", "bronze"];

foreach ($athletes as $key => $values) {

    if (strpos($values,"'s ") > 0){

        $gender="";
        $event="";

        list($gender,$event)=explode("'s ",$values);
    }   

           //connessione al database

       $link = mysqli_connect('192.168.0.97', 'root', 'admin','Olympics');
        if (!$link) {
        die('Could not connect: ' . mysqli_error());
         }
            echo 'Connected successfully'. '<br>';

     //prendo gli ori
      if ($key == 1){

        if (count($values) == 1){

         $ath_key = $athletes[$key];
         $country_key = $country[$key];
         $medals_key = $medals[$key-1];

         mysqli_query($link,"INSERT INTO MedalResults (Year,City,Season,Sport,Event,Athlete,Gender,Country,Medal) VALUES ('$year', '$city', '$season', '$sport', '$event', '$ath_key',' $gender', '$country_key', '$medals_key')");

        }

        elseif (count($values) > 1){

            foreach($values as $sub_key => $sub_values){

                $ath_key = $athletes[$sub_key+1];
                $country_key = $country[$sub_key+1];
                $medals_key = $medals[$key-1];

              mysqli_query($link,"INSERT INTO MedalResults (Year,City,Season,Sport,Event,Athlete,Gender,Country,Medal) VALUES ('$year', '$city', '$season', '$sport', '$event', '$ath_key',' $gender', '$country_key', '$medals_key')");

        }
        }
       }

       //prendo gli argenti

       if ($key == 2){

        if (count($values) == 1){

          $ath_key = $athletes[$key];
         $country_key = $country[$key];
         $medals_key = $medals[$key-1];     

         mysqli_query($link,"INSERT INTO MedalResults (Year,City,Season,Sport,Event,Athlete,Gender,Country,Medal) VALUES ('$year', '$city', '$season', '$sport', '$event', '$ath_key',' $gender', '$country_key', '$medals_key')");

        }

        elseif (count($values) > 1){

            foreach($values as $sub_key => $sub_values){

             $ath_key = $athletes[$key];
            $country_key = $country[$key];
            $medals_key = $medals[$key-1];


             mysqli_query($link,"INSERT INTO MedalResults (Year,City,Season,Sport,Event,Athlete,Gender,Country,Medal) VALUES ('$year', '$city', '$season', '$sport', '$event', '$ath_key',' $gender', '$country_key', '$medals_key')");
        }
        }
       }

       //prendo i bronzi
       if ($key == 3){

        if (count($values) == 1){        

           $ath_key = $athletes[$key];
            $country_key = $country[$key];
            $medals_key = $medals[$key-1];

           mysqli_query($link,"INSERT INTO MedalResults (Year,City,Season,Sport,Event,Athlete,Gender,Country,Medal) VALUES ('$year', '$city', '$season', '$sport', '$event', '$ath_key',' $gender', '$country_key', '$medals_key')");

        }

        elseif (count($values) > 1){

            foreach($values as $sub_key => $sub_values){

             $ath_key = $athletes[$key];
            $country_key = $country[$key];
            $medals_key = $medals[$key-1];

             mysqli_query($link,"INSERT INTO MedalResults (Year,City,Season,Sport,Event,Athlete,Gender,Country,Medal) VALUES ('$year', '$city', '$season', '$sport', '$event', '$ath_key',' $gender', '$country_key', '$medals_key')");

            }
        }
       }

        mysqli_close($link);
  }

   unset($athletes);
   unset($country);

    }   

?>

在数据库中,我有一些不需要的行,如:

1896 Athina Summer Athletics 100 metres Tom Burke Men USA gold
1896 Athina Summer Athletics 100 metres Fritz Hofmann Men GER silver
1896 Athina Summer Athletics 100 metres Array Men Array bronze
1896 Athina Summer Athletics 100 metres Array Men Array bronze

为什么我得到阵列而不是运动员姓名和国家名称?

第二个问题:一些运动员和国家阵列元素是空的,并且由于if语句我认为排除它们,但我仍然得到运动员和国家null的行。怎么避免这个?

1 个答案:

答案 0 :(得分:0)

你得到的是字符串数组,而不是运动员姓名和国家,其中不止一名运动员在比赛中获得了奖牌。这是1896年100米青铜的情况,其中Alajos Szokoly和Frank Lane画了。因此,它不是不需要的数据,而是复杂的数据。在这种情况下,您的代码不会解析为return sap.ui.core.format.DateFormat.getDateInstance({ pattern: "dd-MM-yyyy" }).format(val).replace(/\-/g, '.');$athletes数组中的特定元素,因此数组存储为占位符以表示数组中的多个元素。解决方案是调整页面刮擦算法,以便破坏数据结构的规范化,并允许将两个运算符存储在一个数组元素中,或者扩展存储算法和数据结构以适应额外的复杂性。