使用function
我可以创建一些参数来显示和隐藏某些内容。
function loggedin(){
return (isset($_SESSION['UserId'])) ? true : false;
}
if (loggedin() === true){
echo"Welcome $name";
} else {
echo"<a href="access.php">Log in</a>";
}
隐藏用户内容的另一种方法如下:
if (loggedin() === false)
如何制作类似的参数来隐藏和显示用户订阅的产品的数据?
也就是说,当您登录已获得或继续订阅产品的用户并且产品的状态为
complete
时,隐藏价格(或隐藏某些内容)而不是price显示产品文件的下载链接。
如何考虑咨询并显示结果?
我有下面的表名为subscribed,这是这样形成的
id_subscribed | id_product | UserId | number_facture | status | date |
1 5 2 4562344 complete null
2 5 9 4562345 incomplete null
产品表
id_product | product | price | archive |
5 null 40.00 data.zip
用户表
UserId | name | last_name | usuername | email | password
2 Juan null juan10 @... q1dxe333f
9 Pedro null pedro1 @... wfrf653gg
答案 0 :(得分:1)
您可以在“订阅”表格中创建检查产品与用户之间关系的函数(或用户或产品类中的方法)。
使用PDO:
/**
* Check if the relationship between product and user
* has status completed.
*
* @param int $id_product
* @param int $UserId
* @return boolean
*/
function checkSubscribeComplete($id_product, $UserId = 1)
{
global $pdo; // using PDO
$res = FALSE; // the default result
/* Just concat the SQL string.
* can do directly in $pdo->prepare
*/
$qString = "SELECT status FROM subscribed";
$qString .= " WHERE ";
$qString .= "id_product = :product";
$qString .= " AND ";
$qString .= "UserId = :user";
$qString .= " AND ";
$qString .= "status = 'complete';";
$query = $pdo->prepare($qString);
/* bindValue from PDO,
* can use directly if injection was not a problem
*/
$query->bindValue(':product', $id_product, PDO::PARAM_INT);
$query->bindValue(':user', $UserId, PDO::PARAM_INT);
// Here exec the query
$query->execute();
// $result is an array of columns
$result = $query->fetch(PDO::FETCH_ASSOC);
// if complete is true we expect to have one column.
if ( count($result) > 0 ) $res = TRUE;
return $res;
}
使用MySQLi:
/*Not sure if it's works*/
/**
* Check if the relationship between product and user
* has status completed.
* Using MySQLi instead of PDO
*
* @param int $id_product
* @param int $UserId
* @return boolean
*/
function checkSubscribeCompleteMI($id_product, $UserId = 1)
{
global $con; // using MySQLI, $con is the connection created before
$res = FALSE; // the default result
/* Just concat the SQL string.
*/
$qString = "SELECT status FROM subscribed";
$qString .= " WHERE ";
$qString .= "id_product = '".$id_product."'";
$qString .= " AND ";
$qString .= "UserId = '".$UserId."'";
$qString .= " AND ";
$qString .= "status = 'complete';";
// exec the query and get the result
$result = mysqli_query($con, $qString);
// count the number of rows in the result
$total = mysqli_num_rows($result); // return int
// if "complete" is true we expect to have one row.
if ( $total > 0 ) $res = TRUE;
return $res;
}
希望这会对你有所帮助。
我不知道您是否已使用PDO,无论如何,如果您想了解更多信息,请查看PHP Database Access: Are You Doing It Correctly?