0

I have a foreach to elaborate some values from my web site and I'd like to insert them into a mysql database.

My problem is where I have an array, because I don't know how to treat them in this case.

This is my code:

   ...

$titles = $html->find("a[class=in-match]"); // 1 per match
$result = $html->find("td[class=h-text-center]/a"); // 1
$best_bets = $html->find("td[class=table-matches__odds colored]/span/span/span"); // 1
$odds = $html->find("td[class=table-matches__odds]"); // 2

function print_odd($odd) {
    if (array_key_exists('data-odd', $odd->attr)) {
        return $odd->attr['data-odd'];
    }

    return $odd->children(0)->children(0)->children(0)->attr['data-odd'];
}

$c=0; $b=0; $o=0; $z=0; // two counters
foreach ($titles as $match) {
    list($num1, $num2) = explode(':', $result[$c++]->innertext); // <- explode
    $num1 = intval($num1);
    $num2 = intval($num2);
    $num3 = ($num1 + $num2);
    if ($num3 > 1) {
        $over15 = "OK";
    } else {
        $over15 = "NO";
    }

    echo "<tr><td class='rtitle'>".
        "<td class='first-cell'>".$match->innertext."</td> ".            
        "<td>".$match->innertext."</td><td> ".$num1.'</td><td> : </td><td>'.$num2 .  " / " .  // <- example use
        "<td class='first-cell'>".$num3 ."</td> "  .
        "<td class='first-cell'>".$over15 ."</td> "  .
        "<td class='odds'>".print_odd($odds[$b++]) . ";" .
            "".print_odd($odds[$b++]) . ";" .
            "".print_odd($odds[$b++]) . "</td>" .
            "</td></tr><br/>";


    $servername = "xx.xxx.xxx.xxx";
    $username = "xxx";
    $password = "xxx";
    $dbname = "xxxxxxxx";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 

    $sql = "INSERT INTO risultati (titles, scorehome, scoreaway, best_bets)
    VALUES ('$match', '$num1', '$num2', '$odds');";

    if ($conn->multi_query($sql) === TRUE) {
        echo "New records created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
    $conn->close();

When I run this, I don't get that 3 "$odds"

Thank you for your attention

EDIT So, I saved almost all data with my code but I have a problem with $odds (You can see my function print_odd). How could to save them?

James69
  • 229
  • 1
  • 6
  • 17

1 Answers1

0

Try the concept... Thanks

$columns = implode(", ",array_keys($insData));
$escaped_values = array_map('mysql_real_escape_string', array_values($insData));
$values  = implode(", ", $escaped_values);
$sql = "INSERT INTO `fbdata`($columns) VALUES ($values)";
Nipun
  • 157
  • 5
  • I tried but maybe I didn't get how to apply in my case... could you give me an example, please? – James69 Jan 18 '17 at 13:07