Best Way

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Best Way

Post by d3ad1ysp0rk »

Tried looking over the phpBB files to see how they do it, then searched around google n php.net, but can't find what I'm looking for anywhere..

How would I make a "config" type file (it's already made), that allows me to have the admin enter a few things (ie. site name, background color, etc..) and those things could be used from then on in all the files?

or would i need to use a database to store the settings..?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Various ways... The easier one is just to store all 'settings' in one file and the include that in all other pages that neds the information...

Code: Select all

// config.php
<?php
 $site['title'] = "My Homepage!";
 $site['name'] = "Dr. Foo";
 $site['email'] = "foo@bar.com";
// ...
?>

// page.php
<?php
 require_once 'config.php';
 function foo() {
  global $site;
  echo 'Hi, my name is '.$site['name'].'!';
 }
You can use a database to store settings, but you still would need a config file to change database settings etc.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

ya but if i wanted to make it so the user could enter the config stuff in the browser (without having to ever look at the source), then I'd have to do something like:

Code: Select all

<?php 
$namesql = "SELECT `name` FROM `settings`";
$name = mysql_result(mysql_query($namesql), 0,0);
$site['name'] = $name;
?>
and then have an install.html page:

Code: Select all

&lt;html&gt;
&lt;body&gt;
&lt;form action="install.php" name="settings" method="post"&gt;
Site Name: &lt;input type="text" name="name"&gt;&lt;br&gt;
&lt;input type="submit" name="submit" value="Submit"&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
then install.php...

Code: Select all

<?php
if(isset($_POST['name'])){
//connect to DB
$sql = "INSERT INTO `settings` (name) VALUES ('$name')";
mysql_query($sql);
echo "Thank you for changing the settings, you may now delete install.html and install.php from the directory.";
?>

would that work? or is there an easier way?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Sure...
There is a nifty techinque for this that alot of buletin boards use. By manually running say install.php questions is asked using <form>'s. The responces given in those are written to a second php-file that is later included/required.

Simple example of this, not very modified from the php manual about fwrite:

Code: Select all

<pre>
<form method="post">
 <input type="text" name="name" />
 <input type="submit" />
</form>
<?php
    if (isset($_POST['name']) and strlen($_POST['name']) > 0) {
        $filename = 'test.txt';
        $somecontent = '<?php echo "'. $_POST['name'] .'"; ?>';
        if (is_writable($filename)) {
            if (!$handle = fopen($filename, 'a')) {
                echo "Cannot open file ($filename)";
                exit;
            }
            if (!fwrite($handle, $somecontent)) {
                echo "Cannot write to file ($filename)";
                exit;
            }
            echo "Success, wrote ($somecontent) to file ($filename)";
            fclose($handle);
        } else {
            echo "The file $filename is not writable";
        }
}

include 'test.txt';
// Write something in the form. Upon submitting, it should be written to the test.txt file.

?>
This way, you can add lines after lines of configuration variables, that you later use to insert into the database (or simply use as is as file as my first post).

Hope it helped...
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

so maybe the "somecontents variable" could be like "<?php\n$name = $_POST['name'];\n?>"

and then i could include it on another file, and name would be whatever they entered?
Sarok
Forum Newbie
Posts: 9
Joined: Fri Jan 02, 2004 12:20 pm
Location: Oslo, Norway

Post by Sarok »

I dont know if you'd prefer a file or a mysql database. But if you want to use a database to store settings, just create a table called settings or something. Then add a Setting and a Value column. Then you can just use a code like below:

Code: Select all

<?php

$query = "SELECT * from settings ORDER BY setting";
$result = mysql_query($query);

while ($row = mysql_fetch_array($result)) &#123;

	$settings&#1111;$row&#1111;"setting"]] = $row&#1111;'value'];
&#125;
 
?>
This script just gets all settings rows and creates variables accordingly, as in $settings['body_bg_color'] = #000000.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

LiLpunkSkateR wrote:so maybe the "somecontents variable" could be like "<?php\n$name = $_POST['name'];\n?>"

and then i could include it on another file, and name would be whatever they entered?
Basicly yes, that is one idea.

You can also if preferred do the same as far getting the data and writing it to a file. but instead of using the file and include/require, you can take it's information and add that to your database and then delete that file.

Mileage may vary.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

i ended up making another table (settings) and it's working fine, thanks for your help.

but i have a totally different question now.
the links script im making ( http://www.404-design.net/links.php or http://www.404-design.net/linkadmin.php with password 4985) has the user add their link, and when they do, it adds it to the db with 2 values.
con(confirmed) = 0
and new = 1;

when i delete, it changes new to 0, and if you click approve, it changes con to 1.
only links with new = 1 and con = 1 are displayed on the links page
on the admin page, only links with new = 1 are (whether its confirmed or not, it gets displayed). Might be easier if you take a look at the links i provided.

now i thought that it would be a waste to keep all the links in the DB before, but I couldnt find a way to delete them without the script messing up.. so either i find a way to delete them and run the script fine, or it'll be a waste of db space (especially if over 100+ people try to submit their links)

it wouldnt be too bad, but it saves EVERY link submitted, whether it's approved or not.

this is the part of code that gets the stuff:

Code: Select all

$rowsql = "SELECT * FROM `$table`";
   $result = mysql_query($rowsql);
   $rows = mysql_num_rows($result);
   $i = 0;
   while($i<$rows){
      $i++;
      $csql = "SELECT `con` FROM `$table` WHERE `id` = '$i' LIMIT 1";
      $n2sql = "SELECT `con` FROM `$table` WHERE `id` = '$i' LIMIT 1";
      $lsql = "SELECT `link` FROM `$table` WHERE `id` = '$i' LIMIT 1";
      $nsql = "SELECT `name` FROM `$table` WHERE `id` = '$i' LIMIT 1";
      $dsql = "SELECT `description` FROM `$table` WHERE `id` = '$i' LIMIT 1";
      $con = mysql_result(mysql_query($csql), 0,0);
      $new = mysql_result(mysql_query($n2sql), 0,0);
      $link = mysql_result(mysql_query($lsql), 0,0);
      $name = mysql_result(mysql_query($nsql), 0,0);
      $description = mysql_result(mysql_query($dsql), 0,0);
      if($con=="1" && $new=="1"){
echo <<<EOT
<a href="$link">$name</a><br>
&nbsp;&nbsp;&nbsp;$description<br><br>
EOT;
      }
   }
now when i delete one of the links that wasnt approved (ie. someone screwing with the script or submitting a site that the webmaster didnt want on it (pr0n, warez, etc..), then it gives me this:

Code: Select all

Warning: mysql_result(): Unable to jump to row 0 on MySQL result index 35 in /home/des404/public_html/links.php on line 38

Warning: mysql_result(): Unable to jump to row 0 on MySQL result index 36 in /home/des404/public_html/links.php on line 39

Warning: mysql_result(): Unable to jump to row 0 on MySQL result index 37 in /home/des404/public_html/links.php on line 40

Warning: mysql_result(): Unable to jump to row 0 on MySQL result index 38 in /home/des404/public_html/links.php on line 41

Warning: mysql_result(): Unable to jump to row 0 on MySQL result index 39 in /home/des404/public_html/links.php on line 42
Sarok
Forum Newbie
Posts: 9
Joined: Fri Jan 02, 2004 12:20 pm
Location: Oslo, Norway

Post by Sarok »

The reason you are getting the error message, (Warning: mysql_result():), is because when you delete a link in your table. The script will try to get mysql_result from an empty row. As in, you got no result checking, so example; if you delete linkid "35", your script will try to get results from an empty row.

To fix this the easy way, just add a if (mysql_num_rows($result) > 0) { do stuff) } else { do nohing, $i++ }

But on another note, you really should make two tables, one with new links, and one with approved links. So when you approve new links you add them to the approved links table, and delete them from the new links table.
Post Reply