How to delete file from directory using class - php

Im trying to delete a file from folder while deleting data from database. I have used a class.
I get the following error:
Call to undefined function getCodeName()
//This function is to get the file name from the database
public static function getCodeName($product_id) {
$db = Database::getDB();
$query = "SELECT * FROM products WHERE productID = '$product_id'";
$result = $db ->query($query);
$row = $result->fetch();
$product = new Product ($row['productCode']);
return $product;
}
//Delete file from folder and other data from database
public static function deleteProduct($product_id) {
$fileName = getCodeName($product_id);
$dir = "../images/";
unlink($dir.DIRECTORY_SEPARATOR.$fileName);
$db = Database::getDB();
$query = "DELETE FROM products
WHERE productID = '$product_id'";
$row_count = $db->exec($query);
return $row_count;
}

Use php code
unlink() function set full pat for delete file

Related

Php cannot redeclare a function twice

Why is php not able to declare a function more than one time? I am using while loop with mySQL that contains list of IDs and I am trying to parse the ID to this function. But I get Fatal error: Cannot redeclare GetValue() (previously declared.... How do I fix this?
<?php
session_start();
ob_start();
$id = "";
$db = new PDO("mysql:host=localhost; dbname="DB";charset=utf8",'uid','');
$sqlID = "SELECT ID FROM Zip";
$stmtID = $db->query($sqlID);
$pdfArray = array();
while($dataID = $stmtID->fetch(PDO::FETCH_OBJ)){
$id = $dataID->ID;
$sql = "SELECT * FROM Datas WHERE ID = '$id'";
$stmt = $db->query($sql);
GetValue($id);
}
function GetValue($x){
//
}
?>
instead of:
GetValue($x){
//
}
try:
function GetValue($x){
//
}

Using two SELECT queries in php database connection

here is my_sqli database connection
class FbChatMock {
// Holds the database connection
private $dbConnection;
private $_dbHost = 'localhost';
private $_dbUsername = 'root';
private $_dbPassword = '';
public $_databaseName = 'erp5_temp2';
public function __construct() {
$this->dbConnection = new mysqli($this->_dbHost, $this->_dbUsername,
$this->_dbPassword, $this->_databaseName);
if ($this->dbConnection->connect_error) {
die('Connection error.');
}
}
i am getting two parameters in the getchat function:
public function getchat($userId, $id){
$meesage = array();
$query = "SELECT u.user_id FROM `users` u where u.id='$id'";
$resultObj = $this->dbConnection->query($query);
$user = $resultObj->fetch_assoc())
$userid = $user;
}
I am getting the $userid variable from the query and using it inside this query:
$query = "SELECT u.id,c.message,c.sent_on FROM `chat` c JOIN
`users` u ON c.user_id=u.user_id where u.id='$id' AND c.user_id='$userid'";
// Execute the query
$resultObj = $this->dbConnection->query($query);
// Fetch all the rows at once.
while ($rows = $resultObj->fetch_assoc()){
$meesage[] = $rows;
}
return $meesage;
And the problem is that my first sql query is not working correctly . i have tested it by showing $userid value from echo.
Chage this line
$userid = $user;
To this
$userid = $user['user_id'];
Because $user it is array with all columns you have selected, and you need get only ID from it.
Your problem lies here :
$user = $resultObj->fetch_assoc())
$userid = $user;
because mysqli::fetch_assoc() with retrieve an associative array. Thus you have to access it like so (given the column name in your select):
$userid = $user["user_id"];

PHP OOP: Function not return all row in database to php

I've created a function that suppose to return all row in database to browser, but it is return only one value. Below is my table:
And this is my code:
SqlDatabase.php Classes:
public function query($sql){
$result = mysqli_query($this->connection, $sql);
return $result;
}
test.php:
$database = new SqlDatabase;
//testing function
public static function find_comments($photo_id=0){
global $database;
$sql = "select * from comments";
$sql .= " where photograph_id = {$photo_id}";
$sql .= " order by created ASC";
print_r(self::sql_find($sql));
}
// This is a testing function
public static function sql_find($sql){
global $database;
$result_set = $database->query($sql);
$object_array = array();
while ($row = $database->fetch_assoc($result_set)){
$object_array = $row['author'];
} return $object_array;
}
When i try to run this function, It is only return one last value "vai het loc" to PHP:
Comments::find_comments(77);
I tried several ways and do researchs but haven't found any solutions yet. Not sure what am i missing. Need some guide to fetch all row in photograph_id not only one value.
Thanks for helps.
You're overwriting the value, not appending it to the array.
Change this line:
$object_array = $row['author'];
to this:
$object_array[] = $row['author'];
In your sql_find() function and you should get the object.

