Jump to content

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?
Link to comment
https://forums.phpfreaks.com/topic/21660-skips-array-on-second-pass/
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]
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.
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.
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]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.