Welcome Guest, Not a member yet? Register   Sign In
function calls
#1

[eluser]shobekhan[/eluser]
I am new to code igniter. I wrote a controller and did associate a model with it. Whenever I call the function in the controller it is getting called more than 1. I have a controller called Shop in which I have a function product.

when I call it on we with index.php/shop/product it is getting called more than once any idea why
#2

[eluser]gtech[/eluser]
not without seeing your code! Smile
#3

[eluser]shobekhan[/eluser]
<?php

CONTROLLER CLASS CODE

class Shop extends Controller {


var $appurl = "http://www.rosenborgsolutions.com/mobile/index.php";


function Shop()

{
parent::Controller();
$this->load->library('parser');
$this->load->model('Shopmodel');
$this->load->library('session');

}






function product($id = 0, $foldername = '', $prodname = '')

{

//$this->load->library('parser');

//$this->load->model('Shopmodel');

$products = $this->Shopmodel->getProducts($id, 0);



//$folders = $this->Shopmodel->getFolders();

$folders = array();



$session_id = $this->session->userdata('session_id');



$data = array(

'appurl' => $this->appurl,

'page_title' => 'Accessory Highway - ' . $foldername . ' - ' . $prodname,

'product_details' => $products,

'left_product_categories' => $folders,

'session_id' => $session_id

);



$this->parser->parse('shop/productdetail', $data);
}

}

?>
#4

[eluser]shobekhan[/eluser]
<?php
class Shopmodel extends Model {

function Shopmodel()
{
// Call the Model constructor
parent::Model();
}



function getProducts($productid = 0, $folderid = 0)
{
$sql = "SELECT products.id as productid, folders.id as folderid, folders.name as foldername,
manufacturer.id as manufacturerid, manufacturer.name as manufacturername,
fk_modelid as modelid, model.name as modelname, product_number, imageurl,
price, company_price, products.name, products.short_description, products.description, suggested_retail,
compatibility, substitute1, substitute2,
CONCAT( created.firstname, ' ', created.lastname ) createdby, UNIX_TIMESTAMP( products.createdon ) createdon, CONCAT( modified.firstname, ' ', modified.lastname ) modifiedby, UNIX_TIMESTAMP( products.modifiedon ) modifiedon,
products.isactive, products.isdeleted, products.orderid, products.stock
FROM t_products as products
INNER JOIN t_folders folders ON products.fk_folderid = folders.id
LEFT JOIN t_manufacturer manufacturer ON products.fk_manufacturerid = manufacturer.id
LEFT JOIN t_model model ON products.fk_modelid = model.id
LEFT JOIN t_users created ON created.id = products.createdby
LEFT JOIN t_users modified ON modified.id = products.modifiedby
WHERE products.isdeleted =0
";

if($productid!=0)
$sql .= " AND products.id = " . $productid;

if($folderid!=0)
$sql .= " AND folders.id = " . $folderid;

$sql .= " ORDER BY products.orderid, products.name";

$query = $this->db->query($sql);

$data = array();

if ($query->num_rows() > 0)
{
foreach ($query->result_array() as $row)
{
$data[$row['productid']]['productid'] = $row['productid'];
$data[$row['productid']]['folderid'] = $row['folderid'];
$data[$row['productid']]['foldername'] = $row['foldername'];
$data[$row['productid']]['manufacturerid'] = $row['manufacturerid'];
$data[$row['productid']]['manufacturername'] = $row['manufacturername'];
$data[$row['productid']]['modelid'] = $row['modelid'];
$data[$row['productid']]['modelname'] = $row['modelname'];
$data[$row['productid']]['product_number'] = $row['product_number'];
$data[$row['productid']]['imageurl'] = $row['imageurl'];
$data[$row['productid']]['price'] = $row['price'];
$data[$row['productid']]['company_price'] = $row['company_price'];
$data[$row['productid']]['product_name'] = $row['name'];
$data[$row['productid']]['short_description'] = $row['short_description'];
$data[$row['productid']]['description'] = $row['description'];
$data[$row['productid']]['suggested_retail'] = $row['suggested_retail'];
$data[$row['productid']]['compatibility'] = $row['compatibility'];
$data[$row['productid']]['substitute1'] = $row['substitute1'];
$data[$row['productid']]['substitute2'] = $row['substitute2'];
$data[$row['productid']]['createdon'] = $row['createdon'];
$data[$row['productid']]['modifiedon'] = $row['modifiedon'];
$data[$row['productid']]['modifiedby'] = $row['modifiedby'];
$data[$row['productid']]['createdon'] = $row['createdon'];
$data[$row['productid']]['createdby'] = $row['createdby'];
$data[$row['productid']]['isactive'] = $row['isactive'];
$data[$row['productid']]['orderid'] = $row['orderid'];
$data[$row['productid']]['stock'] = $row['stock'];

}
}

return $data;
}
}
?>
#5

[eluser]gtech[/eluser]
I have scanned through the code, and it looks Ok, maybee you need to give a bit more information here

is the controller getting called more than once?
OR
is the model getting called more than once?


what makes you think it is getting called more than once?


A handy tip if you put some debug in your code, you can follow it through and help narrow down the problem... somthing like

Code:
....
  echo "well I am just about to call getProducts";
  $products = $this->Shopmodel->getProducts($id, 0);
....

if you put some echo's in your code it will appear on your browser, and it will show you the code path.. You can then take it out when it works.
#6

[eluser]shobekhan[/eluser]
I have trace code

// TRACE

$trace = "function - product \n" . " Came in product at " . date("Y-m-d H:iConfused") . "\n\n";

$filename = "trace/trace.txt";



// Let's make sure the file exists and is writable first.

if (is_writable($filename)) {

if (!$handle = fopen($filename, 'a')Wink {

echo "Cannot open file ($filename)";

exit;

}



// Write $somecontent to our opened file.



if (fwrite($handle, $trace) === FALSE) {

echo "Cannot write to file ($filename)";

exit;

}



fclose($handle);

}

// TRACE


when I comment this line

$this->parser->parse('shop/productdetail', $data);

it comes only once in the function

otherwise it comes 4 times




Theme © iAndrew 2016 - Forum software by © MyBB