My values before explode 90PAS010,80PAS010
Then I split them using explode function
$site = $row["site"];
$exploded_site = explode(',', $site);
I want to print a table using fpdf by using every data obtained. This is my codes:
$sql = mysqli_query($connect, "select * from site WHERE siteid='$exploded_site[0]'");
while($row=mysqli_fetch_array($sql)){
//do print table
}
The problem is it only shows 1 data only because $exploded_site[0]. How to change the [0] automatically when array have more than one value??
Thanks before
One of the solutions would be to perform a SQL query which fetches all site ids at once, and one of the many ways to do that would be using the IN
condition.
$site = $row["site"];
/**
* Function sanitizes and quotes each site id and returns a string to be used in the SQL IN operator.
*
*
* @param string $site
* @param mysql string
* @return string
*/
function sanitizeSiteIds($site, mysqli $connect) {
// split each site ids into arrays.
$exploded_site = explode(',', $site);
// escape all the site ids.
$sanitizedSites = array_map(function($site) use($connect) {
return mysqli_real_escape_string($connect, trim($site));
}, $exploded_site);
// join all the site ids to something like:
$sitesInStr = implode(',', $sanitizedSites);
return $sitesInStr;
}
$site = sanitizeSiteIds($site, $connect);
// query will fetch all site Ids
$sql = mysqli_query($connect, "select * from site WHERE siteid IN ($site)");
while($row=mysqli_fetch_array($sql)){
//do print table
}