I have table -> subkategori
and I can't insert the id_kategori attribute to table portfolio
i have a code for the model :
function get_subcategories($id_kategori){
$query = $this->db->query("SELECT id_kategori from subkategori WHERE id_subkategori=".$this->input->post('a'));
return $query->result();
}
and the controller here :
if ($this->input->post('a')!=''){
$this->load->model('Model_app');
$this->model_app->get_subcatoegories('$id_kategori');
}else{
$id_kategori = '';
}
$data = array(
'id_subkategori'=>$this->db->escape_str($this->input->post('a')),
'id_kategori'=>$id_kategori
);
$this->input->post('a') as you mentioned here I assume that you have input in the form and its name is a.
In Controller
if (!empty($this->input->post('a')))
{
$this->load->model('Model_app');
$id = $this->model_app->get_subcatoegories($this->input->post('a'));
if (empty($id)) {
$id_kategori = '';
} else {
$id_kategori = $id[0]['id_kategori'];
}
}else{
$id_kategori = '';
}
$data = array(
'id_subkategori'=>$this->db->escape_str($this->input->post('a')),
'id_kategori'=>$id_kategori
);
In Model
function get_subcategories($id_kategori)
{
$query = $this->db->query("SELECT id_kategori FROM subkategori WHERE id_subkategori = $id_kategori");
return $query->result_array();
}
You can get last insterted if with $this->db->insert_id();
In your, if statement you didn't assign the database value to the $id_kategori variable. Hence while inserting the empty value into the table it inserts as 0(int field).
And also when you pass a variable to a function it should not be single quoted. You have to pass the post variable but you are passing a variable which is undefined.
if ($this->input->post('a')!=''){
$this->load->model('Model_app');
$category = $this->model_app->get_subcatoegories($this->input->post('a'));
$id_kategori = $category['id_kategori];
}else{
$id_kategori = '';
}
$data = array(
'id_subkategori'=>$this->db->escape_str($this->input->post('a')),
'id_kategori'=>$id_kategori
);
Related
I'm stuck with setting the existing customers id be equal to the purchase id. This is model for entry.
On the else block, the error says:
"Message: Object of class CI_DB_mysqli_result could not be converted
to string"
and error on this line: "$purchase['cust_id'] = $cust;".
I'm a beginner in php and Codeigniter
Class Entry_m extends CI_Controller{
public function create_submit($customer, $purchase){
$array = array('lname' => $customer['lname'], 'fname' => $customer['fname'], 'mn' => $customer['mn']);
$this->db->like($array);
$query = $this->db->get('customer');
$count = $query->num_rows();
if($count ===0)
{
$this->db->insert('customer', $customer);
$cust_id = $this->db->insert_id();
$purchase['cust_id'] = $cust_id;
$this->db->insert('purchase', $purchase);
return $cust_id = $this->db->insert_id();
}else{
$cust = $this->db->select('cust_id')->where($array)->get('customer');
$purchase['cust_id'] = $cust;
$this->db->insert('purchase', $purchase);
}
}
}
try this
$cust = $this->db->select('cust_id')->where($array)->get('customer')->row()->cust_id;
Instead
$this->db->insert('customer', $customer);
$cust_id = $this->db->insert_id();
use
$cust_id = $this->db->insert_id('customer', $customer);
I have a set of PHP functions for a URL shortener. I'd like to add another function which concerns updating a whole column in a MySQL table. So far I found the query which is: UPDATE urls SET redirect = 'http://example.com'
I'd like to have a text input where once I type a new URL and click "UPDATE ALL", the whole column in the database changes without having to do it from the database using the query I mentioned above. The only thing that stops me from doing so is the function(BOTTOM).
Here are the functions I mentioned earlier:
function createLink($slug,$url,$title,$disc,$thumb,$redirect){
global $nsqlQuery;
$f = array('slug','url','title','disc','thumb','redirect','time');
$v = array($slug,$url,$title,$disc,$thumb,$redirect,time());
$row = array_combine($f,$v);
$result = $nsqlQuery->insert("urls",$row);
return $result ? $result : false;
}
function getLink($by='NA',$val='NA'){
global $nsqlQuery;
if($by=='NA'):
$where = null;
else:
$f = array($by);
$v = array($val);
$where = array_combine($f,$v);
endif;
$result = $nsqlQuery->select("urls",'*',$where);
return $result ? $result : false;
}
function deleteLink($id){
global $nsqlQuery;
$f = array('id');
$v = array($id);
$where = array_combine($f,$v);
$result = $nsqlQuery->delete("urls",$where);
return $result ? $result : false;
}
Here's the public function that concerns updating the table and setting a new value to a certain column.
public function update($table,$row,$where,$where_condition=null,$dev='OFF'){
$query = "UPDATE ".$table." SET ".$this->getSetClause($row)." ".$this->getWhereClause($where,$where_condition);
if($dev=='ON')
return $query;
$response = $this->query($query);
return $response ? true : false;
}
Here's my progress so far which prompts a 500 error.
function updateRedirect($redirect){
global $nsqlQuery;
$where = $redirect;
$result = $nsqlQuery->update("urls",$where) SET ".$nsqlQuery->select("urls",'*',$where);
return $result ? $result : false;
}
The whole nsqlQuery.php file just in case.
I insert in the database a csv file. how will i return the id and use it to insert in another table. it always displays array to string conversion errror. is there something wrong with "return"
here is my controller
public function uploadThree(){
$file = $_FILES['file']['tmp_name'];
$handle = fopen($file,"r");
while(($fileop = fgetcsv($handle,1000,",")) !==false)
{
$appname = $fileop[0];
$servname = $fileop[1];
$ciname = $fileop[2];
$servid = $this->some_model->insertBulkServ($servname); //i tried to get the value here then insert below
$appid = $this->some_model->insertBulkSingleApp($appname);//i tried to get the value here then insert below
$this->some_model->insertBulkCI($ciname);
$this->some_model->ASMAP($appid,$servid);
}
if($success == TRUE)
redirect(base_url().'some_controller/uploadPage');
}
MODEL
public function insertBulkServ($service) {
/* Inserts csv file for a service */
$service = $this->db->escape_str($service);
$queryStr = "Select service from appwarehouse.service where service = '$service' and VISIBILITY = 'VISIBLE'";
$query = $this->db->query($queryStr);
if($query->num_rows()>0){
$queryStr = "SELECT id FROM appwarehouse.service WHERE service='$service' AND visibility = 'VISIBLE';";
$query = $this->db->query($queryStr);
$row = $query->result();
return $row;
//in here how do i get the ID how do i return it
}else{
$queryStr = "INSERT INTO appwarehouse.service(service) VALUES ('$service');";
$query = $this->db->query($queryStr);
$queryStr = "SELECT id FROM appwarehouse.service WHERE service='$service' AND visibility = 'VISIBLE';";
$query = $this->db->query($queryStr);
$row = $query->result();
return $row;
}
}
public function insertBulkSingleApp($app_name) {
/* Inserts csv file for an application */
$app_name = $this->db->escape_str($app_name);
$queryStr = "Select * from appwarehouse.application_table where app_name = '$app_name' and VISIBILITY = 'VISIBLE'";
$query = $this->db->query($queryStr);
if($query->num_rows()>0){
$queryStr = "SELECT id FROM appwarehouse.application_table WHERE app_name='$app_name' AND visibility = 'VISIBLE';";
$query = $this->db->query($queryStr);
$row = $query->result();
return $row;
}
else{
$queryStr = "INSERT INTO appwarehouse.application_table(app_name)
VALUES ('$app_name');";
$query = $this->db->query($queryStr);
$queryStr = "SELECT id FROM appwarehouse.application_table WHERE app_name='$app_name' AND visibility = 'VISIBLE';";
$query = $this->db->query($queryStr);
$row = $query->result();
return $row;
}
}
public function ASMAP($appid,$servid) {
$appid = $this->db->escape_str($appid);
$servid = $this->db->escape_str($servid);
$queryStr = "Select * from appwarehouse.app_service where app_id = '$appid' AND serv_id = '$servid' and VISIBILITY = 'VISIBLE'";
$query = $this->db->query($queryStr);
if($query->num_rows()>0){
return true;
}
else{
$queryStr = "INSERT INTO appwarehouse.app_service(app_id,serv_id)
VALUES ('$appid','$servid');";
$query = $this->db->query($queryStr);
return true;
}
}
What you probably want is:
$this->db->insert_id()
The insert ID number when performing database inserts.
More Info: http://ellislab.com/codeigniter/user-guide/database/helpers.html
You need to do it it is very simple
After the query insert
$queryStr = "INSERT INTO appwarehouse.service(service) VALUES ('$service');";
Instead of select use this
return $this->db->insert_id();
Or if you really need to return the object
$queryStr = "SELECT id FROM appwarehouse.application_table WHERE app_name='$app_name' AND visibility = 'VISIBLE';";
$query = $this->db->query($queryStr);
Return id instead of object
return $query->row()->id;
One more thing to note. insert_id is the last inserted id so you dont have to run select query to get id.
Also use row() to select single record. result() selects multiple records so you will get an array. see here.
I'm currently trying to fetch two images location from my database, how do I return both columns and if both empty then echo another image. This is what I've got so far.
How do I return both photo and photo_small so that I may echo them in a php file.
PUBLIC FUNCTION Profile_Pic($uiD) {
$sth = $this->db->prepare("SELECT photo,photo_small FROM users WHERE uiD = :id");
$sth->execute(array(':id' => $uiD));
if ($sth->rowCount() > 0) {
$data = $row['photo'];
return $data;
} else {
$data = './icons/users.png';
return $data;
}
}
PUBLIC FUNCTION Profile_Pic($uiD) {
$sql = "SELECT photo,photo_small FROM users WHERE uiD = ?";
$sth = $this->db->prepare($sql);
$sth->execute(array($uiD));
$data = $sth->fetch();
if (empty($data['photo'])) {
$data['photo'] = './icons/users.png';
}
if (empty($data['photo_small'])) {
$data['photo_small'] = './icons/users.png';
}
return $data;
}
if you want to replace both images if even one is absent
PUBLIC FUNCTION Profile_Pic($uiD) {
$sql = "SELECT photo,photo_small FROM users WHERE uiD = ?";
$sth = $this->db->prepare($sql);
$sth->execute(array($uiD));
$data = $sth->fetch();
if (empty($data['photo']) || empty($data['photo_small'])) {
$data['photo'] = './icons/users.png';
$data['photo_small'] = './icons/users.png';
}
return $data;
}
Just return all of the values you want in an array.
You can ensure that both photo and photo_small are not empty strings or NULL by using empty().
Don't forget to retrieve your row using PDOStatement::fetch().
You should not use rowCount() to determine the number of rows returned in a SELECT statement. According to the documentation for PDOStatement::rowCount():
For most databases, PDOStatement::rowCount() does not return the number of rows affected by a SELECT statement.
Try this:
$row = $sth->fetch(PDO::FETCH_ASSOC);
if ($row && !empty($row['photo']) && !empty($row['photo_small'])) {
$data = array('photo' => $row['photo'], 'photo_small' => $row['photo_small']);
return $data;
} else {
$data = array('photo' => './icons/users.png', 'photo_small' => './icons/users.png');
return $data;
}
Then when you call the function, your returned result can be used like this:
$uiD = 1;
$result = Profile_Pic($uiD);
$photo = $result['photo'];
$photo_small = $result['photo_small'];
Hi I am having an issue getting the view to show any results for the following code.
Controller:
$datedue = 2011-01-27;
$username = $this->tank_auth->get_username();
$sql = "SELECT taskid FROM relationships WHERE username = ? AND datedue = ?";
$tasks = $this->db->query($sql, array($username, $datedue));
$count = 0;
$taskdetails = array();
foreach($tasks->result() as $row)
{
$taskid = $row->taskid;
$subsql = "SELECT * FROM tasks WHERE id = ?";
$taskdetails[$count] = $this->db->query($subsql, array($taskid));
$count++;
}
$data['taskdetails'] = $taskdetails;
$data['total'] = $count;
$this->header();
$this->load->view('dashboard', $data);
View:
<?php
foreach($taskdetails as $entry)
{
foreach($entry->result() as $row)
{
echo $row->name;
}
}
?>
Any help would be nice thanks.
The reason why your view is not displaying something is because you're query is not completely correct.
$datedue = 2011-01-27;
should be enclosed in quotes, since the date is a string.
$datedue = "2011-01-27";
Also, you're not correctly following the concept of MVC. All the database querying and results should occur inside of the Model. And all of the data handling inside the Controller.
The Controller nor the View should handle any of the database connections, that is the duty of the Model. Therefore I would advise to create a Model and put all of the Database querying in one or two functions. The Controller would then call these functions and receive the data back. Then it should manipulate it and pass it to the View in a clean matter (by this I mean, that the View should NEVER call result()
Here is how you're code should be structured:
CONTROLLER
class Tasks extends Controller {
function Tasks ()
{
parent::Controller();
$this->load->model('tasks_model');
$this->load->database();
}
function index()
{
$datedue = "2011-01-27";
$username = $this->tank_auth->get_username();
$tasks = $this->tasks_model->getTasks($username, $datedue);
$count = count($tasks);
$data['taskdetails'] = $tasks;
$data['total'] = $count;
$this->header();
$this->load->view('dashboard', $data);
}
}
MODEL
class Tasks_model extends Model {
function Tasks_model()
{
// Call the Model constructor
parent::Model();
}
function getTasks($username, $datedue) {
$sql = "SELECT taskid FROM relationships WHERE username = ? AND datedue = ?";
$tasks = $this->db->query($sql, array($username, $datedue));
$taskdetails = array();
foreach($tasks->result() as $row){
$taskid = $row->taskid;
array_push( $taskdetails, $this->getTasksDetails( $taskid ) );
}
return $taskdetails;
}
function getTasksDetails($taskid) {
$subsql = "SELECT * FROM tasks WHERE id = ?";
$taskdetails = $this->db->query($subsql, array($taskid));
return $taskdetails->row();
}
}
VIEW:
foreach($taskdetails as $task)
{
echo $task->name;
}
verify your task table contain the name field
view
foreach($taskdetails as $entry)
{
foreach($entry->result() as $row)
{
echo $row[0];
}
}
why you are passing array in this line
$taskdetails[$count] = $this->db->query($subsql, array($taskid));
instead write
$taskdetails[$count] = $this->db->query($subsql, $taskid);