laffin
Members-
Posts
1,200 -
Joined
-
Last visited
Everything posted by laffin
-
Look into php fork but hopefully your web hosting has it enabled. The problem you will encounter is finding out if your background task is running, but I found a site that may do the trick: http://nsaunders.wordpress.com/2007/01/12/running-a-background-process-in-php/ but instead of refreshing the page, you store it in your scheduling database.
-
You can't php is called thru the webserver. if u can have a background task to update your db is a better solution, as this wont execute the script at particular intervals (which takes time, to load php, load the script, parse it and execute), but requires a dedicated server, and disabling php max runtime (set_time_limit(0)
-
Need help making login verification cleaner...
laffin replied to pneudralics's topic in PHP Coding Help
verify.php <?php //Check to see if cookie is set if (isset($_COOKIE['id']) && isset($_COOKIE['ip']) && isset($_COOKIE['uniqueid'])){ $cookieid = $_COOKIE['id']; $cookieip = $_COOKIE['ip']; $cookieuniqueid = $_COOKIE['uniqueid']; //Select id and loginip according to cookie uniqueid $query = "SELECT * FROM user WHERE uniqueid = '$cookieuniqueid' LIMIT 1"; if ($result = mysql_query ($query)) { while ($row = mysql_fetch_array ($result)) { $id = $row['id']; $uniqueid = $row['uniqueid']; $ip = $row['ip']; } } //Compare cookie ip and id with query loginip and id --- if cookies match database then we allow if (!($_COOKIE['id'] == $id && $_COOKIE['ip'] == $ip && $_COOKIE['uniqueid'] == $uniqueid)) { header('Location: index.php'); die(); //End cookie compare } //End cookie set } //Redirect to index if cookie is not set else { header('Location: index.php'); die(); } ?> Notice I removed the header/footer, and added die functions (the header function doesnt like any outbound info beforehand) sample.php <?php require('verify.php'); require('header.php'); //If not logged in user wont see the below.. ?> Login user sees this <?php require('footer.php'); ?> Although your code can be optimized a lot, this was more of instruction of how to include other aspects to your code. I do agree with teamotomic, is that you should start using functions, but that is beyond the scope of the request. -
if(!preg_match('/^[a-z]+$/i',$string)) echo 'Illegal characters';
-
Undefined offset when using a flat file with pipe symbols
laffin replied to Eejut's topic in PHP Coding Help
uhm, why not use explode, split is deprecated list ($name, $pass, $fileidnumber) = explode('|', $line); -
Its better known as variable variables <?php $animals=array('dog','cat','mouse'); foreach($animals as $val) { $new_var="animal_{$val}"; $$new_var=$new_var; } var_dump($animal_dog,$animal_cat,$animal_mouse); ?>
-
Writting a script to arrange images........ need some help
laffin replied to TGWSE_GY's topic in PHP Coding Help
correct <input type="text" name="orders[image_id]" value="1"> will return as $_POST['orders'][image_id] = 1 -
Can I use a $variable to determine which DB row data is entered?
laffin replied to Modernvox's topic in PHP Coding Help
why not just scrape the state/city pages as well? I notices that state pages all use http://geo.craigslist.org while cities have their own subdomain. -
Writting a script to arrange images........ need some help
laffin replied to TGWSE_GY's topic in PHP Coding Help
u can grab the array keys with $keys=array_keys($array) this gives a new array of the keys but if yer working with code an array element at a time its easier using foreach foreach($array as $key=>$val) { } using foreach is by far very easy to use -
Writting a script to arrange images........ need some help
laffin replied to TGWSE_GY's topic in PHP Coding Help
why not just use the image id as the index of the array? <input type="text" name="orders[image_id]" value="1"> -
Actually I did think of one more way.... if you create all accounts beforehand u can have a status field, where ya can set it as in use or not in use with a query, you can retirve how many accts are not in use with this, you can generate a number between 1 and not in use accts with another query u can fetch the id dummy code $arr=mysql_fetch_row(mysql_query("SELECT Count(id) FROM users WHERE status='not in use')); $not_in_user=$arr[0]; $newid=rand(1,$not_in_use); $arr=mysql_fetch_assoc(mysql_query('SELECT * FROM users WHERE status='not in use' AND LIMIT=1 OFFSET=$newid")); $id=$arr['id'] That would be the simplest, but as said, all accounts must be already in the database.... Hopefully above dummy code is some help
-
I would set up a bitstring. but 10,000,000,000 is quite a large bitstring. so u may just opt for a bitmask file
-
I worked on some code as well, as it was an interesting project. As with crabfingers, I used a recursive function, with a structured array. But a very completly different code, I tried to stick with your <li> directives, but also added headers (folder names). as well as some indentation so you can see in html the levels it retrived. Neat little project Its not bad. <?php function sitemap($basedir='./',$includes=array('html','htm','php')) { $pages=array(); if($handle=@opendir($basedir)) { while(false!==($fn=readdir($handle))) { if(substr($fn,0,1)=='.') continue; if(is_dir($basedir.$fn)) { $subpages=sitemap("{$basedir}{$fn}/"); if(!empty($subpages)) { $pages[]=$fn; $pages[]=$subpages; } } else { $fileinfo=pathinfo($fn); if(in_array($fileinfo['extension'],$includes)) { $page=file_get_contents($basedir.$fn); $title=preg_match('@<title>(.+)</title>@i',$page,$title)?$title[1]:$fileinfo['basename']; $pages[]="{$title}\t{$fileinfo['dirname']}/{$fn}"; } } } closedir($handle); } return $pages; } function displaysitemap($sitemap=array(),$level=0) { $indent=str_pad(' ',($level+1*4)); echo "{$indent}<ul>\n"; foreach($sitemap as $entry) { if(is_array($entry)) displaysitemap($entry,$level+1); else { $info=explode("\t",$entry); echo "{$indent}<li>". (isset($info[1])?"<a href=\"{$info[1]}\">":'') . $info[0] . (isset($info[1])?'</a>':'') ."</li>\n"; } } echo "{$indent}</ul>"; } displaysitemap(sitemap()) ?>
-
In Response to uniflare: This is true, that the current script has no benefit. However, I dont see it using that much more resources than using your web servers mime handler, you are just transferring the mime handling to a php script. but it can be the base for a more functional script, hit counter, hot-link prevention and so forth.
-
its a bit confusing, but you should really use a debugger to find out what's happening. all the similar names confused me, but here it goes // function countall('foo') function countAll($arg1) { // if(3==0) if (func_num_args() == 0) { die("You need to specify at least one argument"); } else { // $args=array('foo','bar','baz'); $args = func_get_args(); // Returns an array of arguments // Remove the defined argument from the beginning // $args=array('bar','baz'); array_shift($args); // $count=strlen('foo'); taken from function arguments, not get_func_args $count = strlen ($arg1); foreach ($args as $arg) { $count += strlen($arg); } } return $count; } echo countAll("foo", "bar", "baz"); // Displays ’9’
-
the '+' is a sign that the url parameter is still encoded.
-
nope, in your processing script use urldecode. $month=urldecode($_GET['month']);
-
PHP + MYSQL Categories > Sub Categories | Help!
laffin replied to GFXUniverse's topic in PHP Coding Help
PHP Portion: Bow that you got your mysql table all setup, next you are probably wondering, how to load and use this in php. well there are plenty of ways, u can traverse the tree manually or use more complex coding and use a recursive function in either instance, you should build a simple array of the categories function get_categories() { $category=array(); $res=mysql_query('SELECT * FROM category'); while($arr=mysql_fetch_assoc($res)) { $category[$arr['parent']][$arr['id']=$arr['name']; } return $category; } which should give an array, if your example structure is used something like myself I prefer recursion method, which is a bit more coding, but makes the traversal much simpler. -
PHP + MYSQL Categories > Sub Categories | Help!
laffin replied to GFXUniverse's topic in PHP Coding Help
The easiest ways are pretty much simple and can only provide a number of level of categories. And In all actuality, you are pretty close to a multilevel category system already! now if you remove the category table, and rename this sub-category table as category. your set now ya revise your category structure (with ids) so you can see a bit more visually now your root categories don't have parents, so u leave parent as 0; so you can traverse the tree pretty easily: to get all root categories SELECT * FROM category WHERE parent=0 which should return 1 & 2 than go from there changing parent. -
Actually, I had some buggy code. I should have used the percentage range rather than the percentage. but some of this talk has given rise to a similar system (with the original percentage system also included) <?php $items = array( array('lint',40), array('copper coin',35), array('silver coin',15), array('gold coin',5), array('bank note',5) ); // How many loops should we do $loops=500; // Find the total weight of all items $total_weight=0; foreach($items as $key=>$val) { $items[$key][2]=$total_weight; $total_weight=$items[$key][3]=$val[1]+$total_weight; } // initialize our stat counter foreach(array_keys($items) as $val) $stat[$val]=0; for($loop=0;$loop<$loops;$loop++) { $chance=rand(0,$total_weight); // Check Against Item Percent Markers $marker=0; foreach($items as $key=>$val) if($chance<=$val[3]) break; // Statistics $stat[$key]++; } // Display Found stats for each item header('Content-Type: text/plain'); echo "Loop of {$loops}\n"; echo "Just by weights\n"; foreach($items as $key=>$val) { echo "{$val[0]} found {$stat[$key]} times (%". (($stat[$key]/$loop)*100) .") base weight ($val[1]) base percentage(%". number_format((($val[1]/$total_weight)*100),2) .")\n"; } // original code with percentile system $item_count=count($items); // Find the total weight of all items $total_weight=0; foreach($items as $val) { $total_weight+=$val[1]; } // add in percentage markers $weight=0; foreach($items as $key=>$val) { // we want to keep percentage markers as int // so multiplying the marker by 10,000, gives us 2 extra digits, a bit finer resolution $weight+=(int)(($val[1]/$total_weight)*10000); $item_perc[$val[0]]=$weight; } // sort our percentage markers sort($item_perc); // initialize our stat counter foreach(array_keys($item_perc) as $val) $stat[$val]=0; for($loop=0;$loop<$loops;$loop++) { $chance=rand(0,10000); // Check Against Item Percent Markers foreach($item_perc as $key=>$val) if($chance<$val) break; // Statistics $stat[$key]++; } // Display Found stats for each item echo "\npercentage system\n"; foreach($items as $key=>$val) { echo "{$val[0]} found {$stat[$key]} times (%". (($stat[$key]/$loop)*100) .") base weight ($val[1]) base percentage(%". number_format((($val[1]/$total_weight)*100),2) .")\n"; } ?> Output: Loop of 500 Just by weights lint found 201 times (%40.2) base weight (40) base percentage(%40.00) copper coin found 180 times (%36) base weight (35) base percentage(%35.00) silver coin found 72 times (%14.4) base weight (15) base percentage(%15.00) gold coin found 19 times (%3. base weight (5) base percentage(%5.00) bank note found 28 times (%5.6) base weight (5) base percentage(%5.00) percentage system lint found 185 times (%37) base weight (40) base percentage(%40.00) copper coin found 171 times (%34.2) base weight (35) base percentage(%35.00) silver coin found 85 times (%17) base weight (15) base percentage(%15.00) gold coin found 28 times (%5.6) base weight (5) base percentage(%5.00) bank note found 31 times (%6.2) base weight (5) base percentage(%5.00)
-
That wont work very well, in which you have two of the same weight. reason for the conversion for percentages. $items = array( 'lint' => 40, 'copper coin' => 30, 'silver coin' => 15, 'gold coin' => 5, 'bank note' => 5 ); it will never display bank note, as it has the same value as gold coin, and you have to have them in highest to lowest order format.
-
Nice code salathe, I think for simplicity yours wins hands down. But its nice to see the various routines you can use to perform such a task
-
Actually, i rechecked your code again. Your system can provide for such a system as I describe above. But I would avoid using those values as described. I would reduce the weights assigned as much as I can, as it creates an array entry for each probability percentage. But its some nice code nonetheless.
-
Exactly AlexWD, The only difference between your code and mine, is that your item list weight system must be always 100. So you always have to adjust the values if you add new items. But its a good example of a weighted system, as your output shows, the values are approximately what you assigned the weights as. That is why i use the system that I had shown in the example. total weight = sum(item weight). item_percentage=(total_wight/item_weight). So you can assign a system where, u have some meaning: define('RARITY_FREQUENT',1000); define('RARITY_COMMON',600); define('RARITY_UNCOMMON',500); define('RARITY_UNIQUE',250); define('RARITY_RARE',100); define('RARITY_LEGENDARY',10); and the code will balance out the the system. Without readjusting all the other values. But its some good code nonetheless.
-
Your still not accounting for the weight system. try using the stats, of each item. u will see yours is pretty much random, while mine consistantly matches up with a slight variation. you will see your code, is pretty much still random. while in a weighted system, the percentages match. $total_weight=sum(items_weight) so your percentage of each item, depends on all the items together.