Quantcast
Channel: DaniWeb Solved Topics
Viewing all articles
Browse latest Browse all 582

how to fetch array data from one table and insert into another table in php

$
0
0

I have a table called volume_issue that looks like this:

 +----+-------------------------------------------------------+-----------------+-----------+---------------------+
| id | url                                                   | journal_title   | issn      | volume_issue        |
+----+-------------------------------------------------------+-----------------+-----------+---------------------+
|  1 | https://www.ajol.info/index.php/asan/issue/view/17048 | Africa Sanguine | 1560-8646 | Vol 19, No 2 (2018) |
|  2 | https://www.ajol.info/index.php/asan/issue/view/16693 | Africa Sanguine | 1560-8646 | Vol 19, No 1 (2017) |
+----+-------------------------------------------------------+-----------------+-----------+---------------------+

I am trying to extract all records associated with the urls listed in the variable url and insert them in a table known as citations. The code i am using to do this lookw like this:

<?php
$query1 = "SELECT * FROM volume_issue where id between 1 and 2";
$sql = $con->prepare($query1);
$sql->execute();
$sql->SetFetchMode(PDO::FETCH_ASSOC);
while ($row = $sql->fetch()) {
    $volume_issue_id = $row['id'];
    $url = $row['url'];
    $volume_issue = $row['volume_issue'];
    $journal_title = $row['journal_title'];
    $html2 = file_get_html($url);
    $html2 = file_get_html($url);

    foreach ($html2->find('table[class="tocArticle"]') as $div2) {
        $DOM = new DOMDocument();
        $DOM->loadHTML($div2);
        $Detail = $DOM->getElementsByTagName('td');

        $i = 0;
        $j = 0;
        foreach ($Detail as $sNodeDetail) {
            $aDataTableDetailHTML[$j][] = trim($sNodeDetail->textContent);
            $j = $i % count($aDataTableDetailHTML[$j]) == 0 ? $j + 1 : $j;
        }
    }
}

$result = [];
foreach ($aDataTableDetailHTML as $key => $value) {
    foreach ($value as $key1 => $value1) {
        echo '<pre>';
        $result[$key1][] = $value1;
    }
}
array_walk($result, function(&$item) {
    $item = implode('|', $item);
});

foreach ($result as $ref) {
    $newref[] = explode('|', $ref);
}

print_r($newref);

foreach ($newref as $key => $values) {
    $title = $values[0];

    $authors = str_replace(',            ',', ',$values[2]);

    $pages = $values[3];
    $doi = $values[4];
    $query2 = "INSERT INTO citations (title,authors,journal_title,volume_issue,pages,doi) VALUES (:title,:authors,:journal_title,:volume_issue,:pages,:doi)";
    $sql2 = $con->prepare($query2);
    $sql2->execute(array(
        ':title' => $title,
        ':authors' => $authors,
        ':journal_title' => $journal_title,
        ':volume_issue' => $volume_issue,
        ':pages' => $pages,
        ':doi' => $doi,
    ));
}

My problem is that this code is mot inserting journal title and volume_issue correctly as desired. How can i make this code insert the right volume issue for the right citation? I am on my wits end.


Viewing all articles
Browse latest Browse all 582

Latest Images

Trending Articles



Latest Images