PHP - query worked but the results are strange : all null - php

I use a data base ( created with phpmyadmin) in a function for my future website. The query worked but the result is strange : null .
This is my code :
public function select_toutcompetences($requete){
$query = "SELECT niv , nom FROM competences WHERE element = '$requete' ";
$result = $this->_db->query($query);
$tableau = array();
if ($result->rowCount() == 0){
return $tableau;
} else {
while($row = $result->fetch()) {
$tableau[] = new Competence($row->niv,$row->nom);
}
return $tableau;
}
}
In my view :
<?php if (!empty($table)) {?>
<table>
<caption>Les compétences</caption>
<tr>
<th>Niveau</th>
<th>Nom</th>
<tr>
<?php foreach ($table as $i=> $element) { ?>
<tr>
<td><?php echo $element->niveau()?></td>
<td><?php echo $element->nom()?></td>
</tr>
<?php } ?>
</table>
<?php }?>
With a var_dump , I got this :
object(Competence)[5]
private '_niveau' => null
private '_nom' => null
null
My class :
<?php
class Competence {
private $_niveau;
private $_nom;
public function Livre($niveau,$nom) {
$this->_niveau = $niveau;
$this->_nom = $nom;
}
public function niveau() {
return $this->_niveau;
}
public function nom() {
return $this->_nom;
}
}
?>
Thanks you for your help. I searched but I didn't understand why it doesn't work.
PS : I know my query isn't securized. I would it to work first.
Edit : the sql data base : comptences.sql

It is because your class does not have constructor. Method Livre is not called when object is initiating $tableau[] = new Competence($row->niv,$row->nom);. Try this (I have replaced Livre with __construct):
<?php
class Competence {
private $_niveau;
private $_nom;
public function _contruct($niveau,$nom) {
$this->_niveau = $niveau;
$this->_nom = $nom;
}
public function niveau() {
return $this->_niveau;
}
public function nom() {
return $this->_nom;
}
}
Also be sure you have same values in database result.

Try making the variables in your class public as well.
public $_niveau;
public $_nom;

Related

Creating a Cursor Based Pagination System in PHP

