Jump to content

Recommended Posts

Hi, I'm trying to use the str_replace function to replace the name of certain colours that I have listed within the Clothing Table in my database:

 

RecordExample1: GoldGreen?NavyRed?RoyalWhite?WhiteRed?

RecordExample2: Black?BlackGrey?DeepNavy?

RecordExample3: BlackGrey?NavyWhite?

 

Each individual colour is separated by the "?".

 

Now I have another Table called ProductColours that lists all of these individual colours, and a form that changes the values.

 

So on that form, whenever I change the value of one of the colours I most likely will need to update the collection of colours in the Clothing Table. That is where the string replacement comes in, however I cannot get it to be completely accurate and only effect the exact original match of the previous name.

 

Take RecordExample 2, I am changing the name of the colour "Black" to "Blue", so that should change the string to: Blue?BlackGrey?DeepNavy? however it changes all occurrences of Black so it ends up as Blue?BlueGrey?DeepNavy? !

The same will happen if I were to change White to Pink, which would effect 3 incorrect colours.

 

How do I stop it from doing that?

 

code:

<?php
            //Updating Clothing Records
            $sql = "SELECT AvailableColours FROM Clothing";
            $rs = mysql_query($sql, $conn) or die(mysql_error());
            $ToFind = "?";

            if(mysql_affected_rows($conn) > 0)
            {      
               while($row = mysql_fetch_array($rs)) 
               {
                  $AvailableColoursStr = $row['AvailableColours'];
                  echo $AvailableColoursStr." is the old record<br>";

                  $AvailableColours1 = str_replace("$OldColourName", "$NewColourName", "$AvailableColoursStr");

                  echo $AvailableColours1." is the new record<br>";
                  $sqlUpdate1 = "UPDATE Clothing SET AvailableColours = '$AvailableColours1' WHERE AvailableColours ='$AvailableColoursStr'";
                  $rsUpdate1 = mysql_query($sqlUpdate1,$conn) or die('Problem with query: ' . $sqlUpdate1 . '<br />' . mysql_error());
                  $AvailableColours1 ="";
               }  
            }
?>

So, something like this?:

 

            //Updating Clothing Records
            $sql = "SELECT AvailableColours FROM Clothing";
            $rs = mysql_query($sql, $conn) or die(mysql_error());
            $ToFind = "?";

            if(mysql_affected_rows($conn) > 0)
            {      
               while($row = mysql_fetch_array($rs)) 
               {
                  $AvailableColoursStr = $row['AvailableColours'];
                  echo $AvailableColoursStr." is the old record<br>";

                  $AvailableColours1 = explode($ToFind, $AvailableColoursStr)

                  foreach($AvailableColours1 AS $Acolour)
                  {
                     if($Acolour == $EditThisColourName)
                     {
                        $AColour = str_replace("$EditThisColourName", "$ColourName", "$AColour");
                     }
                     $AvailableColour2.=$AColour."?";
                  }
           

                  echo $AvailableColours1." is the new record<br>";
                  //$sqlUpdate1 = "UPDATE Clothing SET AvailableColours = '$AvailableColours1' WHERE AvailableColours ='$AvailableColoursStr'";
                  //$rsUpdate1 = mysql_query($sqlUpdate1,$conn) or die('Problem with query: ' . $sqlUpdate1 . '<br />' . mysql_error());
                  $AvailableColours1 ="";
               }  
            }

Okay, I have it working at last, thanks!

 

            //Updating Clothing Records
            $sql = "SELECT AvailableColours FROM Clothing";
            $rs = mysql_query($sql, $conn) or die(mysql_error());
            $ToFind = "?";

            if(mysql_affected_rows($conn) > 0)
            {      
               while($row = mysql_fetch_array($rs)) 
               {
                  $AvailableColoursStr = $row['AvailableColours'];
                  echo $AvailableColoursStr." is the old record<br>";

                  $AvailableColour1 = explode($ToFind, $AvailableColoursStr);

                  foreach($AvailableColour1 AS $AColour)
                  {
                     if($AColour !=="")
                     {
                        if($AColour == $EditThisColourName)
                        {
                           $AColour = str_replace("$EditThisColourName", "$ColourName", "$AColour");
                        }
                        $AvailableColour2.=$AColour."?";
                     }
                  }

                  echo $AvailableColour2." is the new record<br>";
                  $sqlUpdate1 = "UPDATE Clothing SET AvailableColours = '$AvailableColour2' WHERE AvailableColours ='$AvailableColoursStr'";
                  $rsUpdate1 = mysql_query($sqlUpdate1,$conn) or die('Problem with query: ' . $sqlUpdate1 . '<br />' . mysql_error());
                  $AvailableColours1 ="";
                  $AvailableColour2 ="";
               }  
            }

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.