DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets

  • submit to reddit

Recent Snippets

                    scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
 public void handle(final KeyEvent keyEvent) {
   if (keyEvent.getCode() == KeyCode.F5) {
    System.out.println("F5 pressed");
    //Stop letting it do anything else
    keyEvent.consume();
   }
 }
});


final KeyCombination keyComb1 = new KeyCodeCombination(KeyCode.R,
                                    KeyCombination.CONTROL_DOWN);
scene.addEventHandler(KeyEvent.KEY_RELEASED, new EventHandler<KeyEvent>() {
                @Override
                public void handle(KeyEvent event) {
                    if (keyComb1.match(event)) {
                        System.out.println("Ctrl+R pressed");
 
                    }
                }
            });                
                    <html lang="en">
    <head>
        <title>Impress Demo</title>
    </head>
    <body>
        <div id="impress">
            <div class="step slide" id="start">
                    <p style='width:1000px;font-size:80px;
                text-align:center'>Creating Stunning Visualizations</p>
                <p>Impress.js </p>
                </div>
 
            <div class="step slide" id="slide2" data-x="-1200" data-y="0">
                    <p style='width:1000px;font-size:80px;'>
                First Slide Moves From Left To Right</p>
                <p>Impress.js </p>
                </div>
        </div>
        <script type="text/javascript" src="js/impress.js"></script>
        <script type="text/javascript">impress().init();</script>
    </body>
</html>                
                    //Add attachment in a PDF document.

//open document
com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document("input.pdf");
//setup new file to be added as attachment
com.aspose.pdf.FileSpecification fileSpecification = new com.aspose.pdf.FileSpecification("sample.txt", "Sample text file");
//add attachment to document's attachment collection
pdfDocument.getEmbeddedFiles().add(fileSpecification);
// Save updated document containing table object
pdfDocument.save("output.pdf");

//Delete all the attachments from the PDF document.

//open document
com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document("input.pdf");
//delete all attachments
pdfDocument.getEmbeddedFiles().delete();
//save updated file
pdfDocument.save("output.pdf");

//Get an individual attachment from the PDF document.

//open document
com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document("input.pdf");

//get particular embedded file
com.aspose.pdf.FileSpecification fileSpecification = pdfDocument.getEmbeddedFiles().get_Item(1);
//get the file properties
System.out.printf("Name: - " + fileSpecification.getName());
System.out.printf("\nDescription: - " + fileSpecification.getDescription());
System.out.printf("\nMime Type: - " + fileSpecification.getMIMEType());
// get attachment form PDF file
try {
    InputStream input = fileSpecification.getContents();
    File file = new File(fileSpecification.getName());
    // create path for file from pdf
    file.getParentFile().mkdirs();
    // create and extract file from pdf
    java.io.FileOutputStream output = new java.io.FileOutputStream(fileSpecification.getName(), true);
    byte[] buffer = new byte[4096];
    int n = 0;
    while (-1 != (n = input.read(buffer)))
    output.write(buffer, 0, n);

    // close InputStream object
    input.close();
    output.close();
} catch (IOException e) {
e.printStackTrace();
}
// close Document object
pdfDocument.dispose();
                
                    add_action('wp_dashboard_setup', 'remove_dashboard_widgets');
function remove_dashboard_widgets() {
     global $wp_meta_boxes;
     // remove unnecessary widgets

     // var_dump( $wp_meta_boxes['dashboard'] ); // use to get all the widget IDs

     unset(
          $wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins'],
          $wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary'],
          $wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']
     );
     // add a custom dashboard widget

     wp_add_dashboard_widget( 'dashboard_custom_feed', __('News from Example.com', 'sagive'), 'dashboardSelectedFeedDisplay', 'side', 'high' ); //add new RSS feed output

}
function dashboardSelectedFeedDisplay() {
     echo '<div class="rss-widget">';
     wp_widget_rss_output(array(
          'url' => 'http://www.example.com/feed',
          'title' => 'Example Site Welcome Title',
          'items' => 2,
          'show_summary' => 1,
          'show_author' => 1,
          'show_date' => 1,
     ));
     echo "</div>";
}                
                    jQuery(function($){
    var itemscount = $("#top-menu li").size();
    var percentage = 100 / itemscount;
     
    $("#top-menu > li").css('width', percentage+'%');
});                
                    //Converting PDF to DOC

//[C#]
 
// Path of input PDF document
String filePath = @"d:\\Source.pdf";
// Instantiate the Document object
Aspose.Pdf.Document document = new Aspose.Pdf.Document(filePath);
// Create DocSaveOptions object
DocSaveOptions saveOptions = new DocSaveOptions();
// Set the recognition mode as Flow
saveOptions.Mode = DocSaveOptions.RecognitionMode.Flow;
// Set the Horizontal proximity as 2.5
saveOptions.RelativeHorizontalProximity = 2.5f;
// Enable the value to recognize bullets during conversion process
saveOptions.RecognizeBullets = true;
// Save the resultant DOC file
document.Save(@"d:\\Resultant.doc", saveOptions);

//[VB.NET]

' Path of input PDF document
Dim filePath As String = "d:\\Source.pdf"
' Instantiate the Document object
Dim document As Aspose.Pdf.Document = New Aspose.Pdf.Document(filePath)
' Create DocSaveOptions object
Dim saveOptions As DocSaveOptions = New DocSaveOptions()
' Set the recognition mode as Flow
saveOptions.Mode = DocSaveOptions.RecognitionMode.Flow
' Set the Horizontal proximity as 2.5
saveOptions.RelativeHorizontalProximity = 2.5F
' Enable the value to recognize bullets during conversion process
saveOptions.RecognizeBullets = True
' save the resultnat DOC file
document.Save("d:\\Resultant.doc", saveOptions)
 
//Converting PDF to Docx format

//[C#]

//open pdf document
Document pdfDocument = new Document("source.pdf");
// instantiate DocSaveOptions object
DocSaveOptions saveOptions = new DocSaveOptions();
// specify the output format as DOCX
saveOptions.Format = DocSaveOptions.DocFormat.DocX;
//save document in docx format
pdfDocument.Save("output.docx", saveOptions);
 
//[VB.NET]

'open pdf document
Dim pdfDocument As Document = New Document("source.pdf")
' instantiate DocSaveOptions object
Dim saveOptions As DocSaveOptions = New DocSaveOptions()
' specify the output format as DOCX
saveOptions.Format = DocSaveOptions.DocFormat.DocX
' save document in docx format
pdfDocument.Save("output.docx", saveOptions)
                
                    //Create a Pivot Table in the Workbook (Code Snippet).
 