I am trying to figure out how how to create my own simple cursor based pagination system in PHP and am having difficulty trying to understand how star starting_after and starting_before works as mentioned in this medium.com post for how the company stripe deals with cursor pagination. In my case I am using the id column that is in ascending order to hopefully make this work. The issue that I am having is getting an id for the first "page". Currently my first page would direct to the second page since starting_after leads to the next page and not the currently page. Any advice for how to build this out would be awesome. I already created page based pagination, but think that cursor pagination would be more useful for most of my cases.
I have attached the two files that I have created thus far to try to get this to work.
Pagination class
<?php
require_once "DB.php";
class New_Pagination {
private $table = "";
private $limit;
private $starting_after = "";
private $starting_before = "";
private $db;
public function __construct() {
$this->db = DB::getInstance();
}
public function getLimit(): int {
return $this->limit;
}
public function setLimit(int $limit): void {
$this->limit = $limit;
}
public function getStartingAfter(): string {
return $this->starting_after;
}
public function setStartingAfter(string $starting_after): void {
$this->starting_after = $starting_after;
}
public function getStartingBefore(): string {
return $this->starting_before;
}
public function setStartingBefore(string $starting_before): void {
$this->starting_before = $starting_before;
}
public function getTable(): string {
return $this->table;
}
public function setTable(string $table): void {
$this->table = $table;
}
public function idExists($id) {
$result = $this->db->find(self::getTable(), [
"select" => "id",
"conditions" => "id = $id",
"fetchType" => "single"
]);
if (empty($result)) {
return FALSE;
} else {
return $result->id;
}
}
public function getData($starting_after, $starting_before) {
self::setStartingAfter($starting_after);
self::setStartingBefore($starting_before);
$starting_after = self::getStartingAfter();
$starting_before = self::getStartingBefore();
$data = [];
$order = !empty($starting_after) ? "ASC" : "DESC";
if (empty($starting_after) && empty($starting_before)) {
$data["data"] = $this->db->find(self::getTable(), [
"select" => "*",
"order" => "id ASC",
"limit" => self::getLimit(),
"fetchType" => "all"
]);
} else {
$data["data"] = $this->db->find("carousel_image", [
"select" => "*",
"conditions" => "id >= '$starting_after' OR id <= '$starting_before'",
"order" => "id $order",
"limit" => self::getLimit(),
"fetchType" => "all"
]);
}
$next = self::idExists($data["data"][count($data["data"]) - 1]->id + 1);
$previous = self::idExists($data["data"][0]->id - 1);
$data["cursor"] = [
"next" => $next,
"previous" => $previous
];
return $data;
}
public function generateLink() {
$test = self::getData("", "");
$test2 = [];
$test2[0] = $test;
$i = 0;
do {
$test2[$i] = $test;
$test = self::getData($test["cursor"]["next"], "");
$i++;
$test2[$i] = $test;
} while ($test["cursor"]["next"] !== FALSE);
$test2[$i] = $test;
echo "<ul>";
$j = 1;
foreach ($test2 as $key => $val) {
if ($val["cursor"]["next"] !== FALSE) {
$url = "/process.php?starting_after=" . $val["cursor"]["next"];
echo "<li>";
echo "<a href='$url'>$j</a>";
echo "</li>";
$j++;
}
}
echo "<ul>";
}
}
Test file
$pagination = new New_Pagination();
$pagination->setLimit(2);
$pagination->setTable("carousel_image");
echo "<pre>";
$pagination->generateLink();
echo "</pre>";
The cursors are useful to prevent scan big tables and allow to move in very big sources (files, external resources, etc., etc.). In the majority of the cases, cursors are provided by binary libraries and supported by the core of the related system (mysql, files). If you try to emulate this behavior in not natural way you must take care because you could add overhead and get unexpected results.
In the other hand, is very useful to have a pagination class, but be aware, this class have some problems.
getData is very expensive because it performs 3 queries to get a batch of results
the class is too verbose, the getters and setters add too much noise
the results are sorted using very rare criteria. Remember, if you are in search results you need the same order moving to the next page and moving to the previous page
My suggestions...
Create interfaces
<?php
interface CursorAble {
public function fetchNext ($startingAfter);
public function fetchPrev ($startingBefore);
public function getPreviousLink ();
public function getNextLink ();
}
interface Pageable {
public function getCollectionSize ();
public function getPageSize ();
public function getPagesCount ();
public function getPageLinks ();
}
When you create interfaces you ensures that the classes expose the desired behavior and furthermore delegate the specialized details to the concrete implementations. The concrete implementations can define it dependencies in the constructor, something very good when you relies on dependency injection.
CursorAble implementation example
<?php
class PdoCursorAbleTable implements CursorAble {
private $pdo;
private $table;
private $results;
private $pageSize;
public function __construct (PDO $pdo, $table, $pageSize = 100) {
$this->pdo = $pdo;
$this->table = $table;
$this->pageSize = (int)$pageSize ?: 100;
}
public function fetchNext ($startingAfter) {
$s = $this->pdo->prepare("select * from {$this->table} where id > :starting_after limit {$this->pageSize}");
$s->bindValue(':starting_after', $startingAfter, PDO::PARAM_INT);
$s->execute();
$this->results = $s->fetchAll() ?: [];
return $this->results;
}
public function fetchPrev ($startingBefore) {
$s = $this->pdo->prepare("select * from {$this->table} where id < :starting_before limit {$this->pageSize}");
$s->bindValue(':starting_before', $startingBefore, PDO::PARAM_INT);
$s->execute();
$this->results = $s->fetchAll() ?: [];
return $this->results;
}
public function getPreviousLink () {
return !$this->results ? '' : '?starting_before=' . $this->results[0]->id;
}
public function getNextLink () {
if (!$this->results || count($this->results) < $this->pageSize) return '';
return '?starting_after=' . $this->results[count($this->results)]->id;
}
}
And the Pageable example
<?php
class PdoPageableTable implements Pageable {
private $pdo;
private $table;
private $pageSize;
private $collectionSize;
public function __construct (PDO $pdo, $table, $pageSize = 100) {
$this->pdo = $pdo;
$this->table = $table;
$this->pageSize = $pageSize;
}
public function getCollectionSize () {
if ($this->collectionSize === null) {
$s = $this->pdo->prepare("select count(id) from {$this->table}");
$s->execute();
$this->collectionSize = $s->fetchColumn('0');
}
return $this->collectionSize;
}
public function getPageSize () {
return $this->pageSize;
}
public function getPagesCount () {
return ceil($this->collectionSize / $this->getPageSize());
}
public function getPageLinks () {
$pages = [];
foreach (range(1, $this->getPagesCount()) as $page) {
$pages[] = '?page=' . $page;
}
return $pages;
}
}
The test file
<?php
$pagination = new PdoCursorAbleTable($pdo, 'carousel_image', 2);
echo "<pre>";
$startingAfter = 0;
$results = $pagination->fetchNext($startingAfter);
foreach ($results as $result) {
// do something
}
echo $pagination->getNextLink();
echo "</pre>";

