Jump to content

[SOLVED] Create an array and echo certain rows.


samtwilliams

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'];
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.