forked from codepo8/csv-to-webservice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
200 lines (174 loc) · 8.86 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>CSV to web service - creating a searchable web interface automatically from CSV files with YQL</title>
<link rel="stylesheet" href="http://yui.yahooapis.com/2.7.0/build/reset-fonts-grids/reset-fonts-grids.css" type="text/css">
<link rel="stylesheet" href="styles.css" type="text/css">
</head>
<body>
<div id="doc" class="yui-t7">
<div id="hd" role="banner">
<h1>CSV to web service</h1>
<ul><li><a href="index.php">Index</a></li><li><a href="http://github.com/codepo8/csv-to-webservice">Download on GitHub</a></li></ul>
</div>
<div id="bd" role="main">
<p>This is a demo page for the <code>csvtoservice.php</code> script that uses <a href="http://developer.yahoo.com/yql">YQL</a> to convert CSV resources to web interfaces.</p>
<h2>What you can do with this</h2>
<p>This script allows you to get any CSV file from the internet and turn it into a web interface that empowers users to search and filter the data.</p>
<p>The following is an example, try it out. Simply hit the search button and see the results.</p>
<div id="example">
<h3>Olympic Winter Medals Albertville 1992</h3>
<p>Simply enter your search criteria in the appropriate field and submit the form.</p>
<?php
include('csvtoservice.php');
$content = csvtoservice(
'http://winterolympicsmedals.com/medals.csv',
array(
'filter'=> array('city'),
'rename'=> array(
'noc'=>'country'
),
'preset'=> array(
'year'=> '1992'
),
'prefill'=> array(
'discipline'=> 'Alpine Skiing',
),
'uppercase'=>true,
'choices'=> array(
'medal'=> array(
"Gold",
"Silver",
"Bronze",
),
),
'notinform'=> array(
'eventgender',
),
'grep_specific'=> array(
'sport',
),
)
);
// if it could be loaded and parsed...
if($content){
// show the form
echo '<h4>Filters</h4>';
echo $content['form'];
// show the table
if($content['table']){
echo '<h4>Results</h4>';
echo $content['table'];
}
}
?>
</div>
<h2>The code</h2>
<p>The above search form and table result is powered by a simple dataset: <a href="http://winterolympicsmedals.com/medals.csv">http://winterolympicsmedals.com/medals.csv</a>. All you need to convert it to what you see above is the following code in your PHP documents:</p>
<pre><code><?php
include('csvtoservice.php');
$content = csvtoservice(
'<a href="http://winterolympicsmedals.com/medals.csv">http://winterolympicsmedals.com/medals.csv</a>',
array(
'filter'=> array('city'),
'rename'=> array(
'noc'=>'country'
),
'preset'=> array(
'year'=> '1992'
),
'prefill'=> array(
'discipline'=> 'Alpine Skiing',
),
'uppercase'=>true,
'choices'=> array(
'medal'=> array(
"Gold",
"Silver",
"Bronze",
),
),
'notinform'=> array(
'eventgender',
),
'grep_specific'=> array(
'sport',
),
)
);
if($content){
if($content['form']){
echo '<h4>Filters</h4>';
echo $content['form'];
}
if($content['table']){
echo '<h4>Results</h4>';
echo $content['table'];
}
}
?></code></pre>
<p>This is already a complex example, the simplest way to show a form and a result table for any CSV file is the following:</p>
<pre><code><?php
include('csvtoservice.php');
$content = csvtoservice('http://winterolympicsmedals.com/medals.csv');
if($content){
if($content['form']){
echo '<h4>Filters</h4>';
echo $content['form'];
}
if($content['table']){
echo '<h4>Results</h4>';
echo $content['table'];
}
}
?></code></pre>
<p>You can see this <a href="simplest-example.php">in action here</a> and it is part of the source code on GitHub.</p>
<h2>The parameters and options</h2>
<p>In essence, all you need to provide is a URL that points to a CSV file and the script does the rest. You assign a variable to the main function that will get the HTML as properties. For example:</p>
<pre><code>$myservice = csvtoservice('http://winterolympicsmedals.com/medals.csv');</code></pre>
<p>The returned properties will be:</p>
<ul>
<li><code>$myservice['form']</code> - the HTML form with all possible fields contained in the dataset.</li>
<li><code>$myservice['table']</code> - the data table of the information returned by the form submission - this will only show up once the form is submitted.</li>
<li><code>$myservice['json']</code> - the data in raw JSON format (for debugging).</li>
<li><code>$myservice['yql']</code> - the YQL statement (for debugging).</li>
<li><code>$myservice['num_matches']</code> - the number of matches found.</li>
</ul>
<p>You can see all of the information in the <a href="example-with-logging.php">example with logging</a>.</p>
<p>If you want to tweak the outcome of the form and the table and you want to change the data names or remove parts of it you can set an options array:</p>
<pre><code>$myservice = csvtoservice(
'http://winterolympicsmedals.com/medals.csv',
array(
'filter' => array(<em>fieldnames</em>),
'rename'=> array(
'<em>field</em>'=>'<em>new name</em>',
'<em>field2</em>'=>'<em>new name 2</em>'
),
'preset'=> array(
'<em>field</em>'=>'<em>preset value</em>',
'<em>field2</em>'=>'<em>preset value 2</em>'
),
'prefill'=> array(
'<em>field</em>'=> '<em>value</em>',
'<em>field2</em>'=> '<em>value 2</em>',
),
'uppercase'=><em>Boolean</em>
)
);</code></pre>
<ul>
<li><code>rename</code> allows you to rename fields. In the above example the country who won the medals was defined as <em>NOC</em>, which makes sense, but reads much better as <em>country</em>.</li>
<li><code>filter</code> contains an array of fields to not show in the form or the table. This allows you to get rid of some parts of the data.</li>
<li><code>preset</code> is an array of fields to preset with a hard value. These fields will be part of the query of the data but will not be added to the form or displayed. This allows you to pre-filter the data. In the above example this was the year of the games.</li>
<li><code>prefill</code> is an array of fields to pre-fill the form with in case you want to give the end user a hint what they can search for.</li>
<li><code>uppercase</code> is an boolean value if the script should uppercase the first letter of the field name or not ("City" instead of "city").</li>
<li><code>choices</code> is an array of options of which any one can be chosen. In the above example, medals can only be Gold, Silver or Bronze.</li>
<li><code>notinform</code> is an array of fields that do not show in form but are shown in the results.</li>
<li><code>grep_specific</code> is an array of fields for which the sql query performs an exact match instead of a LIKE.</li>
</ul>
<p>That's it, really... Have fun!</p>
</div>
<div id="ft" role="contentinfo"><p>Written by <a href="http://wait-till-i.com">Chris Heilmann</a>, powered by <a href="http://developer.yahoo.com/yql/">YQL</a> - demo csv by the <a href="http://www.guardian.co.uk/news/datablog">Guardian Data Blog</a>.</p></div>
</div>
</body>
</html>