//Instantiating an Workbook object
 Workbook workbook = new Workbook();
 //Obtaining the reference of the first worksheet
 Worksheet sheet = workbook.getWorksheets().get(0);
 //Name the sheet
 sheet.setName("Data");
 Cells cells = sheet.getCells();
 
 //Setting the values to the cells
 Cell cell = cells.get("A1");
 cell.setValue("Employee");
 cell = cells.get("B1");
 cell.setValue("Quarter");
 cell = cells.get("C1");
 cell.setValue("Product");
 cell = cells.get("D1");
 cell.setValue("Continent");
 cell = cells.get("E1");
 cell.setValue("Country");
 cell = cells.get("F1");
 cell.setValue("Sale");

 cell = cells.get("A2");
 cell.setValue("David");
 cell = cells.get("A3");
 cell.setValue("David");
 cell = cells.get("A4");
 cell.setValue("David");
 cell = cells.get("A5");
 cell.setValue("David");
 cell = cells.get("A6");
 cell.setValue("James");
 cell = cells.get("A7");
 cell.setValue("James");
 cell = cells.get("A8");
 cell.setValue("James");
 cell = cells.get("A9");
 cell.setValue("James");
 cell = cells.get("A10");
 cell.setValue("James");
 cell = cells.get("A11");
 cell.setValue("Miya");
 cell = cells.get("A12");
 cell.setValue("Miya");
 cell = cells.get("A13");
 cell.setValue("Miya");
 cell = cells.get("A14");
 cell.setValue("Miya");
 cell = cells.get("A15");
 cell.setValue("Miya");
 cell = cells.get("A16");
 cell.setValue("Miya");
 cell = cells.get("A17");
 cell.setValue("Miya");
 cell = cells.get("A18");
 cell.setValue("Elvis");
 cell = cells.get("A19");
 cell.setValue("Elvis");
 cell = cells.get("A20");
 cell.setValue("Elvis");
 cell = cells.get("A21");
 cell.setValue("Elvis");
 cell = cells.get("A22");
 cell.setValue("Elvis");
 cell = cells.get("A23");
 cell.setValue("Elvis");
 cell = cells.get("A24");
 cell.setValue("Elvis");
 cell = cells.get("A25");
 cell.setValue("Jean");
 cell = cells.get("A26");
 cell.setValue("Jean");
 cell = cells.get("A27");
 cell.setValue("Jean");
 cell = cells.get("A28");
 cell.setValue("Ada");
 cell = cells.get("A29");
 cell.setValue("Ada");
 cell = cells.get("A30");
 cell.setValue("Ada");

 cell = cells.get("B2");
 cell.setValue("1");
 cell = cells.get("B3");
 cell.setValue("2");
 cell = cells.get("B4");
 cell.setValue("3");
 cell = cells.get("B5");
 cell.setValue("4");
 cell = cells.get("B6");
 cell.setValue("1");
 cell = cells.get("B7");
 cell.setValue("2");
 cell = cells.get("B8");
 cell.setValue("3");
 cell = cells.get("B9");
 cell.setValue("4");
 cell = cells.get("B10");
 cell.setValue("4");
 cell = cells.get("B11");
 cell.setValue("1");
 cell = cells.get("B12");
 cell.setValue("1");
 cell = cells.get("B13");
 cell.setValue("2");
 cell = cells.get("B14");
 cell.setValue("2");
 cell = cells.get("B15");
 cell.setValue("3");
 cell = cells.get("B16");
 cell.setValue("4");
 cell = cells.get("B17");
 cell.setValue("4");
 cell = cells.get("B18");
 cell.setValue("1");
 cell = cells.get("B19");
 cell.setValue("1");
 cell = cells.get("B20");
 cell.setValue("2");
 cell = cells.get("B21");
 cell.setValue("3");
 cell = cells.get("B22");
 cell.setValue("3");
 cell = cells.get("B23");
 cell.setValue("4");
 cell = cells.get("B24");
 cell.setValue("4");
 cell = cells.get("B25");
 cell.setValue("1");
 cell = cells.get("B26");
 cell.setValue("2");
 cell = cells.get("B27");
 cell.setValue("3");
 cell = cells.get("B28");
 cell.setValue("1");
 cell = cells.get("B29");
 cell.setValue("2");
 cell = cells.get("B30");
 cell.setValue("3");

 cell = cells.get("C2");
 cell.setValue("Maxilaku");
 cell = cells.get("C3");
 cell.setValue("Maxilaku");
 cell = cells.get("C4");
 cell.setValue("Chai");
 cell = cells.get("C5");
 cell.setValue("Maxilaku");
 cell = cells.get("C6");
 cell.setValue("Chang");
 cell = cells.get("C7");
 cell.setValue("Chang");
 cell = cells.get("C8");
 cell.setValue("Chang");
 cell = cells.get("C9");
 cell.setValue("Chang");
 cell = cells.get("C10");
 cell.setValue("Chang");
 cell = cells.get("C11");
 cell.setValue("Geitost");
 cell = cells.get("C12");
 cell.setValue("Chai");
 cell = cells.get("C13");
 cell.setValue("Geitost");
 cell = cells.get("C14");
 cell.setValue("Geitost");
 cell = cells.get("C15");
 cell.setValue("Maxilaku");
 cell = cells.get("C16");
 cell.setValue("Geitost");
 cell = cells.get("C17");
 cell.setValue("Geitost");
 cell = cells.get("C18");
 cell.setValue("Ikuru");
 cell = cells.get("C19");
 cell.setValue("Ikuru");
 cell = cells.get("C20");
 cell.setValue("Ikuru");
 cell = cells.get("C21");
 cell.setValue("Ikuru");
 cell = cells.get("C22");
 cell.setValue("Ipoh Coffee");
 cell = cells.get("C23");
 cell.setValue("Ipoh Coffee");
 cell = cells.get("C24");
 cell.setValue("Ipoh Coffee");
 cell = cells.get("C25");
 cell.setValue("Chocolade");
 cell = cells.get("C26");
 cell.setValue("Chocolade");
 cell = cells.get("C27");
 cell.setValue("Chocolade");
 cell = cells.get("C28");
 cell.setValue("Chocolade");
 cell = cells.get("C29");
 cell.setValue("Chocolade");
 cell = cells.get("C30");
 cell.setValue("Chocolade");

 cell = cells.get("D2");
 cell.setValue("Asia");
 cell = cells.get("D3");
 cell.setValue("Asia");
 cell = cells.get("D4");
 cell.setValue("Asia");
 cell = cells.get("D5");
 cell.setValue("Asia");
 cell = cells.get("D6");
 cell.setValue("Europe");
 cell = cells.get("D7");
 cell.setValue("Europe");
 cell = cells.get("D8");
 cell.setValue("Europe");
 cell = cells.get("D9");
 cell.setValue("Europe");
 cell = cells.get("D10");
 cell.setValue("Europe");
 cell = cells.get("D11");
 cell.setValue("America");
 cell = cells.get("D12");
 cell.setValue("America");
 cell = cells.get("D13");
 cell.setValue("America");
 cell = cells.get("D14");
 cell.setValue("America");
 cell = cells.get("D15");
 cell.setValue("America");
 cell = cells.get("D16");
 cell.setValue("America");
 cell = cells.get("D17");
 cell.setValue("America");
 cell = cells.get("D18");
 cell.setValue("Europe");
 cell = cells.get("D19");
 cell.setValue("Europe");
 cell = cells.get("D20");
 cell.setValue("Europe");
 cell = cells.get("D21");
 cell.setValue("Oceania");
 cell = cells.get("D22");
 cell.setValue("Oceania");
 cell = cells.get("D23");
 cell.setValue("Oceania");
 cell = cells.get("D24");
 cell.setValue("Oceania");
 cell = cells.get("D25");
 cell.setValue("Africa");
 cell = cells.get("D26");
 cell.setValue("Africa");
 cell = cells.get("D27");
 cell.setValue("Africa");
 cell = cells.get("D28");
 cell.setValue("Africa");
 cell = cells.get("D29");
 cell.setValue("Africa");
 cell = cells.get("D30");
 cell.setValue("Africa");

 cell = cells.get("E2");
 cell.setValue("China");
 cell = cells.get("E3");
 cell.setValue("India");
 cell = cells.get("E4");
 cell.setValue("Korea");
 cell = cells.get("E5");
 cell.setValue("India");
 cell = cells.get("E6");
 cell.setValue("France");
 cell = cells.get("E7");
 cell.setValue("France");
 cell = cells.get("E8");
 cell.setValue("Germany");
 cell = cells.get("E9");
 cell.setValue("Italy");
 cell = cells.get("E10");
 cell.setValue("France");
 cell = cells.get("E11");
 cell.setValue("U.S.");
 cell = cells.get("E12");
 cell.setValue("U.S.");
 cell = cells.get("E13");
 cell.setValue("Brazil");
 cell = cells.get("E14");
 cell.setValue("U.S.");
 cell = cells.get("E15");
 cell.setValue("U.S.");
 cell = cells.get("E16");
 cell.setValue("Canada");
 cell = cells.get("E17");
 cell.setValue("U.S.");
 cell = cells.get("E18");
 cell.setValue("Italy");
 cell = cells.get("E19");
 cell.setValue("France");
 cell = cells.get("E20");
 cell.setValue("Italy");
 cell = cells.get("E21");
 cell.setValue("New Zealand");
 cell = cells.get("E22");
 cell.setValue("Australia");
 cell = cells.get("E23");
 cell.setValue("Australia");
 cell = cells.get("E24");
 cell.setValue("New Zealand");
 cell = cells.get("E25");
 cell.setValue("S.Africa");
 cell = cells.get("E26");
 cell.setValue("S.Africa");
 cell = cells.get("E27");
 cell.setValue("S.Africa");
 cell = cells.get("E28");
 cell.setValue("Egypt");
 cell = cells.get("E29");
 cell.setValue("Egypt");
 cell = cells.get("E30");
 cell.setValue("Egypt");

 cell = cells.get("F2");
 cell.setValue(2000);
 cell = cells.get("F3");
 cell.setValue(500);
 cell = cells.get("F4");
 cell.setValue(1200);
 cell = cells.get("F5");
 cell.setValue(1500);
 cell = cells.get("F6");
 cell.setValue(500);
 cell = cells.get("F7");
 cell.setValue(1500);
 cell = cells.get("F8");
 cell.setValue(800);
 cell = cells.get("F9");
 cell.setValue(900);
 cell = cells.get("F10");
 cell.setValue(500);
 cell = cells.get("F11");
 cell.setValue(1600);
 cell = cells.get("F12");
 cell.setValue(600);
 cell = cells.get("F13");
 cell.setValue(2000);
 cell = cells.get("F14");
 cell.setValue(500);
 cell = cells.get("F15");
 cell.setValue(900);
 cell = cells.get("F16");
 cell.setValue(700);
 cell = cells.get("F17");
 cell.setValue(1400);
 cell = cells.get("F18");
 cell.setValue(1350);
 cell = cells.get("F19");
 cell.setValue(300);
 cell = cells.get("F20");
 cell.setValue(500);
 cell = cells.get("F21");
 cell.setValue(1000);
 cell = cells.get("F22");
 cell.setValue(1500);
 cell = cells.get("F23");
 cell.setValue(1500);
 cell = cells.get("F24");
 cell.setValue(1600);
 cell = cells.get("F25");
 cell.setValue(1000);
 cell = cells.get("F26");
 cell.setValue(1200);
 cell = cells.get("F27");
 cell.setValue(1300);
 cell = cells.get("F28");
 cell.setValue(1500);
 cell = cells.get("F29");
 cell.setValue(1400);
 cell = cells.get("F30");
 cell.setValue(1000);

 //Adding a new sheet
 int sheetIndex = workbook.getWorksheets().add();
 Worksheet sheet2 = workbook.getWorksheets().get(sheetIndex);
 //Naming the sheet
 sheet2.setName("PivotTable");
 //Getting the pivottables collection in the sheet
 PivotTableCollection pivotTables = sheet2.getPivotTables();
 //Adding a PivotTable to the worksheet
 int index = pivotTables.add("=Data!A1:F30", "B3", "PivotTable1");
 //Accessing the instance of the newly added PivotTable
 PivotTable pivotTable = pivotTables.get(index);
 //Showing the grand totals
 pivotTable.setRowGrand(true);
 pivotTable.setColumnGrand(true);
 //Setting the PivotTable report is automatically formatted
 pivotTable.setAutoFormat(true);
 //Setting the PivotTable autoformat type.
 pivotTable.setAutoFormatType(PivotTableAutoFormatType.REPORT_6); 
 //Draging the first field to the row area.
 pivotTable.addFieldToArea(PivotFieldType.ROW, 0);
 //Draging the third field to the row area.
 pivotTable.addFieldToArea(PivotFieldType.ROW, 2);
 //Draging the second field to the row area.
 pivotTable.addFieldToArea(PivotFieldType.ROW, 1);
 //Draging the fourth field to the column area.
 pivotTable.addFieldToArea(PivotFieldType.COLUMN, 3);
 //Draging the fifth field to the data area.
 pivotTable.addFieldToArea(PivotFieldType.DATA, 5);
 //Setting the number format of the first data field
 pivotTable.getDataFields().get(0).setNumber(7);
 //Saving the Excel file 
 workbook.save("f:\\test\\pivotTable_test.xls");
 
