2

I'm crawling through the old page with more than 10 000 comments which I'm trying to import to WordPress.

I'm using simple_html_dom.php library, which in this case is not important.

What I'm doing is getting a URL with 24 first posts crawling through them and getting an element with comments.

$url = 'http://xx/aktualnosci,wszystkie,0,'.$x.'.html'; //some URL with first 24 posts
$html = file_get_html($url);

$articlesCount = 0;
$commentsCount = 0;

foreach ($html->find('ul.news_codrugi li') as $article) { //get all 24 posts urls
    $rawLink = $article->find('a');

    foreach ($rawLink as $testLink) {    
        $link = 'http://xx/'.$testLink->href;

        $rawTitle = $testLink->href;
        $rawTitle = explode(",", $rawTitle);
        $ggTitle = $rawTitle[1];
        $htmlNew = file_get_html($link);

        foreach ($htmlNew->find('div.komentarz_lista') as $comment) { //comment element
            $comm = $comment->find('p');
            foreach ($comm as $commText) {
                $cleanerCommText = trim(strip_tags($commText));
                $item['commRaw'] = $cleanerCommText;
                $comments[] = $item;
            }
            $commentsCount++;
        }
        $articlesCount++;
    }
    //unset($articles);
}

For this moment everything is pretty fine, I've got all comments in Array. The problem is that the comments text, date and author are in

item without any class or ID so I've got no trigger to get them separately, so my array is

[0] => text, [1] => date and author, [3] => text, [4] => date and author etc

I'm trying to put it in to a new array like [text] => text, [sign] => date and author :

$x = $commentsCount;
echo $x.'<br />';

$rawComm = array_column($comments, 'commRaw');
$rawCommCount = count($rawComm);

echo 'Pobrane wpisy: '.$rawCommCount.'<br />';
$z = 0;

foreach($rawComm as $commItem) {
    if($z % 2 == 0) {
        $commArr['text']    = $commItem;
    }else{
        $commArr['sign']    = $commItem;
        //echo $commItem;
    }
    echo 'Numer wpisu: '.$z.'<br />';
    $z++;
}

In the last loop foreach($rawComm as $commItem) when I echo the values everything is fine, I've got Comment Text and Comment Date and Author printed properly. But when I'm trying to put it into a new array $commArr I'm getting double items, so my array is twice bigger with doubled everything.

And why do I need it in a new array? Because I want to put it into a DB.

So at this point, I don't know what causes this problem.

4
  • why cant you directly insert into the db with the existing array? Commented Feb 1, 2020 at 14:08
  • Because I need to get the User name of comment then walk thru another DB find his email nest it together and then put it in to end DB. Besides that, in this case I will have to do two SQL queries, one for text, then next one for update the first one with the date and author Commented Feb 1, 2020 at 14:33
  • And also in this case it doesn't matter, what I want to know is that why I'm getting everything doubled in the last loop. Commented Feb 1, 2020 at 14:54
  • Your foreach returns all ['text'] or ['sign'] in your array like $values[1]; $values[2]; . echo 'Numer wpisu: '.$z.'<br />'; $z++;. so they are not double they the values in your array with same request you ask to be displayed. Commented Feb 1, 2020 at 17:19

2 Answers 2

1

You are getting the array two times because you are adding whole array value $commItem to $commArr during both odd and even numbers using if condition. That's why you are getting array double time.

Replace your code

foreach($rawComm as $commItem) {
    if($z % 2 == 0) {
        $commArr['text']    = $commItem;
    }else{
        $commArr['sign']    = $commItem;
        //echo $commItem;
    }
    echo 'Numer wpisu: '.$z.'<br />';
    $z++;
}

to this one

foreach($rawComm as $commItem) {    
    $commArr[] = array('text'=>$commItem[0], 'sign'=>$commItem[1]);
}

I think this might work for you :).

Sign up to request clarification or add additional context in comments.

3 Comments

I dont know what OP array looks like but this solution will give an Notice: Undefined offset: error, when I try with the array in my answer on localhost.
@Dilek I am answering for MattWhistler 's question and not yours. It will not give error to him. If you need just recheck and convert for your requirement. Or else post a new question, I will surely help.
@DroidDev I cannot do it like that. Simply because $rawCom looks like that: [0] => text, [1] => sign, [2] => text, [3] => sign ... etc So every second value from this array is the signature of previous value witch contain text of comment. I cannot manage it other way because foreach ($htmlNew->find('div.komentarz_lista') as $comment) contains two <p> one is text of comment second is sign (the date and username) of comment
0

I am not a wp coder and been years I used it for a demo! You can use key like this, atleast how I would do in php.

    $a = array(
      array(
        'id' => 5698,
        'first_name' => 'Peter',
        'last_name' => 'Griffin',
      ),
      array(
        'id' => 4767,
        'first_name' => 'Ben',
        'last_name' => 'Smith',
      ),
      array(
        'id' => 3809,
        'first_name' => 'Joe',
        'last_name' => 'Doe',
      )
    );
//Collect array values excrated from foreach
    $Collected_array_result = array();
    foreach($a as $key => $value ) {
        $Collected_array_result[':'.$key] = $value;
    }
   //Create another array from that values 
    print_r($Collected_array_result);

Output

Array ( [:0] => Array ( [id] => 5698 [first_name] => Peter [last_name] => Griffin ) [:1] => Array ( [id] => 4767 [first_name] => Ben [last_name] => Smith ) [:2] => Array ( [id] => 3809 [first_name] => Joe [last_name] => Doe ) );

And how to put in db

$stmt = $pdo->prepare("INSERT INTO comments ( " . implode(', ',array_keys($a)) . ") VALUES (" . implode(', ',array_keys($Collected_array_result)) . ")");
$result = $stmt->execute($Collected_array_result);

Get names from array and create a new array with names:

$first_name = array_column($a, 'first_name', 'id');
print_r($first_name);

output

Array ( [5698] => Peter [4767] => Ben [3809] => Joe );

UPDATE : On @Dharman comment for sql injection and insert data in db using prepared statement, wasnt asked for insert query in question but in case you use that query, please filter values from array or use like following.

$first_name = array_column($a, 'first_name');
$first = implode(', ', $first_name);
 echo $first;

$last_names = array_column($a, 'last_name');
$last = implode(', ', $last_names);
 echo $last;

$id = array_column($a, 'id');
$iddd = implode(', ', $id);
 echo $iddd;

$sql = "INSERT INTO comments (first_name, last_names) VALUES (?,?)";
$stmt= $pdo->prepare($sql);
$stmt->execute([$first, $last]);

imploded all values in array and added to query 1 by 1.

7 Comments

And now how to manage it to be like: every odd value from my array got key ['text'] and every even value got key ['sign']??
array_chunk() not exactly what I wanted but doing enough :) thank you all very much for help :)
Actualy $first_name = array_column($a, 'first_name', 'id'); print_r($first_name); in answer is your solution, but print_r(array_chunk($a, 2)); will do the job is well. depends what you need.
No because my array doesn't look like that. Keys of my array are just numbers 0,1,2,3,4 .. etc depend on how mane comments are taken from posts. Every even key of my array is the text of comment and every odd key from array contains date and username for previous key
If the array keys are not constant and controlled by the programmer, your code can still be vulnerable to SQL injection. Better avoid building the statement dynamically like this.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.