seojunkie Posted September 22, 2006 Share Posted September 22, 2006 My database has a table of products with similar names except for the numeric end. So the products might look like:prod1-30prod1-36prod2-24prod2-30I need to have all of the products have all of the proper endings. If one doesn't exist, I need to add it to the database so that it ends up looking like this:prod1-24prod1-30prod1-36prod2-24prod2-30prod2-36To do this, I came up with this code to find the missing products:[code]$products_query = mysql_query("SELECT ..." $type = array(); $type[] = "-20"; $type[] = "-20"; $type[] = "-36"; for ($i = 0; $i < count($type); ++$i) { while ($products = mysql_fetch_array($products_query)) { if (strpos($products['products_model'], $type[$i]) === FALSE) { echo 'Add '.$type[$i] . ' to ' .$products['products_model'].'<br>'; } } } [/code]This will work on the first pass but when it goes back to check the next "type," the code gets skipped. I thought it has something to do with the array pointer but using reset($products_query) results in an invalid argument error.Would someone please point out my mistake? Is there a better way to code what I need to do? Quote Link to comment https://forums.phpfreaks.com/topic/21660-skips-array-on-second-pass/ Share on other sites More sharing options...
steveclondon Posted September 22, 2006 Share Posted September 22, 2006 Products_query is your result from the query not an array. $products is the array. Quote Link to comment https://forums.phpfreaks.com/topic/21660-skips-array-on-second-pass/#findComment-96702 Share on other sites More sharing options...
seojunkie Posted September 22, 2006 Author Share Posted September 22, 2006 I tried adding reset($products); at the end of the for loop and the message it gives is[quote]Warning: reset(): Passed variable is not an array or object in[/quote] Quote Link to comment https://forums.phpfreaks.com/topic/21660-skips-array-on-second-pass/#findComment-96708 Share on other sites More sharing options...
Daniel0 Posted September 22, 2006 Share Posted September 22, 2006 Try this: [code]$products_query = mysql_query("SELECT ...");$type = array(); $type[] = "-20"; $type[] = "-20"; $type[] = "-36"; for($i = 0; $i < count($type)-1; $i++) { while($products = mysql_fetch_array($products_query)) { if(strpos($products['products_model'], $type[$i]) === FALSE) { echo "Add {$type[$i]} to {$products['products_model']}<br />"; } }}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/21660-skips-array-on-second-pass/#findComment-96731 Share on other sites More sharing options...
akitchin Posted September 22, 2006 Share Posted September 22, 2006 if i may make a suggestion (and this isn't going to fix whatever issue you're having, just a thought), foreach() is more handy for going through arrays, especially where non-numerical keys are concerned. Quote Link to comment https://forums.phpfreaks.com/topic/21660-skips-array-on-second-pass/#findComment-96736 Share on other sites More sharing options...
seojunkie Posted September 22, 2006 Author Share Posted September 22, 2006 I tried making the change[code]for($i = 0; $i < count($type)-1; $i++)[/code]but it didn't fix it. Unless I'm missing something, that change would only limit how many types are checked. But since it never reaches the end anyway, it has no effect on the code. Quote Link to comment https://forums.phpfreaks.com/topic/21660-skips-array-on-second-pass/#findComment-96747 Share on other sites More sharing options...
akitchin Posted September 22, 2006 Share Posted September 22, 2006 alright, here's a better idea of how to go about this. first off, grab the numeric ends for each product like so:[code]SELECT SUBSTRING_INDEX(products_model, '-', -1) AS type FROM products BLAH[/code]use that query, and load it into an array like so:[code]<?php$current_types = array();while ($blah = mysql_fetch_assoc($resource)){ $current_types[] = $blah['type'];}?>[/code]$current_types will now contain all the types pulled (may want to add a where clause to make sure it only pulls the types for the products you want). run an intersection to pull the values in the current types that are NOT in the types you've got listed like so:[code]<?php$types = array('-20', '-36');$types_missing_from_list = array_intersect($current_types, $types);foreach ($types_missing_from_list AS $type_number){ echo "<br>Add $type_number to product something or other.";}?>[/code]keep in mind this will only work for one product name. a few changes will allow you to run this once such that it tells you for every product name you've got, for now try to digest this. Quote Link to comment https://forums.phpfreaks.com/topic/21660-skips-array-on-second-pass/#findComment-96751 Share on other sites More sharing options...
seojunkie Posted September 22, 2006 Author Share Posted September 22, 2006 No, that won't work. The types will vary with products. So product A might have one of the types, product B two of the types and so on. Besides, there would be no way to reference back to the product name using that method.I did try replacing my while loop with a foreach statement but it didn't work right. All of the products were not displayed. But that may have been due to me using the code incorrectly. Here is what I tried:[code]foreach (($arr = tep_db_fetch_array($products_query)) as $value){ if (strpos($value , $type[$i]) === FALSE) { echo 'Add '.$type[$i] . ' to ' .$value .'<br>'; } }[/code] Quote Link to comment https://forums.phpfreaks.com/topic/21660-skips-array-on-second-pass/#findComment-96842 Share on other sites More sharing options...
sasa Posted September 22, 2006 Share Posted September 22, 2006 try[code].... for ($i = 0; $i < count($type); ++$i) { mysql_data_seek($products_query,0); while ($products = mysql_fetch_array($products_query)) ....[/code] Quote Link to comment https://forums.phpfreaks.com/topic/21660-skips-array-on-second-pass/#findComment-96881 Share on other sites More sharing options...
seojunkie Posted September 22, 2006 Author Share Posted September 22, 2006 Great. That seems to have done it. I appreciate everyone's help. Quote Link to comment https://forums.phpfreaks.com/topic/21660-skips-array-on-second-pass/#findComment-96893 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.