Jump to content

Recommended Posts

Morning All,

 

I have a settings table in my database which contains various settings for my website which i need to echo on certain pages but i am stuck on how to create the array of results so i can echo certain values. My table looks like this;

 

paramvalue

index_titleMy Website.

index_introThis is the introduction of my page.

 

And my code like this;

 

<?PHP include('inc/flat_settings.inc.php');
// DB CONNECTIONS HERE
mysql_connect('localhost',$dbusername,$dbpassword) or die( "Unable to connect to database server.");
@mysql_select_db($database) or die( "Unable to select database.");
// DB QUERY HERE
$query_globals = "SELECT param, value FROM `globals`";
$result_globals = mysql_query($query_globals);
while ($row_globals = mysql_fetch_array($result_globals)) {

$arr = array($row_globals['param'] => $row_globals['value']);

}
?>

 

I want to be able to echo my value like this;

 

echo $arr['index_title'] which should return "My Website."

 

At the moment it seems nothing is being captured in my array.

Hope someone can help

Sam

<?PHP include('inc/flat_settings.inc.php');
// DB CONNECTIONS HERE
mysql_connect('localhost',$dbusername,$dbpassword) or die( "Unable to connect to database server.");
@mysql_select_db($database) or die( "Unable to select database.");
// DB QUERY HERE
$query_globals = "SELECT param, value FROM `globals`";
$result_globals = mysql_query($query_globals);
$arr = mysql_fetch_array($result_globals);

}
?>

 

try that just echo out $arr['index_title'];

When i run this;

<?PHP include('inc/flat_settings.inc.php');
// DB CONNECTIONS HERE
mysql_connect('localhost',$dbusername,$dbpassword) or die( "Unable to connect to database server.");
@mysql_select_db($database) or die( "Unable to select database.");
// DB QUERY HERE
$query_globals = "SELECT param, value FROM `globals`";
$result_globals = mysql_query($query_globals);
while ($row_globals = mysql_fetch_assoc($result_globals)) {

$arr = array($row_globals['param'] => $row_globals['value']);
   
   echo '<pre>';
   echo htmlspecialchars(print_r($arr, true));
   echo '</pre>';
}

echo $arr['global_title_index'];
echo $arr['global_logo_title'];
?>

 

I get the following results but only echos global_logo_title;

 

Array

(

    [global_title_index] => Title of my page

)

 

Array

(

    [global_logo_title] => My logo title

)

 

My logo title

What I don't understand is what you are trying to accomplish. You are simply calling for an array and then adding it into another array why is this?

 

Can this not work:

<?PHP include('inc/flat_settings.inc.php');
// DB CONNECTIONS HERE
mysql_connect('localhost',$dbusername,$dbpassword) or die( "Unable to connect to database server.");
@mysql_select_db($database) or die( "Unable to select database.");
// DB QUERY HERE
$query_globals = "SELECT param, value FROM `globals`";
$result_globals = mysql_query($query_globals);
$row_globals = mysql_fetch_array($result_globals);

echo $row_globals['global_title_index'];
echo $row_globals['global_logo_title'];
?>

This seems to echo no results though?

 

My database looks exactly like this;

 

id param value help

1 global_title_index Title of my page Title bar of the index page.

2 global_logo_title My logo title Logo title text.

 

 

Using the code you supplied surley only allows me to echo the first row calling it by the increment number the array sets?

 

I tried this with your code;

 

echo $row_globals['1'];

and the result that was displayed was "Title of my page"

 

Surely i need to create the array with a loop?

FYI

 

<?PHP include('inc/flat_settings.inc.php');
// DB CONNECTIONS HERE
mysql_connect('localhost',$dbusername,$dbpassword) or die( "Unable to connect to database server.");
@mysql_select_db($database) or die( "Unable to select database.");
// DB QUERY HERE
$query_globals = "SELECT param, value FROM `globals`";
$result_globals = mysql_query($query_globals);
$arr = array();
while ($row_globals = mysql_fetch_array($result_globals)) {
$arr[$row_globals['param']] = $row_globals['value'];
}
?>

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.