Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Regular Coder
    Join Date
    Jul 2003
    Location
    New Zealand
    Posts
    435
    Thanks
    1
    Thanked 0 Times in 0 Posts

    question about taking words out of string

    ive got a session called cart which contains a whoole lot of product id's. i want to make a page firstly that will take each id from the var and print it on the page with a br after it.

    secondly i need to be able to take certain id's out of the string but i dont know how to do it..

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The answer depends on how you store the product ids in the session. Are they contained in an array (ok), an object (ok) or a string(bad)?

    Same goes for your second question. How does the string look like? As a general answer, you can use preg_match() or the various string functions to get certain parts of the string, but how exactly depends on the string, obviously.

    Show us some code of yours and the data that's stored in the session.
    De gustibus non est disputandum.

  • #3
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i use that sort of listings all the time with strings --> tabdelimited strings, like '45|45|23|67|89|65'

    I then convert that string into an array like
    $ids = explode("|", $var)

    And then i acces then with $ids[0] or so (if each element has a fixed meaning for the code) or i run a foreach() - loop on them
    foreach ($ids as $value){
    echo ('<br />You already purchased the items with ID = ' . $value);
    }
    (or you can use the loop to build a collection of ID's you'll then use for a select or whatever)

  • #4
    Regular Coder
    Join Date
    Jul 2003
    Location
    New Zealand
    Posts
    435
    Thanks
    1
    Thanked 0 Times in 0 Posts
    i use a string similar to raf's example, if echoed it would look like:
    1025, 102326, 10326, 19361
    etc

    so what's the code to seperate each and echo them onto a page with a linedrop?

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    $str '1025, 102326, 10326, 19361';
    foreach (
    explode(', '$str) as $value) print $value '<br />'
    De gustibus non est disputandum.

  • #6
    Regular Coder
    Join Date
    Jul 2003
    Location
    New Zealand
    Posts
    435
    Thanks
    1
    Thanked 0 Times in 0 Posts
    tyvm

  • #7
    Regular Coder
    Join Date
    Jul 2003
    Location
    New Zealand
    Posts
    435
    Thanks
    1
    Thanked 0 Times in 0 Posts
    and another thing : once youve taken the product id's out of the string, how can you permanently delete it from the string?

  • #8
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So many options ....

    Normally, if i keep delimited strings, then i don't remove elements from it. If it needs to be dynamical, then you best use a table or array or so. Annaway, you can:
    - remove substrings from the original string;
    - compose a new string without the processed elements;
    - remove the element from the array and then compose a new string with a foreach-loop

    Option 2 is the easiest and probably most performant.
    An example. Suppose we echoed the elements with checkboxes that the user could select and we only processand remove the selected items. The checkboxes get this name 'chkxxxxx' where xxxxx is the element

    PHP Code:
    $str $_SESSION['yourstringname'] ;
    //$str  can then look like '1025, 102326, 10326, 19361'
    $elements explode(', '$str) ;
    $str '9999999999999';   // just a dummy element to make the listbuilding easier. Else you need a countervariable to see if it's the first element of the list so that ypu son't include the ', '
    foreach ($elements as $value) {
       if (isset(
    $_POST['chk' $value]) {    //only checked boxes are setin the post collection. We recompose the names just like you did when you build the form
           
    your code to delete the item or whatever
       
    } else {
           
    $str .= ', ' $value  ;  // elements that weren't selected aren't processed and will still be in the string
       
    }
    }
    $_SESSION['yourstringname'] = $str 
    You could off course work directly on the sessionvariable but this 'go between $str' variable given you more flexability and will save you rework if you need to change the datasource.
    Last edited by raf; 11-23-2003 at 11:31 AM.

  • #9
    Regular Coder
    Join Date
    Jul 2003
    Location
    New Zealand
    Posts
    435
    Thanks
    1
    Thanked 0 Times in 0 Posts
    ok... how would you create a new string without say $string

    so say take $String out of $str??

  • #10
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm not sure i get what you're after.

    The code in ly previous ppost lets you do that
    PHP Code:
    $str $_SESSION['yourstringname'] ;
    //$str  can then look like '1025, 102326, 10326, 19361'
    $elements explode(', '$str) ; 
    Breaks up your string into an array. From there on, you can select or exclude the elements that you want and after that, you can use the froeach part to store it back into a string.

    Or you could just runs a check against some value inside the foreach loop and only keep the parts that meet or don't meet your condition.

    But your question is to cryptic to answer. Can you give an example ?

  • #11
    Regular Coder
    Join Date
    Jul 2003
    Location
    New Zealand
    Posts
    435
    Thanks
    1
    Thanked 0 Times in 0 Posts
    im trying to write a shopping cart script, the cart entries are stored in a session namec cart and seperated by ", " and i need to seperate each value to use it in a mysql query

  • #12
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, then it's
    PHP Code:
    $str $_SESSION['yourstringname'] ;
    //$str  can then look like '1025, 102326, 10326, 19361'
    $elements explode(', '$str) ;
    foreach (
    $elements as $value) {
       
    ypur sql stuff like inserts here

    if you need to proces all fields seperately like in inserts and some updates

    Or, if you can use it as a collection, you can use it straigh like that.
    Example:
    $sql = "SELECT var1, var2, var3 FROM table WHERE IDvalue In (" . $_SESSION['cart'] . ")" ;

    (So then the session-var contains the ID's of the records.)

  • #13
    Regular Coder
    Join Date
    Jul 2003
    Location
    New Zealand
    Posts
    435
    Thanks
    1
    Thanked 0 Times in 0 Posts
    lol ive already written all that... what i need is that every time i go round the foreach statement it shows a remove link and i need to take that id out of the session
    and leave the rest in there.

  • #14
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Then you have
    PHP Code:
    $str $_SESSION['yourstringname'] ;
    //$str  can then look like '1025, 102326, 10326, 19361'
    $elements explode(', '$str) ;
    foreach (
    $elements as $value) {
       echo 
    '<a href="deletethisitem.php?item="' $value '" title="remove this item from the list">Remove item</a>'

    And inside deletethisitem.php
    PHP Code:
    $str $_SESSION['yourstringname'] ;
    //$str  can then look like '1025, 102326, 10326, 19361'
    $newstr '9999999999999';   // just a dummy element to make the listbuilding easier. Else you need a countervariable to see if it's the first element of the list so that ypu son't include the ', '
    foreach ($elements as $value) {
       if (
    $value== $_GET['item']) {    
          
    $sql="delete from table where blablaID = " $value
    } else {
           
    $newstr .= ', ' $value  ;  // elements that weren't selected aren't processed and will still be in the string
       
    }
    }
    $_SESSION['yourstringname'] = $newstr 

  • #15
    Regular Coder
    Join Date
    Jul 2003
    Location
    New Zealand
    Posts
    435
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thanks.. took a while to get my message through but thats pretty much what im looking for. cheers

    --edit--

    theres one more thing :O

    on my checkout page ive modified the first script to show the name, id, price and a textbox with quantity in it, but each textbox must have a different name.. how do i like create an auto increment on textbox's in a foreach loop?

    also when ive clicked submit on the new page i need the second script basically to make each of the seperated id's back into a string in this format

    "AAAAAAxBB, "
    where A is the value that we were seperating in the first script and BB is the quantity (from the textbox's)

    help appreciated.
    Last edited by Scrowler; 12-02-2003 at 05:16 AM.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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