Jump to content


Photo

Skips array on second pass


  • Please log in to reply
9 replies to this topic

#1 seojunkie

seojunkie
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 22 September 2006 - 01:18 PM

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:

$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>'; 
         }   
      }  
    } 

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?

#2 steveclondon

steveclondon
  • Members
  • PipPipPip
  • Advanced Member
  • 161 posts

Posted 22 September 2006 - 01:22 PM

Products_query is your result from the query not an array. $products is the array.

#3 seojunkie

seojunkie
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 22 September 2006 - 01:27 PM

I tried adding reset($products); at the end of the for loop and the message it gives is

Warning: reset(): Passed variable is not an array or object in



#4 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 22 September 2006 - 02:03 PM

Try this:
$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 />";
		}  
	}
}


#5 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 22 September 2006 - 02:14 PM

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.

#6 seojunkie

seojunkie
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 22 September 2006 - 02:29 PM

I tried making the change
for($i = 0; $i < count($type)-1; $i++)
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.

#7 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 22 September 2006 - 02:45 PM

alright, here's a better idea of how to go about this.  first off, grab the numeric ends for each product like so:

SELECT SUBSTRING_INDEX(products_model, '-', -1) AS type FROM products BLAH

use that query, and load it into an array like so:

<?php
$current_types = array();
while ($blah = mysql_fetch_assoc($resource))
{
  $current_types[] = $blah['type'];
}
?>

$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:

<?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.";
}
?>

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.

#8 seojunkie

seojunkie
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 22 September 2006 - 05:05 PM

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:
foreach (($arr = tep_db_fetch_array($products_query)) as $value)
{
         if (strpos($value , $type[$i]) === FALSE)
         {
           echo 'Add '.$type[$i] . ' to ' .$value .'<br>';
         } 
}



#9 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 22 September 2006 - 06:42 PM

try
....
    for ($i = 0; $i < count($type); ++$i) 
    { mysql_data_seek($products_query,0);
      while ($products = mysql_fetch_array($products_query))  
....


#10 seojunkie

seojunkie
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 22 September 2006 - 07:11 PM

Great. That seems to have done it. I appreciate everyone's help.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users