psychowolvesbane Posted April 14, 2008 Share Posted April 14, 2008 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 =""; } } ?> Link to comment https://forums.phpfreaks.com/topic/101111-solved-str_replace-exact-replacement-problem/ Share on other sites More sharing options...
laffin Posted April 14, 2008 Share Posted April 14, 2008 ya ya will encounter that. u can use explode/implode to temporarily split everything into an array using the '?' as a seperator, than do yer search/replace then recombine everything back Link to comment https://forums.phpfreaks.com/topic/101111-solved-str_replace-exact-replacement-problem/#findComment-517120 Share on other sites More sharing options...
psychowolvesbane Posted April 14, 2008 Author Share Posted April 14, 2008 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 =""; } } Link to comment https://forums.phpfreaks.com/topic/101111-solved-str_replace-exact-replacement-problem/#findComment-517125 Share on other sites More sharing options...
psychowolvesbane Posted April 14, 2008 Author Share Posted April 14, 2008 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 =""; } } Link to comment https://forums.phpfreaks.com/topic/101111-solved-str_replace-exact-replacement-problem/#findComment-517144 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.