//Task2: Create a Pivot Chart based on the Pivot Table
 
//Instantiating an Workbook object
 Workbook workbook = new Workbook("f:\\test\\pivotTable_test.xls");
 //Adding a new sheet
 int sheetIndex = workbook.getWorksheets().add(SheetType.CHART);
 Worksheet sheet3 = workbook.getWorksheets().get(sheetIndex);
 //Naming the sheet
 sheet3.setName("PivotChart");
 //Adding a column chart       
int chartIndex = sheet3.getCharts().add(ChartType.COLUMN, 0, 5, 28, 16);
Chart chart = sheet3.getCharts().get(chartIndex);
//Setting the pivot chart data source
chart.setPivotSource("PivotTable!PivotTable1");
chart.setHidePivotFieldButtons(false);
//Saving the Excel file 
workbook.save("f:\\test\\pivotChart_test.xls");
                
                    import com.intellij.javaee.dataSource.DataSource;
import com.intellij.javaee.dataSource.DataSourceManager;
import com.intellij.javaee.dataSource.ServerInstance;
import com.intellij.openapi.project.Project;
import java.lang.reflect.Method;
import java.sql.Connection;
...

DataSourceManager dataSourceManager = 
DataSourceManager.getInstance(e.getProject());
DataSource dataSource = 
dataSourceManager.getDataSourceByName("BB21_TST"); 
Method getConnectionMethod  = 
dataSource.getClass().getDeclaredMethod("getConnection", Project.class, ServerInstance.class);
getConnectionMethod.setAccessible(true);
Connection conn =
 (Connection) getConnectionMethod.invoke(dataSource, p, null);                
                    //Loading MSG Files

