I have this class
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'];
}
}
}
assume that the object "db" has been declared. and my way of getting into the getmsg function is
$user = new user();
$user->getmsg();
now I want to get the content of $dateposted, how can i do that?
any help would be greatly appreciated. thank you.
try this procedure
<?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');
Your issue is, that you have to return the complete message array to access the wanted data like this (your class with some improvements):
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'];
or you can use a message object. But the solution is, to save the data somewhere and/or return the full result.
You really need to read about variable scope, in particular:
Any variable used inside a function is by default limited to the local function scope.
In other words, your variables $whoposted
etc are destroyed when the getmsg()
function exits.
Not to mention the fact that, within the function, their values are overwritten on each iteration of the loop.
You probably want to define properties within your objects within which you store data. However, it'd be a strange thing indeed for a user
object to retrieve news articles associated with multiple users. Perhaps a more sensible design would be to have a usertype
object representing admin users; and from there instantiate (from the results of the database query) separate news
objects to represent each article?
Define a class to represent your news articles:
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;}
}
Define a class to represent your "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;
}
}
Then you can do something like:
$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();
}
All of that said, most of this is just reinventing the wheel to build a (very simple) custom ORM. There are plenty of PHP ORM solutions already out there, many of which are free and open-source.