Results 1 to 3 of 3

Thread: [RESOLVED] Edit a String

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    156

    Resolved [RESOLVED] Edit a String

    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?

    PHP Code:
    <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)-->

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Edit a String

    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:

    PHP Code:
    $name "Red rose";
    $desc "A red rose";

    $plantdesc implode(" - ", array($namedesc));
    //$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.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2009
    Posts
    156

    Re: Edit a String

    Thanks kows your first example is just what I'm looking for!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width