How to assign php while loop into smarty to get required result

i want to show featured ads in slider problem is there is two table have to use to get ads. First one is class_ads that have all info of ad like title, description, price etc and second table is class_ads_pictures from where i have to match id of each ad and get the name of folder and picture name from table ....
Please review my poor logic and teach me
<?php
require_once "include/include.php";
global $db;
global $lng;
$smarty = new Smarty;
$smarty = common($smarty);
$featured_ads = mysql_query("SELECT * FROM class_ads where active=1 and featured=1 limit 50");
while ($tmp = mysql_fetch_array($featured_ads)) {
$ad_id = $temp['id'];
$ad_title = $temp['title'];
//check for image if featured ad have image (Need to fetch only single image)
$featured_ads_images = mysql_query("select from class_ads_pictures where ad_id=$ad_id order by order_no");
$img = mysql_fetch_array($featured_ads_images);
$img_id = $img['id'];
$ad_img = $img['picture'];
$img_folder = $img['folder'];
// Problem is how to assign and what have to assign, to get display in html file ...
}
$smarty->assign('what', $what);
$db->close();
if($db->error!='') { $db_error = $db->getError(); $smarty->assign('db_error',$db_error); }
$smarty->display('header_featured.html');
close();
?>
Below code should work. However you should not use mysql but PDO or mysqli because mysql is deprecated.
<?php
require_once "include/include.php";
global $db;
global $lng;
$smarty = new Smarty;
$smarty = common($smarty);
$ads = array();
$featured_ads = mysql_query("SELECT * FROM class_ads where active=1 and featured=1 limit 50");
while ($temp = mysql_fetch_assoc($featured_ads)) {
$record = array();
$record['ad_id'] = $temp['id'];
$record['ad_title'] = $temp['title'];
//check for image if featured ad have image (Need to fetch only single image)
$featured_ads_images = mysql_query("select * from class_ads_pictures where ad_id={$record['ad_id']} order by order_no");
$img = mysql_fetch_assoc($featured_ads_images);
$record['img_id'] = $img['id'];
$record['ad_img'] = $img['picture'];
$record['img_folder'] = $img['folder'];
$ads[] = $record;
}
$smarty->assign('ads', $ads);
$db->close();
if($db->error!='') { $db_error = $db->getError(); $smarty->assign('db_error',$db_error); }
$smarty->display('header_featured.html');
close();
?>
In Smarty you can simple use it:
{foreach item=item from=$ads}
{$item.ad_id}
{$item.ad_title}
{$item.img_id}
{$item.ad_img}
{$item.img_folder}
{/foreach}

Get the value of return within class

Goodday. The question is, how can I get the return value and pass to my other php file?
myclass.php file
class myClass{
function banner(){
$sql = "Select * From tblBanner";
$query = mysql_query($sql) or die (mysql_error());
$row = mysql_fetch_assoc($query);
$banner = $row;
return $banner;
}
}
my index.php file
include 'myclass.php';
$banner = new myclass;
$ban = $banner->banner;
echo $ban;
This is what im trying to do. Im a newbie so spare with me. Thank you.
Like I already wrote in the comments, $banner->banner would be a property of the class "myClass". But you want to get a method, so you have to use $banner->banner().
myclass.php file
class myClass{
function banner(){
$sql = "Select * From tblBanner";
$query = mysql_query($sql) or die (mysql_error());
$row = mysql_fetch_assoc($query);
$banner = $row;
return $banner;
}
}
my index.php file
include 'myclass.php';
$banner = new myclass;
$ban = $banner->banner();
// example for array:
foreach($ban as $banner) {
echo $banner;
}
// example for string:
echo $ban;
An example for $banner->banner:
myclass.php file
class myClass{
public $banner = 'Banner Property';
function banner(){
$sql = "Select * From tblBanner";
$query = mysql_query($sql) or die (mysql_error());
$row = mysql_fetch_assoc($query);
$banner = $row;
return $banner;
}
}
my index.php file
include 'myclass.php';
$banner = new myclass;
$ban = $banner->banner;
echo $ban;

Categories