我有这个班级
class user{
public function getmsg(){
$sql3 = "SELECT * FROM news WHERE usertype = 'admin'";
$result = mysqli_query($this->db,$sql3);
while($user_data = mysqli_fetch_array($result)){
$whoposted = $user_data['whoposted'];
$dateposted = $user_data['dateposted'];
$title = $user_data['title'];
$content = $user_data['content'];
$whoposted = $user_data['usertype'];
}
}
}
假设已声明对象“db”。我进入getmsg函数的方式是
$user = new user();
$user->getmsg();
现在我想获取$ dateposted的内容,我该怎么做?
任何帮助将不胜感激。谢谢。
答案 0 :(得分:1)
你真的需要阅读variable scope,特别是:
函数内使用的任何变量默认都限制在本地函数范围内。
换句话说,当$whoposted
函数退出时,您的变量getmsg()
等会被销毁。
更不用说在函数中,它们的值在循环的每次迭代中被覆盖。
您可能想要在您存储数据的对象中定义properties。但是,对于user
对象来说,检索与多个用户关联的新闻文章确实是一件奇怪的事情。也许更明智的设计是拥有代表管理员用户的usertype
对象;从那里实例化(从数据库查询的结果)单独的news
个对象来代表每篇文章?
定义一个代表您的新闻文章的类:
class NewsArticle {
const FORMAT_DATETIME_MYSQL = 'Y-m-d H:i:s';
protected $author;
protected $date;
protected $title;
protected $content;
function __construct($arr) {
$this->author = $arr['whoposted']; // would probably actually pass this
// to a getUser() function that returns
// a User object
$this->date = DateTime::createFromFormat(
self::FORMAT_DATETIME_MYSQL,
$arr['dateposted']
);
$this->title = $arr['title'];
$this->content = $arr['content'];
}
function getAuthor() {return $this->author;}
function getDate() {return strftime('%c', $this->date->getTimestamp());}
function getTitle() {return $this->title;}
function getContent() {return $this->content;}
}
定义一个类来表示你的" usertypes":
class UserType {
const SQL_GET_NEWS = 'SELECT * FROM news WHERE usertype = ?';
protected $type;
function __construct($type) {
$this->type = $type;
}
function getNewsArticles() {
global $mysqli;
if ($stmt = $mysqli->prepare($db, self::SQL_GET_NEWS)) {
$stmt->bind_param('s', $this->type);
$stmt->execute();
$result = $stmt->get_result();
$articles = array();
while ($row = $result->fetch_assoc())
$articles[] = new NewsArticle($row);
$stmt->close();
return $articles;
}
return null;
}
}
然后你可以这样做:
$adminType = new UserType('admin');
foreach ($adminType->getNewsArticles() as $article) {
echo '<h2>', $article->getTitle(), '</h2>';
echo '<p>By ', $article->getAuthor(), ' at ', $article->getDate(), '</p>';
echo $article->getContent();
}
所有这些都说,大多数情况只是重新发明轮子来构建一个(非常简单的)自定义ORM。已经有很多PHP ORM解决方案,其中许多是免费和开源的。
答案 1 :(得分:0)
您的问题是,您必须返回完整的消息数组才能访问所需的数据(您的类有一些改进):
class user
{
private $msg = null;
private function loadData(){
$sql3 = "SELECT * FROM news WHERE usertype = 'admin'";
$result = mysqli_query($this->db, $sql3);
$this->msg = mysqli_fetch_assoc($result);
}
public function getmsg()
{
if(null === $this->msg){
$this->loadData();
}
return $this->msg;
}
}
$user = new user();
// since PHP 5.4
echo $user->getmsg()['dateposted'];
// for older PHP versions you can use this
$msg = $user->getmsg();
echo $msg['dateposted'];
或者您可以使用消息对象。但解决方案是,将数据保存在某处和/或返回完整的结果。
答案 2 :(得分:-1)
尝试此程序
<?php
class user{
public function getmsg($key){
$result=array();
$sql3 = "SELECT * FROM news WHERE usertype = 'admin'";
$result = mysqli_query($this->db,$sql3);
$user_data = mysqli_fetch_array($result);
return $user_data[$key];
}
}
$user=new user();
echo $user->getmsg('dateposted');