Jump to content

loop problem


hchsk

Recommended Posts

hi, i can't seem to find the problem in this foreach loop. if anyone could help or give a suggestion, i'd really appreciate it, it's been endless trouble. as far as i can tell, the foreach loop isnt executing at all, even though its been given a valid array and format.

 

the purpose of the code is to check if the time given by the function inactive() is in a certain range, if so, add an item to the rss page this code is within. but to also check it doesnt issue the same warning twice, and also to do this for multiple inactive($vars).

 

$nodoubleiw == 000000000000 at the start, and should be updated every time something changes. (each digit represents a team, and their inactivity status, 1-3)

 

//INCLUDE INACTIVITY.PHP
	include("scripts/inactivity.php");

//OPEN NODOUBLEIW.TXT
	$myFile = "../nodoubleiw.txt";
	$nodoubleiw = file($myFile);

////CHECK FOR INACTIVITY
		///killinactivedoubles.txt == 000000000000
		///0 == none issued
		///1 == 6hr issued
		///2 == 1hr issued
		///3 == inactivity issued
		///change value of teams slot when issued (+=1)
		///if not inactive reset value of teams slot


	$nodoubleiw = preg_split('//', $nodoubleiw[0], -1,PREG_SPLIT_NO_EMPTY);

//this is not functioning -> troubleshoot

	foreach ($nodoubleiw as $key => $value) {
		$search = 'team' . ($key+1);
		$inactive = inactive($search);
		if ((intval($value) == 3) && (stristr($inactive, 'inactive') == false)) {$value = 0;}
		if ((intval($inactive[days]) == 0) && (intval($inactive[hours]) <= 6)) {$warning = $team . 'is in danger of inactivity! Less than 6 hours left!'; $value += 1;}
		if ((intval($inactive[days]) == 0) && (intval($inactive[hours]) <= 1)) {$warning = $team . 'is in danger of inactivity! Less than 1 hour left!'; $value += 1;}
		if (intval($inactive[seconds]) <= 0) {$warning = $team . 'has been declared inactive. If you are the Captain of ' . $team . ', please contact a judge for information concerning your shootout.'; $value += 1;}
		$newnodoubleiw = $value;
	}



//UPDATE NODOUBLEIW.TXT
	$myFile = "../nodoubleiw.txt";
	$fh = fopen($myFile, 'w');
	fwrite($fh, $newnodoubleiw);

Link to comment
Share on other sites

typo in my copy & paste, but not in the real thing.

 

$newnodoubleiw = $value; should be $newnodoubleiw .= $value;

 

 

 

 

also, note, replacing the foreach loop with

 

foreach ($nodoubleiw as $key => $value) {

$newnodoubleiw = 'whyy';

}

 

also returns a blank $newnodoubleiw. why could this be?

Link to comment
Share on other sites

first thing i did, but couldnt glean anything from it

 

 

array(12) { [0]=>  string(1) "0" [1]=>  string(1) "0" [2]=>  string(1) "0" [3]=>  string(1) "0" [4]=>  string(1) "0" [5]=>  string(1) "0" [6]=>  string(1) "0" [7]=>  string(1) "0" [8]=>  string(1) "0" [9]=>  string(1) "0" [10]=>  string(1) "0" [11]=>  string(1) "0" }

Link to comment
Share on other sites

i tried this, still to a blank value of $newnodoubleiw

 

		foreach ($nodoubleiw as $key => $value) {
		$search = 'team' . ($key+1);
		$inactive = inactive($search);
		if ((intval($value) == 3) && (stristr($inactive, 'inactive') == false)) {$nodoubleiw[$key] = 0;}
		if ((intval($inactive[days]) == 0) && (intval($inactive[hours]) <= 6)) {$warning = $team . 'is in danger of inactivity! Less than 6 hours left!'; $nodoubleiw[$key] += 1;}
		if ((intval($inactive[days]) == 0) && (intval($inactive[hours]) <= 1)) {$warning = $team . 'is in danger of inactivity! Less than 1 hour left!'; $nodoubleiw[$key] += 1;}
		if (intval($inactive[seconds]) <= 0) {$warning = $team . 'has been declared inactive. If you are the Captain of ' . $team . ', please contact a judge for information concerning your shootout.'; $nodoubleiw[$key] += 1;}
		$newnodoubleiw .= $nodoubleiw[$key];
	}

 

 

 

