Julian Posted May 7, 2007 Share Posted May 7, 2007 Hi Guys I have to print the array like this: $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'), ); Numbers here are days from database, field 'days'. What I'm trying to do is obtain the days for this array from the database and loop until last day. $days = array( row_days['day'] => array ('/days.php?days=row_days['day']')); Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/50381-looping-an-array/ Share on other sites More sharing options...
neel_basu Posted May 7, 2007 Share Posted May 7, 2007 Confused ------------- 2=>array('/weblog/archive/2004/Jan/02') Why are you trying to do that as here is only one Entry. You should use '/days.php?days='.$row_days['day'] Cause varible can not work in Single Quotes. Quote Link to comment https://forums.phpfreaks.com/topic/50381-looping-an-array/#findComment-247415 Share on other sites More sharing options...
Julian Posted May 7, 2007 Author Share Posted May 7, 2007 Forget the example... There's a table containing the day in fields, example id | days | month 1 | 4 | may 2 | 6 | may In order to display correctly on my calendar the array should be like this: $days = array( 4=>array('/'),//here goes the page linked 6=>array('/'),//here goes other page linked ); I just want to make a query to the DB that loop and make this array like this: $sql = "SELECT id_calendar, day FROM calendar ORDER BY id_calendar"; $res = mysql_query($sql) or die(mysql_error()); $today=>array(); while (list($id_calendar, $day) = mysql_fetch_row($res)) { $new_type_array[$id_calendar] = $day; Quote Link to comment https://forums.phpfreaks.com/topic/50381-looping-an-array/#findComment-247441 Share on other sites More sharing options...
Psycho Posted May 7, 2007 Share Posted May 7, 2007 I *think* I understand (not really, but maybe): $sql = "SELECT id_calendar, day FROM calendar ORDER BY id_calendar"; $res = mysql_query($sql) or die(mysql_error()); $today=>array(); while ($row = mysql_fetch_row($res) { $arrayVal = '/weblog/archive/2004/Jan/' . $row[day]; $today[$row[id_calendar]] = array ($arrayVal); } Quote Link to comment https://forums.phpfreaks.com/topic/50381-looping-an-array/#findComment-247460 Share on other sites More sharing options...
Julian Posted May 7, 2007 Author Share Posted May 7, 2007 Thanks for the great help. I get an error with the code, anyway let me try to explain better what I'm trying to do: Here's how the result should print: $days = array( 4=>array('/'),//here goes the page linked 6=>array('/'),//here goes other page linked ); I trying to achieve this with variables obtained from a database like: $days = array( $var1=>array('$var2'), ); Quote Link to comment https://forums.phpfreaks.com/topic/50381-looping-an-array/#findComment-247528 Share on other sites More sharing options...
Psycho Posted May 7, 2007 Share Posted May 7, 2007 Julian, Your posts are not clear. What is meant by "here goes the page linked" and "here goes other page linked". You say that is how you want them to print out? I'm not understanding if you want to create an array or print something to the screen (which is what your last post stated). The code I posted will create an array as you are asking. If there is a typo either fix it or state what the error is. I have cleaned it up here: <?php $sql = "SELECT id_calendar, day FROM calendar ORDER BY id_calendar"; $res = mysql_query($sql) or die(mysql_error()); $today=array(); while ($row = mysql_fetch_assoc($res)) { $arrayVal = '/weblog/archive/2004/Jan/' . $row[day]; $today[$row[id_calendar]] = array ($arrayVal); } echo "<pre>"; print_r($today); echo "</pre>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/50381-looping-an-array/#findComment-247561 Share on other sites More sharing options...
Julian Posted May 7, 2007 Author Share Posted May 7, 2007 I'm very sorry mjdamato. Your code worked great. Look here: $days = array( 4=>array('/users.php?day=4') ); This array will print a calendar on screen and show day "4" as a link to /users.php?day=4, and so on. I stored those days into a database. What I'm trying to do is a query to database that replace the "4" for a variables obtained from the field "day" on the database and loop in order to get everyday I inserted on the DB. I hope I explained right this time. I really appreciate that you spare your precious time with this. Thanks a lot. Quote Link to comment https://forums.phpfreaks.com/topic/50381-looping-an-array/#findComment-247584 Share on other sites More sharing options...
Julian Posted May 7, 2007 Author Share Posted May 7, 2007 Here's what I have so far: $days = array( //while ($row_calendar = mysql_fetch_assoc($calendar)) { $row_calendar['dia']=>array('/weblog/archive/2004/Jan/'.$row_calendar['dia']) //} ); If I uncomment the "while" I get an error. I think I'm not doing the loop well. Maybe someone can help me! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/50381-looping-an-array/#findComment-247614 Share on other sites More sharing options...
Psycho Posted May 7, 2007 Share Posted May 7, 2007 There is nothing wrong with your loop, per say. But I think the script is interpreting the code as if you are trying to put a loop into the array as opposed to creating a loop to add values to the array. I'm confused, you said my code worked, but you apparently still have a problem. I did notice that I had changed some of the variable names because of the code I was testing on. Does this not work for what you want: <?php $sql = "SELECT id_calendar, day FROM calendar ORDER BY id_calendar"; $res = mysql_query($sql) or die(mysql_error()); $days = array(); while ($row_calendar = mysql_fetch_assoc($res)) { $arrayVal = '/weblog/archive/2004/Jan/' . $row_calendar[day]; $days[$row_calendar[id_calendar]] = array ($arrayVal); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/50381-looping-an-array/#findComment-247620 Share on other sites More sharing options...
sasa Posted May 7, 2007 Share Posted May 7, 2007 try $days = array(); while ($row_calendar = mysql_fetch_assoc($calendar)) { $days[$row_calendar['dia']][] = '/weblog/archive/2004/Jan/'.$row_calendar['dia']; } Quote Link to comment https://forums.phpfreaks.com/topic/50381-looping-an-array/#findComment-247621 Share on other sites More sharing options...
Julian Posted May 7, 2007 Author Share Posted May 7, 2007 got this error sasa: Parse error: parse error, unexpected T_WHILE, expecting ')' Mr, mjdamato the code you posted worked but I can't change the syntax because the script won't work. Quote Link to comment https://forums.phpfreaks.com/topic/50381-looping-an-array/#findComment-247626 Share on other sites More sharing options...
Psycho Posted May 7, 2007 Share Posted May 7, 2007 Mr, mjdamato the code you posted worked but I can't change the syntax because the script won't work. I don't follow you. It worked but it doesn't work? What doesn't work? Are you getting error messages or incorrect output. Be specific please. Quote Link to comment https://forums.phpfreaks.com/topic/50381-looping-an-array/#findComment-247661 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.