...

View Full Version : question about taking words out of string



Scrowler
11-19-2003, 04:40 AM
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..

mordred
11-19-2003, 11:02 AM
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.

raf
11-19-2003, 12:23 PM
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)

Scrowler
11-19-2003, 07:41 PM
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?

mordred
11-19-2003, 07:56 PM
$str = '1025, 102326, 10326, 19361';
foreach (explode(', ', $str) as $value) print $value . '<br />';

Scrowler
11-20-2003, 04:56 AM
tyvm

Scrowler
11-23-2003, 04:59 AM
and another thing : once youve taken the product id's out of the string, how can you permanently delete it from the string?

raf
11-23-2003, 11:28 AM
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



$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.

Scrowler
11-26-2003, 03:09 AM
ok... how would you create a new string without say $string

so say take $String out of $str??

raf
11-26-2003, 06:58 PM
I'm not sure i get what you're after.

The code in ly previous ppost lets you do that


$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 ?

Scrowler
11-27-2003, 08:00 PM
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

raf
11-27-2003, 10:27 PM
Well, then it's


$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.)

Scrowler
11-28-2003, 04:25 AM
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.

raf
11-28-2003, 08:57 AM
Then you have


$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


$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 ;

Scrowler
11-28-2003, 09:04 AM
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.

Scrowler
12-06-2003, 08:10 AM
anyone?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum