woop Posted September 21, 2009 Share Posted September 21, 2009 I have some code which works: $days = array( 9=>array('http://www.url.info/732/2009/September/9.php','linked-day'), 18=>array('http://www.url.info/732/2009/September/18.php','linked-day'), 21=>array('http://www.url.info/732/2009/September/21.php','linked-day'), ); but I need all the stuff in the middle of the array to come from a database query. So I set up a query and while statement to grab this from the db and store as a variable ($stringy). I checked that the variable has the array content by echoing which gives: 9=>array('http://www.url.info/732/2009/September/9.php','linked-day'), 18=>array('http://www.url.info/732/2009/September/18.php','linked-day'), 21=>array('http://www.url.info/732/2009/September/21.php','linked-day'), With being fairly new to php I thought that I could use the following code: $days = array($stringy); But it isn't working Does anyone know how I can achieve a similar effect - to grab the contents of the array statement from pieces inside a db then use a variable inside an array to start it like above? Many thanks. Quote Link to comment https://forums.phpfreaks.com/topic/174971-solved-variable-in-an-array/ Share on other sites More sharing options...
Bricktop Posted September 21, 2009 Share Posted September 21, 2009 Hi woop, Could you post your Database query code? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/174971-solved-variable-in-an-array/#findComment-922164 Share on other sites More sharing options...
woop Posted September 21, 2009 Author Share Posted September 21, 2009 mysql_connect("localhost", "user", "pass") or die(mysql_error()); mysql_select_db("db") or die(mysql_error()); $query = "SELECT * FROM table WHERE monthofyear = 'September'"; $result = mysql_query($query) or die(mysql_error()); $classy = '700'; while($row = mysql_fetch_array($result)){ $stringy .= $row['dateofmonth'] . "=>array('http://www.url.info/" . $classy . "/" . $row['year'] . "/" . $row['monthofyear'] . "/" . $row['dateofmonth'] . "','linked-day'), <br />"; } This is roughly the code. And hen I echo $stringy it echos the text I need in the array... Thanks for any help Quote Link to comment https://forums.phpfreaks.com/topic/174971-solved-variable-in-an-array/#findComment-922521 Share on other sites More sharing options...
DavidAM Posted September 21, 2009 Share Posted September 21, 2009 Uh, why not just go ahead and put it in the array during the loop? while($row = mysql_fetch_array($result)){ // $days[$row['dateofmonth']] = array('url string', 'text string'); $days[$row['dateofmonth']] = array("'http://www.url.info/" . $classy . "/" . $row['year'] . "/" . $row['monthofyear'] . "/" . $row['dateofmonth'] . "'", 'linked-day'); } Quote Link to comment https://forums.phpfreaks.com/topic/174971-solved-variable-in-an-array/#findComment-922567 Share on other sites More sharing options...
woop Posted September 22, 2009 Author Share Posted September 22, 2009 Hi David, I just tried that but i get errors: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/user/public_html/url.info/732/page.php on line 87 This is a tough cookie to crack. Thanks for your help and ideas. Quote Link to comment https://forums.phpfreaks.com/topic/174971-solved-variable-in-an-array/#findComment-922790 Share on other sites More sharing options...
ozestretch Posted September 22, 2009 Share Posted September 22, 2009 Did you change anything else other than what was suggested? Quote Link to comment https://forums.phpfreaks.com/topic/174971-solved-variable-in-an-array/#findComment-922794 Share on other sites More sharing options...
woop Posted September 23, 2009 Author Share Posted September 23, 2009 nope. Only the value of $classy which doesn't make a difference. Any other suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/174971-solved-variable-in-an-array/#findComment-923215 Share on other sites More sharing options...
ozestretch Posted September 23, 2009 Share Posted September 23, 2009 while($row = mysql_fetch_array($result)){ // $days[$row['dateofmonth']] = array('url string', 'text string'); $days[$row['dateofmonth']] = array('http://www.url.info/' . $classy . '/' . $row['year'] . '/' . $row['monthofyear'] . '/' . $row['dateofmonth'], 'linked-day'); } See how you go with that Quote Link to comment https://forums.phpfreaks.com/topic/174971-solved-variable-in-an-array/#findComment-923216 Share on other sites More sharing options...
mikesta707 Posted September 23, 2009 Share Posted September 23, 2009 that means that your query failed. echo mysql_error() and see what error you get Quote Link to comment https://forums.phpfreaks.com/topic/174971-solved-variable-in-an-array/#findComment-923219 Share on other sites More sharing options...
ozestretch Posted September 23, 2009 Share Posted September 23, 2009 that means that your query failed. echo mysql_error() and see what error you get Is the reason why I asked if they changed any other code :-\ Quote Link to comment https://forums.phpfreaks.com/topic/174971-solved-variable-in-an-array/#findComment-923222 Share on other sites More sharing options...
woop Posted September 23, 2009 Author Share Posted September 23, 2009 Thanks ozestretch but I still get the same error. Mike I already have the "echo mysql_error()" in the query bit. Not sure if there's somewhere else I need to add it. I'm not sure if I described this properly initially, but the variable $days needs to contain all of the quoted text below: 9=>array('http://www.url.info/732/2009/September/9.php','linked-day'), 18=>array('http://www.url.info/732/2009/September/18.php','linked-day'), 21=>array('http://www.url.info/732/2009/September/21.php','linked-day'), ...which is pulled ffrom the while loop. Sometimes there will be zero lines, other times up to 30 lines. Each line has an array in it (as in the above example) so far when I echo the value of $stringy from the code below: mysql_connect("localhost", "user", "pass") or die(mysql_error()); mysql_select_db("db") or die(mysql_error()); $query = "SELECT * FROM table WHERE monthofyear = 'September'"; $result = mysql_query($query) or die(mysql_error()); $classy = '700'; while($row = mysql_fetch_array($result)){ $stringy .= $row['dateofmonth'] . "=>array('http://www.url.info/" . $classy . "/" . $row['year'] . "/" . $row['monthofyear'] . "/" . $row['dateofmonth'] . "','linked-day'), <br />"; } ... I get the correct contents: 9=>array('http://www.url.info/732/2009/September/9.php','linked-day'), 18=>array('http://www.url.info/732/2009/September/18.php','linked-day'), 21=>array('http://www.url.info/732/2009/September/21.php','linked-day'), ... but then I'd need to get this 'text' (which includes array statements) inside an array: $days = array($stringy); With the suggestions so far do they create a list of arrays inside an array? I am really stumped with this. Have been working on it for days Quote Link to comment https://forums.phpfreaks.com/topic/174971-solved-variable-in-an-array/#findComment-923224 Share on other sites More sharing options...
ozestretch Posted September 23, 2009 Share Posted September 23, 2009 When you run this <?php mysql_connect("localhost", "user", "pass") or die(mysql_error()); mysql_select_db("db") or die(mysql_error()); $query = "SELECT * FROM table WHERE monthofyear = 'September'"; $result = mysql_query($query) or die(mysql_error()); $classy = '700'; while($row = mysql_fetch_array($result)){ $stringy .= $row['dateofmonth'] . "<br />"; } echo $stringy; ?> What is outputted? Quote Link to comment https://forums.phpfreaks.com/topic/174971-solved-variable-in-an-array/#findComment-923225 Share on other sites More sharing options...
woop Posted September 23, 2009 Author Share Posted September 23, 2009 Guys - my bad. I thought that it was ok that I did a db connection and query outside a function, and then the while statement for the result inside a function that was called. Now that I put everything together inside the function it works thanks to your help. I'm sorry that I messed you guys around, but I really appreciate your help -it is now working thanks to your comments here. I'm a math teacher developing a portal for me to upload lessons and homework for my middle school students, and you help has now made my site work (though I have a lot of other stuff to do). Thanks again - and my students will be more than happy that they have no excuse (now that they can view their homework automatically online now!) I can't say thank you enough. Quote Link to comment https://forums.phpfreaks.com/topic/174971-solved-variable-in-an-array/#findComment-923229 Share on other sites More sharing options...
ozestretch Posted September 23, 2009 Share Posted September 23, 2009 Apologise to your students for us, we meant them no harm If help needed again, ensure you post ALL relevant code Quote Link to comment https://forums.phpfreaks.com/topic/174971-solved-variable-in-an-array/#findComment-923231 Share on other sites More sharing options...
redarrow Posted September 23, 2009 Share Posted September 23, 2009 Maths teacher can we see the web site in question please be fun..... Quote Link to comment https://forums.phpfreaks.com/topic/174971-solved-variable-in-an-array/#findComment-923261 Share on other sites More sharing options...
woop Posted September 23, 2009 Author Share Posted September 23, 2009 Thanks ozestretch. @redarrow - I'll try to remember to come back and post it when I finally get it finished. There are other components for me to fix first, but then I'll put it out there. Quote Link to comment https://forums.phpfreaks.com/topic/174971-solved-variable-in-an-array/#findComment-923356 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.