cliftonbazaar Posted October 7, 2010 Share Posted October 7, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/215304-tightening-up-code/ Share on other sites More sharing options...
cliftonbazaar Posted October 7, 2010 Author Share Posted October 7, 2010 Sorry but the line should be while ($info = $result->fetch_field()) {$info->name = explode(";", ${$info->name});} Can't seem to edit my first post Quote Link to comment https://forums.phpfreaks.com/topic/215304-tightening-up-code/#findComment-1119646 Share on other sites More sharing options...
PFMaBiSmAd Posted October 7, 2010 Share Posted October 7, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/215304-tightening-up-code/#findComment-1119649 Share on other sites More sharing options...
cliftonbazaar Posted October 7, 2010 Author Share Posted October 7, 2010 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). Quote Link to comment https://forums.phpfreaks.com/topic/215304-tightening-up-code/#findComment-1119658 Share on other sites More sharing options...
PFMaBiSmAd Posted October 7, 2010 Share Posted October 7, 2010 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 )) Quote Link to comment https://forums.phpfreaks.com/topic/215304-tightening-up-code/#findComment-1119659 Share on other sites More sharing options...
cliftonbazaar Posted October 7, 2010 Author Share Posted October 7, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/215304-tightening-up-code/#findComment-1119928 Share on other sites More sharing options...
PFMaBiSmAd Posted October 7, 2010 Share Posted October 7, 2010 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; Quote Link to comment https://forums.phpfreaks.com/topic/215304-tightening-up-code/#findComment-1119932 Share on other sites More sharing options...
cliftonbazaar Posted October 9, 2010 Author Share Posted October 9, 2010 Works absolutely perfect, thanks for that Quote Link to comment https://forums.phpfreaks.com/topic/215304-tightening-up-code/#findComment-1120647 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.