Jump to content

John_A

Members
  • Posts

    70
  • Joined

  • Last visited

Profile Information

  • Gender
    Not Telling

John_A's Achievements

Member

Member (2/5)

0

Reputation

  1. Thanks both for your input. As I said, it was a bespoke shopping cart script, that I inherited, which already had a Google feed but the sizes were in a comma separated list for each product (used to be OK with Google) but now needs to be one size per product. I wrote neither the initial script nor the google feed add-on. I've been tasked with this, and not a lot of time to do it. I'm already working on a local dev server, not the live site. TBH it would have taken me longer to do everything you suggest to ask the question "properly" than it did for me to fix it myself. I didn't explode the commas because the CSV list is built from another query on the database for the prodID, all individual rows as product options for size, so didn't see the point in building it again then exploding it to get back to where I started. It's a CSV in the original as that used to be OK with Google. I'm pretty sure I gave as much information as was needed to answer / help if someone was inclined to. In fact I posted a duplicate elsewhere, and it was answered with a solution - too late for me but it looked like it would have worked. Hey ho. Lesson learnt.
  2. OK, thanks for your help so far. Partially thanks to your input I changed my approach completely, now manipulating the feed as I create it line by line later in the same file, rather than messing with the array beforehand, and have it working just as I need. Hopefully, one day, I'll be as awesome as you and not need to ask for help. Stay safe, and thanks again!
  3. Going through the array for sizes was messing with the feed output later on, so I changed it to a foreach instead. The plan is for the first foreach loop to manipulate the product array as necessary, then once done a separate foreach going through the now correct array to output the feed. Here's the one I need to fix: - foreach ($result as $key => $thisProduct) { echo $thisProduct["prodID"] . '<br/>'; $thisproductSizes = ""; $sizesResult = $dbA->query("SELECT $ExtraFieldsValues.content From $ExtraFields, $ExtraFieldsValues Where $ExtraFields.name = 'size' and $ExtraFields.extraFieldID = $ExtraFieldsValues.extraFieldID and $ExtraFieldsValues.productID =".$thisProduct["prodID"]." Order By $ExtraFieldsValues.position"); $sizesCount = $dbA->count($sizesResult); for ($sf2 = 0; $sf2 < $sizesCount; $sf2++) { $sizesRecord = $dbA->fetch($sizesResult); $thisproductSizes .= ",".$sizesRecord["content"]; } if (strlen($thisproductSizes) > 0){ // there's at least one size so needs to be dealt with $thisproductSizes = substr($thisproductSizes,1); // do stuff here to make one product into many // each with item_group_id = prodID, // and id = prodID-xx (size), with only one size } else { $thisproductSizes = ""; } echo $thisproductSizes . '<br />'; } But, thinking about it, if there's only 1 size that's fine, it's if there's 2 or more that there'll be an issue...
  4. There probably is but it's for an automatic google product feed that will run once every month so efficiency isn't really a concern. It's part of a bespoke ecommerce script I inherited and can't really change much on, the table structures are what they are and can't be changed at this stage. So what I have is the $products array which includes most of the fields I need straight out of it, but the sizes needs to be a 2nd query within that. The $product array should be updated on-the-fly with duplicates where necessary with only one size in each, and the id and item group id updated too.
  5. It turns out I don't actually have the sizes in the $product array as above, they're a query within the loop that builds the feed in it's current state. $result is the query to fetch all products... $count = $dbA->count($result); for ($f = 0; $f < $count; $f++) { $record = $dbA->fetch($result); $productSizes = ""; $result2 = $dbA->query("SELECT $ExtraFieldsValues.content From $ExtraFields, $ExtraFieldsValues Where $ExtraFields.name = 'size' and $ExtraFields.extraFieldID = $ExtraFieldsValues.extraFieldID and $ExtraFieldsValues.prodID =".$record["prodID"]." Order By $ExtraFieldsValues.position"); $count2 = $dbA->count($result2); for ($f2 = 0; $f2 < $count2; $f2++) { $record2 = $dbA->fetch($result2); $productSizes .= ",".$record2["content"]; } if (strlen($productSizes) > 0){ $productSizes = substr($productSizes,1); } echo $productSizes . '<br/>'; } The $products array includes the other fields I referenced above along with quite a few others: - but does NOT include the item group id field, which should be added blank or populated as above for each product. I'm not sure if this makes it easier, or harder?
  6. I have an existing PHP array ($products) with various fields in it.... id / title / description / link / image link etc. etc. as per requirements for a Google Products feed (https://support.google.com/merchants/answer/7052112). My issue is that, for clothing, Google want a separate record for each size available. My array includes the "size" field as a comma separated list e.g. 6,8,10,12,14 or XS,S,M,L,XL etc. etc. So before creating the feed (a text file, which is currently outputting fine except for the size field issue) I need to duplicate each id where there's more than 1 size in that field, for each size, and then manipulate the fields a little so that (ignoring all the duplicated fields that would remain unchanged) instead of the single record : - id size item group id 52 6,8,10,12,14,16,18,20,22,24,26 I'd have 11 records, item group id would be what the id is, the size appended to the id, and the size field only one (in sequence of those from the original), so: - id size item group id 52-6 6 52 52-8 8 52 . .. ... 52-24 24 52 52-26 26 52 That's just one product....there are quite a lot, each with multiple sizes, but all in the same format within the $products array. As always, any help / pointers / solutions much appreciated!
  7. Thanks again. It's part of a URL building function...to replace any non-alphanumeric characters with spaces, which are in turn replaced with dashes or underscores in the next part...
  8. Thanks for your reply Jacques1. I did check the manual before posting, and it says to use preg_replace_callback() instead. But if, as you say, the modifier in my code does nothing anyway, is my suggestion (i.e. to simply remove it) not the best approach in this case?
  9. I have this snippet which is giving a deprecated warning re the "e" pattern modifier in PHP5.5: - $result = preg_replace("/\[^a-zA-Z0-9]/e"," ",$result);I'm not even sure what the "e" modifier does, as it's not my code...maybe something to do with removing duplicates? Anyway, what's the best way to replace this so that it doesn't give the warning in PHP5.5, and will work in PHP7.0.0? I've got a feeling this might work :- $result= preg_replace("/\[^a-zA-Z0-9]/"," ",$result);but feel I might have over-simplified it, and it maybe doesn't quite do the same thing? Any help much appreciated!
  10. Thanks very much for everyone's input. I think I've got what I need with this: - <?php $targetsection=144; $fullSectionsArray = array(1,1505,1507,1509,1510,1511); $editedSectionsArray = array(); $invertedSectionsArray = array(); $pathsArray = array(); foreach ($fullSectionsArray as &$theSection) { $thispath = get_my_path($theSection); $pathsArray[$theSection] = $thispath; } foreach( $pathsArray as $mKey => &$mVal ) { if( in_array($targetsection, $mVal ) ) { $editedSectionsArray[] = $mKey; } else { $invertedSectionsArray[] = $mKey; } } // $node is the name of the node we want the path of function get_my_path($node) { global $tableSections; // look up the parent of this node $query = "SELECT parent FROM $tableSections WHERE sectionID = " . $node; $result = mysql_query($query); $record = mysql_fetch_assoc($result); // save the path in this array $myPath = array(); // only continue if this $node isn't the root node (that's the node with no parent) if ($record['parent'] != '' && $record['parent'] != '0' && $record['parent'] != '1') { // the last part of the path to $node, is the name // of the parent of $node $myPath[] = $record['parent']; // we should add the path to the parent of this node to the path $myPath = array_merge(get_my_path($record['parent']), $myPath); } // return the result return $myPath; } ?> vinny42 - while I don't doubt your code works and is perhaps the better approach, I start with an array in, and need two arrays out the other end (the template system of the 3rd party application in use expects arrays) and, although I said earlier I don't care about the intermediate path nodes, at some point I might like to list the full paths of all filtered descendants rather than just the immediate parent sections and so the get_my_path function is handy for this. Barand - yours looks a lot more like what I was expecting. I haven't tested it but don't see why it wouldn't work. I may well end up switching to this if I decide I really don't care about full paths! Again, thanks to all for their input
  11. Thanks, but that's as far from simple as it's possible to get (for me, at least!). None of that looks remotely familiar to me, and so I'd rather not use it in case something goes wrong in future. Also, I tried dropping it in on my test server and got 500 errors / blank pages and can't really debug it myself (see previous sentence!). Sorry, I really do appreciate your help, but I think I'd rather a "simple" solution I can drop in to #21
  12. I posted all the code (at that time, - db stuff) in post #20? But since then I've changed my approach a little and feel I'm much closer to a solution (see above).
  13. Ok, that didn't work for a number of reasons....while it seemed to on my test server, it was obviously much more tolerant of my rubbish code as the production server spat it out for numerous reasons, primarily the wrong number of arguments passed to the function in one instance, and the function didn't return an array as expected (I forgot I changed that to true / false). The biggest issue was that the hierarchy seemed to fail after only going 2 generations up....? Anyway, I decided to approach it slightly differently and go back to the function returning a path, then I'll compare the arrays and do what needs doing there...trouble is I've now hit a brick wall as multi-dimensional arrays aren't really a strong point of mine.... What I have: - <?php $targetsection=144; $fullSectionsArray = array(1,1505,1507,1509,1510,1511); $editedSectionsArray = array(); $invertedSectionsArray = array(); $pathsArray = array(); foreach ($fullSectionsArray as &$theSection) { $thispath = get_my_path($theSection); $pathsArray[$theSection] = $thispath; } print_r($pathsArray); /////////////////////////////////////////// // what to do here to populate the other // // 2 arrays - $editedSectionsArray and // // $invertedSectionsArray ??? // /////////////////////////////////////////// echo '<strong>Full Array:</strong><br/>'; echo '<pre>'; print_r($fullSectionsArray); echo '</pre>'; echo '<strong>Edited Array:</strong><br/>'; echo '<pre>'; print_r($editedSectionsArray); echo '</pre>'; echo '<strong>Inverted Array:</strong><br/>'; echo '<pre>'; print_r($invertedSectionsArray); echo '</pre>'; // $node is the name of the node we want the path of function get_my_path($node) { global $tableSections; // look up the parent of this node $query = "SELECT parent FROM $tableSections WHERE sectionID = " . $node; $result = mysql_query($query); $record = mysql_fetch_assoc($result); // save the path in this array $myPath = array(); // only continue if this $node isn't the root node (that's the node with no parent) if ($record['parent'] != '' && $record['parent'] != '0' && $record['parent'] != '1') { // the last part of the path to $node, is the name // of the parent of $node $myPath[] = $record['parent']; // we should add the path to the parent of this node to the path $myPath = array_merge(get_my_path($record['parent']), $myPath); } // return the result return $myPath; } ?> which produces a $pathsArray like: - Array ( [1] => Array ( ) [1505] => Array ( [0] => 1586 [1] => 28 [2] => 222 [3] => 406 ) [1507] => Array ( [0] => 1586 [1] => 28 [2] => 222 [3] => 406 ) [1509] => Array ( [0] => 28 [1] => 222 [2] => 406 ) [1510] => Array ( [0] => 1586 [1] => 28 [2] => 222 [3] => 406 ) [1511] => Array ( [0] => 1586 [1] => 28 [2] => 222 [3] => 406 ) ) All I need to do now (!) is: - add to the $editedSectionsArray, any of $pathsArray in which any value in the sub-array (if there is one) = the $targetsection (e.g. 1505, 1507, 1510 & 1511 in the above example) - taking these key numbers and adding them as values to $editedSectionsArray. add to the $invertedSectionsArray any $pathsArray which either have no sub-arrays, or where the sub-array does not contain $targetsection (e.g. 1 & 1509 in the above example) - taking the key numers and adding them as values to $invertedSectionsArray. Any pointers would be, as always, greatly appreciated.
  14. For the record, I wasn't expecting anyone to do this for me...as I said a nod in the right direction is all I wanted as I didn't want to waste time going down the wrong route. Thanks to all for their input, here's what I have which seems to work Any comments welcome... <?php $targetsection=144; $editedSectionsArray = array(); $invertedSectionsArray = array(); $fullSectionsArray = array(2,3,5,8,13,21,34,55,89); foreach ($fullSectionsArray as &$theSection) { if(get_my_path($theSection,$targetsection)==true && $theSection != $targetsection) { // target section features nowhere in ancestry array_push($invertedSectionsArray,$theSection); } else { // it is there, so add to this array instead array_push($editedSectionsArray,$theSection); } } echo '<strong>Full Array:</strong><br/>'; echo '<pre>'; print_r($fullSectionsArray); echo '</pre>'; echo '<strong>Edited Array:</strong><br/>'; echo '<pre>'; print_r($editedSectionsArray); echo '</pre>'; echo '<strong>Inverted Array:</strong><br/>'; echo '<pre>'; print_r($invertedSectionsArray); echo '</pre>'; // $node is the name of the node we want the path of function get_my_path($node,$targetsection) { global $tableSections; // look up the parent of this node $query = "SELECT parent FROM $tableSections WHERE sectionID = " . $node; $result = mysql_query($query); $record = mysql_fetch_assoc($result); // save the path in this array $myPath = array(); array_push($myPath,$node); // only continue if this $node isn't the root node (that's the node with no parent) if ($record['parent'] != '' && $record['parent'] != '0') { // the last part of the path to $node, is the name // of the parent of $node myPath[] = $record['parent']; if ($record['parent'] == $targetsection) $excludeThis = 1; // we should add the path to the parent of this node to the path $myPath = array_merge(get_my_path($record['parent']), $myPath); } // return the result if (isset($excludeThis)) { return false; } else { return true; } } ?>
  15. Do you mean storing this descendants array somewhere and comparing it whenever I need to? The heirarchy is dynamic and subject to change, quite how often I'm not sure, and so the comparison really needs to be done at that moment...or did you mean something else?
×
×
  • 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.