Manish Jangir
Manish Jangir

Reputation: 5437

How can i use this variable outside this function

after calling DemoSlideListing($apiobj,'tech',12,2) it prints the echo portion of the function but i want to use the variable $TotalResults outside this function i mean anywhere on the page. how can i do this. Thanks in advance. please help

function DemoSlideListing($apiobj, $query, $per_page, $page) {
    $data = $apiobj->search_slides($query, $per_page, $page);
    foreach($data as $key) {
        $title = $key['TITLE'];
        $title2 = substr($title, 0, 35);
        $TotalResults = $key['TOTALRESULTS'];
        echo '<td valign="top"><div id="slide_thumb">
</div>
<div id="slide_thum_des"><strong>Views :</strong> '.$info['VIEWS'].'<br />
<a href="'.$key['DOWNLOADURL'].'">'.$title2.'....</a></div>
</td>';

    }
}

Upvotes: 0

Views: 74

Answers (1)

Michael Berkowski
Michael Berkowski

Reputation: 270607

Return it from the function. Please review the PHP documentation on function return values.

function DemoSlideListing($apiobj,$query,$per_page,$page){
  $data = $apiobj->search_slides($query,$per_page,$page); 

    // Declare $TotalResults as an array
    $TotalResults = array();

    foreach ($data as $key){
    $title = $key['TITLE'];
    $title2 = substr($title, 0, 35);

    // Append current value to TotalResutls
    $TotalResults[] = $key['TOTALRESULTS'];

    echo '<td valign="top"><div id="slide_thumb">
</div>
<div id="slide_thum_des"><strong>Views :</strong> '.$info['VIEWS'].'<br />
<a href="'.$key['DOWNLOADURL'].'">'.$title2.'....</a></div>
</td>';

  }

  // Return the value
  return $TotalResults;
}   

// Call as:
$totalresults = DemoSlideListing($apiobj,$query,$per_page,$page);

// $totalresults holds the array that $TotalResults held at the time the function execution completed.
print_r($totalresults);

Upvotes: 2

Related Questions