PDA

Click to See Complete Forum and Search --> : [RESOLVED] Edit a String


LingoOutsider
Aug 3rd, 2009, 02:44 AM
Using Information from a SQL database I'm generating a string with values from two 'cells' [Plant Name], [Plant Description] so it now reads [Plant Name - Description].

I'm now looking to edit the [Plant Description] through the new string [Plant Name - Description].

Because I can identify [Plant Name] I think this is possible but I have no clue on how to do this? any ideas?


<textarea name="ItemDescription[<?php echo $rowR; ?>]" cols="50" rows="2" id="textarea"><?php
$savedref=$_GET['save'];
if($savedref){
$query = mysql_query("SELECT * FROM database WHERE ref='$savedref'");
$editedtext = mysql_fetch_assoc($query);
$newDescription = explode(".---.",$editedtext['worktype']);
echo $newDescription[($rowR-1)];}else{

$description = mysql_query("SELECT * FROM treetable WHERE (Tree='$nm[$rowR]' OR Shrub='$nm[$rowR]') AND Type_Code='$Des[$rowR]'") or die(mysql_error());
$desc = mysql_fetch_assoc($description);

if(isset($ItemDescription)){echo $ItemDescription[$rowR];}else{echo $nm[$rowR]." - ".$desc[Type];}}

?></textarea>

<!--(code just shown as an example not expected to be adapted thanks)-->

kows
Aug 3rd, 2009, 03:04 AM
I don't know if you're storing this new description in a database or if it's just a variable that you're working with, but you can easily split your string up, edit one part, and then put it back together. I would recommend explode() (as you used in your "example") and implode(). quick, dirty and extremely simple example:

$name = "Red rose";
$desc = "A red rose";

$plantdesc = implode(" - ", array($name, desc));
//$plantdesc = "Red rose - A red rose"

$newname = "Blue rose";

list($name, $desc) = explode(" - ", $plantdesc);
//$name = "Red rose", $desc = "A red rose"

$name = $newname;
$plantdesc = implode(" - ", array($name, $desc));
//$plantdesc = "Blue rose - A red rose"
okay, after writing that, I realized it was probably a bad example; you could have just rebuilt $plantdesc by imploding an array with $newname and $desc instead of using explode(), then defining $name as $newname, and then rebuilding it. but, like I said, it was just a quick and dirty example. if this is -exactly- what you want to do, then the method I just described would be better than my example. but if you have more than 2 elements to your array and you want to preserve those elements but "process" (essentially, append them) them, the above example works well!

anyway, so -- yes, it's simple to replace either the name or description of your new string. but you essentially need to either re-build the string from scratch, or break it apart and then put it back together. you could probably use regex and preg_replace() to do something too, but that could be overkill.

LingoOutsider
Aug 3rd, 2009, 07:45 AM
Thanks kows your first example is just what I'm looking for!