Jump to content

Recommended Posts

I recently discovered arrays... when I say recent, I mean today, haha...

 

I've looked around and its incredible hard to find a tutorial on it.

 

Let's say this is my array:

$names = array("Brian","Billy","Bob","Joe");

 

How am I suppose to update my array?

mysql_query("UPDATE site_table SET friends = 'what do i put in here' WHERE user_id = 2");

 

Also, if I want to add another name to the array, how am I to do that?

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/89108-solved-inserting-and-updating-arrays/
Share on other sites

Here is how you would add a name later on in the script

 

<?php

$names = array("Brian","Billy","Bob","Joe");

//Add a name
$names[] = "Cindy";

?>

 

As for your query question, I would need to see how you had your database table setup to know how to insert the information.

So you want it to appear in the database in a text field as one line like so?

Brian Billy Bob Joe

 

If thats how you want it, then you would do this:

 

<?php

$names = array("Brian","Billy","Bob","Joe");

$query = "UPDATE site_table SET friends = '" . implode($names, " ") . "' WHERE user_id = 2"
$result = mysql_query($query)or die(mysql_error());

?>

 

Thanks pocobueno1388 for your rapid responses.

 

OK, I think I might not have understood the use of arrays...

 

Am I still able to call upon the the friends field as an array? like $friends[3]? And it would show Bob.

 

Yes, you will still be able to call which name you want from the array by using the key like you did.

 

But Bob would be $friend[2] because the counter starts at 0...here is an example.

 

<?php

$names = array("Brian","Billy","Bob","Joe");
                  0       1      2     3

 

Does that make sense?

They would be in a string. Although you can easily change it back to an array if you wanted like this:

 

<?php

//we will say this is the variable from the database
$friends = $row['friends'];

//put them back into an array
$friends = explode(" ", $friends);

?>

Wow, awesome pocobueno1388... you are very knowledgeable.

 

One last thing and I'll leave you alone. Let's say instead of a " " space, since I might have a name that includes the last name "Bob Barker". I want all my array stored in the database with brackets.

 

For example:

[brian][billy][bob Barker][Joe][Cindy]

 

How am I suppose to explode that into an array?

Maybe it's this?

$friends = explode("[", $friends, "]");

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.