Julian Posted May 8, 2007 Share Posted May 8, 2007 Hi guys. I posted this problem I'm having but I think I didn't explain well. Here's the problem: <?php $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'), ); ?> I have to fill the array from a database-request loop in that exact format. Here's what I did: $days = array( while (list($row_calendar) = mysql_fetch_assoc($calendar)) { $row_calendar['dia'] => array('/weblog/archive/2004/Jan/'.$row_calendar['dia']), } ); I get this error with this: parse error, unexpected T_WHILE, expecting ')' . I think I shouldn't try to do the loop inside the array, maybe someone can help me do the trick. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/50531-solved-fill-the-array-from-a-database-request/ Share on other sites More sharing options...
per1os Posted May 8, 2007 Share Posted May 8, 2007 I do not think you can put a while loop inside an array definition try this: <?php $days = array(); while (list($row_calendar) = mysql_fetch_assoc($calendar)) { $days[$row_calendar['dia']] = '/weblog/archive/2004/Jan/'.$row_calendar['dia']; } ?> Should work. Quote Link to comment https://forums.phpfreaks.com/topic/50531-solved-fill-the-array-from-a-database-request/#findComment-248300 Share on other sites More sharing options...
Julian Posted May 8, 2007 Author Share Posted May 8, 2007 Thanks frost110. I take out the loop from the array, but how I fill the array with the database-request. The way you posted just echo '/weblog/archive/2004/Jan/'. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/50531-solved-fill-the-array-from-a-database-request/#findComment-248326 Share on other sites More sharing options...
per1os Posted May 8, 2007 Share Posted May 8, 2007 Dip my bad, you are listing the thing, here try this: <?php $days = array(); // row calendar is now an array so the dia reference should work. while ($row_calendar = mysql_fetch_assoc($calendar)) { $days[$row_calendar['dia']] = '/weblog/archive/2004/Jan/'.$row_calendar['dia']; } ?> Or this might work <?php $days = array(); while (list($row_calendar) = mysql_fetch_assoc($calendar)) { // since row_calendar is listed it should not be an array. $days[$row_calendar] = '/weblog/archive/2004/Jan/'.$row_calendar; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/50531-solved-fill-the-array-from-a-database-request/#findComment-248332 Share on other sites More sharing options...
Julian Posted May 8, 2007 Author Share Posted May 8, 2007 Thanks again frost110 But still doesn't work. For example the resulting array should be like this: <?php $days = array( 2=>array('/weblog/archive/2004/Jan/02'), 3=>array('/weblog/archive/2004/Jan/03'), 8=>array('/weblog/archive/2004/Jan/08'), 22=>array('/weblog/archive/2004/Jan/22'), ); ?> I tried both options and I get no results, maybe what I missing here is that the loop result should look exactly as the example above. Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/50531-solved-fill-the-array-from-a-database-request/#findComment-248405 Share on other sites More sharing options...
per1os Posted May 8, 2007 Share Posted May 8, 2007 Are you sure the query is even running through and is correct? I do not see where the query is ran etc so I cannot say for a fact that it is correct etc. I would check your query via phpMyAdmin or something similiar and make sure that results are being returned. Quote Link to comment https://forums.phpfreaks.com/topic/50531-solved-fill-the-array-from-a-database-request/#findComment-248412 Share on other sites More sharing options...
Julian Posted May 8, 2007 Author Share Posted May 8, 2007 Thanks frost110 The query is simple mysql_select_db($database_concierge, $concierge); $query_calendar = "SELECT * FROM calendar"; $calendar = mysql_query($query_calendar, $concierge) or die(mysql_error()); $row_calendar = mysql_fetch_assoc($calendar); $totalRows_calendar = mysql_num_rows($calendar); This array will print a link on a calendar for each number that correspond in the DB. When I use the code you posted I get no errors, but it doesn't work on the array, for example I did this to check the results from the database: $days = array(); $row_calendar['dia'] = '/weblog/archive/2004/Jan/'.$row_calendar['dia']; ); It worked, but only show me the last record on the DB, that's why I need to do a loop. Very appreciated your suggestions. Quote Link to comment https://forums.phpfreaks.com/topic/50531-solved-fill-the-array-from-a-database-request/#findComment-248436 Share on other sites More sharing options...
per1os Posted May 8, 2007 Share Posted May 8, 2007 I think you are confused, that or I am confused. <?php $days = array(); // row calendar is now an array so the dia reference should work. while ($row_calendar = mysql_fetch_assoc($calendar)) { $days[$row_calendar['dia']] = '/weblog/archive/2004/Jan/'.$row_calendar['dia']; } print_r($days); ?> What does that print out? The code here $days = array(); $row_calendar['dia'] = '/weblog/archive/2004/Jan/'.$row_calendar['dia']; ); Does not make any sense at all and I do not understand what you are trying to do with it. Maybe if you explain what the logic you are hoping to achieve from that I can help, but yea that should throw a syntax error and would only pull up the last one because you cleared the array and the only portion you are putting into was the last value retrieved from the DB since it is outside the loop. Quote Link to comment https://forums.phpfreaks.com/topic/50531-solved-fill-the-array-from-a-database-request/#findComment-248447 Share on other sites More sharing options...
sasa Posted May 8, 2007 Share Posted May 8, 2007 let do same debug 1st try <?php mysql_select_db($database_concierge, $concierge); $query_calendar = "SELECT * FROM calendar"; $calendar = mysql_query($query_calendar, $concierge) or die(mysql_error()); // $row_calendar = mysql_fetch_assoc($calendar); echo 'Total rows is: '; echo $totalRows_calendar = mysql_num_rows($calendar); ?> if output some number greater then 0 try <?php mysql_select_db($database_concierge, $concierge); $query_calendar = "SELECT * FROM calendar"; $calendar = mysql_query($query_calendar, $concierge) or die(mysql_error()); //$row_calendar = mysql_fetch_assoc($calendar); $totalRows_calendar = mysql_num_rows($calendar); $days = array(); while ($row_calendar = mysql_fetch_assoc($calendar)) { $days[$row_calendar['dia']] = '/weblog/archive/2004/Jan/'.$row_calendar['dia']; } echo "<pre>\n"; print_r($days); echo "</pre>"; ?> what is output? Quote Link to comment https://forums.phpfreaks.com/topic/50531-solved-fill-the-array-from-a-database-request/#findComment-248449 Share on other sites More sharing options...
Julian Posted May 8, 2007 Author Share Posted May 8, 2007 This prints: Array ( ) Only that. The post I did before I had an error. This is how I can get a result: $days = array( //do the query within the array $row_calendar['dia'] = '/weblog/archive/2004/Jan/'.$row_calendar['dia']; ); Quote Link to comment https://forums.phpfreaks.com/topic/50531-solved-fill-the-array-from-a-database-request/#findComment-248455 Share on other sites More sharing options...
per1os Posted May 8, 2007 Share Posted May 8, 2007 Than the issue lies within the query as it is pulling an empty result set. Make sure you have data in your database. Quote Link to comment https://forums.phpfreaks.com/topic/50531-solved-fill-the-array-from-a-database-request/#findComment-248456 Share on other sites More sharing options...
Julian Posted May 8, 2007 Author Share Posted May 8, 2007 If I do a query without the array like this: <? while (list($row_calendar) = mysql_fetch_assoc($calendar)) { echo '<br />'; echo $row_calendar['dia']; echo '<br />'; It prints out: 10 11 12 13 14 Quote Link to comment https://forums.phpfreaks.com/topic/50531-solved-fill-the-array-from-a-database-request/#findComment-248463 Share on other sites More sharing options...
per1os Posted May 8, 2007 Share Posted May 8, 2007 If I do a query without the array like this: <? while (list($row_calendar) = mysql_fetch_assoc($calendar)) { echo '<br />'; echo $row_calendar['dia']; echo '<br />'; That code does not make sense...you are using the list feature which basically takes the first part of the array and assigns it to the variable...there for the ['dia'] should not work because $row_calendar is just a string not an array...unless you have serialized data which than of course would need to be unserialized... Am I completely missing something here because given the statement above I am completely lost. Quote Link to comment https://forums.phpfreaks.com/topic/50531-solved-fill-the-array-from-a-database-request/#findComment-248468 Share on other sites More sharing options...
Julian Posted May 8, 2007 Author Share Posted May 8, 2007 Sorry. This how I get the result: <?php while ($row_calendar = mysql_fetch_assoc($calendar)) { echo '<br />'; echo $row_calendar['dia']; echo '<br />'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/50531-solved-fill-the-array-from-a-database-request/#findComment-248471 Share on other sites More sharing options...
per1os Posted May 8, 2007 Share Posted May 8, 2007 Well I am out of suggestions either way. Given that what sasa and I posted above should work flawlessly unless you are somehow for some reason resetting the $days array AFTER the while loop is ran. Post the full code of what it currently is with the loop assigning data to $days. Just to be sure there are no "mishap" errors in it. Quote Link to comment https://forums.phpfreaks.com/topic/50531-solved-fill-the-array-from-a-database-request/#findComment-248479 Share on other sites More sharing options...
Julian Posted May 8, 2007 Author Share Posted May 8, 2007 Original code: <?php $days = array( 2=>array('/weblog/archive/2004/Jan/02'), 3=>array('/weblog/archive/2004/Jan/03'), 8=>array('/weblog/archive/2004/Jan/08'), 22=>array('/weblog/archive/2004/Jan/22'), ); echo generate_calendar(2004, 1, $days, 3); ?> Changed code: <?php $days = array(); while (list($row_calendar) = mysql_fetch_assoc($calendar)) { $days[$row_calendar] = '/weblog/archive/2004/Jan/'.$row_calendar; } echo generate_calendar(2004, 1, $days, 3); ?> Quote Link to comment https://forums.phpfreaks.com/topic/50531-solved-fill-the-array-from-a-database-request/#findComment-248501 Share on other sites More sharing options...
sasa Posted May 8, 2007 Share Posted May 8, 2007 more debug <?php mysql_select_db($database_concierge, $concierge); $query_calendar = "SELECT * FROM calendar"; $calendar = mysql_query($query_calendar, $concierge) or die(mysql_error()); $days = array();$c = 0; while ($row_calendar = mysql_fetch_assoc($calendar)) { $c++; $d = $row_calendar['dia']; $l = '/weblog/archive/2004/Jan/'.$d; echo "Racord no $c --> dia = $d --> long = $l \n <pre>"; $days[$d][] = $l; print_r($days); echo "\n </pre><hr /> \n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/50531-solved-fill-the-array-from-a-database-request/#findComment-248528 Share on other sites More sharing options...
Julian Posted May 8, 2007 Author Share Posted May 8, 2007 Bull-eye Sasa That's exactly what I was looking for... you took out a lot of weight from my head.... Thanks a lot Frost and Sasa. Quote Link to comment https://forums.phpfreaks.com/topic/50531-solved-fill-the-array-from-a-database-request/#findComment-248535 Share on other sites More sharing options...
sasa Posted May 8, 2007 Share Posted May 8, 2007 OK remove echo and print from code Quote Link to comment https://forums.phpfreaks.com/topic/50531-solved-fill-the-array-from-a-database-request/#findComment-248547 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.