scottybwoy Posted March 26, 2009 Share Posted March 26, 2009 I'm using a script written by someone else that uses a caching key to determine whether to run part of a script again. It takes the original value that is to be processed, then stores it to determine if it needs to process that same original value again. Here's the code striped to the relevant parts : foreach($categories as $c) { $c = trim($c); $subcats = explode($config['csv']['categories_subcat_delimiter'], $c); foreach($subcats as $k => $v) { if (!empty($v)) { $subcats[$k] = trim($v); } } $key = md5(strtolower(implode($config['csv']['categories_subcat_delimiter'], $subcats))); if (!isset($categories_cache[$key])) { $c_parent_id = 0; $category_language = 1 $c_error = false; foreach ($subcats as $sc) { $subcat_query = tep_db_query("SELECT c.categories_id FROM " . TABLE_CATEGORIES . " c INNER JOIN " . TABLE_CATEGORIES_DESCRIPTION . " cd ON c.categories_id = cd.categories_id WHERE language_id = '$category_language' AND categories_name = '$sc' AND parent_id = '$c_parent_id'"); if ($subcat_query != null && tep_db_num_rows($subcat_query) == 1) { $fetched = tep_db_fetch_array($subcat_query); $c_parent_id = (int)$fetched['categories_id']; } elseif ((bool)$config['settings']['auto_add_categories']) { $m_fields = array(); $m_values = array(); $m_fields[] = "`parent_id`"; $m_values[] = "'" . (int)$c_parent_id . "'"; $m_fields[] = "`date_added`"; $m_values[] = "'".strftime('%Y-%m-%d %H:%M:%S',time())."'"; $new_category_query = 'INSERT INTO ' . TABLE_CATEGORIES . ' ('.implode(',',$m_fields).') VALUES('.implode(',',$m_values).')'; if(!tep_db_query($new_category_query)) { csv_import_message(sprintf(CSV_CATEGORY_INSERT_ERROR, $i, $sc), 'error'); $skipped[$i] = $data[$i]; continue; } $c_parent_id = tep_db_insert_id(); foreach($languages_ids as $lang => $lang_id) { $m_fields = array( '`categories_id`', '`language_id`', '`categories_name`', ); $m_values = array( "'$c_parent_id'", "'$lang_id'", "'$sc'", ); $new_category_language_query = tep_db_query('INSERT INTO ' . TABLE_CATEGORIES_DESCRIPTION . ' ('.implode(',',$m_fields).') VALUES('.implode(',',$m_values).')'); } } else { $c_error = true; break; } } if($c_error) { continue; } $categories_cache[$key] = $c_parent_id; } $cId[] = $categories_cache[$key]; } } Now I suppose my real question is, whether the md5 hash is relevant for the $categories_cache[$key]; Two of the values I have for $c in the foreach loop are "Processors|AMD|AM2" and "Processors|AMD|AM2+" When this is md5 hashed it returns the wrong parent_id and so alters the expected functionality of the rest of the script. Could this be down to md5 skipping the "+" symbol at the end? Can I just use "Processors|AMD|AM2+" as the $key value? Quote Link to comment https://forums.phpfreaks.com/topic/151229-solved-md5-and-caching-keys/ Share on other sites More sharing options...
MadTechie Posted March 26, 2009 Share Posted March 26, 2009 I guess you could replace the key but MD5ing with a plus would be different from one without the plus! IE 'Processors|AMD|AM2' Key would be '45d5f03ba9a7bcce8adbb8048ff1bb06' 'Processors|AMD|AM2+' Key would be '225f8e1ddbd44df7fc0b5fa8aaacac76' Quote Link to comment https://forums.phpfreaks.com/topic/151229-solved-md5-and-caching-keys/#findComment-794485 Share on other sites More sharing options...
redarrow Posted March 26, 2009 Share Posted March 26, 2009 works for me,so it not md5 is it? <?php $name=md5("redarrow+"); if(md5('redarrow+')==$name){ echo match; }else{ echo no_match; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/151229-solved-md5-and-caching-keys/#findComment-794500 Share on other sites More sharing options...
scottybwoy Posted March 26, 2009 Author Share Posted March 26, 2009 OK, So my problem doesn't lie there. I have established it by trying it out too. But I have got closer. The caching code only works with consecutive categories in a row. i.e. "Processors", "AMD", "AM2" etc. I've also noticed that the values of $c already have this broken down, so the values of $c are just : "Processors" or "AMD" or "AM2". But when another sub category comes into play, i.e. "AM2+" it has no way of knowing what it's parent should be, in this case : "AMD". Can anyone look over the code in the first post, to come up with any ideas of how to retain that information in the cache so that it the id can be either found or not found for the rest of the script? Otherwise I think I'll have to take the cache out, which will slow things down, especially as there will be lots of products. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/151229-solved-md5-and-caching-keys/#findComment-794517 Share on other sites More sharing options...
MadTechie Posted March 26, 2009 Share Posted March 26, 2009 If items with a plus are the same as ones without a plud then can't you jusr remove it ? $subcats = str_replace("+","",explode($config['csv']['categories_subcat_delimiter'], $c)); Quote Link to comment https://forums.phpfreaks.com/topic/151229-solved-md5-and-caching-keys/#findComment-794580 Share on other sites More sharing options...
scottybwoy Posted March 27, 2009 Author Share Posted March 27, 2009 No, the + makes it inherently different. That's the point. I don't think this is now the problem, just part of it. Quote Link to comment https://forums.phpfreaks.com/topic/151229-solved-md5-and-caching-keys/#findComment-795022 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.