I am a very NOOB in Redis and this is the first time that I am using this application.I hope you can help me.
I am trying to create a list of words from a dictionary and put in into Redis database. I have a text file containing 200,000 words. How can I put it in my database?
I am using Laravel and My Redis configuration is working fine, because I am able to execute this command.
$redis = Redis::connection();
Redis::set('name', 'MYname');
$name = Redis::get('name');
echo $name;
Thanks in advance!
What is the dictionary used for?
If it is just 200,000 words, you can simply put all of them into a set.
for word in text_file:
Redis::sadd('dictionary_name', word)
and use
Redis::sismember('dictionary_name', word)
to check whethor or not the word is in the dictionary.
Here's documentation about redis set
Finally, I am able to upload my data through this command.
$filename = 'file.txt';
$fp = #fopen($filename, 'r');
if ($fp) {
$array = explode("\n", fread($fp, filesize($filename)));
}
$redis = Redis::connection();
Redis::sadd('dictionary', $array);
Related
This is the first time that I try to generate a Word document with Laravel.
I have in database columns that I should to generate in a Word document.
For example I have the following database:
I should generate a document file which contains hello John for example
So all the elements that is inside '<<>>' are a dynamic variables .
How can I get from database the value of the column preamble and set the variable name in the same time?
I found that exists a package names phpwordn but I found that I should have a template that is a word file with dynamic variables n but in my case I have no template I have all the components of my Word file in the database.
Is it possible to do that?
If you have any idea or an example to do that help me.
You can use preg_replace_callback() to convert all special tags to their corresponding values. In your case a tag has a form of <<foo>>, so here it goes:
$values = [
'me' => 'user14053977',
'saviour' => 'RoboRobok',
];
$template = 'My name is <<me>> and <<saviour>> saved me!';
$result = preg_replace_callback(
'#<<(\w+)>>#',
function (array $matches) use ($values): string {
[$tag, $tagName] = $matches;
return $values[$tagName] ?? $tag;
},
$template
);
// $result is now "My name is user14053977 and RoboRobok saved me!"
It's a pure PHP code. Now it's just the matter of using the values in Laravel and injecting $result to your Word document.
I have a program that can print an individual PDF when on a students file. I'm doing this using niklasravnsborg/laravel-pdf package in Laravel 5.7. It's working great because I can just stream the pdf from a view into the browser then print from there.
Now I'm wanting to batch print the PDF's at the end of the day instead of one by one. How can I do this? There's no documentation for this on the package repository.
Several ways I've thought of doing this: one, save each PDF as an image file then try to print all files in that folder at the end of the day. If I do this, how would I print all files in that folder?
Next: does anyone know a way to maybe append a new PDF to a variable containing all the previously looped pdf's?
For example:
$finalpdf;
$students = Student::all();
foreach($students as $student){
$pdf = PDF::loadView('pdf.document', $student);
$finalpdf .+ $pdf; //i know this line doesn't work, but how to alter it?
return $pdf->save('document.pdf');
}
After a few days of playing around, hopefully this solution helps someone in the future. I used another package called "lara pdf merger". By writing my files to a save destination, adding those files to a merged file, then deleting the saved files after download, I was able to get my functionality. This is my controller code:
use PDF;
use Illuminate\Filesystem\Filesystem;
use LynX39\LaraPdfMerger\Facades\PdfMerger;
public function batchPrint(){
$pdfMerger = PDFMerger::init();
$i = 0;
$students = Student::whereDate('updated_at', Carbon::today('America/Los_Angeles'))->get();
foreach ($students as $student) {
$pdf = PDF::loadView('printTables', compact('student'));
$pdf->save('pdf/document'.$i.'.pdf');
$pdfMerger->addPDF('pdf/document'.$i.'.pdf');
$i++;
}
$pdfMerger->merge();
$pdfMerger->save("file_name.pdf", "browser");
$file = new Filesystem;
$file->cleanDirectory('pdf');
}
I wish to store PDO results in redis cache so from online resources I gathered i do this.
$domain = 'www.example.com';
function getStat($domain) {
global $pdo;
global $redis;
$statement = "SELECT * FROM mc_visitor_session WHERE website = \'$domain\'";
$hash = md5($statement);
if (!$redis->get($hash . '-cache')) {
$query = $pdo->query($domain);
if ($result = $query->execute()) {
$record = $query->fetchAll(\PDO::FETCH_ASSOC);
$redis->set($hash . '-cache', serialize($record));
$redis->expire($hash . '-cache', 86400);
echo 'RESULT FROM MYSQL';
pretty_print($record);
}
}
$results = unserialize($redis->get($hash . '-cache'));
//will show this if it's already in cache.
echo 'RESULT FROM REDIS';
pretty_print($results);
}
getStat($domain);
As you can see the above code works well. However I wish to work it with pdo prepared statements instead of using pdo query without preparing and safely executing the queries. But I need to get the hash from the query statement as well to be use as the key in redis.
This is just one of the query i want to cache with redis and other queries contains more than 1 WHERE statement which requires multiple PDO Bind Parameter.
This may not be the best way to do it so may I ask for suggestions on how I can improve this to make it safer.
I saw this "Smart" Caching System using PDO and Memcache elsewhere on SO.
Wrap your PDO call inside a function that will hash the statement and an array of parameters.
$name = 'querycache-'.md5(serialize(array($sql, $params)));
You also need to ask yourself if fine-tuning your database, using proper indexes and letting it use its own caching system won't be faster than caching to Redis.
Write $query = $pdo->query($statement); instead of $query = $pdo->query($domain);
So I'm just trying to use xeditable (http://vitalets.github.io/x-editable/docs.html#gettingstarted) to make changes to my database via AJAX.
Since I'm new to this concept and I'm (forcefully) working with PHP for the first time, I need some help.
I setup the frontend part, and a script called (say) script.php is handling the data for me (I need to write the new value in my database).
I can't really understand what to do in the script. Can someone guide me towards it? The docs above don't really do it for me.
Looking in a project I worked on a few months back (sorry about the mysql_ stuff – not my choice!)
Something like:
<?
include your/database/connection_stuff.php;
// Can't remember if x-editable passes the table in as well or not
$table = mysql_real_escape_string($_GET['table']);
// If not,
$table = 'name_of_table';
$value = mysql_real_escape_string($_POST['value']);
$name = mysql_real_escape_string($_POST['name']);
$pk = mysql_real_escape_string($_POST['pk']);
$result = mysql_query("UPDATE `$table` SET `$name` = '$value' WHERE id = '$pk'");
?>
Will do the trick.
I'm having a time migrating images from Wordpress to Drupal with the Migrate 2.4 module. Here are my mappings:
$this->addFieldMapping('field_image','images');
$this->addFieldMapping('destination_file', 'images');
$this->addFieldMapping('field_image:source_dir')
->defaultValue('/Users/grafa/htdocs/wordpress/wp-content/uploads');
$this->addFieldMapping('field_image:file_class')
->defaultValue('MigrateFileUri');
The images come from a function that queries the wp_postmeta table then returns the result to the prepareRow() function.
function getImages($row) {
$post_id = $row->id;
$results = db_query("
SELECT pm.post_id, pm.meta_key, pm.meta_value FROM streetroots_wp.wp_postmeta AS pm LEFT JOIN streetroots_wp.wp_posts AS p ON pm.post_id=p.id WHERE p.post_parent = $post_id AND pm.meta_key='_wp_attached_file';");
$images = array();
foreach($results as $result) {
$images[] = $result->meta_value;
}
return !empty($images) ? $images : NULL;
}
This basically returns the image name and relative path from the wp_postmeta table something like '2012/05/figure1.jpg'. I then use prepareRow() like this:
function prepareRow($row) {
$row->images = $this->getImages($row);
}
I'm guessing there's something funky with how I'm using the new-ish migrate module that handles the file fields. The sql is outputs the file names correctly but it doesn't seem like the images are getting copied over. This is a Drupal 7 using Migrate 2.4. Any help is appreciated.
You might want to have a look at the beer.inc lines 377 - 383 within migrate_example folder and at the associated content type *migrate_example_beer* which has an image field defined not a file field which was my mistake and lead to my images not being populated.
Hope this helps!
Migrate 2.5 has largely simplified the handling of files, you should check it out!