Jump to content

Archived

This topic is now archived and is closed to further replies.

seojunkie

Skips array on second pass

Recommended Posts

My database has a table of products with similar names except for the numeric end. So the products might look like:

prod1-30
prod1-36
prod2-24
prod2-30

I 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-24
prod1-30
prod1-36
prod2-24
prod2-30
prod2-36

To 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?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
try[code]
....
    for ($i = 0; $i < count($type); ++$i)
    { mysql_data_seek($products_query,0);
      while ($products = mysql_fetch_array($products_query)) 
....[/code]

Share this post


Link to post
Share on other sites

×

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.