Let's break it down:
Code:
require(DIR_WS_MODULES . zen_get_module_directory('require_languages.php'));
The above is used within page modules in order to load language files relevant to the page being referenced at runtime ... ie: the result of $_GET['main_page']
Code:
$breadcrumb->add(NAVBAR_TITLE);
This adds the current page reference to the breadcrumb bar, using NAVBAR_TITLE (from the lang file loaded earlier) as the text name on the link.
Code:
$products_new_array = array();
this line is redundant and not used here
--------------------------
All the above are not needed if this is to be run in a function
--------------------------
Code:
$products_new_query_raw = "SELECT products_id
FROM " . TABLE_PRODUCTS . " p
WHERE products_status = 1 ";
This is a basic query to retrieve products_id for all "active" products from the database
Code:
$products_new_query_raw = $db->bindVars($products_new_query_raw, ':languageID', $_SESSION['languages_id'], 'integer');
This line is irrelevant, because you're not referencing languages in your query.
It would normally be used to sanitize the SQL query and ensure that the value passed for language id was truly an integer before getting to the database. Basically, binding data-types in the query, enforcing proper use, largely to prevent injection risks
Code:
$check_products_all = $db->Execute($products_new_query_raw);
This executes the query defined earlier, with all results ending up in the $check_products_all object
Code:
$array_ids = array();
initialize array which will hold the requested product ids
Code:
while (!$check_products_all->EOF)
{
begin a routine to loop through all retrieved records
Code:
array_push($array_ids,$check_products_all->fields['products_id']);
store retrieved records into the $array_ids array.
A faster approach would be this simpler line:
Code:
$array_ids[] = $check_products_all->fields['products_id'];
Code:
$check_products_all->MoveNext();
}
MoveNext continues the while loop ... which ends when the end of the retrieved records is reached.
If you wanted to convert this all to a function, you'd need to reference the $db object as a global -- otherwise your database connection details are unreachable within the local scope of your function.
ie:
Code:
function my_get_all_product_ids() {
global $db;
$products_new_query_raw = "SELECT products_id
FROM " . TABLE_PRODUCTS . " p
WHERE products_status = 1 ";
$check_products_all = $db->Execute($products_new_query_raw);
$array_ids = array();
while (!$check_products_all->EOF)
{
$array_ids[] = $check_products_all->fields['products_id'];
$check_products_all->MoveNext();
}
return $array_ids;
}
Proof of concept:
- upload the above code by inserting it in a file in the extra_functions folder
- run this from somewhere on your site:
Code:
$var1 = my_get_all_product_ids();
echo '<pre>';
print_r($var1);
echo '</pre>';
Bookmarks