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;  

Recommended Answers

All 5 Replies

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 :)

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 :(

Fetch the elemts as key value pairs and try

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

$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.

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."],";
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.