Forum Moderators: coopster

Message Too Old, No Replies

Array problem

need some help with an array

         

DonWon

2:19 pm on Jul 3, 2008 (gmt 0)

10+ Year Member



Im making a calendar with some events. below is the array to put the events in the day. the problem Im having is making this array dynamic from database output. I'm getting syantax issues. :S

any ideas

$days = array(

2=>array('/weblog/archive/2004/Jan/02','linked-day'),

3=>array('/weblog/archive/2004/Jan/03','linked-day'),

8=>array('/weblog/archive/2004/Jan/08','linked-day'),

22=>array('/weblog/archive/2004/Jan/22','linked-day'),

);

eelixduppy

2:23 pm on Jul 3, 2008 (gmt 0)



Hello and Welcome to WebmasterWorld!

Can I see the relevant code that constructs this array from the database? What seems to not be working correctly? I notice you have an extra comma in there at the end..

DonWon

2:26 pm on Jul 3, 2008 (gmt 0)

10+ Year Member



hi there, thanks for the reply.

it is not outputting from a db YET.

if I put the following code in the script it will add the enent no problem, but I need to create this array from information from my DB.

$days = array(

2=>array('/weblog/archive/2004/Jan/02','linked-day'),

3=>array('/weblog/archive/2004/Jan/03','linked-day'),

8=>array('/weblog/archive/2004/Jan/08','linked-day'),

22=>array('/weblog/archive/2004/Jan/22','linked-day'),

);

so for example the key "2" is ferring to the day.

any ideas?

eelixduppy

2:32 pm on Jul 3, 2008 (gmt 0)



Read up in this thread taken from our library on extracting data from MySQL: [webmasterworld.com...]

Then come up with your best-try solution to your problem so that we have some code to go through and we'll get this working for you. A hint is that the query returns an array for each row in the table; you can take this and append it to another array with the correct index for the day.

DonWon

3:01 pm on Jul 3, 2008 (gmt 0)

10+ Year Member



wow thanks for that, ok I have fixed the problem kinda.
i have it doing what i need but the problem is it is not making multiple entries.

do{
$days= array($day["daystart"]=>array('index.php?','linked-day'));
} while ($row_Get_User = mysql_fetch_array($Get_User));

i have tries array_push but doesnt seem to work

eelixduppy

3:17 pm on Jul 3, 2008 (gmt 0)



So I'm assuming a few things in the code below, but it should get you started a little further:

$container = array(); #holds everything
$Get_User = mysql_query(......
#
while($row = mysql_fetch_assoc($Get_User))
$container[$row['day']] = array($row['archive'], $row['linked_day']);

Try something like this, replacing the query and the specific names of the columns (day, archive, and linked_day).

DonWon

3:21 pm on Jul 3, 2008 (gmt 0)

10+ Year Member



here is more code , incase it helps. ill try and go through what you just wrote.

mysql_select_db($database, $Backoffice);
$query_Get_User = "SELECT *, from_unixtime(datestart,'%d') as daystart,from_unixtime(datefinish,'%d') as dayfinish,from_unixtime(datefinish,'%n') as monthfinish,from_unixtime(datestart,'%m') as monthstart,from_unixtime(datefinish,'%Y') as yearfinish,from_unixtime(datestart,'%Y') as yearstart from todo where user_id = ".$_SESSION['User_id']." and (from_unixtime(datestart,'%m') =07 or from_unixtime(datefinish,'%m') = 07)";

$Get_User = mysql_query($query_Get_User, $Backoffice) or die(mysql_error());
$day = mysql_fetch_array($Get_User);
$totalRows_Get_User = mysql_num_rows($Get_User);

do{
$days= array($day["daystart"]=>array('index.php?','linked-day'));
} while ($row_Get_User = mysql_fetch_array($Get_User));

DonWon

3:28 pm on Jul 3, 2008 (gmt 0)

10+ Year Member



nearly there

this is the array output

Array ( [29] => Array ( [0] => index.php [1] => linked-day ) ) Array ( [29] => Array ( [0] => index.php [1] => linked-day ) [04] => Array ( [0] => index.php [1] => linked-day ) )

i have the same problem as before it is only out display the last event day

DonWon

3:31 pm on Jul 3, 2008 (gmt 0)

10+ Year Member



ok sorted it :)
it was the format of the day 04 doesnt work but 4 does.

thank you very much for your time and help

eelixduppy

3:33 pm on Jul 3, 2008 (gmt 0)



I'm not sure I understand the issue. If your code is similar to what you had and not what I had, you called mysql_fetch_array before the do-while loop, which moved the iterator ahead by one, therefore losing the first row in the table because you did nothing with the data that was returned. I hope this is what you are talking about, otherwise, can you please explain to me in more detail what is wrong with the array now? We are almost there :)

PHP_Chimp

3:33 pm on Jul 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you want to be using a do/while loop?
As this will mean that the do part will always be executed, regardless of what data is returned (other than the error).

Dont you just what a while loop, as then while there is data this loop will get executed...as per eelixduppy's example above.

eelixduppy

3:33 pm on Jul 3, 2008 (gmt 0)



wow, I am a slow typer today. Glad you figured it out! :)