Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
explode and implode, foreach
Old 06-17-2010, 09:30 PM explode and implode, foreach
Experienced Talker

Posts: 35
Trades: 0
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=;

foreach(
$array as $key=>$user_attack){
     if(
$user_attack[0] == $i){
         
$user_attack[1] += ;
        }
     else{
         echo
"you messed up" ;
        }
    }
echo 
double_implode('-',',',$array) ;


?>

Last edited by Direct; 06-17-2010 at 09:45 PM..
Direct is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-17-2010, 10:36 PM Re: explode and implode, foreach
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
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.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 06-17-2010, 10:41 PM Re: explode and implode, foreach
Experienced Talker

Posts: 35
Trades: 0
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
Direct is offline
Reply With Quote
View Public Profile
 
Old 06-17-2010, 10:52 PM Re: explode and implode, foreach
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
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.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 06-17-2010, 10:58 PM Re: explode and implode, foreach
Experienced Talker

Posts: 35
Trades: 0
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
Direct is offline
Reply With Quote
View Public Profile
 
Old 06-17-2010, 11:27 PM Re: explode and implode, foreach
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
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.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 06-17-2010, 11:43 PM Re: explode and implode, foreach
Experienced Talker

Posts: 35
Trades: 0
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
Direct is offline
Reply With Quote
View Public Profile
 
Old 06-18-2010, 12:04 AM Re: explode and implode, foreach
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
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.
mgraphic is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to explode and implode, foreach
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.26774 seconds with 12 queries