Jump to content

Recommended Posts

I have some code which works:

 

 

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:

 

 

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.

 

Link to comment
https://forums.phpfreaks.com/topic/174971-solved-variable-in-an-array/
Share on other sites

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

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');
}

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.

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

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:

 

 

...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:

 

 

... 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 :(

 

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?

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.

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.