/
namewise.php
181 lines (137 loc) · 3.99 KB
/
namewise.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
<?php
$error=null;
if( empty($_POST['name']) )
{
$error="Please enter a Name";
header("location: index.php?errorType=2&error=".$error);
die();
}
//Input validation
if(empty($_POST['year']) || ((int)$_POST['year'] < 1944 ) || ((int)$_POST['year'] > 2013 ) )
{
$error="Please enter a valid Year between 1944 to 2013";
header("location: index.php?errorType=2&error=".$error);
die();
}
//Set the year
$year=(int)$_POST['year'];
$name=$_POST['name'];
$name_upper=strtoupper($_POST['name']);
$gender=$_POST['gender'];
$nameList=array(); //Holds the matched names incase its a partial search
for($i=$year;$i<2014;$i++)
{
$fh=fopen("names/".$gender."_cy".$i."_top.csv","r");
while(!feof($fh))
{
$record=fgetcsv($fh);
if(strpos($record[0],$name_upper) === 0 )
{
if(empty($nameList[$record[0]]))
{
$nameList[$record[0]]=array();
}
$record[2]=(int)preg_replace("/[^0-9]/", "", $record[2]);
$nameList[$record[0]][$i]=array($record[1],$record[2]);
}
}
fclose($fh);
}
//Make bar graphs for the names
foreach($nameList as $_name=>$data)
{
$amounts=array();
$labels=array();
foreach($data as $year=>$val)
{
$amounts[]=$val[0];
$labels[]=$year;
}
$nameList[$_name]['graph']=makeGraph($amounts,$labels);
}
?>
<html>
<head>
<title>::Baby Names::</title>
<link rel='stylesheet' href='style.css' />
</head>
<body>
<h1 align="left" style="margin:10px;color:#fff">
::Baby Names::
</h1>
<table width='99%' style="margin:auto;padding:20px;box-sizing:border-box" border=0 >
<tr>
<td align="center" style="color:#fff;font-size:20px;padding:10px">
<div style='float:left'>
<a href='index.php' style="font-size:16px;color:#aaf" >Go To Home </a>
</div>
</td>
</tr>
<tr>
<td width="50%" align="center" style="padding-right:20px;vertical-align:top">
<?php
foreach($nameList as $name=>$record )
{
$img=$record['graph'];
unset($record['graph']);
?>
<div style="background-color:#fff;padding:20px 10px;font-size:16px;font-weight:bold;border:solid #222 1px">
<div style="margin:10px;background-color:#eee;border-radius:10px;padding:10px;border:solid #222 1px;text-align:left;color:#233">
<center><?php echo $name; ?></center>
<table width="100%">
<tr>
<td align="center" >
<?php echo "<img src='data:image/jpeg;base64,".$img."' />"; ?>
</td>
</tr>
</table>
</div>
</div>
<?php
}
?>
</td>
</tr>
</table>
</body>
</html>
<?php
function makeGraph($values,$labels){
$values[]='0';
$labels[]='';
// Standard inclusions
include_once("charts/pChart.class");
include_once("charts/pData.class");
// Dataset definition
$DataSet = new pData;
$DataSet->AddPoint($values,"Serie2");
$DataSet->AddPoint($labels,"Xlabel");
$DataSet->AddSerie("Serie2");
$DataSet->SetAbsciseLabelSerie('Xlabel');
$DataSet->SetSerieName("No Of Births","Serie2");
// Initialise the graph
$Test = new pChart(700,230);
$Test->setFontProperties("Fonts/tahoma.ttf",8);
$Test->setGraphArea(50,30,585,200);
$Test->drawFilledRoundedRectangle(7,7,693,223,5,240,240,240);
$Test->drawRoundedRectangle(5,5,695,225,5,230,230,230);
$Test->drawGraphArea(255,255,255,TRUE);
$Test->drawScale($DataSet->GetData(),$DataSet->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2,TRUE);
$Test->drawGrid(4,TRUE,230,230,230,50);
// Draw the 0 line
$Test->setFontProperties("Fonts/tahoma.ttf",6);
$Test->drawTreshold(0,143,55,72,TRUE,TRUE);
// Draw the bar graph
$Test->drawOverlayBarGraph($DataSet->GetData(),$DataSet->GetDataDescription());
// Finish the graph
$Test->setFontProperties("Fonts/tahoma.ttf",8);
$Test->drawLegend(600,30,$DataSet->GetDataDescription(),255,255,255);
$Test->setFontProperties("Fonts/tahoma.ttf",10);
$Test->drawTitle(50,22,"Change in name popularity",50,50,50,585);
ob_start();
$Test->Stroke();
$img=ob_get_clean();
$img=base64_encode($img);
return $img;
}
?>