PHP MySQL : inserting Json response values into database by iterating through loops -


i have json response want store in database. here unable loop through values within inner loops.

json twitter data:

array (   [0] => stdclass object   (     [created_at] => thu apr 23 05:23:39 +0000 2015     [id_str] => 591110161239945216     [entities] => stdclass object     (       [hashtags] => array       (         [0] => stdclass object         (           [text] => helloworld           [indices] => array           (             [0] => 0             [1] => 11           )         )       )      )    )         ) 

here trying access [text]=>helloworld field , insert database. want iterate through loop using while. returns me error if there no hashtags posted.

code :

i=0 while($i<3) {    $tweet_id     = $json[$i]->id_str;    $created_at   = $json[$i]->created_at;    $hashtag_text = $json[$i]->entities->hashtags[$i]->text;     $this->sql="insert twitter_scan('tweetid','created_at','hashtag_text').                        values('$tweet_id','$created_at','$hashtag_text')";    $this->query=mysqli_query($this->conn,$this->sql);    $i++; } 

i have 3 tweets in account out of 1 tweet has #hashtag other 2 plain text. when run code, 1st iteration works well, 2nd , 3rd iteration returns me error php notice:undefined offset: 1 , php notice:undefined offset: 2.

this because other 2 tweets contain no hashtags , still code trying access it. here want store null value database when there no value field. how can achieve ?

thank help.

there 1 hashtag on 0, 2nd & 3rd loop $i 1, 2 not present. replace $i 0 & add check else store blank $hashtag_text. try -

$hashtag_text = !empty($json[$i]->entities->hashtags[0]->text) ? $json[$i]->entities->hashtags[0]->text : ''; 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -