Edit Variables contained in a file

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
Smudly
Forum Commoner
Posts: 71
Joined: Wed Jun 09, 2010 10:09 pm

Edit Variables contained in a file

Post by Smudly »

I want to create a page that contains all sorts of Variables with values such as Prices, Name of Site, Rates, etc.

I want to create an admin page, that allows the admin to easily edit the values inside this php file. How does this work without the variables being reset to their default values everytime the page is reloaded.

I tried google but wasn't sure what exactly to search for. I'll take anything you can give me. A link to learn more about how to do something like this or even some tips & tricks.
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: Edit Variables contained in a file

Post by yacahuma »

why dont you use a database(mysql)?
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Re: Edit Variables contained in a file

Post by liljester »

or if you dont want to install a database service, you could try the sqlite database driver for php, it uses text files. however a real database would be the better option.
Thizzle
Forum Newbie
Posts: 12
Joined: Fri Jul 23, 2010 12:29 pm

Re: Edit Variables contained in a file

Post by Thizzle »

PHPmyadmin, SQL, and Apache.

The language you would do it in is PHP. The admin page is going to take some coding know how and won't be cheap.
Smudly
Forum Commoner
Posts: 71
Joined: Wed Jun 09, 2010 10:09 pm

Re: Edit Variables contained in a file

Post by Smudly »

Thank you for your replies. I have been working on doing it in sql. So far I have a table called 'vars'. Inside this table, I have 2 Rows. The first row is the default values that I have set. The second row are the new values for every variable. I've been working on the admin page, and am doing a while loop, to get every value for every column where id=2. It shows up properly, however updating it is a bit more complicated that anticipated.

The name of each value is set by $meta->name. (This is the column name for the current value). When I hit edit, it should update each column value where the name is equal to the column name. This is what I have right now, and it isn't updating properly (I'm not getting any errors).

Any input appreciated.

Code: Select all

<?php

include_once('admin.php');
include_once(connect.php');


// In admin area, Calculate how much each credit, view, impression is costing at the current price
// Do if statements and determine if the value should have a calculation to get rate

$varsquery = mysql_query("SELECT * FROM vars WHERE id='2'");
$row = mysql_fetch_array($varsquery);

$varsquery1 = mysql_query("SELECT * FROM vars WHERE id='1'");
$row1 = mysql_fetch_array($varsquery1);

$edit = $_GET['edit'];
$newvalue = "";

echo "
<html>
<head>
<title>Variables</title>
<style>
a:link{
text-decoration: none;
color: #519904;
}
a:visited{
text-decoration: none;
color: #519904;
}
a:hover{
text-decoration: none;
color: #4296ce;
}
#border{
	border-right-style: solid;
	border-bottom-style: solid;
	border-width: 1px;
}
</style>
</head>
<body>
";

echo "<div style='font-size: 28px; text-align: center;'>Variables</div><br />
<table align='center'>
<tr>
<th bgcolor='#cccccc'><a href='changevars.php?sort=number'>#</a></th>
<th bgcolor='#cccccc'><a href='changevars.php?sort=variable'>Variable</a></th>
<th bgcolor='#cccccc'><a href='changevars.php?sort=value'>Value</a></th>
<th bgcolor='#cccccc'><a href='changevars.php?sort=rate'>Rate</a></th>
<th bgcolor='#cccccc'><a href='changevars.php'>Change</a></th>
<th bgcolor='#cccccc'><a href='changevars.php?sort=default'>Default</a></th>
</tr><form action='changevars.php' method='POST'>";

$i = 1;
$color = 0;
while ($i < mysql_num_fields($varsquery)) {
    $meta = mysql_fetch_field($varsquery, $i);
    if (!$meta) {
        echo "No information available<br />\n";
    }
	
	if ($color % 2){
	$bgcolor = "#cccccc";
	}
	else{
		$bgcolor = "#ffffff";
	}
	
		echo "<tr bgcolor='$bgcolor'><td align='center' width='20' bgcolor='#eeeeee' id='border'>$i</td>";
		echo "<td align='center' width='200' id='border'>$meta->name</td>";
		echo "<td align='center' width='200' id='border'>" .$row[$i]. "</td>";
		echo "<td align='center' width='100' id='border'>0</td>";
		echo "<td align='center' width='200' id='border'><input type='text' name='$meta->name'></td>";
		echo "<td align='center' width='200' id='border' bgcolor='#77ff77'>" .$row1[$i]. "</td>";
		echo "</tr>";
		$newvalue .= $_GET[$meta->name];
    $i++;
	$color++;
}
echo "<tr><td colspan='6' align='center'><input type='submit' name='edit' value='Edit'>&nbsp;<input type='reset' name='reset' value='Reset'></td></tr></form>"; 
echo "</table>";

// Th
if ($edit){

// Update new value
$sql = mysql_query("UPDATE vars SET `credits500`='$newvalue[0]' WHERE id='2'");

}

// Footer
echo "
</body>
</html>
";

?>
Last edited by Smudly on Sun Jul 25, 2010 4:01 pm, edited 1 time in total.
Thizzle
Forum Newbie
Posts: 12
Joined: Fri Jul 23, 2010 12:29 pm

Re: Edit Variables contained in a file

Post by Thizzle »

I read what your trying to do several times and can't figure out what your trying to do... What I'm thinking you want is to "update" your table based on the primary key(id) selected.

Also, I would really work on your naming... Going over your code some of your names are a pain.

Also when asking help on a message board I would remove the "include" file where your passwords are.
Post Reply