I'm having a mysql field that could be like:
<----------
1,2
1
3,5,6,2323,54,233
45,66
---------->
I'm using $explode = explode("," $fetch['field']); and if($result != 0) { // make a list like this:
<a href="index.php?id=45">45</a>
<a href="index.php?id=66">66</a>
} else {
echo 'Result: 0';
}
explode - find the number of parts
Moderator: General Moderators
- markusn00b
- Forum Contributor
- Posts: 298
- Joined: Sat Oct 20, 2007 2:16 pm
- Location: York, England
Re: explode - find the number of parts
What is your question, JKM? Pardon my ignorance.JKM wrote:I'm having a mysql field that could be like:
<----------
1,2
1
3,5,6,2323,54,233
45,66
---------->
I'm using $explode = explode("," $fetch['field']); and if($result != 0) { // make a list like this:
<a href="index.php?id=45">45</a>
<a href="index.php?id=66">66</a>
} else {
echo 'Result: 0';
}
explode() returns an array of elements that were split according to the delimiter. Just like with any other array, you can find how many elements one has using the count() function.
Hope this helps,
Mark.
Re: explode - find the number of parts
Sorry for not writing my question in the main text element (I wrote it in the subject).
Lets say that count($explode); returns 3, how can I list the three results?
Lets say that count($explode); returns 3, how can I list the three results?
- markusn00b
- Forum Contributor
- Posts: 298
- Joined: Sat Oct 20, 2007 2:16 pm
- Location: York, England
Re: explode - find the number of parts
You generally loop over the array.JKM wrote:Sorry for not writing my question in the main text element (I wrote it in the subject).
Lets say that count($explode); returns 3, how can I list the three results?
Code: Select all
$str = "my, delimited, text";
$arr_str = explode(", ", $str);
foreach ($arr_str as $key => $node) {
printf("%d = %s\n", $key, $node);
}
For debugging purposes, you might use the print_r() function to display an array's contents.
Other resources:
- http://php.net/for - the for loop
- http://php.net/while - the while loop
- http://php.net/var_dump - var_dump()
Mark.