This topic is locked

Item ID

5/26/2025 5:01:54 PM
PHPRunner General questions
C
Cornelius Smit author

Good day, Runners. How can I set a default "Item ID" for a snippet, as I need to use the snippet on every page and do not want to rename it every time? Thank you in Advance

ChatGPT 5/26/2025

Certainly! In PHPRunner, if you want to set a default "Item ID" for a snippet that you plan to use across multiple pages, you can achieve this by modifying the snippet to accept a parameter. Here’s how you can do it:

  1. Modify the Snippet:

    • Open your snippet in the PHPRunner editor.
    • At the top of your snippet code, you can define a default value for "Item ID" using a variable. For example:
      $defaultItemId = 1; // Set this to your desired default Item ID


  2. Using the Parameter:

    • When you write your logic that uses "Item ID", check if it's set; if not, use the default value. For instance:
      $itemId = isset($itemId) ? $itemId : $defaultItemId;


  3. Calling the Snippet:

    • When you include the snippet in your pages, you can pass the "Item ID" if needed. If you want to use it with the default value, simply call it without passing any parameters:
      include('your_snippet.php');
    • If you want to override the default value, you can pass the "Item ID" like this:
      $itemId = 5; // Example of overriding the default
      include('your_snippet.php');



  4. Save and Test:

    • Save your changes and test the snippet on different pages to ensure it uses the default "Item ID" correctly when no value is provided.



By following these steps, you can set a default "Item ID" for your snippet in PHPRunner, making it easier to use across multiple pages without renaming or manually setting the ID each time.

Sergey Kornilov admin 5/26/2025

What kind of problem you are trying to solve by assigning the same specific ItemID to all code snippets on different pages?

C
Chris Whitehead 5/28/2025

As Sergey has already asked, it would be helpful to know a bit more about why you want to do this, why use this instead of a variable or will the value change each time the application is loaded..

Anyway, here's my two bobs worth. Create a function which is set in the "after application initialised" event in the events section and have a default as that.

function myDefaultValue( $default = 10 ) {
// some code here
return $default;
}

// call it anywhere by
$myvalue = myDefaultValue();

// or you can over ride it by
$myvalue = myDefaultValue( 42 );

// if you never need to over ride the value then why not use a constant set in the "after application initialised" event?
define( 'MY_DEFAULT_VALUE',62 );

//Then call by
$myvalue = MY_DEFAULT_VALUE;