|
-
Feb 25th, 2007, 06:18 PM
#1
Thread Starter
WiggleWiggle
[RESOLVED] Seperated by commas, insert into db...
i have a textbox named "meta_tags". The user puts tags in the box separated by commas (eg: "dogs,pets,cats")
How to i separate them so i can insert them into a database?
My usual boring signature: Something
-
Feb 25th, 2007, 06:53 PM
#2
Re: Seperated by commas, insert into db...
-
Feb 25th, 2007, 06:53 PM
#3
Thread Starter
WiggleWiggle
Re: Seperated by commas, insert into db...
 Originally Posted by lintz
use the Explode function
Ok i understand it but
how to i loop them through a database?
Last edited by dclamp; Feb 25th, 2007 at 06:58 PM.
My usual boring signature: Something
-
Feb 25th, 2007, 07:45 PM
#4
Re: Seperated by commas, insert into db...
I don't know what you mean. if it's what I think, then I don't see how you're confused with this.
explode creates an array. if your delimiter is a comma, then you just explode by the commas, and then you could build your query by doing a for() loop through the contents of that array. you could either do an insert query for every loop through, or create a large query that inserts all of the items using the same criteria. example query structure of the latter:
Code:
INSERT INTO table_name (field1, field2, field3) VALUES
('valueA1', 'valueA2', 'valueA3'),
('valueB1', 'valueB2', 'valueB3');
-
Feb 25th, 2007, 07:52 PM
#5
Thread Starter
WiggleWiggle
Re: Seperated by commas, insert into db...
 Originally Posted by kows
I don't know what you mean. if it's what I think, then I don't see how you're confused with this.
explode creates an array. if your delimiter is a comma, then you just explode by the commas, and then you could build your query by doing a for() loop through the contents of that array. you could either do an insert query for every loop through, or create a large query that inserts all of the items using the same criteria. example query structure of the latter:
Code:
INSERT INTO table_name (field1, field2, field3) VALUES
('valueA1', 'valueA2', 'valueA3'),
('valueB1', 'valueB2', 'valueB3');
i understand how to put info into a db.
I mean, say i have this string:
PHP Code:
$string = "1,2,3,4,5";
$string = explode(",", $string);
echo $string['0']; //1
//...
$string could be 5 number, or it can be 100 number. How do i put that into a db, since i dont know how many arrays there are?
My usual boring signature: Something
-
Feb 25th, 2007, 07:56 PM
#6
Re: Seperated by commas, insert into db...
if you want to know how many entities are in an array, use count().
edit: also, it's easier to reference your array using $string[0] (instead of $string['0']) because your array key is an integer rather than a string.
-
Feb 25th, 2007, 07:57 PM
#7
Re: Seperated by commas, insert into db...
Try this (untested)
PHP Code:
$string = "1,2,3,4,5";
$string = explode(",", $string);
for($i=0;$i<=Count($string);$i++) {
INSERT INTO table_name (field1) VALUES
('$string[$i]')
}
Last edited by lintz; Feb 25th, 2007 at 08:14 PM.
Reason: Fixed typo
-
Feb 25th, 2007, 07:58 PM
#8
Thread Starter
WiggleWiggle
Re: Seperated by commas, insert into db...
sorry if i sound stupid, but i dont under stand the loop part of this whole thing
My usual boring signature: Something
-
Feb 25th, 2007, 07:59 PM
#9
Thread Starter
WiggleWiggle
Re: Seperated by commas, insert into db...
didnt see lintz post. testing now
My usual boring signature: Something
-
Feb 25th, 2007, 08:13 PM
#10
Hyperactive Member
Re: Seperated by commas, insert into db...
 Originally Posted by dclamp
sorry if i sound stupid, but i dont under stand the loop part of this whole thing
do you not understand how "loops" work or how looping works with inserting into a db?
-
Feb 26th, 2007, 07:11 AM
#11
Re: [RESOLVED] Seperated by commas, insert into db...
Why not a foreach loop?
Code:
$nums = explode(',', $string);
foreach($nums as $num) {
}
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 26th, 2007, 07:25 PM
#12
Thread Starter
WiggleWiggle
Re: [RESOLVED] Seperated by commas, insert into db...
 Originally Posted by CornedBee
Why not a foreach loop?
Code:
$nums = explode(',', $string);
foreach($nums as $num) {
}
little late for that
My usual boring signature: Something
-
Feb 26th, 2007, 08:07 PM
#13
Re: [RESOLVED] Seperated by commas, insert into db...
At least he's offering a solution
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|