Jump to content

[SOLVED] How to update this array?


YourNameHere

Recommended Posts

I am saving a string that looks this: username1, username2, username3...

into a single cell in a database table.

I am doing this so that when I retrieve that string using normal methods, I can turn it into an array.

 

However, my question is: I will need to update that cell and add user names to it. and then save it back to the database.

I don't have any example code as I haven't started because I am not sure where to start.

 

This is what I was thinking.

 

It will start out with one username "username1".

That is what the original value will be. I then need to pull that info out (easy) and populate an array

 

$original = 'username1';
$userArray = array($original);

I then need to turn around and add 'username2' to $userArray. and add it to the DB so it is formatted as username1, username2, username3...

Link to comment
https://forums.phpfreaks.com/topic/180945-solved-how-to-update-this-array/
Share on other sites

Be sure to check out the array functions in the PHP manual...

Also using this method you may also want to check out the "explode" and "implode" functions as well...

 

But here is something to help you out:

 

If you are wanting to replace a value within an array here is some code:

$userarray=array("username1","username2");

$testusername="username1";

if(in_array($testusername,$userarray)){    //First let's make sure the username is in the array
  $key=array_search($testusername,$userarray);  //if it is, let's find out where
  $userarray[$key]=$newusername;  //Now we use the key to change it to something else
}else{
  echo "That username is not in the array!"; //if it's not in the array we print an error message
}

 

If you are just wanting to add some new info to an array use this:

$userarray=array("username1","username2");
$newusername="username3";
array_push($userarray,$newusername);

Thanks for the reply! It's helpful info but not a complete solution to my issue. Once I get the new username into the array, how would I save it back to the DB formatted like I need?

 

Would I explode the array and add them together as a string?

 

But how would I do that dynamically?

No, if you are wanting to change an array into a string of usernames separated by a comma you want to use the implode function.

http://php.net/manual/en/function.implode.php

 

Example:

 

$userarray=array("username1","username2","username3");
$separated=implode(",",$userarray);
echo $separated;

 

This would output:  username1,username2,username3

 

Ok so, I have it working so far, but when I echo it it echos the right string but when I place the imploded array into the query. it breaks and when I echo the query out this is what I get.

 

Code:

    // Add to the chat players column
    $j = "Select players from chats where name = '$chatId'";
    $acceptResult = mysql_query($j);
    $arrayJ = mysql_fetch_array($acceptResult);
    
    $newusername=$_SESSION['username'];
    
    //add the new player to the array
    array_push($arrayJ,$newusername);
    
    //add it back to the DB after converting to string
    $separated = implode(",",$arrayJ);
    
    $DMsql = "update chats SET players = '$seperated' where name = '$chatId'";
    
    if (!$DMsql)
    {
        die(mysql_error());
    } else {
        echo "Update worked!<br>";
    }

 

When I echo $j

I get

 

update chats SET players = '' where name = 'Chat_1257848622'

 

Why is $seperated not showing in the query? Do I need to escape a sting that is like "Cody,YourNameHere"?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.