Jump to content

external variables


hchsk

Recommended Posts

it stops the return whatever; from being passed. even if didn't however, it wouldn't quite solve my problem, though i am interested to hear more about everything i can. i want a solution that stops the script from affecting ANYTHING outside of it, including database modifications, and overwriting files. all i want is a variable from inside of it

Link to comment
Share on other sites

ok, well i need this for several different scripts, but i'll give one example. heres a script i use to print a countdown to a date that changes, depending on the last post within a certain category on an RSS page:

 

<?php


//CONVERT SECONDS TO TIME
function Sec2Time($time){
 if(is_numeric($time)){
   $value = array(
     "years" => 0, "days" => 0, "hours" => 0,
     "minutes" => 0, "seconds" => 0,
   );
   if($time >= 31556926){
     $value["years"] = floor($time/31556926);
     $time = ($time%31556926);
   }
   if($time >= 86400){
     $value["days"] = floor($time/86400);
     $time = ($time%86400);
   }
   if($time >= 3600){
     $value["hours"] = floor($time/3600);
     $time = ($time%3600);
   }
   if($time >= 60){
     $value["minutes"] = floor($time/60);
     $time = ($time%60);
   }
   $value["seconds"] = floor($time);
   return (array) $value;
 }else{
   return (bool) FALSE;
 }
}


/////////////////////////////////CALL FUNCTION:
/////////////////////////////////inactive('team#');

function inactive($team) {

$doc = new DomDocument;
$doc->load('http://www.lowersouthlounge.com/updates.php');

$xpath = new DOMXPath($doc);
$categories = $xpath->query("//item/category");
$pubDates = $xpath->query("//item/pubDate");

foreach ($pubDates as $pubDate) {$pub[] = $pubDate->nodeValue;}
foreach ($categories as $categorie) {$category[] = $categorie->nodeValue;}



$first = 1;
for ($counter = 0; $counter <= count($category); $counter +=1){

 if ((stristr($category[$counter], $team) != false) && ($first == 1)){ $lasteventtime = $pub[$counter]; $first = 0; $check = 1;}

}

//GET TIME NOW
$ltime = time() + 3*60*60 ;
$mytime = date('D, j M Y h:i:s',$ltime);

//GET DIFFERENCE
$inactive = (strtotime($lasteventtime)+259200) - strtotime($mytime);

$inactive = Sec2Time($inactive);

$inactive[display] = "$inactive[days]:$inactive[hours]:$inactive[minutes]";
if (intval($inactive[seconds]) < 0) {$inactive[display] = "<i>this team is inactive</i>";}


if ($check != 1) {return 'error';} else {return $inactive;}
}

?>

 

 

now as you can see, it takes a parameter, $team, which is the category it searches for. now i need this script to serve two functions. i need to be able to include it in a page, and print the string $inactive[display] provided by the function when given different parameters at different points on the page. i also need it to supply the return value to another php script, but when it's included in the php script, it cant print out anything on it's own, i need to include the script, and when needed call the function with the tobedetermined parameter and get the return value.

 

now that i'm typing and thinking about it more, i'm thinking about a solution that would just set a new variable $inactive[forscript], and repeatedly call the function with the different values before printing the variable $inactive[forscript], and that would contain what i needed. i'm not sure, but that might accomplish what i need. however, i would still like to find a way that i can do what i asked.

 

 

/////i just remembered why the above solution does not work, look at my p.s.

 

 

thanks again

 

 

ps. i would like to use xml ajax to dynamically update every second the results of the functions i call and print within the website, this requires me to use a print or echo to display the result, but that will ruin my ability to access the function with different values via an include in the other script. but now that i'm thinking about THAT, that would still not work, because the echo for ajax.responseText would have to be echoed outside of the function, which ruins my ability to call the function with different parameters multiple times on a page

Link to comment
Share on other sites

thats very odd, i'm not sure, theyre not in the script, when i posted it in the code tags the forum added those, here it is again outside of the tags:

 

 

 

<?php

 

 

//CONVERT SECONDS TO TIME

function Sec2Time($time){

  if(is_numeric($time)){

    $value = array(

      "years" => 0, "days" => 0, "hours" => 0,

      "minutes" => 0, "seconds" => 0,

    );

    if($time >= 31556926){

      $value["years"] = floor($time/31556926);

      $time = ($time%31556926);

    }

    if($time >= 86400){

      $value["days"] = floor($time/86400);

      $time = ($time%86400);

    }

    if($time >= 3600){

      $value["hours"] = floor($time/3600);

      $time = ($time%3600);

    }

    if($time >= 60){

      $value["minutes"] = floor($time/60);

      $time = ($time%60);

    }

    $value["seconds"] = floor($time);

    return (array) $value;

  }else{

    return (bool) FALSE;

  }

}

 

 

/////////////////////////////////CALL FUNCTION:

/////////////////////////////////inactive('team#');

 

function inactive($team) {

 

$doc = new DomDocument;

$doc->load('http://www.lowersouthlounge.com/updates.php');

 

$xpath = new DOMXPath($doc);

$categories = $xpath->query("//item/category");

$pubDates = $xpath->query("//item/pubDate");

 

foreach ($pubDates as $pubDate) {$pub[] = $pubDate->nodeValue;}

foreach ($categories as $categorie) {$category[] = $categorie->nodeValue;}

 

 

 

$first = 1;

for ($counter = 0; $counter <= count($category); $counter +=1){

 

if ((stristr($category[$counter], $team) != false) && ($first == 1)){ $lasteventtime = $pub[$counter]; $first = 0; $check = 1;}

 

}

 

//GET TIME NOW

$ltime = time() + 3*60*60 ;

$mytime = date('D, j M Y h:i:s',$ltime);

 

//GET DIFFERENCE

$inactive = (strtotime($lasteventtime)+259200) - strtotime($mytime);

 

$inactive = Sec2Time($inactive);

 

$inactive[display] = "$inactive[days]:$inactive[hours]:$inactive[minutes]";

if (intval($inactive[seconds]) < 0) {$inactive[display] = "<i>this team is inactive</i>";}

 

 

if ($check != 1) {return 'error';} else {return $inactive;}

}

 

?>

Link to comment
Share on other sites

It should be $inactive['display']; Use quotes.

 

Also, instead of checking if $first == 1, you can alternatively check $count == 0. And I don't know why you have $check = 1 either. Same with last time. I guess it's best if you start at the end of the array $category and stop there because that's what you're doing now - you're just storing the last entry anyways.

Link to comment
Share on other sites

What's with all the

 

?

 

http://www.phpfreaks.com/forums/index.php/topic,242385.0.html

 

Somewhat explains it, but not really.

I never had that problem when I edit my codes. o.O

 

hchsk - Read up on associative arrays. If you just put display without quotes, it means it's a constant. And I doubt it is unless you defined it. Can you include in the code for the ob_start()?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.