2

Im trying to setup a dynamic settings database for my CMS

Database is set up with two columns.

Name | Value

Template | Exige

InstallFolder | v2

Basically I want to pull all the data out and put it into different variables.

for example i'm trying to set my base_folder(installation folder) into a variable.

So say $base_folder = $CoreSettings[1] output which should show (v2)

$sql = "SELECT * FROM core_settings";
$query = mysqli_query($dbc, $sql) or die (mysqli_error($dbc));

$CoreSettings = array();

while($row = mysqli_fetch_assoc($query)){

  // add each row returned into an array
   $CoreSettings[] = $row; 

 }

 $base_folder = $CoreSettings;
  foreach( $CoreSettings as $key => $val)
   { $$key = $val; }

var_dump($base_folder);

This then outputs:

array(5) { [0]=> array(2) { ["name"]=> string(8) "Template" ["value"]=> string(5) "exige" } [1]=> array(2) { ["name"]=> string(13) "InstallFolder" ["value"]=> string(2) "v2" } [2]=> array(2) { ["name"]=> string(16) "MaintainanceMode" ["value"]=> string(1) "0" } [3]=> array(2) { ["name"]=> string(4) "Logo" ["value"]=> string(8) "Logo.png" } [4]=> array(2) { ["name"]=> string(8) "SiteName" ["value"]=> string(18) "Black Nova Designs" } } 

I would like to just get the InstallFolder Value.

so

$base_folder = V2

Sorry if this comes across stupid just really racked my brain about, and code is probably quite messed up due to trying multiple methods.

But in the long run I would like to get every setting out of the database and be able to assign a variable to each setting.

for example

$SiteName = $CoreSettings[4];
$maintenance = $CoreSettings[2];

Thanks In advanced. Kyle

2
  • In your foreach loop it's $val['name'] or "InstallFolder". But I think you want to make 2 functions. 1 for getting the data from 1 id and 1 for getting all data Commented Aug 5, 2015 at 10:32
  • Could you show a quick example, sorry just trying to refocus... Commented Aug 5, 2015 at 10:35

1 Answer 1

1

Index you $CoreSettings array with the values of the 1st column, then you can access the values by the appropriate key:

$CoreSettings = array();

while($row = mysqli_fetch_assoc($query)){

   // add each row returned into an array
   $CoreSettings[$row['name']] = $row['value']; 

}

echo $CoreSettings['InstallFolder']; //outputs V2
echo $CoreSettings['Template']; //outputs Exige
Sign up to request clarification or add additional context in comments.

4 Comments

Using that doesn't seem to output anything at all.
@KyleHolmes See edit, i missed the fact you where using mysqli_fetch_assoc
Thank you!! So for future proofing if I want to use anything now all I need to do. $base_folder = $CoreSettings['InstallFolder']; and $SiteName = $CoreSettings['SiteName']; Thanks, Kyle
@KyleHolmes No problem, gald i could help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.