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?
Printable View
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?
use the Explode function
Ok i understand it butQuote:
Originally Posted by lintz
how to i loop them through a database?
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.Quote:
Originally Posted by kows
I mean, say i have this string:
$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?PHP Code:$string = "1,2,3,4,5";
$string = explode(",", $string);
echo $string['0']; //1
//...
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.
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]')
}
sorry if i sound stupid, but i dont under stand the loop part of this whole thing
didnt see lintz post. testing now
Quote:
Originally Posted by dclamp
do you not understand how "loops" work or how looping works with inserting into a db?
Why not a foreach loop?
Code:$nums = explode(',', $string);
foreach($nums as $num) {
}
little late for that ;)Quote:
Originally Posted by CornedBee
At least he's offering a solution :D