//[C#]

//Create an instance of MapiMessage from file
MapiMessage msg = MapiMessage.FromFile(@"d:\Test.msg");

//Get subject
Console.WriteLine("Subject:" + msg.Subject);

//Get from address
Console.WriteLine("From:" + msg.SenderEmailAddress);

//Get body
Console.WriteLine("Body" + msg.Body);

//Get recipients information
Console.WriteLine("Recipient: " + msg.Recipients);

//Get attachments
foreach (MapiAttachment att in msg.Attachments)
{
    Console.Write("Attachment Name: " + att.FileName);
    Console.Write("Attachment Display Name: " + att.DisplayName);
}


[VB.NET]

'Create an instance of MapiMessage from file
Dim msg As MapiMessage = MapiMessage.FromFile("d:\Test.msg")

'Get subject
Console.WriteLine("Subject:" + msg.Subject)

'Get from address
Console.WriteLine("From:" + msg.SenderEmailAddress)

'Get body
Console.WriteLine("Body" + msg.Body)

'Get recipients information
Console.WriteLine("Recipient: " & msg.Recipients.ToString())

'Get attachments
For Each att As MapiAttachment In msg.Attachments
    Console.Write("Attachment Name: " & att.FileName)
    Console.Write("Attachment Display Name: " & att.DisplayName)
