mysqli fetch array return null as result - php

have no idea my result return nothing
if ($stmt2->execute()) {
$photo_items = $stmt2->get_result();
while ($imgArray = $photo_items->fetch_array()) {
}
echo $imgArray[] = $imgArray;
}
and I also tried this
echo $imgArray[] = $imgArray['mycolname'];

Try
while ($img = $photo_items->fetch_array()) {
$imgArray[] = $img;
}
To view the array elements, try:
print_r($imgArray);

Related

json array is repeating index

this is my code :
if ($API->connect("192.168.81.130", "admin", "")) {
$API->write('/ip/route/print', false);
$API->write('=.proplist=.id', false);
$API->write('=.proplist=dst-address', false);
$API->write('=.proplist=pref-src', false);
$API->write('=.proplist=gateway');
$result = $API->read();
$API->disconnect();
foreach ($result as $route){
$response['id'] = $route['.id'];
$response['dst-address'] = $route['dst-address'];
if (isset($route['pref-src'])) {
$response['pref-src'] = $route['pref-src'];
} else {
$response['pref-src'] = "";
}
$response['gateway'] = $route['gateway'];
$array[] = $response;
echo json_encode($array);
}
}
and the output is :
[{"id":"*2","dst-address":"0.0.0.0\/0","pref-src":"","gateway":"192.168.1.1"}][{"id":"*2","dst-address":"0.0.0.0\/0","pref-src":"","gateway":"192.168.1.1"},{"id":"*420639B3","dst-address":"192.168.81.0\/24","pref-src":"192.168.81.130","gateway":"ether1"}]
result for "[{"id":"*2","dst-address":"0.0.0.0/0","pref-src":"","gateway":"192.168.1.1"}]" is show twice.
i want the out put like this :
> [{"id":"*2","dst-address":"0.0.0.0\/0","pref-src":"","gateway":"192.168.1.1"},{"id":"*420639B3","dst-address":"192.168.81.0\/24","pref-src":"192.168.81.130","gateway":"ether1"}].
can anyone help me?.
You need to initialize your array each time round the loop otherwise you are just adding to it each time, hence the duplication
You also need to move the echo of the json string to outside the loop
foreach ($result as $route){
$response = array();
$response['id'] = $route['.id'];
$response['dst-address'] = $route['dst-address'];
if (isset($route['pref-src'])) {
$response['pref-src'] = $route['pref-src'];
} else {
$response['pref-src'] = "";
}
$response['gateway'] = $route['gateway'];
$array[] = $response;
}
echo json_encode($array);

PHP String in Array returns only first charachter

