Permalink
Browse files

added XML code type

  • Loading branch information...
1 parent cf65e52 commit af40fada14fc262a46fcd8534f0623283e146e0d @peric committed Sep 24, 2012
Showing with 95 additions and 45 deletions.
  1. +1 −1 css/style.css
  2. +17 −6 index.html
  3. +77 −38 js/functions.js
View
@@ -8,7 +8,7 @@
#examplecode {
display: none;
}
-#mysqlcode {
+#generatedcode {
border:1px solid #999999;
width:100%;
margin:5px 0;
View
@@ -6,24 +6,25 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Get countries list from geonames">
<meta name="author" content="Drazen Peric">
-<meta name="keywords" content="countries, list, mysql, geonames">
-<title>Get countries in MySQL format</title>
+<meta name="keywords" content="countries, list, mysql, geonames, xml">
+<title>Get countries in MySQL/XML format</title>
<link rel="stylesheet" href="css/bootstrap.min.css" />
<link rel="stylesheet" href="css/bootstrap-responsive.min.css" />
<link rel="stylesheet" type="text/css" href="css/style.css" />
-<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
+<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script type="text/javascript" src="js/functions.js"></script>
</head>
<body>
<div id="container" class="container-fluid">
<div class="navbar">
<div class="navbar-inner">
<h2>Get countries</h2>
+ <p>Get world countries in MySQL/XML format</p>
</div>
</div>
<div class="row-fluid">
<div class="span12" id="inner-container">
- <p class="lead">This script will generate MySQL "Countries" table depending on what fields you select. All data is fetched from geonames.org</p>
+ <p class="lead">This script will generate <strong>MySQL</strong> or <strong>XML</strong> "Countries" table structure depending on what fields you select. All data is fetched from geonames.org</p>
<p>
<a id="showexamplecode" href="#">MySQL Example code</a>
</p>
@@ -45,7 +46,8 @@ <h2>Get countries</h2>
.
</code>
</p>
- <br/>
+ <hr/>
+ <h5>Select columns:</h5>
<label class="checkbox">
<input type="checkbox" class="options" name="options" value="countryCode" checked="checked">countryCode
</label>
@@ -97,11 +99,20 @@ <h2>Get countries</h2>
<label class="checkbox">
<input type="checkbox" class="options" name="options" value="geonameId">geonameId
</label>
+ <h5>Select type:</h5>
+ <label class="radio">
+ <input type="radio" class="codetype" name="codetype" id="sqltype" value="sqltype" checked>
+ SQL
+ </label>
+ <label class="radio">
+ <input type="radio" class="codetype" name="codetype" id="xmltype" value="xmltype">
+ XML
+ </label>
<p>
<button id="getcode" class="btn btn-primary" type="button">Get it!</button>
</p>
<hr />
- <textarea id="mysqlcode" rows="20"></textarea>
+ <textarea id="generatedcode" rows="20"></textarea>
<hr />
<div id="signature">
Web by <a href="http://www.echobehind.com" target="_blank"><span class="dp">DP</span></a>
View
@@ -1,7 +1,8 @@
$(document).ready(function () {
var allValues = [];
var options = [];
- var columns = {
+ var settings = {type: "sqltype"};
+ var columnsAttr = {
'countryCode': "char(2) NOT NULL DEFAULT ''",
'countryName': "varchar(80) NOT NULL DEFAULT ''",
'currencyCode': "char(3) DEFAULT NULL",
@@ -23,7 +24,6 @@ $(document).ready(function () {
$('#showexamplecode').click(function (e) {
e.preventDefault();
-
$('#examplecode').toggle();
})
@@ -41,11 +41,15 @@ $(document).ready(function () {
if ($this.is(':checked'))
options.push($(this).val());
});
-
+ $('.codetype').each(function () {
+ var $this = $(this);
+ if ($this.is(':checked'))
+ settings.type = $this.val();
+ });
fetchCountries();
});
- $("#mysqlcode").focus(function() {
+ $("#generatedcode").focus(function() {
var $this = $(this);
$this.select();
@@ -65,52 +69,87 @@ $(document).ready(function () {
var currAttr = options[j];
value[currAttr] = data.geonames[i][currAttr];
}
-
allValues.push(value);
}
- console.log(allValues);
- console.log(allValues.length);
- console.log(allValues[0].countryName)
generateScript();
});
}
var generateScript = function () {
var oLength = options.length;
var valuesLength = allValues.length;
+ var sql = "";
+ var xml = "";
- // create table
- var sql =
- "CREATE TABLE IF NOT EXISTS `countries` (" +
- "\n `idCountry` int(5) NOT NULL AUTO_INCREMENT,";
- for (var i = 0; i < oLength; i++) {
- var currAttr = options[i];
- sql += "\n `" + options[i] + "` " + columns[options[i]] + ",";
- }
- sql += "\n PRIMARY KEY (`idCountry`)";
- sql += "\n) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=0;\n\n";
-
- // insert into
- sql += "INSERT INTO `countries` (";
- for (var i = 0; i < oLength; i++) {
- sql += "`" + options[i] + "`, ";
- }
- sql = sql.substring(0, sql.length - 2);
- sql += ") VALUES";
- for (var i = 0; i < valuesLength; i++) {
- sql += "\n("
- for (var j = 0; j < oLength; j++) {
- var currValue = allValues[i][options[j]];
- if (typeof currValue === "string")
- sql += "'" + currValue.replace(/\x27/g, '\\\x27') + "', ";
- else if (typeof currValue === "number")
- sql += "" + currValue + ", ";
+ if (settings.type === "sqltype") {
+ // create table
+ sql += "CREATE TABLE IF NOT EXISTS `countries` (" +
+ "\n `idCountry` int(5) NOT NULL AUTO_INCREMENT,";
+ for (var i = 0; i < oLength; i++) {
+ var currAttr = options[i];
+ sql += "\n `" + options[i] + "` " + columnsAttr[options[i]] + ",";
+ }
+ sql += "\n PRIMARY KEY (`idCountry`)";
+ sql += "\n) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=0;\n\n";
+
+ // insert into
+ sql += "INSERT INTO `countries` (";
+ for (var i = 0; i < oLength; i++) {
+ sql += "`" + options[i] + "`, ";
}
sql = sql.substring(0, sql.length - 2);
- sql += "),"
+ sql += ") VALUES";
+ for (var i = 0; i < valuesLength; i++) {
+ sql += "\n("
+ for (var j = 0; j < oLength; j++) {
+ var currValue = allValues[i][options[j]];
+ if (typeof currValue === "string")
+ sql += "'" + currValue.replace(/\x27/g, '\\\x27') + "', ";
+ else if (typeof currValue === "number")
+ sql += "" + currValue + ", ";
+ }
+ sql = sql.substring(0, sql.length - 2);
+ sql += "),"
+ }
+ sql = sql.substring(0, sql.length - 1);
+
+ // set sql code
+ $('#generatedcode').text(sql);
+ } else if (settings.type === "xmltype") {
+ xml += "<countries>";
+ for (var i = 0; i < valuesLength; i++) {
+ xml += "\n <country";
+ for (var j = 0; j < oLength; j++) {
+ var currOption = options[j];
+ var currValue = allValues[i][options[j]];
+ xml += " " + currOption + "=\"" + currValue + "\"";
+ }
+ xml += " />";
+ }
+ xml += "\n</countries>";
+
+ /*for (var i = 0; i < valuesLength; i++) {
+ sql += "\n("
+ for (var j = 0; j < oLength; j++) {
+ var currValue = allValues[i][options[j]];
+ if (typeof currValue === "string")
+ sql += "'" + currValue.replace(/\x27/g, '\\\x27') + "', ";
+ else if (typeof currValue === "number")
+ sql += "" + currValue + ", ";
+ }
+ //sql = sql.substring(0, sql.length - 2);
+
+ //sql += "),"
+ }*/
+
+ /*
+ <states>
+ <state name="ALABAMA" abbreviation="AL" />
+ <state name="ALASKA" abbreviation="AK" />
+ ...
+</states>*/
+ // set xml code
+ $('#generatedcode').text(xml);
}
- sql = sql.substring(0, sql.length - 1);
-
- $('#mysqlcode').text(sql);
}
});

0 comments on commit af40fad

Please sign in to comment.