Jump to content

array help


woop

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

$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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.