woop Posted August 13, 2010 Share Posted August 13, 2010 A bit more help needed... I have a table with a column called date. Dates in this column are in the format MonthDay, examples: Jan31, Feb2, Apr9, Jul27. I need to query the table for all dates in a month to get an array that looks like this: $events = array(1,7,8,17,18,23); So far I have: $month='Feb'; (or whatever month is given to the variable) $query = "SELECT date FROM table WHERE date LIKE '$month%'"; $events = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)) { echo substr($row['date'], 3) . ','; } } This echos the results with a comma, but there is a trailing comma which is not needed, and also I am trying to force this into an array, but I think there must be a more natural way of retrieving this array. Basically the rest of my script works well if it is given an array like this: $events = array(1,7,8,17,18,23); Any help appreciated. Link to comment https://forums.phpfreaks.com/topic/210664-array-help/ Share on other sites More sharing options...
kickstart Posted August 13, 2010 Share Posted August 13, 2010 Hi You need to put it in an array I think, not display it as something that looks like an array declaration <?php $month='Feb'; (or whatever month is given to the variable) $DaysRequiredArray = array(); $query = "SELECT date FROM table WHERE date LIKE '$month%'"; $events = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)) { $DaysRequiredArray = substr($row['date'], 3); } ?> All the best Keith Link to comment https://forums.phpfreaks.com/topic/210664-array-help/#findComment-1098951 Share on other sites More sharing options...
woop Posted August 13, 2010 Author Share Posted August 13, 2010 Thanks for your help kickstart - i get query errors using your code though. Definitely understand that I don't want to just arrange the pieces of an array, but need the final array to provide be a line of php like: $events = array(1,7,8,17,18,23); Because the rest of my code works when i replace my query with this line (simply telling the script the numbers/dates that events exist) I'll keep playing around an if you have any ideas please share. Link to comment https://forums.phpfreaks.com/topic/210664-array-help/#findComment-1098956 Share on other sites More sharing options...
AbraCadaver Posted August 13, 2010 Share Posted August 13, 2010 $month='Feb'; $query = "SELECT date FROM table WHERE date LIKE '$month%'"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)) { $events = substr($row['date'], 3); } echo implode(',', $events); Link to comment https://forums.phpfreaks.com/topic/210664-array-help/#findComment-1098960 Share on other sites More sharing options...
marcus Posted August 13, 2010 Share Posted August 13, 2010 $month='Feb'; $query = "SELECT date FROM table WHERE date LIKE '$month%'"; $result = mysql_query($query) or die(mysql_error()); $events = array(); while($row = mysql_fetch_array($result)) { $events[] = substr($row['date'], 3); } echo implode(',', $events); Link to comment https://forums.phpfreaks.com/topic/210664-array-help/#findComment-1098964 Share on other sites More sharing options...
kickstart Posted August 13, 2010 Share Posted August 13, 2010 I'll keep playing around an if you have any ideas please share. Hi I just missed out [] when adding to the array in the loop. Changing the array name to events <?php $month='Feb'; (or whatever month is given to the variable) $events = array(); $query = "SELECT date FROM table WHERE date LIKE '$month%'"; $EventCursor = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)) { $events = substr($row['date'], 3); } ?> That will give you an array called $events which contains the list of days. All the best Keith Link to comment https://forums.phpfreaks.com/topic/210664-array-help/#findComment-1098981 Share on other sites More sharing options...
woop Posted August 13, 2010 Author Share Posted August 13, 2010 Thanks mgallforever and kickstart - those seem to do the job. One last question, if I need the array to start with "0," is there an easy way to do this? Link to comment https://forums.phpfreaks.com/topic/210664-array-help/#findComment-1098986 Share on other sites More sharing options...
kickstart Posted August 13, 2010 Share Posted August 13, 2010 Hi They do by default, although with arrays like this one it is probably easier to use foreach to loop through them rather than particular indexes. All the best Keith Link to comment https://forums.phpfreaks.com/topic/210664-array-help/#findComment-1098988 Share on other sites More sharing options...
woop Posted August 13, 2010 Author Share Posted August 13, 2010 Thanks Keith but for some reason unless i explicitly include a zero, the rest of the script is missing off the first entry in the array!? Any ideas? Link to comment https://forums.phpfreaks.com/topic/210664-array-help/#findComment-1098989 Share on other sites More sharing options...
AbraCadaver Posted August 13, 2010 Share Posted August 13, 2010 Ha, my bad. Make sure to use: $events[] = substr($row['date'], 3); Link to comment https://forums.phpfreaks.com/topic/210664-array-help/#findComment-1098990 Share on other sites More sharing options...
woop Posted August 13, 2010 Author Share Posted August 13, 2010 Thanks AC everyone's been so helpful getting me up to the point that the array is working along with the rest of the script. Just a minor thing to deal with now that the rest of the script is missing the first date off in the stuff it does. It seems that I need to explicitly put a zero at the start of the array: example: $events = array(0,1,7,8,17,18,23); Just not sure how. Link to comment https://forums.phpfreaks.com/topic/210664-array-help/#findComment-1098993 Share on other sites More sharing options...
kenrbnsn Posted August 13, 2010 Share Posted August 13, 2010 Where you're initializing the array, use this <?php $events = array(0); ?> instead of <?php $events = array(); ?> Ken Link to comment https://forums.phpfreaks.com/topic/210664-array-help/#findComment-1098996 Share on other sites More sharing options...
woop Posted August 13, 2010 Author Share Posted August 13, 2010 Thanks Ken - That did it. And thank you everyone else on the thread for your time and code - I have learnt a lot and you have saved me a lot of time and hair-pulling today. Really appreciate it. Link to comment https://forums.phpfreaks.com/topic/210664-array-help/#findComment-1098997 Share on other sites More sharing options...
kickstart Posted August 13, 2010 Share Posted August 13, 2010 Thanks Keith but for some reason unless i explicitly include a zero, the rest of the script is missing off the first entry in the array!? Sorry, get what you mean now. I thought you meant an array where the first index was 0 (which is the default) whereas what you wanted was the first element of the array to have a value of 0. All the best Keith Link to comment https://forums.phpfreaks.com/topic/210664-array-help/#findComment-1098999 Share on other sites More sharing options...
woop Posted August 13, 2010 Author Share Posted August 13, 2010 No probs - you were a huge help Keith. Link to comment https://forums.phpfreaks.com/topic/210664-array-help/#findComment-1099002 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.