I have a php function to display a list of revslider's sliders (wp plugin), the string returns only the first letter of the sliders' names
here is my code :
function jobboard_revslider(){
if (class_exists('RevSlider')) {
$theslider = new RevSlider();
$arrSliders = $theslider->getArrSliders();
$arrA = array();
$arrT = array();
foreach($arrSliders as $slider){
$arrA[] = $slider->getAlias();
$arrT[] = $slider->getTitle();
}
if($arrA && $arrT){
$result = array_combine($arrA, $arrT);
}
else
{
$result = false;
}
return $result;
}
}
I tried all I know and other answers around here but no hope.
I would really appreciate a push !
Thanks
Check sizeof ($ array)> 0 do this for both arrays. Also just try to echo what you are getting in getalias and gettitle methods before storing it in array.
function jobboard_revslider(){
if (class_exists('RevSlider')) {
$theslider = new RevSlider();
$arrSliders = $theslider->getArrSliders();
$arrA = array();
$arrT = array();
foreach($arrSliders as $slider){
$arrA[] = substr($slider->getAlias(), 1);
$arrT[] = substr($slider->getTitle(), 1);
}
if($arrA && $arrT){
$result = array_combine($arrA, $arrT);
}
else
{
$result = false;
}
return $result;
}

PHP function not returning correctly

So basically I have an array, and I have a function that will return an array value, but it is not returning anything.
$skills = array("Attack");
$i = 0;
$hs = file_get_contents("localhost/player=".$_GET["player"]);
foreach($skills as $value) {
$hs[$i] = explode(",",$hs[$i]);
$stats[$value]["rank"] = $hs[$i][0];
$stats[$value]["level"] = $hs[$i][1];
$stats[$value]["xp"] = $hs[$i][2];
$i++;
}
function getSkill($skill) {
//echo $skill;
return $stats[$skill]["level"];
}
I have tried getSkill("Attack"); and it doesn't work;
but if I do echo $stats["Attack"]["level"]; it works fine.
You don't pass the $stats array to your function so it can't know what is in it.
function getSkill($stats, $skill) {
//echo $skill;
return $stats[$skill]["level"];
}
and call it with getSkill($stats,"Attack");

Array within an array using Facebook Graph APi

I have been working on this problem for the last 2 days, searched over and over again .. nothing. Understanding that I am not an expert here - it's good! lol
I am trying to get the information found in the link below;
https://graph.facebook.com/570215713050551_4508656/comments/?fields=likes.fields(id,username,name,profile_type)
to then export into a csv.
Now I have current numerous other api tools, but this one has stumped me.
Basically, need to get the data foreach then run that again plus do the "next" paging etc.
Totally lost here.
My current code is here.
<?php
//Export and Download the Liker Data from each comment here ..
$id = $_GET['data'];
$commentor = $_GET['commentor'];
$toget = 'https://graph.facebook.com/'.$id.'/comments/?fields=likes.fields(id,username,name,profile_type)';
$data = #file_get_contents($toget);
$data = json_decode($data,true);
if($data['data'] == FALSE){
echo "gay!";
die;
}
$alldata = array();
function moredata($data){
global $alldata;
foreach ($data["data"] as $eachdata){
$onedata['id'] = $eachdata['id'];
foreach ($eachdata["likes"] as $ex){
$onedata['uid'] = $$ex['data'][0]['id'];
$onedata['name'] = $ex['data'][0]['name'];
$onedata['username'] = $ex['data'][0]['username'];
$onedata['profile_type'] = $ex['data'][0]['profile_type'];
//$onedata['link'] = $eachdata['link'];
}
$alldata[] = $onedata;
$onedata = array();
}
if (array_key_exists('next', $data['paging'])) {
$nextpagelink = $data['paging']['next'];
$nextdata = json_decode(file_get_contents($nextpagelink),true);
moredata($nextdata);
}
}
moredata($data);
... ETC ETC to get out the csv
Any help here would be amazing! Thanks guys.
It was little tricky but can be solved the issue with nested recursion.
I have tried your code and made few changes and it worked. Check the code below
$alldata = array();
$arrlikedata = array();
function moredata($data){
global $alldata;
global $arrlikedata;
foreach ($data["data"] as $eachdata)
{
$onedata['id'] = $eachdata['id'];
if(isset($eachdata["likes"])){
$onedata['likes'] = more_like_data($eachdata["likes"]);
}
else{
$onedata['likes'] = array();
}
$alldata[] = $onedata;
$arrlikedata = array();
}
if (array_key_exists('next', $data['paging'])) {
$nextpagelink = $data['paging']['next'];
$nextdata = json_decode(file_get_contents($nextpagelink),true);
moredata($nextdata);
}
}
function more_like_data($likedata)
{ global $alldata;
global $arrlikedata;
if(isset($likedata["data"])){
foreach ($likedata["data"] as $ex){
if(isset($ex)){
$onedata1['uid'] = $ex['id'];
$onedata1['name'] = $ex['name'];
$onedata1['username'] = (isset($ex['username']))?$ex['username']:'';
$onedata1['profile_type'] = $ex['profile_type'];
$arrlikedata[] = $onedata1;
$onedata1 = array();
}
}
}
if(isset($likedata['paging'])){
if (array_key_exists('next', $likedata['paging']))
{
$nextpagelink = $likedata['paging']['next'];
$nextlikedata = json_decode(file_get_contents($nextpagelink),true);
return more_like_data($nextlikedata);
}
else{
return $arrlikedata;
}
}
else{
return $arrlikedata;
}
}
moredata($data);
print "<pre>";
print_r($alldata);
print "</pre>";

drupal: getting nodeautoterm node ids from taxonomy ids

I'm using drupal's NAT module and need to get the nid from the term id.
Here's what I tried:
foreach ( (array)nat_get_nids($termid) as $natid ) {
$NatName = $natid->name;
}
print $natid;
This does not work.
Node auto term's get nid function is like this:
function nat_get_nids($tids, $get_nodes = FALSE) {
static $nid_cache = NULL;
static $node_cache = NULL;
$return = array();
// Keep processing to a minimum for empty tid arrays.
if (!empty($tids)) {
// Sort tid array to ensure that the cache_string never suffers from order
// issues.
sort($tids);
$cache_string = implode('+', $tids);
if ($get_nodes) {
if (isset($node_cache[$cache_string])) {
return $node_cache[$cache_string];
}
elseif (isset($nid_cache[$cache_string])) {
// If the nid cache stores the same string, node_load() each nid and
// return them.
$return = array();
foreach (array_keys($nid_cache[$cache_string]) as $nid) {
$return[$nid] = node_load($nid);
}
$node_cache[$cache_string] = $return;
return $return;
}
}
else {
if (isset($nid_cache[$cache_string])) {
return $nid_cache[$cache_string];
}
elseif (isset($node_cache[$cache_string])) {
// If the node cache stores the same string, retrieve only the nids and
// return them.
foreach ($node_cache[$cache_string] as $nid => $node) {
$return[$nid] = $node->name;
}
// Cache extracted results.
$nid_cache[$cache_string] = $return;
return $return;
}
}
// Results have not been cached.
$tids = implode(', ', $tids);
$result = db_query("SELECT n.nid, t.name FROM {nat} n INNER JOIN {term_data} t USING (tid) WHERE n.tid IN (". db_placeholders($tids) .")", $tids);
while ($node = db_fetch_object($result)) {
if ($get_nodes) {
$return[$node->nid] = node_load($node->nid);
}
else {
$return[$node->nid] = $node->name;
}
}
if ($get_nodes) {
$node_cache[$cache_string] = $return;
}
else {
$nid_cache[$cache_string] = $return;
}
}
return $return;
}
Thanks in advance!
Edit: Try based on the first answer:
foreach (nat_get_nids($termid) as $nid => $node_name) {
}
print $node_name;
It looks like nat_get_nids is returning an associative array, so your for loop should look like
foreach (nat_get_nids($termid) as $nid => $node_name) {
...
}
$nids = nat_get_nids(array($termid));

Categories