How to get everything outside the database with foreach and class

I have a problem. I want some data out my database.
I have two page's a categorie.php here I want that he shows everything out the database. And I have a second page. Here are my classes. I have trying a foreach on the categorie.php but if I do that, than shows he 1 thing out the database 4 times the same and not the another data.
Below you can see my code.
I hope that you can help me.
Thank you.
This is my categorie.php
<?php
require_once '../app/functions/second.php';
require_once '../app/db/dbpassword.php';
require_once 'include/head.php';
if (isset($_GET['categorie']) && !empty($_GET['categorie'])) {
$id = $_GET['categorie'];
$dbh = new PDO("mysql:host=$host; dbname=$dbname;", $usernamedb,
$passworddb);
$cate = new Categorie($dbh);
$cate->loadCate($id);
// $page->loadId($id);
$categorie = $cate->getCategorie();
$titel = ucwords($categorie);
?>
<h2 class="center_h2"><?= $titel ?></h2>
<?php foreach ($cate as $key) {
$titelart = $cate->getTitel();
$beschrijving = $cate->getBeschrijving();
$plaatje = $cate->getImage();
$id = $cate->getId();
var_dump($titelart);
} ?>
<?php
} else {
echo "Dit bericht is verwijderd of is verplaats.";
}
require_once 'include/footer.php';
?>
This is my class page
<?php
class Categorie {
protected $dbh;
public function __construct($new_dbh){
$this->dbh = $new_dbh;
}
public function loadCate($cate){
$query = $this->dbh->prepare('SELECT * FROM schilderijen WHERE categorie=?');
$query->execute(array($cate));
while ($row = $query->fetch(PDO::FETCH_OBJ)) {
$this->id = $row->id;
$this->categorie = $row->categorie;
$this->titel = $row->titel;
$this->beschrijving = $row->beschrijving;
$this->plaatje = $row->plaatje;
}
}
public function getId(){
return $this->id;
}
public function getCategorie(){
return $this->categorie;
}
public function getTitel(){
return $this->titel;
}
public function getBeschrijving(){
return $this->beschrijving;
}
public function getImage(){
return $this->plaatje;
}
}
?>
Ok so you have a problem with the use of your class. In the while after your SQL request, you apply the value the instance variable like $this->id = $row->id; but this variable will be rewrite with the next row value.
Use a static function for your SQL request and return an array of Categorie like that :
class Categorie {
protected $id, $categorie, $title, $beschrijving, $plaatje;
public function __construct($id, $categorie, $title, $beschrijving, $plaatje){
$this->id = $id;
$this->categorie = $categorie;
$this->title = $title;
$this->beschrijving = $beschrijving;
$this->plaatje = $plaatje;
}
public static function loadCate($dbh, $cate){
$query = $dbh->prepare('SELECT * FROM schilderijen WHERE categorie=?');
$query->execute(array($cate));
$res = array();
while ($row = $query->fetch(PDO::FETCH_OBJ)) {
$res[] = new Categorie($row->id, $row->categorie, $row->titel, $row->beschrijving, $row->plaatje);
}
return $res;
}
public function getId(){
return $this->id;
}
public function getCategorie(){
return $this->categorie;
}
public function getTitle(){
return $this->title;
}
public function getBeschrijving(){
return $this->beschrijving;
}
public function getImage(){
return $this->plaatje;
}
}
And you can use it like that:
$categories = Categorie::loadCate($dbh, $id);
foreach($categories as $categorie){
var_dump($categorie->getTitle());
}

OOP-php - Can not retrive data from database

