Jump to content

Can't get data from my variable ..


zoobzen

Recommended Posts

Hello,

 

I am a real noob here and I am pulling my hair out over this problem ..

 

I have a variable defined and I am only able to get a 1 or 0 value when calling it .. it is only one line, and interestingly, other just like it are working fine. Here is the line ..

 

$this->mProdCat = (int)Catalog::GetCategoryByProduct($this->_mProductId);

 

it is inside a function that is calling to the following ..

 

// Retrieves category id from product id

  public static function GetCategoryByProduct($productId)

  {

    // Build SQL query

    $sql = 'CALL catalog_get_category_by_product(:product_id)';

 

    // Build the parameters array

    $params = array (':product_id' => $productId);

 

// Execute the query and return the results

    return DatabaseHandler::GetAll($sql, $params);

  }

 

Finally, this is how I am using it ..

 

$this->mProductSize = Catalog::GetProdsByCategory($this->mProdCat);

 

Everything seems to be doing what it should except since I get this value of 1 (with varified data), I don't populate the list with the proper data .. This is probably easy for the experts, but me .. well ... Thanks for any help offered ..

Link to comment
Share on other sites

Can you help us to know, Which one is causing issue

$this->mProdCat = (int)Catalog::GetCategoryByProduct($this->_mProductId); // you say this is main issue

OR

$this->mProductSize = Catalog::GetProdsByCategory($this->mProdCat); //you say you are using this

 

Both are different functions ? I got confused sorry.

 

Secondly, In first function variable name is '$this->_mProductId' and in another '$this->mProdCat'. Please make sure it is not like this - '$this->_mProdCat'.

 

Do let us know further to help you.

 

Link to comment
Share on other sites

This is the varibale that is giving the problem..

 

$this->mProdCat = (int)Catalog::GetCategoryByProduct($this->_mProductId);

 

I am using the return of this as the variable input to the following ..

 

$this->mProductSize = Catalog::GetProdsByCategory($this->mProdCat);

 

I have read (and reread) my typing and verified my typing. and I am still only getting as return of the value 1 from    this->ProdCat  I am stumped..

 

Thanks for the interest ..

 

Frank

Link to comment
Share on other sites

Remove the (int) from this line:

$this->mProdCat = (int)Catalog::GetCategoryByProduct($this->_mProductId);

 

Regardless of what Catalog::GetCategoryByProduct() returns, you're forcing it to convert to an int.  If it returns nothing, it'll give a 0 otherwise it'll give a 1.  However, the function looks like it'd return an array so you may need to do some extra processing to get the actual product category you want.

Link to comment
Share on other sites

I have tried casting the result as an int after this statement (becasue this->mProductSize requires an int) but same result. I am a real noob and I suspect that I may need additional processing since mProdCat is a variable (should only contain one number). I do not need to do anything with mProductSize though and it returns a much larger array.

 

Any suggestions on how I may "process further"?  I have tried giving mProductSize things like mProdCat[0] and mprodCat['category_id'] to no avail.

 

Thanks .. Frank

Link to comment
Share on other sites

Something else interesting here, when I run the following code:

 

$this->mProdCat = Catalog::GetCategoryByProduct($this->_mProductId);

if (array_key_exists('category_id', $this->mProdCat))

        $this->$mProdSelect =

          (int)$this->mProdCat['category_id'];

      else

        trigger_error('CategoryId not set');

 

$this->mProdCat  reads back as the term Array in my browser .. when I run the query manually:

 

SELECT category_id

  FROM product_category

  WHERE product_id = inProductId;

 

I get the expected result under category_id

 

Can anyone offer anything?

 

Thanks ..

 

Frank

 

Link to comment
Share on other sites

I solved my problem by using a table join and sub query in mySQL in order to eliminate the mProdCat variable that was giving me trouble. It is more efficient anyway, but I sure wonder what I was doing wrong with this .. Thanks anyway ..

 

Frank

Link to comment
Share on other sites

Since I don't know the intracacies of your exact database handler I can't be sure but regardless of the actual SQL used, DatabaseHandler::GetAll($sql, $params); sounds like it should return more than one result (say you wanted all products in a category).  In this case, any result given by the sql will probably use something like mysql_fetch_array (for MySQL databases) which returns an array to allow more than one row to be returned.

 

With the code above, I expect you would have actually been looking for was $this->_mProdCat[0]['category_id'] (note the [0] because you want to access the first (only) row returned.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.