How to quickly and easily add dummy entries in database

Untitled

If you have been programming for a while you’ve definitely been in a situation where you are developing something that accesses database but there are not enough entries or no entry that all in the database. You cannot verify results unless there are some entries in the database. So what can you do in this situation? Manually adding entries is okay for a few rows but that doesn’t give you the reality of a whether the application will work in a full-scale database.  there are tools available for creating such a dummy entries. While most of them are paid or limited in the number of rows they can add, the wonderful tool at generatedata.com is both free and open source.

You need to download the php application and run it on your local computer. The website is only for demo purpose and allows only 100 records. As as might have guessed you need a php server for this such as Xampp.

Step 1. Download the application as a zip file from the github repository – https://github.com/benkeen/generatedata .

Step 2. Extract downloaded zip file and copy the contents into the server’s htdocs folder

Step 3. Start your server and open the downloaded app in your browser.

step 4. The application will ask you to configure itself.

1

You need to create a new database in your local mysql instance where the application will store all its configuration data. Now enter the name of the created database and click next. Fill the other details asked and proceed. The application will now download various plugins from the Internet and store them in the database you provided.

2

 

Step 5. With this you complete the installation. You can now create dummy data for your database using this tool. Let us see how you can do that.

 

Steps to generate dummy data

Step 1. Provide all the field names. Then provide the type of data for each field. You can select from the various predefined types such as numbers, a defined range, date, credit card numbers, locations, user-defined list and even name and surname of specific gender.

3

 

 

Step 2. Select the application type for which you want to generate the data. You can create the fields for mysql database, JSON, HTML table, Excel file, CSV and many other formats including xml.

4

 

Step 3. Provide the database name in which you want to insert this data. The database name will only be used only for creating the script and not for performing any instructions so you can be safe. Provide whether you want to insert the new entries for update the existing one. Yes it even supports updating the existing entries. Isn’t that cool?

5

 

Step 4. Enter the number of entries you want to be created and hit generate. That’s it. You’ll be presented with the script for entering be generated data.

6

 

This tool is free, easy to use, effective and open source. No advertisements or any fee applicable. And the best of all it runs on your own computer so you do not need any Internet connection for generating such data. It also provides developer documentation for creating new data types and new export formats, making it a highly extensible and customizable solution.

PHP – How To Download Multiple Files Together As A ZIP File

Consider a situation where you have a bunch of files on your server and you want to send all of them to a user. Of course you could have the user download one file at a time but this is not a very good solution for obvious reasons (large number of files, for starters). This is a very common situation faced by many PHP programmers. A very convenient solution is to create a zip file including all the files to be sent and transfer the zip file. This provides a great convenience to the user who now doesn’t have to download each file separately.

But how do you do it in PHP? It’s actually very simple. Let’s see how.

Here is the complete code for doing it –


<?php
       $files;
   
       if ($handle = opendir('./uploads')) {
           while (false !== ($entry = readdir($handle))) {
               if ($entry != &quot;.&quot; &amp;&amp; $entry != &quot;..&quot;) {
                   $files[] = './uploads/' . $entry;
               }
           }
           closedir($handle);
       }

       # create new zip object
       $zip = new ZipArchive();
   
       # create a temp file &amp; open it
       $tmp_file = tempnam('.','');
       $zip->open($tmp_file, ZipArchive::CREATE);

       # loop through each file
       foreach($files as $file){
           # download file
           $download_file = file_get_contents($file);

           #add it to the zip
           $zip->addFromString(basename($file),$download_file);
       }

       # close zip
       $zip->close();

       # send the file to the browser as a download
       header('Content-disposition: attachment; filename=Papers.zip');
      header('Content-type: application/zip');
      readfile($tmp_file);
?>

Now let’s see how it works. Please note that this code works only in PHP version 5.2.0 and above.

This code traverses the directory “./uploads” and adds all files to the $files array. If you want to apply more filtering on the files that should be selected then you can do that in the while loop. The check for “.” and “..” are needed to prevent infinite recursion in directory traversal.

if ($handle = opendir('./uploads')) {
        while (false !== ($entry = readdir($handle))) {
            if ($entry != &quot;.&quot; &amp;amp;&amp;amp; $entry != &quot;..&quot;) {
                $files[] = './uploads/' . $entry;
            }
        }
        closedir($handle);
    }

This creates a new ZipArchive object, crates a temporary file and opens that file through the ZipArchive class. You can read more about this class here at the official PHP manual.

# create new zip object
    $zip = new ZipArchive();
# create a temp file &amp;amp; open it
    $tmp_file = tempnam('.','');
    $zip->open($tmp_file, ZipArchive::CREATE);

Next we each file we filtered previously and add them to the zip archive file. The file_get_contents($file) function reads the complete specified file into a string. addFromString adds the content of file provided into a string to the zip file.

# loop through each file
    foreach($files as $file){
        # download file
        $download_file = file_get_contents($file);
        #add it to the zip
        $zip->addFromString(basename($file),$download_file);
    }

Now that the zip file is prepared, we close it and present it to the browser. header is used to send raw HTTP headers as per the HTTP standard. the Content-disposition tells the browser that the sent data is a file and that the browser must use a download dialog for saving the file. you can control the file name of zip file through the filename header as used below.

# close zip
$zip->close();
# send the file to the browser as a download
header('Content-disposition: attachment; filename=Papers.zip');
header('Content-type: application/zip');
readfile($tmp_file);

That’s it on the PHP side. You can use an AJAX request to make the download happen without reloading the current page. A simple way is to use jQuery to send the AJAX request similar to this –

<script>
    function downloadPapers(){
        $.ajax({
            type: &quot;POST&quot;,
            url: &quot;download_papers.php&quot;,
            data: &quot;&quot;,
            success: function(msg) {   }
        });
    }
</script>

You can read more about AJAX in jQuery here at the official documentation. This PHP code combined with an AJAX request is a very elegant way of downloading multiples together as a single zip file.