0

Hi,

I am looping the data and displaying it from the database. But only one data set is displaying in the loop. I dont now where i am doing wrong. Here is my code. Please help me

$resultViewed = $this->model_report_product->getMostViewed();
        $stringResult ="var data= google.visualization.arrayToDataTable([ ['Product', 'Views'],";

     foreach($resultViewed as $results)
     {               
                 $stringResult = $stringResult . "['".$results['name']."',".$results['quantity']."],";

     }
     $stringResult .= "]);";

    $this->data['Viewed'] = $stringResult;  
4
Contributors
5
Replies
6
Views
5 Years
Discussion Span
Last Post by kurtopia
0

Put a var_dump above the foreach loop and make sure it has data

var_dump($resultViewed);

If $resultViewed has data, put a var_dump inside the loop and check what is in $results

foreach($resultViewed as $results)
{
    var_dump($results);
    $stringResult = $stringResult . "['".$results['name']."',".$results['quantity']."],";
}

Let's see what's in those variables first and then decide what to do next :)

0

var_dump($results);

Hi,
Thanks for replying.
var_dump($resultViewed); , before forloop, is getting all the data properly. But inside the loop i.e var_dump($results); is getting only one(1st) data, not all the data. I dont know why this is happening. Do i need to put forloop or how can i correct it :(

0

Fetch the elemts as key value pairs and try

foreach($resultViewed as $resultKey => $resultValue)
{
    var_dump($resultValue);
    $stringResult = $stringResult . "['".$resultValue['name']."',".$resultValue['quantity']."],";
}

Edited by niranga

1

$stringResult = $stringResult . "['".$resultValue['name']."',".$resultValue['quantity']."],";

The problem is as shown above as bold text. Use another variable to assign. Just like:

$newStringResult[] = $stringResult[$resultValue['name'],$resultValue['quantity']];

The bracket [] defined as array, so, you can get all values within loop as array. Otherwise, you'll only get last value.

0

Are you using codeigniter? Maybe the return value of

$resultViewed = $this->model_report_product->getMostViewed();

is an object instead of an array..

Try using this one:

foreach($resultViewed as $resultKey => $resultValue)
{
    var_dump($resultValue);
    $stringResult = $stringResult . "['".$resultValue->name."',".$resultValue->quantity."],";
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.