In PHP, I'm getting multiple series and dumping various bits of information into an array. I will be left with 180 items (if I put in 30 series details that have 6 events per series)
$eventsArray = explode(',', $events); // This gets passed in via a param
\load_curl();
$array = Array();
$curl_array = array();
$ch = curl_multi_init();
foreach($eventsArray as $count => $id) {
$curl_array[$count] = curl_init("https://www.eventbriteapi.com/v3/series/".$id."/events/?expand=ticket_classes&token=XXX");
curl_setopt($curl_array[$count], CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_array[$count], CURLOPT_VERBOSE, true);
curl_setopt($curl_array[$count], CURLOPT_SSL_VERIFYPEER, false);
curl_multi_add_handle($ch, $curl_array[$count]);
}
do {
curl_multi_exec($ch, $exec);
} while($exec > 0);
foreach($eventsArray as $count => $id) {
$returned = curl_multi_getcontent($curl_array[$count]);
$json = \json_decode($returned,true);
foreach ( $json["events"] as $event) {
$remaining = $event["ticket_classes"][0]["quantity_total"] - $event["ticket_classes"][0]["quantity_sold"];
$array[] = Array('id'=>$event["id"],
'url'=>$event["url"],
'status'=>$event["status"],
'remaining'=>$remaining,
'start'=>date('l, jS F Y', strtotime($event["start"]["local"]))
);
}
}
foreach($eventsArray as $count => $id) {
curl_multi_remove_handle($ch, $curl_array[$count]);
}
curl_multi_close($ch);
foreach($eventsArray as $count => $id) {
curl_close($curl_array[$count]);
}
$array = array_chunk($array, 30);
$array = json_encode($array);
return $array;
I then use EJS to display the information like below. The first item in each chunk is displayed in a row, then second item of each chunk in the next row etc.
<% for(var i=0; i<30; i++) { %>
<tr>
<th><%= this[0][i]['start'] %></th>
<% for (var x=0; x<6; x++) {
if (this[x][i]['status'] == 'live') {
if (this[x][i]['remaining']<=0) {
status='<span class="badge">FULL</span>';
style = 'class="danger"';
}
else {
status ='<a href="'+ this[x][i]['url'] +'">Book now</a><br><span class="badge badge-success">'+this[x][i]['remaining']+' AVAILABLE</span>';
style = 'class="success"';
}
}
else {
status='<span class="badge">COMPLETE</span>';
style = 'class="warning"';
}
%>
<td <%=style%>> <%=status%></td>
<% } %>
</tr>
<% } %>
It works but takes about 5 seconds to run. What could I do to make it faster? Is there much improvement that could be made?