Explode a database field

Angecorse Posted messages 42 Status Member -  
Reivax962 Posted messages 3742 Status Member -
Bonjour,

Inventory:
-> mysql
-> phpmyadmin
->innodb
->php framework: CI 3
DB: blog
table: post

In the database, I have several tags in the 'tags' field:
for example: "video 3d, comique"

They are separated by a space, a comma, or both.

I would like these to be exploded on display so that later, if one of them is clicked, the search will be performed on all articles containing that single tag and display the result.

I would like it to be a function and that it loops through as many tags as there are in the field:

public static function tags(){

}

2 answers

Reivax962 Posted messages 3742 Status Member 1 011
 
Hello,

An alternative to explode is to have fun with regular expressions: "(([^,\s]+)[,\s])*([^,\s]+)"

Xavier
2
Angecorse Posted messages 42 Status Member
 
Thank you Xavier,

In the explode what I was missing was how to handle a space and/or a comma ;)
0
jordane45 Posted messages 30426 Registration date   Status Moderator Last intervention   4 830 > Angecorse Posted messages 42 Status Member
 
You should have told us that from the beginning... you would have had your answer right away...
0
Reivax962 Posted messages 3742 Status Member 1 011
 
But you can still handle two delimiters with explode as Jordane suggested, by first exploding on spaces, then looping through the results (foreach) to perform a second explode on commas, and manually adding all these results to a results array.
0
Angecorse Posted messages 42 Status Member > jordane45 Posted messages 30426 Registration date   Status Moderator Last intervention  
 
Sorry... ;)
0
Angecorse Posted messages 42 Status Member
 
1. Actually, I would like to create a function with
explode
that searches for the field [field] (to explode) from the table [table] in the database.
2. I would like to display it on my page with
 <? php foreach ($explode as $e): <li><?php echo $e->field; ?></li> ?php endforeach; ?> 


1. As soon as it interacts with the database, I get errors (if I create a variable to explode, everything works fine)
2. At this point, I also have errors, because each <li> must display an
explode
of field

So I deleted everything :(
0
jordane45 Posted messages 30426 Registration date   Status Moderator Last intervention   4 830
 
Hello,

I don't quite understand your request... at least... what exactly the question is...

What are you stuck on? The display? The explode? The loop? The search?
What have you already started doing?

Not to mention that we don't know the structure of your "post" table... the code of the view where you want to display your tags... the connection method to your DB (PDO? Mysqli? mysql?)

Anyway.. thank you for clarifying!

P.S.: You mentioned the CI3 framework... do you mean Code Igniter 3?

--
Best regards,
Jordane
1
Angecorse Posted messages 42 Status Member
 
Hello Jordane,

For CI3: Yes, it is indeed CodeIgniter 3 (I specified if there are any helpers or others that are native for this type of function)

→ The connection method: not important, that’s not the subject ;)
→ The table structure: in my opinion, no interest in knowing it

I would like a function that explodes the data contained in the "tags" field of the "blog" table: The most important is stated here.

I give the example again:
In "tags" if I have "video, 3d, comic" or "video 3d comic" (with or without a comma), the function should output as many blocks (block[0], block[1], block[2], block[x],...) as there are words and return the result.

For information, exploding the field will later serve:
→ to dissociate the tags (keywords) during display (but that is not my request).
→ to calculate which tags are the most used in this "tags" field and display them (again, this is not my request)

Don’t bother with what revolves around the function.
0
jordane45 Posted messages 30426 Registration date   Status Moderator Last intervention   4 830
 
Well... if nothing is important and I shouldn't worry...
There you go... in PHP there is the explode function
And for looping with foreach.

Have a good day
0