Jump to content

Recommended Posts

At the moment I have

 

$currentSquares=explode(";", $combatStats['currentSquares']);  //Get all the current square values  $speech=explode(";", $combatStats['speech']);  //Get all the speeches values  $setup=explode(";", $combatStats['setup']);  //Get all the setup values

 

now this is repeated for another 12 lines!

 

The basics of what I am trying to achieve is to get all the values out of a database (which are all imploded arrays) and explode them into the data that I need.

Is there a way of going through a loop and getting each columns heading and then exploding them?

 

I am only a beginner at PHP and the closest I can work out is

 

$query = "SELECT * FROM combat";  if ($result = $mysqli->query($query)) {    while ($info = $result->fetch_field()) {$info->name = ", '".explode(";", ${$info->name})."'";}    $result->close();  }

 

but this doesn't work  :

Link to comment
https://forums.phpfreaks.com/topic/215304-tightening-up-code/
Share on other sites

What method are you using to get the $combatStats array that you show holding all the data in your first piece of code?

 

Also, what is your final goal? Getting all the values (for display or as settings) or getting all the values and then finding specific values among all the data?

Link to comment
https://forums.phpfreaks.com/topic/215304-tightening-up-code/#findComment-1119649
Share on other sites

What method are you using to get the $combatStats array that you show holding all the data in your first piece of code?

 

If I understand your question correctly - the code for this is

 

//Load in the actual fight  $sql_statement = "SELECT * FROM combat WHERE combatID='$other[7]'";  $combatStats = mysqli_fetch_array(mysqli_query($mysqli, $sql_statement), MYSQLI_ASSOC);

 

 

 

Also, what is your final goal? Getting all the values (for display or as settings) or getting all the values and then finding specific values among all the data?

 

It is for a combat game that is database driven - the goal at this point is to get all the information from the combat database and explode them into the variable names, this can then be used during the combat phase (so, for settings).

Link to comment
https://forums.phpfreaks.com/topic/215304-tightening-up-code/#findComment-1119658
Share on other sites

After your code that sets $combatStats, I would do the following (keeps the data in an array called $data in this example for easier access) -

 

$data = array();foreach($combatStats as $key => $value){$data[$key] = explode(";",$value);}

 

 

This will produce an array that looks like -

 

Array(    [currentSquares] => Array        (            [0] => a            [1] => b            [2] => c        )    [speech] => Array        (            [0] => d            [1] => e            [2] => f        )    [setup] => Array        (            [0] => g            [1] => h            [2] => i        ))

 

 

Link to comment
https://forums.phpfreaks.com/topic/215304-tightening-up-code/#findComment-1119659
Share on other sites

Got it all working perfectly (thanks for that!); now that I have programmed all the combat code I now realise that over the half of the variables have changed and they all need to be saved (characters & monsters all move, take damage, change plan e.t.c) so how do I do the opposite and implode all the variables and save them to the database?

 

I had one attempt but managed to corrupt all the data :(

Link to comment
https://forums.phpfreaks.com/topic/215304-tightening-up-code/#findComment-1119928
Share on other sites

Based on the query you posted that gets the data and using the $data array that I showed, the following would produce a query that updates the row with any data in the array -

 

$query = "UPDATE combat SET ";
foreach($data as $key => $value){
$value = implode(';',$value);
$query .= "$key = '$value',";
}
$query = rtrim($query,',');
$query .= " WHERE combatID='$other[7]'";
// execute your query here...
echo $query;

Link to comment
https://forums.phpfreaks.com/topic/215304-tightening-up-code/#findComment-1119932
Share on other sites

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.