explode and implode, foreach
06-17-2010, 09:30 PM
|
explode and implode, foreach
|
Posts: 35
|
I need to use data from an exploded string then edit it then implode and update in a DB. I guess I am stuck on how I can do this with a foreach statement.
Here is my code:
PHP Code:
<?php //connect require("connect.php") ; require("functions.php") ; //get attack data $attack_query = "SELECT name,id FROM attacks" ; $attack_results = mysql_query($attack_query) ;
if($_POST){ while($attacks = mysql_fetch_array($attack_results) ){ if($_POST['attack'] == $attacks['id']){ //get user attacks $user_query = "SELECT attacks FROM user_info WHERE user_id=3" ; $user_results = mysql_query($user_query) ; $user = mysql_fetch_array($user_results) ; $user_attacks = double_explode(",","-",$user['attacks']) ; //check if attack already purchased foreach($user_attacks as $key=>$user_attack){ if( ! $attacks['id'] == $user_attack[0] ){ if($user['attacks'] == ""){ $user['attacks'] = $user['attacks'] . $attacks['id'] . "-1-0," ; mysql_query("UPDATE user_info SET attacks='{$user['attacks']}' WHERE user_id=3") ; break ; } else{ $user['attacks'] = $user['attacks'] . $attacks['id'] . "-1-0," ; mysql_query("UPDATE user_info SET attacks='{$user['attacks']}' WHERE user_id=3") ; break ; } } elseif( $attacks['id'] == $user_attack[0]){ $user_attack[1]++ ; echo $user_attack[1] ; } } } } }
if(! $_POST){ echo"<form method='post' action=''> <select name='attack'>" ; while($attacks = mysql_fetch_array($attack_results) ){ echo "<option value='{$attacks['id']}'>{$attacks['name']}</option>" ; } echo"</select> <input type='submit' value='Submit' /> </form>" ; } ?>
As you can see I am trying to take the [1] element and change it. After it is changed I need to implode that into the original sring then I can UPDATE my db from there
Here is a mini script I was working for proof of concept
PHP Code:
<?php require("functions.php") ;
$attacks = "1-1-exp,2-1-exp" ;
$array = double_explode(',','-',$attacks) ;
$i=2 ;
foreach($array as $key=>$user_attack){ if($user_attack[0] == $i){ $user_attack[1] += 1 ; } else{ echo"you messed up" ; } } echo double_implode('-',',',$array) ;
?>
Last edited by Direct; 06-17-2010 at 09:45 PM..
|
|
|
|
06-17-2010, 10:36 PM
|
Re: explode and implode, foreach
|
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
|
It really depends on whatever this user function double_explode() is doing and how the array is structured from it.
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
|
|
|
|
06-17-2010, 10:41 PM
|
Re: explode and implode, foreach
|
Posts: 35
|
Ah I guess I should explain that.
$string = "1-1-0,2-3-3"
Using the double explode function it becomes a 2d array as
[0][0] = 1
[0][1] =1
[0][2] = 0
etc
|
|
|
|
06-17-2010, 10:52 PM
|
Re: explode and implode, foreach
|
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
|
I guess I don't understand what you are trying to achieve, it appears to me that what you already have would bring it back to string format to save back into the db.
Something else to consider is to keep the data in an array context, and store and extract it from the db using the serialize() and unserialize() functions.
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
|
|
|
|
06-17-2010, 10:58 PM
|
Re: explode and implode, foreach
|
Posts: 35
|
Hmm alright let me try and explain what this script is actually for. It is for a game I am making this is the attack ourchae and training.
If the user already owns the attack they will have to train it. the second number in the string is the level so it is
id-level-exp
I need to get the level seperate from the string hence the explode. Now if the user has the move I need to increase the level then implode the string back together then insert into the DB. That is where I am stuck
|
|
|
|
06-17-2010, 11:27 PM
|
Re: explode and implode, foreach
|
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
|
I tried to make something that would be self-explainatory. I also created two custom new functions.
PHP Code:
$attacks = '1-1-exp,2-1-exp'; $move_user = 2; $attacks = explode(',', $attacks); $array = array(); foreach ($attacks AS $attack) { $attack = decode_user_exp($attack); if ($attack['user'] == $move_user) { $attack['level']++; } else { echo 'You messed up!'; } $array[] = encode_user_exp($attack); } echo '<pre>' . implode(',', $array) . '</pre>';
function decode_user_exp($string, $explode = '-') { $data = explode($explode, (string)$string); return array( 'user' => (int)$data[0], 'level' => (int)$data[1], 'exp' => (string)$data[2], ); }
function encode_user_exp($array, $implode = '-') { return implode($implode, (array)$array); }
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
|
|
|
|
06-17-2010, 11:43 PM
|
Re: explode and implode, foreach
|
Posts: 35
|
I know you want it to be self explanitory but I have been teaching myself php for like a month so you might have to help me out with what it is doing
|
|
|
|
06-18-2010, 12:04 AM
|
Re: explode and implode, foreach
|
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
|
PHP Code:
// String stored in the database (user-level-exp) $attacks = '1-1-exp,2-1-exp'; // The user id who has the move, used to increment the level one point if id matches $move_user = 2; // Convert the string into an array exploded be a comma $attacks = explode(',', $attacks); // Create an empty array used to store the revised data $array = array(); // Loop through the exploded array from the string above foreach ($attacks AS $attack) { // Decode the sub-string value $attack = decode_user_exp($attack); // Check if the user id matches the $move_user value if ($attack['user'] == $move_user) { // User id matches, increment the level by one point $attack['level']++; } else { // User id does not match the $move_user value echo 'You messed up!'; } // Enocde the array back into a string $array[] = encode_user_exp($attack); } // Implode the sub-string back into a full string to store back into the db echo '<pre>' . implode(',', $array) . '</pre>'; // Custom funtion to explode the sub-string into an array function decode_user_exp($string, $explode = '-') { $data = explode($explode, (string)$string); // Return a relational array return array( 'user' => (int)$data[0], 'level' => (int)$data[1], 'exp' => (string)$data[2], ); } // Custom function to implode an array back into a sub-string function encode_user_exp($array, $implode = '-') { return implode($implode, (array)$array); }
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
|
|
|
|
|
« Reply to explode and implode, foreach
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|