我有一个从mysql中的数据生成的目录。我希望在某些类别之后加入广告。我是否只需在ifach类别中添加if语句,以查看某个类别是否与我想要广告的类别相匹配?
try
{
$dbh = new PDO('mysql:host=localhost;dbname=YOURDB', 'USERNAME', 'PASSWORD');
}
catch(PDOException $e)
{
echo $e->getMessage();
die;
}
$stmt = $dbh->query('SELECT category FROM businesses GROUP BY category');
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$categories = $stmt->fetchAll();
foreach ($categories as $category)
{
echo '<h3>'.$category['category'].'</h3>';
$stmt = $dbh->prepare('SELECT * FROM businesses WHERE category = :categoryName');
$stmt->bindParam(':categoryName', $category['category'], PDO::PARAM_STR);
$stmt->execute();
$businesses = $stmt->fetchAll();
foreach ($businesses as $business)
{
echo $business['name'];
echo '<br />';
}
}