How i can use a sql-query code that i already have on Zend-Framework using Model and Controller?
I'm tried of many ways and i can't solve this.
This is a example of "common sql-query code" to test:
"SELECT id_arquivos_retorno FROM sa_arquivos_retorno LIMIT 100"
*My original code it's a big query and it's terrible transform to a zend-select()
This is my Model:
class PedidosInscricoesModel extends Zend_Db_Table_Abstract{
protected $_name = 'sa_arquivos_retorno';
public function getPedidosInscricoes(array $params) {
$this->_db = Zend_Registry::get('db2');
$session = new Zend_Session_Namespace('autenticacao');
$query = $this->query("SELECT id_arquivos_retorno FROM sa_arquivos_retorno LIMIT 100");
$retorno = $this->fetchAll($query)->toArray();
return $retorno;
}}
And That's my Controller:
public function indexAction()
{
$PedidosInscricoesModel = new PedidosInscricoesModel();
$this->view->id_arquivos_retorno = $_REQUEST['id_arquivos_retorno'];
$params = $this->_request->getParams();
$data = $PedidosInscricoesModel->getPedidosInscricoes($params);
$this->view->data = $retorno;
}
My index view:
<?php
foreach($this->data as $dados) {
?>
<tr>
<td><?php echo $dados["id_arquivos_retorno"]; ?></td>
</tr>
<?php
}
?>
-Sorry for bad english guys
I got it!
Just modifying the Model using "zend_db_statement" in this way:
class PedidosInscricoesModel extends Zend_Db_Table_Abstract{
public function getPedidosInscricoes()
{
$db = Zend_Registry::get('db2');
$sql = "SELECT id_arquivos_retorno FROM sa_arquivos_retorno LIMIT 20";
$stmt = $db->query($sql);
$retorno = $stmt->fetchAll();
return $retorno;
}
}
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
Hello I am new to OOP and I have a class with a method and I am trying to return a variable to the class from a method but I am obviously missing something...
Here is my Class:
class Certification {
public $uid;
public $men_id;
public $sm_id;
public $sm_rec;
public $three_days;
public $min_on_test;
public $signed;
public $mid_year;
public $police_check;
public $io_comments;
public $date_certified;
public function __construct($uid)
{
include 'conn.php';
$stmt = $conn->prepare(
'SELECT * FROM certification WHERE uid = :uid');
$stmt->execute(array(':uid' => $uid));
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$this->uid = $row['uid'];
$this->nid = $row['nid'];
$this->nid = $row['date_certified'];
$this->men_id = $row['men_id'];
$this->sm_id = $row['sm_id'];
$this->sm_rec = $row['sm_rec'];
$this->three_days = $row['three_days'];
$this->min_on_test = $row['min_on_test'];
$this->signed = $row['signed'];
//$this->mid_year = $row['mid_year'];
$this->police_check = $row['police_check'];
$this->io_comments = $row['io_comments'];
}
}
public function get_mid_year ($uid) {
include 'conn.php';
$stmt = $conn->prepare(
'SELECT courseReviewed FROM vti_users WHERE uid = :uid');
$stmt->execute(array(':uid' => $uid));
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$this->mid_year = $row['courseReviewed'];
return $this->mid_year;
}
}
}
I thought that this method would return the $mid_year variable to the class but is null when I use var_dump().
Any advice about correcting my flawed logic would be a great help for a beginner.
So i'm new to classes and functions and I'm trying to do one sql query for the entire class for every function to use. I'm a little confused on how it works. From other examples that I've seen the call the query every single function which seems like a lot of usage. (why not call it once and set them all) I know there is an easier way by just querying and making variables including a db file or whatever. This is just practice.
<?php
//User System
class usrsys
{
$results = mysqli_query($con, "SELECT * FROM users WHERE id = '".$_SESSION['coid']."'");
while($row = mysqli_fetch_array($results)) {
$fname = $row['fname'];
}
function username()
{
echo $_SESSION['Username'];
}
function fname()
{
echo $fname;
}
}
//Setting Variable
$user = new usrsys();
?>
Then I call the functions in the next file by:
<?php $user->fname(); ?>
class usrsys
{
public $fname;
public $con;
public function __construct() {
//load your conection class function here and pass it to the $this->con variable.
$results = mysqli_query($this->con, "SELECT * FROM users WHERE id = '".$_SESSION['coid']."'");
while($row = mysqli_fetch_array($results)) {
$this->fname = $row['fname'];
}
}
function username()
{
echo $_SESSION['Username'];
}
function fname()
{
echo $this->fname;
}
}
//Setting Variable
$user = new usrsys();
$user->fname();
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).
Hi I have a couple of php classes. And I am passing a connection to a MySql database.
When I make an instance of the firs class and try to call the invoice class with the method getinvoicedetails() I get error "Command out of sync; you can't run this command".
Any help wil be appreciated.
Here is the code:
<?php //Invoicectrl class
class invoicectrl
{
// define properties
public $invoices = Array();
public $connection;
public $userID;
public $ownerID;
// constructor
public function __construct($userIDin,$ownerIDin) {
$this->userID = $userIDin;
$this->ownerID = $ownerIDin;
}
//function to set connection to db
public function setconnection($conn){
$this->connection = $conn;
}
public function getinvoicelist(){
$res = Array();
$invIDs = Array();
//Get list of invoicenumbers and invoiceIDs
$queryd = "CALL sp_getOwnersInvNumIDs (".$this->ownerID.")";
$result = $this->connection->query($queryd) or die($this->connection->error.__LINE__);
// GOING THROUGH invoices and getting there data
$o=0;
while($row = $result->fetch_assoc()) {
$invIDs[$o]['ID'] = $row['invoiceID'];
$invIDs[$o]['NR'] = $row['invoicenumber']; }
$result->free();
//Set each invoice own info
for($i=0;$i<=count($invIDs)-1;$i++){
$res[$i] = new invoice($invIDs['ID'],$invIDs['NR']);
$res[$i]->setconnection($this->connection);
$res[$i]->getinvoicedetails();
}
return $res;
}
}
?>
And here is the invoice class
<?php //Invoice class
class invoice
{
// define properties
public $invoiceID;
public $invoicenumber;
public $itemIDs=Array();
public $items= Array();
public $customerID;
public $cust_company;
public $cust_name;
public $cust_email;
public $cust_adress1;
public $cust_adress2;
public $cust_adress3;
private $connection;
// constructor
public function __construct($invoiceIDin,$invoicenumberin) {
$this->invoiceID = $invoiceIDin;
$this->invoicenumber = $invoicenumberin;
}
//function to set connection to db
public function setconnection($conn){
$this->connection = $conn;
}
//sets all invoices details
public function getinvoicedetails(){
//Get customer on invoice
$queryd = "select customerID FROM Accounting_Invoice_Customer where invoiceID =".$this->invoiceID."";
$result = $this->connection->query($queryd) or die($this->connection->error.__LINE__);
while($row1 = $result->fetch_assoc()) {
$this->customerID = $row1['customerID'];
}
$result->free();
//Get list of items on invoice
$queryd = "call sp_getItems(".$this->invoiceID.")";
$result = $this->connection->query($queryd) or die($this->connection->error.__LINE__);
$o=0;
while($row2 = $result->fetch_assoc()) {
$this->items[$o]['itemID'] = $row2['itemID'];
//$this->items[$o]['itemnumber'] = $row['itemnumber'];
//$this->items[$o]['itemdescription'] = $row['itemdescription'];
//$this->items[$o]['itempricewotax'] = $row['itempricewotax'];
$o++;
}
$result->free();
}
}
?>
When you run a SQL request, you have to get through the whole result or to close the cursore before sending another request.
The best thing to do, here is to get the whole result at first and then traverse it with a foreach loop to be able to send the second request.
Moreover, it should be a good idea to use INNER JOIN in your sql query.
$queryd = "CALL sp_getOwnersInvNumIDs (".$this->ownerID.")";
$result = $this->connection->query($queryd) or die($this->connection->error.__LINE__);
$invIDs = $result->fetch_all();
$res = array();
foreach($invIDs as $inv){
$inv = new invoice($invIDs['invoiceID'],$invIDs['invoiceNumber']);
$inv->setconnection($this->connection);
$inv->getinvoicedetails();
$res[] =$inv;
}