Membership is FREE – with unlimited access to all features, tools, and discussions. Premium accounts get benefits like banner ads and newsletter exposure. ✅ Signature links are now free for all. 🚫 No AI-generated (LLM) posts allowed. Share your own thoughts and experience — accounts may be terminated for violations.

php array

Status
Not open for further replies.
Joined
Jan 19, 2007
Posts
2,207
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.

Rule #1: Be Respectful

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

Members online

No members online now.

Premium Members

New Threads

Domain Forum Friends

Our Mods' Businesses

*the exceptional businesses of our esteemed moderators
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Siusaidh AcornBot:
    Siusaidh has left the room.
      Siusaidh AcornBot: Siusaidh has left the room.
      Top Bottom