Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Failing to write all data to file

Posted by dabbler (dabbler), 30 May 2003
Hi Graham.

After 3 hours of picking through code and the php manual, I am stuck. Please forgive me if this post is not in following with the intended nature of this forum. It's my last resort. I'd greatly appreciate a little help with this.

I have a set of scripts that run a banner management system. It's great but for one problem that is making unusable. It looks to me like it could be something simple that I just don't know enough to see. Or it's something to do with having an NT server that requires some small change.

Ads.dat holds all of the data for the scripts, the configuration data, and the ad data. When using the admin area, saving the config changes writes to file as it should. The problem is when the ad data (specifically clickthrus) changes and the file is rewritten, it omits the config data. I think I've narrowed it down to two smaller problem areas.

Click.php, which logs the click and redirects
Code:
for ($i = 0; $i < count($ads); $i++) {
   if(ereg("^$id\|\|", $ads[$i])) {
       $data = explode('||', $ads[$i]);
       $data[6]++;
       $ads[$i] = join('||', $data);
       break;
   }
}
if (!$data[9]) {
   die();
}
writeads();
Header("Location: $data[9]");
exit;


And then in ads.inc.php
Code:
$ads = array();
$configAds = array();
$AdsTime = time();
$lines = file($AdsDatFile) or die();
foreach ($lines as $line) {
   $line = chop($line);
   if (($line != '') or (!ereg('^#', $line))) {
       if (ereg('^[0-9]', $line)) {
           $ads[] = $line;
       } else {
           list ($key, $val) = split('=', $line);
           $configAds[$key] = $val;
       }
   }
}
function writeads()
{
   global $AdsDatFile, $ads, $configAds;
   $data = fopen($AdsDatFile, 'w') or die();
   flock($data, 2) or die();
   fputs($data, @join("\n", $ads)."\n");
   while (list ($key, $val) = each ($configAds)) {
       if (($key != '') && ($val != '')) {
           fputs($data, $key.'='.$val."\n");
       }
   }
   fclose($data);
}


Is there an obvious error here?   Everything is fine until a click, and it logs the click. Just loses the $key and $val data somehow. Any help would be greatly appreciated.

Thanks.

Posted by admin (Graham Ellis), 30 May 2003
Hi ... welcome ... and exactly the right use of the forum   ... interesting problem too.

Educated guess:   add the line
Code:
reset($configAds);

before the while (....) line in the writeads function.

In PHP, the each function returns a key / value pair from an array each time you call it, and it returns a false value when it runs out of data.   So far, so good ... except that when you enter such a loop it starts from wherever it happened to reach last time you used each on the same array last.  The frighteningly named "reset" simply resets the internal pointer that each uses so that it starts (again) at the beginning.   There are time where you have to do a reset before a loop of each-es, even if you've never eached through the array before.   I don't think the pointer is automatically initislised when an array is created.

Anyhow - if that's it, great ... if not,  let me know and I'll have a further look (I would probably end up suggesting creating an extra temporary file to write variable into to see more about where the failure is occurring ....)



Posted by dabbler (dabbler), 30 May 2003
Ahhhh, and fantastic, that did the trick.

Thank you. Now everything keeps displaying properly.

It never fails that when I pick up a free script, I have to spend time customizing it to make it work for me the way I'd like, which has been good, I've learned a lot this way.

Thanks again, you're a star.



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho