Forums / General Questions / crash with my custom function zen_get_products_model()

crash with my custom function zen_get_products_model()

Started by cadyman on 3 May 2012, 6:52 PM · Views: 1,255

Results 1 to 17 of 17
3 May 2012, 6:52 PM
#1
cadyman avatar

cadyman

New Zenner

Join Date:
Oct 2010
Posts:
27
Plugin Contributions:
0

crash with my custom function zen_get_products_model()

I made a function file and i put in extra_functions in cart side
I made a function make some friendly URL for o particular picture and ..
when i user $model = zen_get_products_model($products_id) ; zen crash.

i make ca copy of the function zen_get_products_model in my file ( i rename it ) and it works
.. i try with try catch .. i don`t see what is the error.

maybe is a bug .. Check .. and see..

ZenCart 1.5

4 May 2012, 2:48 AM
#2
rodg avatar

rodg

Deceased

Join Date:
Jan 2007
Location:
Australia
Posts:
6,263
Plugin Contributions:
4

Re: crash with my custom function zen_get_products_model()

Hmmm, I think this is because the extra_functions folder is for extra functions, and not intended to replace existing functions, so what is happening is that PHP/Zen is trying to load two functions with the same name, and this will always cause a server error.

Solution#1: Comment out (or rename) the original function.
Solution#2: Keep your renamed function and update any code that calls the original function.

Cheers
Rod

NOTE: We are sorry that Rod is no longer with us.
We are grateful for all his contributions to the Zen Cart community.

Ozpost - The Ultimate Shipping module for Australian Merchants. Click these links for its Homepage, or Download from Zen-Cart.com.

4 May 2012, 4:52 PM
#3
cadyman avatar

cadyman

New Zenner

Join Date:
Oct 2010
Posts:
27
Plugin Contributions:
0

Re: crash with my custom function zen_get_products_model()

RodG:

Hmmm, I think this is because the extra_functions folder is for extra functions, and not intended to replace existing functions, so what is happening is that PHP/Zen is trying to load two functions with the same name, and this will always cause a server error.

Solution#1: Comment out (or rename) the original function.
Solution#2: Keep your renamed function and update any code that calls the original function.

Cheers
Rod

The problem is that

    $category = zen_get_products_category_id($id_products); // Works
    $category = zen_get_categories_name($category); // Works
    $name = zen_get_products_name($id_products); // Works
    $model = zen_get_products_model($id_products); //Crash Not Works
   

So is just for this one .. zen_get_products_model($id_products)

5 May 2012, 6:21 AM
#4
rodg avatar

rodg

Deceased

Join Date:
Jan 2007
Location:
Australia
Posts:
6,263
Plugin Contributions:
4

Re: crash with my custom function zen_get_products_model()

Hmm, OK, try this.

First make sure you've got a backup of your custom zen_get_products_model($id_products) function.

Next, replace your custom code with a copy of the original function.

Does it still crash? If so, there is something amiss with your code.
If not, there is something amiss with the function not being able to work from its new location.

This may not provide the solution, but it'll narrow down the possible causes.

Oh. Very important. Do NOT keep backups of files in either of the relevent folders (or if you do, you must ensure they don't have a .php file extension). Such files will also cause a server crash.

Other than this, I'm pretty much out of ideas as to the next course of action. Possibly attach a debugger and step through the code to find the exact point of failure? (I use Netbeans for this).

Cheers
Rod

NOTE: We are sorry that Rod is no longer with us.
We are grateful for all his contributions to the Zen Cart community.

Ozpost - The Ultimate Shipping module for Australian Merchants. Click these links for its Homepage, or Download from Zen-Cart.com.

7 May 2012, 5:56 PM
#5
cadyman avatar

cadyman

New Zenner

Join Date:
Oct 2010
Posts:
27
Plugin Contributions:
0

Re: crash with my custom function zen_get_products_model()

The code is below.

define("IMO_JPG_CHEIE","Products");
if(ENABLE_SSL == 'true'){ define("IMO_MEDIA_URL",HTTPS_SERVER.DIR_WS_HTTPS_CATALOG);}
else{ define("IMO_MEDIA_URL",HTTP_SERVER.DIR_WS_CATALOG);}

function imo_load_image($id_products=0,$alt,$atribut="default") {
    $category = zen_get_products_category_id($id_products);
    $category = zen_get_categories_name($category);
    $name = zen_get_products_name($id_products);
    $model = imo_get_products_model($id_products);
    $link = IMO_Make_fake_jpg($category,$name,$model,$atribut);

   return sprintf('<img alt="%s" src="%s">',$alt,IMO_MEDIA_URL.IMO_JPG_CHEIE.$link);

}


/*
 * Functie responsabila cu conceperea linkului fake pentru poze
 */
function IMO_Make_fake_jpg($Category="",$Nume="",$Model="", $Atribut="",$term="png") {

    $Nume = removeCharacters($Nume);
    $Category = removeCharacters($Category);

    return  "/" . str_replace(" ","-",$Category) . "/".str_replace(" ","-",$Nume)."_".$Atribut."_".$Model.".".$term;
}


function removeCharacters($string) {
    $removeCharacters = array("&","?","'",'"',"#",";");
    foreach($removeCharacters as $val) {
        $string = str_replace($val,"",$string);
    }
    return $string;
}

function imo_get_products_model($id_products){
    global $db;

    $check = $db->Execute("select products_model
                    from " . TABLE_PRODUCTS . "
                    where products_id='" . (int)$id_products . "'");

    return $check->fields['products_model'];
}

It still crash. when i using zen_... instead of imo_....
Because zen-cart don`t come with a easy way to use multiple picture for each attribute i have to port my old implementation from 1.38 to this version and normal to make an upgrade. All the comment are in Romana .. sorry for that.
When i have time i will test to see where is the problem.

