I have a small function which reads a CSV file and outputs all rows after the header.
Now I want to extend this function to accept an location
so only rows containing this location
are outputted. I'm not exactly sure how I would go about doing this.
Here is the CSV file:
ID,Location,output,wdatatyp,wdb,wbyte,wbit,bitval,rdatatyp,rdb,rbyte,rbit
Lampe1Taster,Wohnzimmer,#output,DB,1,0,0,1,A,0,0,7
Lampe2Ein,Wohnzimmer,#output2,DB,1,0,1,1,A,0,0,6
Lampe2Aus,Küche,#output2,DB,1,0,1,0,A,0,0,6
The location
will be the second coloumn. In this case Wohnzimmer
and Küche
.
Here is the function:
$id = $_GET['location'];
$rows = file('data.csv');
foreach ($rows as $row){
echo $row;
list ($id_num) = explode(",", $row);
if ($id_num == $id){
echo $row;
break;
}
}
This will fetch the row where the FIRST coloumn matches. But I want it to fetch the row where the SECOND coloumn matches.
I will supply the location
like so:
plc.php?function=LoadRoom&location=Wohnzimmer
So if the "location" is Wohnzimmer, I need to output to look exactly like so.
Lampe1Taster,Wohnzimmer,#output,DB,1,0,0,1,A,0,0,7
Lampe2Ein,Wohnzimmer,#output2,DB,1,0,1,1,A,0,0,6
Any help at all is appreciated!