I'm trying to export existing data from a MySQL database, into ruby commands I can run with rake db:seed
.
Here's my code.
# Generate db:seed data for proveedores.
$proveedores = R::findAll('tbproveedores');
$proveedoresE = R::exportAll($proveedores);
foreach ($proveedoresE as &$p) {
$line = 'BookSupplier.create(company: "%s", city: "%s", country: "%s", address: "%s", telephone: "%s", contact: "%s", email: "%s", website: "%s"\n';
$exportedLine = sprintf($line, $p['empresa'], $p['ciudad'], $p['pais'], $p['direccion'], $p['telefono'], $p['personacontacto'], $p['email'], $p['website']);
var_dump($exportedLine);
fwrite($seeds, $exportedLine);
echo "<br />";
}
Notice the \n
newline symbol at the end of the $line variable. I read online that that was all that is needed to use a newline.
The output of my code above is (vertabim, a long line):
BookSupplier.create(company: "Pearson", city: "Lima", country: "Peru", address: "Av. Limon", telephone: "4673535421", contact: "Javier", email: "", website: ""\nBookSupplier.create(company: "Project Management Institute - PMI", city: "Pennsylvania", country: "Estados Unidos", address: "Newtown Square, Pennsylvania", telephone: "1", contact: "Limberg Morales", email: "", website: "http://www.pmi.org/"\nBookSupplier.create(company: "UVirtual - Centro de Excelencia", city: "Santa Cruz", country: "Bolivia", address: "Av. Irala 585", telephone: "1", contact: "Limberg Morales", email: "", website: ""\nBookSupplier.create(company: "Ábaco de Rodolfo Depalma", city: "Buenos Aires", country: "Argentina", address: "Viamonte 1336, 4° (C1053 ACB) Buenos Aires", telephone: "5411-43711675", contact: "Limberg Morales", email: "", website: "http://www.abacoeditorial.com.ar/"\nBookSupplier.create(company: "Pablo Lledó - ProjectManagement", city: "Canadá", country: "Estados Unidos", address: "Victoria, BC, Canadá", telephone: "1", contact: "Limberg Morales", email: "", website: ""\n
I'd like each call to be in it's own line, following my newline insertion. Any suggestions on what I'm doing wrong?
Using new line tags: Newline characters \n or \r\n can be used to create a new line inside the source code.
\n is the newline or linefeed, other side \r is the carriage return. They differ in what uses them. Windows uses \r\n to signify the enter key was pressed, while Linux and Unix use \n to signify that the enter key was pressed.
The sprintf() is an in-built function of PHP which writes a formatted string to a variable. It returns a formatted string.
If you can't (or don't want to) switch to double quotes, you can insert a newline using the ASCII code like so:
> php
<?php printf('line one%cline two', 10);^D
line one
line two
>
The %c
lets you insert any ASCII code and 10 is the code for a newline.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With