Click to See Complete Forum and Search --> : help with explode()


Genixdeae
04-27-2005, 12:42 PM
I might be tring to do something with explode() that isn't do able, not really sure. I have a field in a mysql db that stores period information seperated by commas(,). My calendar script grabs those periods using the LIKE '%arg%' statement, which works fine until you get double digit periods(10, 11, 12, ect). As when it's with double digits it sees them as period 1 and period 1 not period 11.
Is there a way I could use explode to fix this issue?

Here's my current code:
$query = "select * from $table1 where fd_day = $day and fd_month = $new_month_num and fd_year = $year and per like '%$per%'"; //$per is $_GET["per"] (coming from the URL, address bar thingy)

SpectreReturns
04-27-2005, 07:40 PM
You could return it as a full string then use explode(',',$result); to return an array of each section to you.

Genixdeae
04-27-2005, 09:37 PM
that would work, cept i'm a little confused....
lets say my code says$query = "select * from $table1 where fd_day = $day and fd_month = $new_month_num and fd_year = $year";
$result = mysql_query($query, $dbconn);
while($get_events = mysql_fetch_assoc($result)) {
$list_periods = explode(",",$get_events["per"]);
//dont know where to go from here, I've already where my query to the db, so how do i filter the results? or am i thinking about this the wrong way?

Thanks

MarkL
04-28-2005, 01:54 PM
First off, you have a problem with your while statement, in that you are using an assignment operator "=" instead of a comparison operator "==", so you will need to change that before your while statement will function the way you want it to. You are also going to need to initialize the $get_events variable so that it is a value that is going to run you through the while loop at least once, and in a manner that will not cause an infinite loop.

On your explode function, the syntax you are using will also cause problems, if it will allow the function to work at all. The correct syntax for the explode funtion is as follows:

explode ( string separator, string string [, int limit] )

The [, int limit] portion of the function specifies a limit to the number of array elements that will be created. You are treating the string as if it is already an array by using the syntax explode(",",$get_events["per"]) so you will need to change it to explode(",",$get_events)

So you will get a number of array elements corresponding to the number of comma delimited entries in your string, starting with element $list_periods[0].

You will need to know which array element holds the period information you are using, or testing against in order to work with that data. So, if the data is contained in the third block of data it would actually be contained in array element $list_periods[2], and you can refer to that element directly when working with data, examples follow:

echo $list_periods[2]; //this will display the period number
if ($list_periods[2] == 11) { echo "Period 11"; } //this will compare the value of the array element with the specified value

I hope this helps some.

NogDog
04-28-2005, 02:26 PM
First off, you have a problem with your while statement, in that you are using an assignment operator "=" instead of a comparison operator "=="...
No, that is a common idiom for looping through a result set. You're not testing whether something equals something else, but whether or not the mysql_fetch_assoc() returned false or true (where true in this case is anything not false).

MarkL
04-28-2005, 03:29 PM
No, that is a common idiom for looping through a result set. You're not testing whether something equals something else, but whether or not the mysql_fetch_assoc() returned false or true (where true in this case is anything not false).

I stand corrected. Just goes to show you, no matter how much you think you know about a subject, there is always more to learn.

Genixdeae
04-29-2005, 01:52 PM
oi, i feel stupid now....all i needed was on php.net the entire time

http://us4.php.net/function.in-array

that'll let me search the array $lit_periods, which was created by the explode, and if it returns true then finish out the script, otherwise do nothing.....Thanks for the help, greatly appriciate it.