I created a PHP Algorithm and the code for this algorithm which makes every permutation of letters and numbers is this:
<?php
set_time_limit(0);
ini_set('max_execution_time',-1);
ini_set('memory_limit', '-1');
function florg ($n, $elems) {
if($n > 0){
$tmp_set = array();
$res = florg($n-1, $elems);
foreach ($res as $ce) {
foreach ($elems as $e) {
array_push($tmp_set, $ce . $e);
}
}
return $tmp_set;
}
else{
return array('');
}
}
$elems = array('q','w','e','r','t','y','u','i','o','p','a','b','c','d','f','g','h','j','k','l','z','x','c','v','b','n','m','1','2','3','4','5','6','7','8','9','0');
$v = FlOrG(7, $elems);
foreach($v as $child) {
print $child . "\n";
}
?>
Basically what it does, is to make a 7 character text using all possible combinations of letters and numbers on keyboard.
So because this program is a little bit huge, I added these 3 lines at the top of the page:
set_time_limit(0);
ini_set('max_execution_time',-1);
ini_set('memory_limit', '-1');
Now whenever I run this program on XAMPP, I get this error message:
Fatal error: Out of memory (allocated 908066816) (tried to allocate 805306376 bytes) on line 11
And here is line 11:
array_push($tmp_set, $ce . $e);
So I don't understand the relation between this error and this line! I also ran this program online on my web hosting service, but again got the same error message.
I think the code looks fine, therefore my question is how can I run this program ? What is the problem with array_push() in this code ?
I would really appreciate any help or idea from you guys cause I've been had this problem for weeks...
You could try to replace array_push($array, $value) with same-effect-having $array[] = $value notation like in your example:
foreach ($res as $ce) {
foreach ($elems as $e) {
$tmp_set[] = $ce . $e;
}
}
and variable $v = florg(7, $elems);
in this 7 is a big so, and it is combining the values from each other so, first try to add small number than check one by one. like
florg(1,$elems);
florg(2,$elems);
florg(3,$elems);
and so on.
Hope this will helps you
I try to set up some basic fixtures in Mongo DB. To do so I read a set of JSON files and try to insert them into local db (I am connecting with admin rights). Here's the weird part. For some reasons I wrote to versions of the code that should work basically the same so I have one:
$client = new \MongoClient($connectionURI);
$db = $client->selectDB($database);
$collections = $db->listCollections();
foreach ($collections as $collection) {
//echo "Removing all documents from '$collection'" . PHP_EOL;
$collection->remove();
$tmp = explode('.', (string)$collection);
$collectionName = $tmp[1];
$tmp = explode('_', $tmp[0]);
$dbName = $tmp[1];
$country = $tmp[2];
if(file_exists(__DIR__."/fixtures/{$country}/{$dbName}/{$collectionName}.json")) {
echo "Inserting fixture data into '{$collection}'".PHP_EOL;
$data = json_decode(file_get_contents(__DIR__."/fixtures/{$country}/{$dbName}/{$collectionName}.json"));
$doc = $collection->insert($data, ["w" => "majority"]);
}
}
And second one based on iteration of the files to read instead of listing existing collections:
$client = new \MongoClient($connectionURI);
foreach(glob(__DIR__.'/fixtures/*', GLOB_ONLYDIR) as $dir) {
$country = basename($dir);
foreach(glob(__DIR__.'/fixtures/'.$country.'/*', GLOB_ONLYDIR) as $dbDir) {
$collections = array_diff(
scandir(__DIR__."/fixtures/{$country}/".basename($dbDir)), ['.', '..']
);
$dbName = 'test_'.basename($dbDir).'_'.$country;
foreach($collections as $collectionFile) {
$collectionName = pathinfo($collectionFile)['filename'];
$data = [];//json_decode(file_get_contents(__DIR__."/fixtures/{$country}/".basenam e($dbDir)."/{$collectionName}.json"));
// $client->$dbName->$collectionName->insert($data);
$db = $client->selectDB($dbName);
$collection = $db->selectCollection($collectionName);
$collection->insert($data, ["w" => "majority"]);
echo $country.'->'.$dbName.'->'.$collectionName.PHP_EOL;
}
}
}
The trick is that the first implementation works nicely and second throws MongoCursorException with authentication problem. The problem I am having is that both versions try to connect to exact same database and collection in fact. So I am getting following output:
Inserting fixture data into 'test_customers_poland.accounts'
PHP Fatal error: Uncaught exception 'MongoCursorException' with message 'Couldn't get connection: Failed to connect to: 127.0.0.1:27017: SASL Authentication failed on database 'test_customers_poland': Authentication failed.' in /srv/dev-api/src/tests/init_databases.php:96
Stack trace:
#0 /srv/dev-api/src/tests/init_databases.php(96): MongoCollection->insert(Array, Array)
#1 {main}
thrown in /s
rv/dev-api/src/tests/init_databases.php on line 96
Of course I also checked running those snippets separately as well with the same result so the question is: what am I doing wrong in second approach?
I am using clear image SDK for reading barcode from images. The code is working only first time on my browser. When I reload the page, it throws an exception. To make it work again, I restart my web server and it works again. But when I reload the page, it again throws the same exception. The error is given below:
( ! ) Fatal error: Uncaught exception 'com_exception' with message '<b>Source:</b> ClearImage.ClearImage.1<br/><b>Description:</b> SEH Exception 0xC0000005' in C:\wamp\www\test.php on line 11
( ! ) com_exception: <b>Source:</b> ClearImage.ClearImage.1<br/><b>Description:</b> SEH Exception 0xC0000005 in C:\wamp\www\test.php on line 11
If I call the script from command line, it works fine and it does not matter if I call the script again. I dont need to restart the webserver at all.
What is exactly going on with this script? Do I need to close any particular resources or connections in the script so that it can work again? This is the first time I am working with COM object.
<?php
try {
// To insert line breaks in HTML output replace \n with <br>
// Create and Configure ClearImage Object
$Ci = new COM("ClearImage.ClearImage");
} catch (Exception $e) {
echo $e->getMessage();
}
// Create the 1D Barcode Pro object. Use any of the other engines here.
$Bc = $Ci->CreateBarcodePro();
// Open file.
$File = 'c39.tif'; // Use YOUR file name
$File = 'C:\\test.jpg';
$Bc->Image->Open($File);
// $Bc->Code39 = true;
// Set reading parameters. Update values if needed. See Online API help
$Bc->AutoDetect1D = 65535;
/*
* my testing
*/
$Bc->Type = 2;
$Bc->Directions = 2;
$Bc->Algorithm = 2;
/*
* my tesing
*/
$Bc->Algorithm = 2;
// Read Barcodes
$BCcount = $Bc->Find(0);
echo "\nBarcodes: $BCcount\n";
if ($BCcount == 0) {
$Msg = "No barcodes found in $File \n";
exit($Msg);
}
for ($i = 1; $i <= $BCcount; $i ++) {
echo "Barcode #$i ";
$FBc = $Bc->BarCodes($i);
// NOTE: Text value will terminate on the first null in data
echo "Type: $FBc->Type Length: $FBc->Length\n";
echo "Barcode Value: $FBc->Text \n";
echo " Binary Data in Hex: \n ";
$hex_ary = array();
$cnt = 0;
foreach ($FBc->Data as $chr) {
$hex_ary[] = sprintf("%02X", $chr);
$cnt = $cnt + 1;
if ($cnt == 16) // break lines after 16 numbers
{
$hex_ary[] = sprintf("\n");
$cnt = 0;
}
}
echo implode(' ', $hex_ary);
echo "\n";
}
unset($Ci);
I've got a simple php script for getting a csv file into array and inserting each row into MongoDB (CD Collection). But somehow insert returns an error after first successful one:
"Fatal error: in C:\xampp\htdocs\mongo\lesson1\index.php on line 17"
Here's the code. What could possibly cause such an error? DB receives only one (first).
$filename = 'd:/cd_col.csv'; // Each line: Title;No. of CD with movie
$csvfile = fopen($filename,'rb');
while(!feof($csvfile)) {
$csvarray[] = fgetcsv($csvfile);
}
$m = new MongoClient();
$db = $m->mymovies;
$collection = $db->movies;
$id=0;
foreach($csvarray as $key=>$value)
{
$movie = explode(';', $value[0]);
$fmovie = array('_id'=>++$id, 'title'=>$movie[0], 'cdno'=>$movie[1]);
if($collection->save($fmovie)===true) { // this is line 17
echo 'Successful insert: '.$key;
}
}
SOLVED:
Cannot use save in php, that's it.
You should use $collection->insert(); instead.
First of all i want to apologize if this is the most basic question ever! I'm not that good with php but i'm learning.
I can't find a solution or even understand why it's going wrong all the time. I do want to know why this is happening
I'm trying to get the two latest tweets from a twitter account. I don't want to use massive (existing, i know) classes or codes which i don't understand. So i tried the following myself:
$timeline = "http://twitter.com/statuses/user_timeline.xml?screen_name=Mau_ries";
$data = file_get_contents($timeline);
$tweets = new SimpleXMLElement($data);
$i = 0;
foreach($tweets as $tweet){
echo($tweet->text." - ".$tweet->created_at);
if (++$i == 2) break;
}
When i first ran this code i got the text from my tweets, but when i refreshed the page i sometimes get the following error:
Warning: file_get_contents(http://twitter.com/statuses/user_timeline.xml?screen_name=Mau_ries) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in /path/to/file on line 88
Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /public/sites/www.singledays.nl/tmp/index.php:89 Stack trace: #0 /public/sites/www.singledays.nl/tmp/index.php(89): SimpleXMLElement->__construct('') #1 {main} thrown in /path/to/file on line 89
Lines 88 & 89 are these:
$data = file_get_contents($timeline);
$tweets = new SimpleXMLElement($data);
Really weird. Sometimes it work, sometimes not.
Does anybody know this issue and/or a solution? And why does the error seem to occur randomly (Allthough it;s now erroring for a while allready)?
Thanks!
$timeline = "http://twitter.com/statuses/user_timeline.xml?screen_name=Mau_ries";
$data = #file_get_contents($timeline);
if($data){
$fh = fopen("cache/".sha1($timeline),"w");
fwrite($fh, $data);
fclose($fh);
}else{
$fh = #fopen("cache/".sha1($timeline),"r");
$data = "";
while(!feof($fh)){ $data = fread($fh, 1024); }
fclose($fh);
}
if(!$data) die("could not open url or find a cache of url locally");
$tweets = new SimpleXMLElement($data);
$i = 0;
foreach($tweets as $tweet){
echo($tweet->text." - ".$tweet->created_at);
if (++$i == 2) break;
}
There as every one has said debugging you should really cache the results in files and if it fails to download then use the cache the above code will do it for you.