thanks for the help

Link to comment
Share on other sites

array(6) { ["years"]=>  int(0) ["days"]=>  float(2) ["hours"]=>  float(18) ["minutes"]=>  float( ["seconds"]=>  float(15) ["display"]=>  string(6) "2:18:8" } array(6) { ["years"]=>  int(0) ["days"]=>  float(2) ["hours"]=>  float(18) ["minutes"]=>  float( ["seconds"]=>  float(15) ["display"]=>  string(6) "2:18:8" } array(6) { ["years"]=>  int(0) ["days"]=>  float(2) ["hours"]=>  float(18) ["minutes"]=>  float( ["seconds"]=>  float(15) ["display"]=>  string(6) "2:18:8" } array(6) { ["years"]=>  int(0) ["days"]=>  float(2) ["hours"]=>  float(18) ["minutes"]=>  float( ["seconds"]=>  float(15) ["display"]=>  string(6) "2:18:8" } array(6) { ["years"]=>  int(0) ["days"]=>  float(2) ["hours"]=>  float(18) ["minutes"]=>  float( ["seconds"]=>  float(15) ["display"]=>  string(6) "2:18:8" } array(6) { ["years"]=>  int(0) ["days"]=>  float(2) ["hours"]=>  float(18) ["minutes"]=>  float( ["seconds"]=>  float(15) ["display"]=>  string(6) "2:18:8" } array(6) { ["years"]=>  int(0) ["days"]=>  float(2) ["hours"]=>  float(18) ["minutes"]=>  float( ["seconds"]=>  float(15) ["display"]=>  string(6) "2:18:8" } array(6) { ["years"]=>  int(0) ["days"]=>  float(2) ["hours"]=>  float(18) ["minutes"]=>  float( ["seconds"]=>  float(15) ["display"]=>  string(6) "2:18:8" } array(6) { ["years"]=>  int(0) ["days"]=>  float(2) ["hours"]=>  float(18) ["minutes"]=>  float( ["seconds"]=>  float(15) ["display"]=>  string(6) "2:18:8" } array(6) { ["years"]=>  int(0) ["days"]=>  float(2) ["hours"]=>  float(18) ["minutes"]=>  float( ["seconds"]=>  float(15) ["display"]=>  string(6) "2:18:8" } array(6) { ["years"]=>  int(0) ["days"]=>  float(2) ["hours"]=>  float(18) ["minutes"]=>  float( ["seconds"]=>  float(15) ["display"]=>  string(6) "2:18:8" } array(6) { ["years"]=>  int(0) ["days"]=>  float(2) ["hours"]=>  float(18) ["minutes"]=>  float( ["seconds"]=>  float(14) ["display"]=>  string(6) "2:18:8" }

Link to comment
Share on other sites

that just broke it down into an array by character to allow the foreach to loop through it

 

each time the loop hits $newnodoubleiw .= $value; it should be appending the $value from the foreach loop (possibly +=1 if a statement was true)

Link to comment
Share on other sites

Try -

      $newnodoubleiw = '';
      foreach ($nodoubleiw as $key => $value) {
         $search = 'team' . ($key+1);
         $inactive = inactive($search);
         $newval = intval($value);
         if ((intval($value) == 3) && (stristr($inactive, 'inactive') == false)) {$newval = 0;}
         if ((intval($inactive[days]) == 0) && (intval($inactive[hours]) <= 6)) {$warning = $team . 'is in danger of inactivity! Less than 6 hours left!'; $newval += 1;}
         if ((intval($inactive[days]) == 0) && (intval($inactive[hours]) <= 1)) {$warning = $team . 'is in danger of inactivity! Less than 1 hour left!'; $newval += 1;}
         if (intval($inactive[seconds]) <= 0) {$warning = $team . 'has been declared inactive. If you are the Captain of ' . $team . ', please contact a judge for information concerning your shootout.'; $newval += 1;}
         $newnodoubleiw .= $newval;
      }

Link to comment
Share on other sites

haha, your script has a mistake in concatenating $value instead of $nodoubleiw[$key], but otherwise, works perfectly! thanks so much!

 

 

 

lol, we were typing at the same time. i solved it slightly differently too. interesting. thanks again.

Link to comment
Share on other sites

ahh, while that worked in a test.php i was using, it still breaks in the real page im using it in, that constructs an rss.

 

maybe you could help find the error here as well? im going to post the full script, but it is clearly labeled, and the vast majority of it you can ignore.

 

 

<?php

//CHECK WHEN LAST MODIFIED AND ASSIGN TO VAR
	$lastmod = date("D, j M Y H:i:s", (filemtime("report.txt")));
	$lastmod += 10000;

//GET CURRENT DATE/TIME
	$now = date("D, j M Y H:i:s");
	$ltime = time() + 3*60*60 ;
	$nowpub = date('D, j M Y H:i:s',$ltime);

//GET LAST UPDATE
	$lastupdate = date("D, j M Y H:i:s", (filemtime("modifycheck.txt")));

//GET DIFFERENCE
	$lastissue = strtotime($now) - strtotime($lastupdate);

//GET SCORES
	$filename = "scorestore.txt";
	$score = file($filename);

//GET COMMENT
	$filename = "comments.txt";
	$comment = file($filename);

//GET REPORT
	$filename = "report.txt";
	$report = file($filename);


echo <<<END

<rss version="2.0">

<channel>
<title>HCHSLive Network Feed</title>
<link>http://www.lowersouthlounge.com</link>
<pubDate>Wed, 29 Apr 2009 08:10:00 EST</pubDate>
<description>HCHS Live Network</description>
<language>en-us</language>
<lastBuildDate>Wed, 29 Apr 2009 08:10:00 EST</lastBuildDate>
<managingEditor>hchscontact@aol.com</managingEditor>
<webMaster>hchscontact@aol.com</webMaster>


<image>
<title>HCHSLive Network Feed</title>
<url>http://www.lowersouthlounge.com/favicon.ico</url>
<link>http://lowersouthlounge.com</link>
<width>16</width>
<height>16</height>
</image>

END;


///////////////////////// ///////////////////////////////////////////////////////////////// FINISH ME! ///////////////////////////////////
//INCLUDE INACTIVITY.PHP
	include("scripts/inactivity.php");

//OPEN NODOUBLEIW.TXT
	$myFile = "../nodoubleiw.txt";
	$nodoubleiw = file($myFile);

////CHECK FOR INACTIVITY
		///killinactivedoubles.txt == 000000000000
		///0 == none issued
		///1 == 6hr issued
		///2 == 1hr issued
		///3 == inactivity issued
		///change value of teams slot when issued (+=1)
		///if not inactive reset value of teams slot


	$nodoubleiw = preg_split('//', $nodoubleiw[0], -1,PREG_SPLIT_NO_EMPTY);

//this is not functioning -> troubleshoot

      		$newnodoubleiw = '';
	foreach ($nodoubleiw as $key => $value) {
		$search = 'team' . ($key+1);
		$inactive = inactive($search);
		if ((intval($value) == 3) && (stristr($inactive, 'inactive') == false)) {$nodoubleiw[$key] = 0;}
		if ((intval($inactive[days]) == 0) && (intval($inactive[hours]) <= 6)) {$warning = $team . 'is in danger of inactivity! Less than 6 hours left!'; $nodoubleiw[$key] += 1;}
		if ((intval($inactive[days]) == 0) && (intval($inactive[hours]) <= 1)) {$warning = $team . 'is in danger of inactivity! Less than 1 hour left!'; $nodoubleiw[$key] += 1;}
		if (intval($inactive[seconds]) <= 0) {$warning = $team . 'has been declared inactive. If you are the Captain of ' . $team . ', please contact a judge for information concerning your shootout.'; $value += 1;}
		$newnodoubleiw .= $nodoubleiw[$key];
	}




////UPDATE NODOUBLEIW.TXT
	//$myFile = "../nodoubleiw.txt";
	//$fh = fopen($myFile, 'w');
	//fwrite($fh, $newnodoubleiw);

////CREATE WARNING TO BE APPENDED TO ADDFEED.TXT
	//$warning .= '
	//<item>
	// <title>HCHSLive Network Update</title>
	// <category>inactivity</category>
	// <link>http://lowersouthlounge.com/circle.php</link>
	// <description>$warning</description>
	// <pubDate>$LASTEVENTTIME</pubDate>
	//</item>';
////TWEET!
	//include("tweet.php");
	//postToTwitter("HCHSLiveNetwork", "password",  $newreport);
///////////////////////// ///////////////////////////////////////////////////////////////// FINISH ME! ///////////////////////////////////



//SAVE OLDFEED AS $originalfeed
	$myFile = "addfeed.txt";
	$fh = fopen($myFile, 'r');
	$originalfeed = fread($fh, filesize($myFile));
	fclose($fh);

//CHECK IF NEW INFO
if ($lastissue > 0){

//CREATE NEW ITEM
	for ( $counter = 0; $counter < count($report); $counter += 1 ) {

//MAKE CATEGORY
	for ( $counter2 = 1; $counter2 <= 12; $counter2 += 1 ) {  //////////////////////////////////////////////////////////////////CHANGE FUNCTION TO CHECK FOR TEAM NAMES
	$hold = 'team' . $counter2 . ' '; //SPACE CHECKS FOR ERRORS DUE TO DIGIT REPEAT AT 10+
	if (stristr($report[$counter], $hold) != false) {$category = $category . trim($hold) . '/';}
	}

	$luck = rand(0, 34);
	if (trim($report[$counter]) !== '') {

$newfeed = $newfeed . "
<item>
 <title>HCHSLive Network Update</title>
 <category>$category</category>
 <link>http://lowersouthlounge.com/circle.php</link>
 <description>$report[$counter] $comment[$luck]</description>
 <pubDate>$nowpub</pubDate>
</item>";
}

}

//OVERWRITE FILE, FIRST NEW ITEM THEN $originalfeed
$myFile = "addfeed.txt";
$fh = fopen($myFile, 'w');

fwrite($fh, "
$warning
$newfeed
$originalfeed");
fclose($fh);

//EMPTY REPORT.TXT
$myFile = "report.txt";
$fh = fopen($myFile, 'w');
fwrite($fh, "");
fclose($fh);

//CHANGE modifycheck.txt
$myFile = "modifycheck.txt";
$fh = fopen($myFile, 'w');
fwrite($fh, "$now");
fclose($fh);

}


echo <<<END

$newfeed
$originalfeed

<item>
<title>RSS Feed Live!</title>
<category>post</category>
<link>http://lowersouthlounge.com</link>
<description>
HCHS Live Network RSS feed running! TIME STAMP: $lastmod $lastupdate $now || $newnodoubleiw
</description>
<pubDate>Mon, 27 Apr 2009 08:10:00 EDT</pubDate>
</item>

</channel>
</rss>
END;


?>

Link to comment
Share on other sites

<?php

//CHECK WHEN LAST MODIFIED AND ASSIGN TO VAR
	$lastmod = date("D, j M Y H:i:s", (filemtime("report.txt")));
	$lastmod += 10000;

//GET CURRENT DATE/TIME
	$now = date("D, j M Y H:i:s");
	$ltime = time() + 3*60*60 ;
	$nowpub = date('D, j M Y H:i:s',$ltime);

//GET LAST UPDATE
	$lastupdate = date("D, j M Y H:i:s", (filemtime("modifycheck.txt")));

//GET DIFFERENCE
	$lastissue = strtotime($now) - strtotime($lastupdate);

//GET SCORES
	$filename = "scorestore.txt";
	$score = file($filename);

//GET COMMENT
	$filename = "comments.txt";
	$comment = file($filename);

//GET REPORT
	$filename = "report.txt";
	$report = file($filename);


echo <<<END

<rss version="2.0">

<channel>
<title>HCHSLive Network Feed</title>
<link>http://www.lowersouthlounge.com</link>
<pubDate>Wed, 29 Apr 2009 08:10:00 EST</pubDate>
<description>HCHS Live Network</description>
<language>en-us</language>
<lastBuildDate>Wed, 29 Apr 2009 08:10:00 EST</lastBuildDate>
<managingEditor>hchscontact@aol.com</managingEditor>
<webMaster>hchscontact@aol.com</webMaster>


<image>
<title>HCHSLive Network Feed</title>
<url>http://www.lowersouthlounge.com/favicon.ico</url>
<link>http://lowersouthlounge.com</link>
<width>16</width>
<height>16</height>
</image>

END;


///////////////////////// ///////////////////////////////////////////////////////////////// FINISH ME! ///////////////////////////////////
//INCLUDE INACTIVITY.PHP
	include("scripts/inactivity.php");

//OPEN NODOUBLEIW.TXT
	$myFile = "../nodoubleiw.txt";
	$nodoubleiw = file($myFile);

////CHECK FOR INACTIVITY
		///killinactivedoubles.txt == 000000000000
		///0 == none issued
		///1 == 6hr issued
		///2 == 1hr issued
		///3 == inactivity issued
		///change value of teams slot when issued (+=1)
		///if not inactive reset value of teams slot


	$nodoubleiw = preg_split('//', $nodoubleiw[0], -1,PREG_SPLIT_NO_EMPTY);

//this is not functioning -> troubleshoot

      $newnodoubleiw = '';
      foreach ($nodoubleiw as $key => $value) {
         $search = 'team' . ($key+1);
         $inactive = inactive($search);
         $newval = intval($value);
         if ((intval($value) == 3) && (stristr($inactive, 'inactive') == false)) {$newval = 0;}
         if ((intval($inactive[days]) == 0) && (intval($inactive[hours]) <= 6)) {$warning = $team . 'is in danger of inactivity! Less than 6 hours left!'; $newval += 1;}
         if ((intval($inactive[days]) == 0) && (intval($inactive[hours]) <= 1)) {$warning = $team . 'is in danger of inactivity! Less than 1 hour left!'; $newval += 1;}
         if (intval($inactive[seconds]) <= 0) {$warning = $team . 'has been declared inactive. If you are the Captain of ' . $team . ', please contact a judge for information concerning your shootout.'; $newval += 1;}
         $newnodoubleiw .= $newval;
      }




////UPDATE NODOUBLEIW.TXT
	//$myFile = "../nodoubleiw.txt";
	//$fh = fopen($myFile, 'w');
	//fwrite($fh, $newnodoubleiw);

////CREATE WARNING TO BE APPENDED TO ADDFEED.TXT
	//$warning .= '
	//<item>
	// <title>HCHSLive Network Update</title>
	// <category>inactivity</category>
	// <link>http://lowersouthlounge.com/circle.php</link>
	// <description>$warning</description>
	// <pubDate>$LASTEVENTTIME</pubDate>
	//</item>';
////TWEET!
	//include("tweet.php");
	//postToTwitter("HCHSLiveNetwork", "password",  $newreport);
///////////////////////// ///////////////////////////////////////////////////////////////// FINISH ME! ///////////////////////////////////



//SAVE OLDFEED AS $originalfeed
	$myFile = "addfeed.txt";
	$fh = fopen($myFile, 'r');
	$originalfeed = fread($fh, filesize($myFile));
	fclose($fh);

//CHECK IF NEW INFO
if ($lastissue > 0){

//CREATE NEW ITEM
	for ( $counter = 0; $counter < count($report); $counter += 1 ) {

//MAKE CATEGORY
	for ( $counter2 = 1; $counter2 <= 12; $counter2 += 1 ) {  //////////////////////////////////////////////////////////////////CHANGE FUNCTION TO CHECK FOR TEAM NAMES
	$hold = 'team' . $counter2 . ' '; //SPACE CHECKS FOR ERRORS DUE TO DIGIT REPEAT AT 10+
	if (stristr($report[$counter], $hold) != false) {$category = $category . trim($hold) . '/';}
	}

	$luck = rand(0, 34);
	if (trim($report[$counter]) !== '') {

$newfeed = $newfeed . "
<item>
 <title>HCHSLive Network Update</title>
 <category>$category</category>
 <link>http://lowersouthlounge.com/circle.php</link>
 <description>$report[$counter] $comment[$luck]</description>
 <pubDate>$nowpub</pubDate>
</item>";
}

}

//OVERWRITE FILE, FIRST NEW ITEM THEN $originalfeed
$myFile = "addfeed.txt";
$fh = fopen($myFile, 'w');

fwrite($fh, "
$warning
$newfeed
$originalfeed");
fclose($fh);

//EMPTY REPORT.TXT
$myFile = "report.txt";
$fh = fopen($myFile, 'w');
fwrite($fh, "");
fclose($fh);

//CHANGE modifycheck.txt
$myFile = "modifycheck.txt";
$fh = fopen($myFile, 'w');
fwrite($fh, "$now");
fclose($fh);

}


echo <<<END

$newfeed
$originalfeed

<item>
<title>RSS Feed Live!</title>
<category>post</category>
<link>http://lowersouthlounge.com</link>
<description>
HCHS Live Network RSS feed running! TIME STAMP: $lastmod $lastupdate $now || $newnodoubleiw
</description>
<pubDate>Mon, 27 Apr 2009 08:10:00 EDT</pubDate>
</item>

</channel>
</rss>
END;


?>

 

 

is not printing $newnodoubleiw in the final item. eventually, instead of doing this, it will update the nodoubleiw.txt file instead of printing, but this is to make testing the code easier until it works

 

////UPDATE NODOUBLEIW.TXT
	//$myFile = "../nodoubleiw.txt";
	//$fh = fopen($myFile, 'w');
	//fwrite($fh, $newnodoubleiw);

Link to comment
Share on other sites

i dont think you need to worry about that right now, right now i just need the loop to work, and to be able to print $newnodoubleiw which should == 000000000000, and DOES when this is checked separately.

 

what i will do once that works, is to delete the print command, and instead

////UPDATE NODOUBLEIW.TXT

$myFile = "../nodoubleiw.txt";

$fh = fopen($myFile, 'w');

fwrite($fh, $newnodoubleiw);

 

which will overwrite nodoubleiw.txt with the new $newnodoubleiw

 

nodoubleiw.txt is a flat file database that keeps track of inactivity statuses

Link to comment
Share on other sites

i can by testing it outside of the rss with test.php and inline code (but there it works anyway). but i cant in the rss, because the rss will not display if the php prints improper format. but i can achieve what youre asking by assigning those dumps to vars and printing it within an item. the result of that is. . . nothing. it seems like the loop just isnt executing at all. i don't understand it.

 

 

Link to comment
Share on other sites

i dont understand. my filepaths where? its already been demonstrated that all of the arrays are correctly filled, and i'm not overwriting the file yet, i'm just printing a variable! forget about all this code, even this does not work!! :

 

      foreach ($nodoubleiw as $key => $value) {
$newnodoubleiw = 'whynot?';
      }

 

funny thing is that is does in the inline, test.php, but it fails in the rss.php

 

 

but if i place $newnodoubleiw = 'aha'; right outside of the foeach loop, it works!

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.