tags:

views:

67

answers:

3

I have the following codes:

require("class.XMLHttpRequest.php");
function hot($news){
    $url="https://localhost/search.aspx?search=".$news.""; 
 $ajax=new XMLHttpRequest();
 $ajax->setRequestHeader("Cookie","Cookie: host");
 $ajax->open("GET",$url,true);
 $ajax->send(null);
 if($ajax->status==200){
  $rHeader=$ajax->getResponseHeader("Set-Cookie");
  if(substr_count($rHeader, "Present!")>0) { return true; }
 }else{ return false; }
} 

$content1= hot("britney") ? "britney found" : "";
$content2= hot("gaga") ? "gaga found" : "";
$content3= hot("carol") ? "carol found" : ""; 

$filename = 'result.txt';
$handle = fopen($filename, 'a');
fwrite($handle, "$Content1\r\n");
fwrite($handle, "$Content2\r\n");
fwrite($handle, "$Content3\r\n");
fwrite($handle, "$Content4\r\n");
fclose($handle);

i want to shorten the script cuz i have a lot of $ContentN variables maybe something like foreach ??

+2  A: 

Something like this would be very close to your actual code, but might not be recomanded

for($i = 1 ; $i <= 4 ; $i++)
 fwrite($handle, "${Content$i}\r\n");

It is using variable variables : http://php.net/manual/en/language.variables.variable.php

Not the best solution here : why don't you simply use an array ?

$content[1]= hot("britney") ? "britney found" : "";
$content[2]= hot("gaga") ? "gaga found" : "";
$content[3]= hot("carol") ? "carol found" : ""; 
for($i = 1 ; $i <= 4 ; $i++)
 fwrite($handle, $Content[$i]."\r\n");

Or ever better, use the solution of captaintokyo , because you might not want empty lines in your text file.

Loïc Février
-1 for suggesting variable variables+2 for suggesting an array
mattbasta
That's why I was suggesting him to use an array. Variable variables exists and the resulting is closer to his code that using arrays : I first try to be as close as possible to his code and then I suggest arrays which are the good way to to it here.
Loïc Février
yes! it works! thanks a lot
adam
I don't like this, cause you still have to write a line for each 'celebrity'.
captaintokyo
@captaintokyo : agreed, I've modified the answer to point to yours
Loïc Février
+3  A: 

I would do it like this:

$celebrities = array('britney','gaga','carol');
$filename = 'result.txt';
$handle = fopen($filename, 'a');

foreach($celebrities as $celebrity)
{
    if(hot($celebrity)) { fwrite($handle, "{$celebrity}\r\n"); }
}

fclose($handle);

If you need more celebrities, just add them to the array.

captaintokyo
i have blank spaces in both cases
adam
Yes, sorry. But in his case it is easier to test.`if(hot($celebrity)) fwrite($handle, "{$celebrity} found\r\n");` No blank spaces anymore.
Loïc Février
If you don't want blank lines, it's even easier... updated my answer. (Thanks Loïc)
captaintokyo
FLAWLESS !! thanks soo much
adam
i have the celebrity names in a text file each on every line, but that's another topic
adam
In that case, instead of the array you can loop through the lines of the file. The code won't change much. Good luck!
captaintokyo
thanks ! captain
adam
i've done it with $handle = @fopen('array.txt', "r"); if ($handle) { while (!feof($handle)) { $celebrities[] = fgets($handle, 4096); } fclose($handle); }
adam
A: 

Here's a brief code refactoring for you. Updated variable names, trending topics held in an array, and the foreach you asked about. Code is untested.

require("class.XMLHttpRequest.php");

$result_filename = 'result.txt';

$hot_topics = array(
  'britney',
  'gaga',
  'carol'
  );

$handle = @fopen($result_filename, 'a+');
if (!$handle) {
  exit("Unable to open $result_filename");
}

foreach($hot_topics as $topic) {
  if (is_hot($topic)) {
    fwrite($handle, "$topic found\r\n");
  }
}

fclose($handle);

exit("\ncomplete");

function is_hot($news) {
  $url = "https://localhost/search.aspx?search=".$news; 

  $ajax = new XMLHttpRequest();
  $ajax->setRequestHeader("Cookie", "Cookie: host");
  $ajax->open("GET", $url, true);
  $ajax->send(null);

  if ($ajax->status == 200) {
    $rHeader = $ajax->getResponseHeader("Set-Cookie");
    if (substr_count($rHeader, "Present!") > 0) {
      return true;
    }
  }
  return false;
}
Craig
it prints noting to file
adam
Try adding a print $topic statement inside the foreach loop, first before the if(is_hot()) and then inside that conditional to make sure it's working correctly. After that, try adding a print $rHeader to the is_hot() function. This will let you see the information returned. Is there a chance that it's not writing anything to the file because nothing is currently 'hot'?
Craig