Because zen-cart is a modular system it must in future version to have a override system for function like zen_image .. or zen_href ...

8 May 2012, 1:04 PM
#7
rodg avatar

rodg

Deceased

Join Date:
Jan 2007
Location:
Australia
Posts:
6,263
Plugin Contributions:
4

Re: crash with my custom function zen_get_products_model()

CaDyMaN:


function imo_load_image($id_products=0,$alt,$atribut="default") {
$category = zen_get_products_category_id($id_products);
$category = zen_get_categories_name($category);
$name = zen_get_products_name($id_products);
$model = $this->imo_get_products_model($id_products);
$link = IMO_Make_fake_jpg($category,$name,$model,$atribut);

return sprintf('<img alt="%s" src="%s">',$alt,IMO_MEDIA_URL.IMO_JPG_CHEIE.$link);

}

See the text I've added above (in red)? Make the same addition to your code. Let me know how it goes. (You may need to add the same to other parts of your code.. I've only tested for the $model variable since that is the one you've reported a problem with).

BTW, whats the deal with the $category variable? It looks like your setting it to one thing, then immedialty resetting to another.. or was that just to demonstrate that both methods are functional?

Cheers
Rod

NOTE: We are sorry that Rod is no longer with us.
We are grateful for all his contributions to the Zen Cart community.

Ozpost - The Ultimate Shipping module for Australian Merchants. Click these links for its Homepage, or Download from Zen-Cart.com.

8 May 2012, 1:22 PM
#8
rodg avatar

rodg

Deceased

Join Date:
Jan 2007
Location:
Australia
Posts:
6,263
Plugin Contributions:
4

Re: crash with my custom function zen_get_products_model()

Here ya go :)

function IMO_Make_fake_jpg($Category="",$Nume="",$Model="", $Atribut="",$term="png") {

    $Nume = $this->removeCharacters($Nume);
    $Category = $this->removeCharacters($Category);

    return  "/" . str_replace(" ","-",$Category) . "/".str_replace(" ","-",$Nume)."_".$Atribut."_".$Model.".".$term;
}

function imo_load_image($id_products=0,$alt,$atribut="default") {
    $category = zen_get_products_category_id($id_products);
    $category = zen_get_categories_name($category);
    $name = zen_get_products_name($id_products);
    $model = $this->imo_get_products_model($id_products);
    $link = $this->IMO_Make_fake_jpg($category,$name,$model,$atribut);

   return sprintf('<img alt="%s" src="%s">',$alt,IMO_MEDIA_URL.IMO_JPG_CHEIE.$link);

}


function removeCharacters($string) {
    $removeCharacters = array("&","?","'",'"',"#",";");
    foreach($removeCharacters as $val) {
        $string = str_replace($val,"",$string);
    }
    return $string;
}

function imo_get_products_model($id_products){
    global $db;

    $check = $db->Execute("select products_model
                    from " . TABLE_PRODUCTS . "
                    where products_id='" . (int)$id_products . "'");

    return $check->fields['products_model'];
}  

Cheers
Rod

ps. If this works, feel free to buy the zencart dev team (not me) a coffee :)

NOTE: We are sorry that Rod is no longer with us.
We are grateful for all his contributions to the Zen Cart community.

Ozpost - The Ultimate Shipping module for Australian Merchants. Click these links for its Homepage, or Download from Zen-Cart.com.

9 May 2012, 6:31 PM
#9
cadyman avatar

cadyman

New Zenner

Join Date:
Oct 2010
Posts:
27
Plugin Contributions:
0

Re: crash with my custom function zen_get_products_model()

I think you don`t understand

this code return a blank page

function imo_load_image($id_products=0,$alt,$atribut="default") {
    $category = zen_get_products_category_id($id_products);
    $category = zen_get_categories_name($category);
    $name = zen_get_products_name($id_products);
    $model = zen_get_products_model($id_products); // if i comment this will work if not .. error
    return $category."/".$name."/".$model;
}

echo imo_load_image(1); //load image for product with id 1 

 

And from what is know **$this **is used in class methods i don`t use a class ...hmmm

10 May 2012, 2:34 AM
#10
rodg avatar

rodg

Deceased

Join Date:
Jan 2007
Location:
Australia
Posts:
6,263
Plugin Contributions:
4

Re: crash with my custom function zen_get_products_model()

CaDyMaN:

I think you don`t understand

Actually I do (As a result of pasting your code into a suitable part of my test store, getting the same blank page, and then stepping through the code to find the cause and solution.

CaDyMaN:

this code return a blank page

function imo_load_image($id_products=0,$alt,$atribut="default") {
$category = zen_get_products_category_id($id_products);
$category = zen_get_categories_name($category);
$name = zen_get_products_name($id_products);
$model = zen_get_products_model($id_products); // if i comment this will work if not .. error
return $category."/".$name."/".$model;
}

echo imo_load_image(1); //load image for product with id 1


Yes, it will.  I made one small omission to the update code I provided.   Actually it wasn't the code itself, but the way in which you are calling the function (which wasn't in the code that you provided).   

Using the code you've just posted, you just need to change 
  
echo imo_load_image(1); //load image for product with id 1 

To 
echo $this->imo_load_image(1); //load image for product with id 1 

In other words, *all* calls to your imo_xxxxxx  functions need to use the "$this->" syntax. 

> **CaDyMaN:**
>
> And from what is know **$this **is used in class methods

This is true. 

> **CaDyMaN:**
>
> i don`t use a class ...hmmm

That's the problem. You are NOT using class methods, but ZenCart *is*.  :)  

Cheers
Rod

NOTE: We are sorry that Rod is no longer with us.
We are grateful for all his contributions to the Zen Cart community.

Ozpost - The Ultimate Shipping module for Australian Merchants. Click these links for its Homepage, or Download from Zen-Cart.com.

13 May 2012, 9:12 AM
#11
cadyman avatar

cadyman

New Zenner

Join Date:
Oct 2010
Posts:
27
Plugin Contributions:
0

Re: crash with my custom function zen_get_products_model()

RodG:

That's the problem. You are NOT using class methods, but ZenCart is. :)

I`m not a programmer like you RodG but if is right what you say and the function ( my functions ) are included in a class this mean this :

echo "the class is: ". __CLASS__;

should give me the name of class but is not so i`m in class or not. ?

Way i have this problem whit this function zen_get_products_model Hmmm...
It exist only in the admin part ...

Lets see:

The zen_get_products_name are in the function_lookups.php and the and the zen_get_products_model in general.php - admin part.

I included the function in my file

Case close.

P.S. RodG - You should tell me this from the first time because you are a Totally Zenned and i assume you know all the functions in this apps.

13 May 2012, 9:24 AM
#12
schoolboy avatar

schoolboy