I'm a new with OOP model in PHP. I has been trying to retrieve data from database but something related to private makes me stuck.
This is my code.
<?php
require ("UserData.php");
class Database{
public function getUser($sql){
include ("includes/connect.php");
$statement = $conn->prepare ($sql);
$statement->execute();
while ($row = $statement->fetch()) {
$dataSet[] = new UserData($row);
}
if (!empty($dataSet)) {
return $dataSet;
}else{
die();
}
}
}
?>
the second file
<?php
class UserData
{
private $user_id, $phone,$name,$address;
public function _construct($dbrow){
$this->user_id = $dbrow['user_id'];
$this->name = $dbrow['name'];
$this->phone = $dbrow['phone'];
$this->address = $dbrow['address'];
}
public function getUserId(){
return $this->user_id;
}
public function getUserName(){
return $this->name;
}
public function getUserPhone(){
return $this->phone;
}
public function getUserAddress(){
return $this->address;
}
}
?>
and the last one
<?php require ("Database.php"); ?>
<html>
<head>
<title>OOP</title>
</head>
<body>
<?php
include("includes/connect.php");
$db = new Database();
$dataSet = $db -> getUser ("SELECT * from user");
if ($dataSet) {
foreach ($dataSet as $data) {
echo "<p>";
echo "ID" .$data->getUserId()."<br />";
echo "Name" .$data->getUserName()."<br />";
echo "Phone" .$data->getUserPhone()."<br />";
echo "Address" .$data->getUserAddress()."<br />";
echo "</p>";
}
}else{
echo "no result found";
}
?>
</body>
</html>
Well, I tried to var_dump the dataSet but the error shows up.
array(2) { [0]=> object(UserData)#5 (4) {
["user_id":"UserData":private]=> NULL ["phone":"UserData":private]=>
NULL ["name":"UserData":private]=> NULL
["address":"UserData":private]=> NULL } [1]=> object(UserData)#6 (4) {
["user_id":"UserData":private]=> NULL ["phone":"UserData":private]=>
NULL ["name":"UserData":private]=> NULL
["address":"UserData":private]=> NULL } }
So can anyone show me which spots make the code dump?
Everything is okay except this line,
public function _construct($dbrow){ ...
^^^^^^^^^^ it should be double underscore, not single
Your constructor method in UserData class is wrong. It should be,
public function __construct($dbrow){ ...
In PHP 5.6.0+, you can use the magic mathod __debugInfo()
class UserData
{
public function __debugInfo() {
return ['user_id' => $this->user_id, 'phone' => $this->phone, 'name' => $this->name, 'address' => $this->address];
}
}
or makes variables public, which is not intend when you have methods like getUserId
or create method to dump like:
class UserData
{
public function dump() {
var_dump($this);
}
}
btw in class Database method getUser you include all the time when method is called, quick fix: use require_once instead of include, better will be to call include/require/require_once outside of class

codeigniter 3: does my code follow MVC rule

I am new to codeigniter 3 and try to convert my PHP project to MVC by this Framework
Controller file
class home extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('frontend/M_Headers');
}
public function index()
{
$data['slide_image'] = $this->M_Headers->get_all_slide();
$this->load->view('frontend/headers',$data);
}
}
Model file
class M_Headers extends CI_Model {
public function __construct()
{
parent::__construct();
}
public function get_all_slide()
{
$query = $this->db->get('tbl_slide');
return $query->result();
}
}
View file
<div class="slider">
<ul class="rslides" id="slider">
<?php
if(count($slide_image) > 0)
{
foreach($slide_image as $value):
$get_image=$value->sl_image;
if($get_image != ''):
$image_properties = array(
'src' => 'assets/images/slide/'.$get_image,
'alt' => '',
);
?>
<li><?php echo img($image_properties); ?></li>
<?php else: ?>
<li>No Slide</li>
<?php
endif;
endforeach;
}
?>
</ul>
</div>
did am i right that loop result in View or should i do it in
Controller?
which query should i use between active record or bind (where id=?) or they had it own benefit in difference situation?
The loop in the view is correct. Because it is "view logic" (repeating li's) you can use into the view.
Your Model can be better. I personally like to use the model as a representation of the item from the database. Like this:
class M_Headers extends CI_Model
{
private var $tablename = "tbl_slide";
var $id;
var $sl_image;
public function __construct()
{
parent::__construct();
}
public function get_all_slide()
{
$slides = array();
$query = $this->db->get($this->tablename);
foreach($query->result() as $row)
{
$item = new self();
$item->id = $row->id;
$item->sl_image = $row->sl_image;
$slides[] = $item;
}
return $slides;
}
public function get_slide($id)
{
$this->db->where("id", $id);
$query = $this->db->get($this->tablename);
$results = $query->result();
if(isset($results[0]))
{
$row = $results[0];
$this->id = $row->id;
$this->sl_image = $row->sl_image;
return true;
}
return false;
}
}
You can even improve this to make a method that loads the row into the model:
class M_Headers extends CI_Model
{
private var $tablename = "tbl_slide";
var $id;
var $sl_image;
public function __construct()
{
parent::__construct();
}
private function load_with_record($row)
{
$this->id = $row->id;
$this->sl_image = $row->sl_image;
}
public function get_all_slide()
{
$slides = array();
$query = $this->db->get($this->tablename);
foreach($query->result() as $row)
{
$item = new self();
$item->load_with_row($row);
$slides[] = $item;
}
return $slides;
}
public function get_slide($id)
{
$this->db->where("id", $id);
$query = $this->db->get($this->tablename);
$results = $query->result();
if(isset($results[0]))
{
$row = $results[0];
$this->load_with_row($row);
return true;
}
return false;
}
}

How the heck do I return these arrays with actual values from the db? Keep on coming up blank

So the ultimate goal is to print out products as objects in a table using functions from these classes, but the output is a table with the correct amount of rows/columns but they are blank. I've spent 3 hours trying to figure this out and I'm stumped. There's three files
Product.php
<?php
class Product
{
protected $product_id;
protected $product_name;
protected $product_price;
public function __construct($product_id,$product_name, $product_price = '')
{
$this->setProductID($product_id);
$this->setProductName($product_name);
$this->setProductPrice($product_price);
}
public function getProductID() {
return $this->product_id;
}
public function getProductName() {
return $this->product_name;
}
public function getProductPrice() {
return $this->product_price;
}
public function setProductID($product_id)
{
$this->product_id = $product_id;
}
public function setProductName($product_name)
{
$this->product_name= $product_name;
}
public function setProductPrice($product_price)
{
$this->product_price= $product_price;
}
}
ProductMapper.php
<?php
class ProductMapper
{
public function getProducts()
{
$dbConn = getDbConnection();
$stmt = $dbConn->prepare("SELECT * FROM product");
$stmt->execute();
$outArray = array();
while ($row = $stmt->fetch()) {
$outArray[] = new Product($row['product_id'], $row['product_name'], $row['product_cost']);
}
return $outArray;
}
}
and Index.php which is where it is outputted.
<?php
require('classes/functions.php');
require('classes/Product.php');
require('classes/ProductMapper.php');
$productMapper = new ProductMapper();
$rows = $productMapper->getProducts();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Product</title>
</head>
<body>
<tbody>
<?php
foreach ($rows as $row) {
echo "<tr>";
echo "<td>{$row->getProductID($this)}</td>";
echo "<td>{$row->getProductName($id)}</td>";
echo "<td>{$row->getProductPrice($id)}</td>";
echo "</tr>";
}
?>
</tbody>
</table>
</body>
</html>
There are a few minor issues with the code that you provide which prevent it working. Do you have 'error_reporting' set error-reporting?
index.php :
The 'getProduct' methods do not accept any parameters. So you should not call them with any parameters.
ProductMapper.php :
for consistancy i renamed the 'product_cost' column to 'product_price'.
i injected the database connection in the constructor and made it a property. Makes available the connection for any future method.
Some example output:
pid01 name-pid01 23.34
pid02 name-pid02 101.67
Here are the new scripts...
index.php
require('classes/functions.php');
require('classes/Product.php');
require('classes/ProductMapper.php');
$productMapper = new ProductMapper(getDbConnecton());
$rows = $productMapper->getProducts();
// note: the 'getProductID', 'getProductName' and 'getProductPrice' methods
// do not accept or need, any parameters.
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Product</title>
</head>
<body>
<table>
<tbody>
<?php
foreach ($rows as $row) {
echo "<tr>";
echo "<td>{$row->getProductID()}</td>";
echo "<td>{$row->getProductName()}</td>";
echo "<td>{$row->getProductPrice()}</td>";
echo "</tr>";
}
?>
</tbody>
</table>
// ProductMapper.php
class ProductMapper
{
/**
* #var PDO
*/
private $dbConn = null;
public function __construct(PDO $pdo)
{
$this->dbConn = $pdo;
}
public function getProducts()
{
$dbConn = $this->dbConn;
$stmt = $dbConn->prepare("SELECT * FROM product");
$stmt->execute();
$outArray = array();
while ($row = $stmt->fetch()) {
$outArray[] = new Product($row['product_id'], $row['product_name'], $row['product_price']);
}
return $outArray;
}
}

Categories