Working in Joomla, I have my model and view set up, but when the page is loaded, no data appears.
Model:
class mlsModelmls extends JModel
{
/**
* Gets the info
*/
function mlsdata($column)
{
$db =& JFactory::getDBO();
$query = "
SELECT *
FROM ".$db->nameQuote('#__mls')."
WHERE ".$db->nameQuote('MSTMLSNO')." = ".$db->quote('4112065').";
";
$db->setQuery($query);
$row = $db->loadRow();
print_r($row[$column]);
}
}
View:
class mlsViewmls extends JView
{
function mlsnum($tpl = null)
{
$model = &$this->getModel();
$mlsnum = $model->mlsdata(MSTMLSNO);
$this->assignRef( 'mlsnum', $mlsnum );
$agentuid = $model->mlsdata(MSTLISTBRD);
$this->assignRef( 'agentuid', $agentuid );
$listdt = $model->mlsdata(MSTLISTDT);
$this->assignRef( 'listdt', $listdt );
/** Some more assignRef() */
parent::display($tpl);
}
}
TMPL:
<h2 class="price">
<?php echo $this->mlsnum; ?>
</h2>
When the page is loaded, the TMPL looks fine, but no data appears for the <?php echo $this->mlsnum; ?> reference call.
Does each assignRef() need it's own function?
Try to change
print_r($row[$column]);
to this:
return $row[$column];
And this one
parent::display($tpl);
to
return parent::display($tpl);
Otherwise it's just no-result.
Your mlsdata() method doesn't returning anything, therefore you are assigning nothing to the template variable.
Add return $row and remove the print_r.
Try changing your model function to this:
function mlsdata($column) {
$db =& JFactory::getDBO();
$query = " SELECT * FROM ".$db->nameQuote('#__mls')." WHERE ".$db->nameQuote('MSTMLSNO')." = ".$db->quote('4112065').";
$db->setQuery($query);
$row = $db->loadRow();
return $row;
}
Related
I don't know how to pass data from a class to one of my class to the studentShow template file.
MODEL
<?php
class Student{
public function all(){
require_once('config/db.php');
$SQLCommand = "SELECT * FROM people";
$Query = $conn->prepare($SQLCommand);
$Query->execute();
$rows = $Query->fetchAll(PDO::FETCH_OBJ);
return $rows;
}
}
?>
index
<?php
require_once("app/Controller/StudentController.php");
$Student = new StudentController();
$Student->index();
?>
Controller
<?php
require_once("app/Student.php");
class StudentController{
public function index(){
$Student = Student::all();
include ('resource/studentShow.php');
}
}
?>
My Question is: in my Controller how to pass that $student variable to studentShow.php.
Here is the way to achieve that:
public function index() {
$student = Student::all();
extract(['student' => $student]);
ob_start();
include ('resource/studentShow.php');
return ob_get_clean();
}
Read more about ob_start, extract & ob_get_clean
want from a method return an array of objects, what is the best way
retrieving data from the db and then populate a list of objects to be returned.
<?php
class DataObject{
public function GetObjetList(){
// Connect to the database server
$dbh = new PDO("mysql:host=localhost;dbname=bookdb", "webuser", "secret");
// Execute the query return 1200 register
$stmt = $dbh->query('SELECT sku, title FROM products ORDER BY title');
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$sku = $row['sku'];
$title = $row['title'];
return something?? --> how to??
}
}
?>
regards!
PDO already has a fetch mode that returns objects.
Change your code to this:
while ($row = $stmt->fetch(PDO::FETCH_OBJ)) {
class DataObject
{
public function __construct($pdo)
{
$this->db = $pdo;
}
public function GetObjetList()
{
$sql = 'SELECT sku, title FROM products ORDER BY title';
return $this->db->query($sql)->fetchAll(PDO::FETCH_OBJ);
}
}
Put the return after the while, not inside, use FETCH_OBJ and an array:
$rows = array();
$rows = $stmt->fetchAll(PDO::FETCH_OBJ);
return $rows;
I'm trying to pass a variable from one function to another in the same class, the purpose is to use it in a query.
I tried them all, $this, var_dump, fetch_assoc, ecc
How can I pass the $result variable in the query of the function due() ?
<?php
class modUno
{
public static function Uno()
{
$db = JFactory::getDBO();
$query = "SELECT id AS memTotal FROM #__users WHERE username = 'bruno';";
$db->setQuery($query);
$result = $db->query();
return $result->fetch_object()->memTotal;
}
public static function due()
{
$result->due();
$db = JFactory::getDBO();
$query = "SELECT avatar AS memTotal FROM #__comprofiler WHERE id = '$result';";
$db->setQuery($query);
$resulta = $db->query();
return $resulta->fetch_object()->memTotal;
}
}
?>
sorry I have not actually specified well, what I'm trying to do is a module joomla 3.2, therefore consists of three files the one above is the "helper.php" contains the helper class which is used to do retrieving the information, then there's the "mod_chat.php" that perform initialization routines, call helper routines to collect data, finally, there is "default.php" that will take the data from mod_chat.php and generate the HTML!
mod_chat
enter code here
<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
require_once( dirname(__FILE__).'/helper.php' );
$risultato = modUno::uno();
$foto = modUno::due();
require JModuleHelper::getLayoutPath('mod_chat');
?>
default.php
enter code here
<?php defined( '_JEXEC' ) or die( 'Restricted access' ); ?>
<p> risultato: <?php echo $risultato; ?></p>
<p> foto:<img src="http://somesite.com/images/comprofiler/<?php echo $foto; ?>" /></p>
I thank you all the rapid responses, I tried to make the changes that you suggested but keeps giving me error, now I will answer one by one on reported errors
thanks again you are a great comunity
When you want to share a variable among different methods of your class (without passing the variable as a parameter to each method), then the simplest way is to declare the variable as a class member.
E.g.
class myClass {
private $classVar;
function someMethod() {
$this->classVar = "something";
}
function anotherMethod() {
echo $this->classVar;
}
}
Try this
class modUno
{
protected $result;
public static function Uno()
{
$db = JFactory::getDBO();
$query = "SELECT id AS memTotal FROM #__users WHERE username = 'bruno';";
$db->setQuery($query);
$this->result = $db->query();
return $this->result->fetch_object()->memTotal;
}
public static function due()
{
$this->result->due();
$db = JFactory::getDBO();
$query = "SELECT avatar AS memTotal FROM #__comprofiler WHERE id = '$result';";
$db->setQuery($query);
$resulta = $db->query();
return $resulta->fetch_object()->memTotal;
}
}
Okay - since you didn't post the code that is calling both your functions, here is some psuedo code for you:
$result = Uno();
$resulta = Due($result);
Next you have to change your Due() function to be Due($result)
i would refactor all that code to be
<?php
class modUno
{
private $result;
private $db
public function __construct()
{
$this->db = JFactory::getDBO();
}
public function Uno()
{
$query = "SELECT id AS memTotal FROM #__users WHERE username = 'bruno';";
$this->db->setQuery($query);
$this->result = $this->db->query();
return $result->fetch_object()->memTotal;
}
public function due()
{
$result->due();
$query = "SELECT avatar AS memTotal FROM #__comprofiler WHERE id = '$result';";
$this->db->setQuery($query);
$resulta = $this->db->query();
return $resulta->fetch_object()->memTotal;
}
}
?>
I am using PHP with OOP to select rows from the database (MySQL).
When I execute the query, it returns an empty row.
Here is the classe I am using:
<?php
class EmploiManager
{
private $_db;
public function __construct($db)
{
$this->setDb($db);
}
public function category($category)
{
$q = $this->_db->prepare('SELECT * FROM DemandeEmploi WHERE category = :category');
$q->execute(array('category' =>$category));
$donnees = $q->fetch(PDO::FETCH_ASSOC);
return new Emploi($donnees);
}
public function setDb(PDO $db)
{
$this->_db = $db;
}
}
$type = $_GET['category'];
$manager = new EmploiManager($db);
$row = $manager->category($type);
foreach ($row as $demandeE)
{
?>
<div class="list"><h4><? echo $demandeE->title();?></h4> </div>
<?php
}
?>
Can any one tell me what's wrong with that code?
Thanks!
It's my bad, I didn't use a loop to select all the rows.
I corrected the code and it works fine now, here is what it looks like:
public function category($category)
{
$datas = array();
$q = $this->_db->prepare('SELECT * FROM DemandeEmploi WHERE category = :category');
$q->execute(array('category' =>$category));
while ($donnees = $q->fetch(PDO::FETCH_ASSOC))
{
$datas[] = new Emploi($donnees);
}
return $datas;
}
$q->fetch() just returns one row of the results. If you want all the results, you must use $q->fetchAll().
Since you specified PDO::FETCH_ASSOC, the elements of $row will be associative arrays, not objects; aren't you getting errors saying that you're trying to call a method on a non-object? So $demandeE->id() should be $demandeE['id'], and $demandeE->title() should be $demandeE['title'].
Alternatively, you could specify PDO::FETCH_OBJ. Then, the values will be properties, not methods, so it should be $demandeE->id and $demandeE->title (no parentheses).
I want to retrieve data from a table in database and display the result in an array in the form below.
array("1"=>"Value 1", "2"=>"value2")
Here is the function I tried using but I get error when I try to display the array. Please I need help on this. I'm new to OO Php.
<?php
class query {
public function listfields (){
$result = $this->mysqli->query("SELECT id, name FROM fields", MYSQLI_USE_RESULT);
while($row=$result->fetch_assoc()){
$this->fields[$row["id"]] = $row["name"];
}
$result->free();
}
public function fields(){
return $this->fields;
}
}
$list = new query;
$list->listfields();
$field1 = $list->fields();
echo $field1;
?>
Instead of your function fields, you will need a property fields, which you can access.
Furthermore i suggest using getters and setters instead of a public property, im sure you will find out how.
This will return the Data in form array("1"=>"Value 1", "2"=>"value2").
class query {
public $fields;
public function fillfields ()
{
$result = $this->mysqli->query("SELECT id, name FROM fields", MYSQLI_USE_RESULT);
while($row=$result->fetch_assoc()){
$this->fields[] = $row["name"];
}
$result->free();
}
$list = new query;
$list->fillfields();
$field1 = $list->fields[1];
echo $field1;
Try This.
<?php
class query {
public function listfields (){
$fields = array();
$rowcnt =1;
$result = $this->mysqli->query("SELECT id, name FROM fields", MYSQLI_USE_RESULT);
while($row=$result->fetch_assoc()){
$this->fields[$row["id"]] = $row["name"];
//$this->fields[$rowcnt] = $row["name"]; // if you want different indexing.
$rowcnt++;
}
$result->free();
}
public function fields(){
return $this->fields;
}
}
$list = new query();
$list->listfields();
$field1 = $list->fields();
var_dump($field1);
?>