I have a table with column nearbyAttractions which contains values seperated by a comma. For example A001,A002
I am querying that column then converting the contents to an array called $NearbyAttractionsArray.
print_r($NearbyAttractionsArray);
Array ( [0] => A001 [1] => A003 )
I want to run a PDO SQL query to find records that match for each value of the array then output them in a loop.
$arrayQuery = $this->con->prepare("SELECT attid, attTitle, attThumbnail, AttTagline FROM attractions WHERE attid=$NearbyAttractionsArray AND attStatus=1");
$arrayQuery->execute();
when I try this it converts $NearbyAttractionsArray to a string called array...
What would be the correct syntax to output values attid, attTitle, attThumbnail, AttTagline for each row where attId matches one of the values in the array?
I suspect some kind of foreach loop would be needed but cannot find the correct way to write this as a PDO statement.
Many thanks
For perspective my complete class code is:
<?php
class NearbyAttractionsProvider {
private $con, $attId;
public function __construct($con, $attId) {
$this->con = $con;
$this->attId = $attId;
} // end of constructor
public function createNearbyAttractionsList($con, $attId) {
$query = $this->con->prepare("SELECT nearbyAttractions FROM attractions WHERE attId=:attId");
$query->bindParam(":attId", $attId);
$query->execute();
$this->sqlData = $query->fetch(PDO::FETCH_ASSOC);
$NearbyAttractions = $this->sqlData["nearbyAttractions"];
$NearbyAttractionsRemoveCommas = str_replace(',', '',$NearbyAttractions);
$NearbyAttractionsArray = str_split($NearbyAttractionsRemoveCommas, 4);
print_r($NearbyAttractionsArray);
$CountNearbyAttractions = sizeof($NearbyAttractionsArray);
if($NearbyAttractionsArray[0] != "") {
$results = "<h3>There are $CountNearbyAttractions Nearby Attractions</h3>";
$arrayQuery = $this->con->prepare("SELECT attid, attTitle, attThumbnail, AttTagline FROM attractions WHERE
attid=$NearbyAttractionsArray AND attStatus=1");
$arrayQuery->execute();
$results .= "<div class='nearbyAttractionsItemsContainer'>";
while($row = $arrayQuery->fetchAll(PDO::FETCH_ASSOC)) {
$id = $row["attid"];
$img = $row["attThumbnail"];
$title = $row["attTitle"];
$tagline = $row["AttTagline"];
$results .= "<div class='nearbyAttractionListItem'>
<a href='Attraction.php?attId=$id&title=$title' data-AttractionId='$id'
class'NearbyAttractionItem-url'>
<img class='NearbyAttractionItem-thumbnail' src='$img' height='220' width=300'>
<h3 class='NearbyAttractionItem-title'>$title</h3>
<p class='NearbyAttractionItem-tagline'>$tagline</p>
</a>
</div>";
} // End of while loop
$results .= "</div>"; // End of nearbyAttractionsItemsContainer
} // End of IF statement
else {
$results = "<h3>There are No Nearby Attractions</h3>";
} // End of Else statement
return $results;
} // End of public function createNearbyAttractionsList
} // End pf class NearbyAttractionsProvider