Totally Zenned

Join Date:
Jun 2005
Location:
Cumbria, UK
Posts:
10,327
Plugin Contributions:
0

Re: crash with my custom function zen_get_products_model()

CaDyMaN:

I`m not a programmer like you RodG

P.S. RodG - You should tell me this from the first time because you are a Totally Zenned and i assume you know all the functions in this apps.

I think you should be grateful that people are offering FREE ADVICE AND HELP.

I can't belive this jerk !

20 years a Zencart User

13 May 2012, 9:38 AM
#13
cadyman avatar

cadyman

New Zenner

Join Date:
Oct 2010
Posts:
27
Plugin Contributions:
0

Re: crash with my custom function zen_get_products_model()

Lool ... what help i was get it form this. i have to figure my self where is the problem.
Every answer was a confusing one excepts barricades replay.
If you want to help people give the right answer and you will help them if not .. don`t replay. I lose some time with this but never mid ..

This is Zen-Cart ..it was always the same.

Any way if you thing that by give something free you doing me an favor.. you are not. If you dont have community support you products in zero in the free open-source applications.

Soo ...

13 May 2012, 9:43 AM
#14
schoolboy avatar

schoolboy

Totally Zenned

Join Date:
Jun 2005
Location:
Cumbria, UK
Posts:
10,327
Plugin Contributions:
0

Re: crash with my custom function zen_get_products_model()

Go find help somewhere else. Your attitude stinks.

20 years a Zencart User

13 May 2012, 9:44 AM
#15
cadyman avatar

cadyman

New Zenner

Join Date:
Oct 2010
Posts:
27
Plugin Contributions:
0

Re: crash with my custom function zen_get_products_model()

:) ... you are mad because i`m right .. ?

13 May 2012, 10:09 AM
#16
rodg avatar

rodg

Deceased

Join Date:
Jan 2007
Location:
Australia
Posts:
6,263
Plugin Contributions:
4

Re: crash with my custom function zen_get_products_model()

CaDyMaN:

I`m not a programmer like you RodG but if is right what you say and the function ( my functions ) are included in a class this mean this :

echo "the class is: ". CLASS;

> 
> should give me the name of class but is not so i`m in class or not. ?

Waste of my time, but just for grins I added this code to my test bed and it DOES give me the name of the class that I'd inserted the code into (In my case it was 'ozpost'). Needless to say, in your case it would be a different class. 

> **CaDyMaN:**
>
> **Case close.**

Translation:  Thanks Rod, that fixed it, but since I felt you treated my like an idiot I'm now going to try to insult you.                :yes:         

> **CaDyMaN:**
>
> P.S. RodG - You should tell me this from the first time

That wouldn't have been as much fun though.  How else was I supposed to demonstrate my brilliance?   :laugh:

> **CaDyMaN:**
>
> because you are a **Totally Zenned** and i assume you know all the functions in this apps.

All it takes to be 'totally zenned' is having reached 1000 postings.   Some people achieve this by doing nothing more than asking questions, some get there by being an ######, and the rest achieve it by attempting to help others.  

It says *nothing* about how well a person knows zencart,  and for the record, I probably know less than 10% of zencarts functions (if that).  
I DO know my way around an IDE, and am skilled with a dozen or so different programming languages, and with these skills I was able to not only replicate your problem but also ensure that the code I sent you actually worked.       If it didn't work for you then you still have yet another problem (but if that were the case I reckon you would have been back seeking more help rather than the response you gave). 

I will not make the mistake of trying to help you again.

Rgds
Rod

NOTE: We are sorry that Rod is no longer with us.
We are grateful for all his contributions to the Zen Cart community.

Ozpost - The Ultimate Shipping module for Australian Merchants. Click these links for its Homepage, or Download from Zen-Cart.com.

13 May 2012, 10:30 AM
#17
cadyman avatar

cadyman

New Zenner

Join Date:
Oct 2010
Posts:
27
Plugin Contributions:
0

Re: crash with my custom function zen_get_products_model()

:blink: Wow ....:shocking: Nice answer :onfire: i never thing that case close in English for you has this meaning , i will never ask a questing here .. i never receive the correct answer ... any way have a nice day you The RogD - skilled with a dozen or so different programming languages