Enjoy unlimited access to all forum features for FREE! Optional upgrade available for extra perks.

php array

Status
Not open for further replies.
Joined
Jan 19, 2007
Posts
2,208
Reaction score
47
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.
 
Check the number of rows in the result, then include a counter, and if the counter = num_rows don't include the ',' bit on there.

Thats a quick way of doing it.

What you call $row is already an array, your just looping through it, why not perform your array actions on that ?
 
Thanks Skinner -

I got a bit of help on another forum which seems to be holding up (fingers crossed):

PHP:
$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);
}
 
Status
Not open for further replies.

The Rule #1

Do not insult any other member. Be polite and do business. Thank you!

Featured Services

Sedo - it.com Premiums

IT.com

Premium Members

AucDom
UKBackorder
Be a Squirrel
Acorn Domains Merch
MariaBuy Marketplace

New Threads

Domain Forum Friends

Other domain-related communities we can recommend.

Our Mods' Businesses

Perfect
Service
Laskos
*the exceptional businesses of our esteemed moderators
Top Bottom