Next att
 
//Loading MSG files from Stream

//[C#]

byte[] bytes = System.IO.File.ReadAllBytes(@"c:\test.msg");

using (MemoryStream stream =
        new System.IO.MemoryStream(bytes))
{
    stream.Seek(0, System.IO.SeekOrigin.Begin);
    //Create an instance of MapiMessage from file
    MapiMessage msg = MapiMessage.FromStream(stream);

    //Get subject
    Console.WriteLine("Subject:" + msg.Subject);

    //Get from address
    Console.WriteLine("From:" + msg.SenderEmailAddress);

    //Get body
    Console.WriteLine("Body" + msg.Body);

}
 
[VB.NET]

Dim bytes() As Byte = System.IO.File.ReadAllBytes("c:\test.msg")

Dim stream As MemoryStream = New System.IO.MemoryStream(bytes)

stream.Seek(0, System.IO.SeekOrigin.Begin)
'Create an instance of MapiMessage from file
Dim msg As MapiMessage = MapiMessage.FromStream(stream)

'Get subject
Console.WriteLine("Subject:" + msg.Subject)

'Get from address
Console.WriteLine("From:" + msg.SenderEmailAddress)

'Get body
Console.WriteLine("Body" + msg.Body)
                
                    <?php
$sql = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
mysql_select_db($a_db, $sql);

