I have a function that can retrieve the category_id
of the current location. I want to have the category url taken as well as the id of the category of the subject I have taken. How can I do it?
posts table for keep my post: https://prnt.sc/PEqSq2mM5NKM categories table for keep my categories: https://prnt.sc/WK7n8kAGoUWD
My code:
function Get_url($url){
$veri=[];
$datas = $db->prepare('SELECT * FROM posts WHERE url = ?');
$datas->execute([$url]);
$data = $datas->fetch(PDO::FETCH_ASSOC);
foreach($data as $key=>$value){
$datas = $db->prepare('SELECT * FROM categories WHERE id = ?, category_url = ?');
$datas->execute([$value['id']], [$value['category_url']]);
$veri[] = $datas->fetch(PDO::FETCH_ASSOC);
}
}
Try this:
function get_url($url){
global $db;
$veri = array('status' => 'success');
$sql = "SELECT
t2.category_id,
t2.category_url
FROM posts t1
INNER JOIN categories t2 ON t1.category_id = t2.id
WHERE t1.url = ?
LIMIT 1";
$query = $db->prepare($sql);
if( $query->rowCount == 0 ):
$veri = array('status' => 'error');
else:
$row = $query->execute([$url]);
$veri = array('category_id', $row['category_id'], 'category_url', $row['category_url']);
endif;
return $veri;
}
to be able to use the function:
$get_cat = get_url($url);
if( $get_cat['status'] == 'success' ):
$category_id = $get_cat['category_id'];
$category_url = $get_cat['category_url'];
else:
echo 'could not find category';
endif;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.