$m = new MongoClient($dbName);
$db = $m->$a_mongo_db;
$cols = $db->getCollectionNames();


// WATCH OUT!!!! Drops existing tables from MySql, 
foreach ($cols as $k => $v)
{
$drop = "DROP TABLE IF EXISTS $v";
$reply = mysql_query($drop, $sql);
}


foreach ($cols as $k => $v)
{
$c = $db->$v;
$r = $c->find();
$fields = array();
$fieldTypes = array();
foreach ($r as $rec)
{
foreach ($rec as $fname => $field)
{
$ftype = tgetType($field);
if ($fname == "synonyms")
{

}
else if ($ftype == "array")
{
foreach ($field as $subName => $sub_val)
if ($sub_val != null)
{
if ($subName == 'date')
{
$f2type = 'DATE';
}
else
$f2type = tgetType($sub_val);
if ($f2type == 'DATE')
$fieldTypes[$fname . '_' . $subName] = 'DATETIME';
else if ($f2type == "OBJECT")
$fieldTypes[$fname . '_' . $subName] = 'VARCHAR(255)';
else if ($f2type != "array")
$fieldTypes[$fname . '_' . $subName] = $f2type;
}
}
else if ($ftype == "OBJECT")
{
$fieldTypes[$fname] = 'VARCHAR(255)';
$fields[$fname] = true;
}
else if ($field != null)
{
$fieldTypes[$fname] = $ftype;
$fields[$fname] = true;
}
}
}
// create MySql tables
$createCmd = "create table $v (";
foreach ($fieldTypes as $name => $type)
{
$createCmd.="$name $type";
if ($name == '_id')
$createCmd.=" PRIMARY KEY,";
else
$createCmd.=',';
}
$createCmd = trim($createCmd, ',');
$createCmd.=")";
$out = mysql_query($createCmd);
if ($out == false)
echo $createCmd . "\n";

// inserting data
$r = $c->find();
foreach ($r as $rec)
{
$cmd = "insert into $v set ";
foreach ($rec as $fname => $field)
{
$ftype = tgetType($field);
if ($fname == "synonyms")
{

}
else if ($ftype == "array")
{
foreach ($field as $subName => $sub_val)
if ($sub_val != null)
{
if ($subName == 'date')
$f2type = 'DATE';
else
$f2type = tgetType($sub_val);

if ($f2type == "DATE")
{
$cmd.=$fname . "_$subName='$sub_val' ";
$cmd.=',';
}
else if ($f2type == "OBJECT")
{
$val = $sub_val->__toString();
$cmd.=$fname . "_$subName='$val' ";
$cmd.=',';
}
else if ($f2type != "array")
{
if ($f2type == "BIT" || $f2type == "INT")
$cmd.=$fname . "_$subName=$sub_val ";
else
{
$sub_val = remLetters($sub_val);
$cmd.=$fname . "_$subName='$sub_val' ";
}
$cmd.=',';
}
}
}
else if ($ftype == "OBJECT")
{
$val = $field->__toString();
$cmd.="$fname='$val' ";
$cmd.=',';
}
else if ($field != null)
{
if ($ftype == "BIT" || $ftype == "INT")
$cmd.="$fname=$field ";
else
{
$field = remLetters($field);
$cmd.="$fname='$field' ";
}
$cmd.=',';
}
}
$cmd = trim($cmd, ',');
$out = mysql_query($cmd);
if ($out == false)
echo 'SQL error:' . $cmd . "\n";
}
}


// Mapping from Mongo types to MySql types, feel free to change
function tgetType($field)
{
if (is_string($field))
return "TEXT";
else if (is_object($field))
return "OBJECT";
else if (is_bool($field))
return "BIT";
else if (is_int($field))
return "INT";
else if (is_object($field))
return "VARCHAR(255)";
else if (is_array($field))
{
return "array";
}
}

// this is done to avoid SQL errors, but it changes the strings, removing quotes and double quotes
function remLetters($s)
{
$remove[] = "'";
$remove[] = '"';

$out = str_replace($remove, " ", $s);
return $out;
}