Reputation: 307
I have this php script that displays an sql table in a html webpage. Here is the code:
<html>
<head>
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8"><title>Your Page Title</title></head>
<body>
<?php
$database="Batabase_xxx";
mysql_connect ("localhost", "User_xxx", "password_xxx");
mysql_query("SET NAMES 'utf8'");
@mysql_select_db($database) or die( "Unable to select database");
$result = mysql_query( "SELECT id,name,address,phone,hours FROM table_xxx" )
or die("SELECT Error: ".mysql_error());
$num_rows = mysql_num_rows($result);
print "found $num_rows results.<P>";
print "<table width=250 border=1>\n";
while ($get_info = mysql_fetch_row($result)){
print "<tr>\n";
foreach ($get_info as $field)
print "\t<td><font face=arial size=2/>$field</font></td>\n";
print "</tr>\n";
}
print "</table>\n";
?>
</body>
</html>
My problem is that i now have 23 different tables and I would like to select a different one each day. For example: Today I want to select table_1, tommorow table_2 etc. When I reach table_23(the last table) I want to reset and start all over again from table_1. Is that possible?(I want to avoid to merge 23 tables into 1) Please note that i m totally new to this and every help is appreciated. thanks in advance.
Upvotes: 0
Views: 767
Reputation: 901
<html>
<head>
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8"><title>Your Page Title</title></head>
<body>
<?php
if(isset($_COOKIE['daynumber']) && $_COOKIE['daynumber']!=NULL)
{
if($_COOKIE['daynumber']!=23) { $_COOKIE['daynumber'] += 1; }
}
else
{
$_COOKIE['daynumber'] = 1;
}
$database="Batabase_xxx";
mysql_connect ("localhost", "User_xxx", "password_xxx");
mysql_query("SET NAMES 'utf8'");
@mysql_select_db($database) or die( "Unable to select database");
$result = mysql_query( "SELECT id,name,address,phone,hours FROM table_".$_COOKIE['daynumber'])
or die("SELECT Error: ".mysql_error());
$num_rows = mysql_num_rows($result);
print "found $num_rows results.<P>";
print "<table width=250 border=1>\n";
while ($get_info = mysql_fetch_row($result)){
print "<tr>\n";
foreach ($get_info as $field)
print "\t<td><font face=arial size=2/>$field</font></td>\n";
print "</tr>\n";
}
print "</table>\n";
?>
</body>
</html>
Upvotes: 0
Reputation: 674
I would go with storing this in a database table for config values, since it's easy to setup and you can easily add more settings later for other things.
Here's an example. First create the table holding configuration values:
CREATE TABLE config_values (
id int(9) NOT NULL AUTO_INCREMENT,
key_name varchar(50) DEFAULT NULL,
value varchar(200) DEFAULT NULL,
PRIMARY KEY (id)
);
Then you insert an entry for this:
INSERT INTO config_values(key_name, value) VALUES ('day_counter', 1);
Then before you execute your script, you see what day it is, and use in in the table name
$current_day_query = mysql_query(
"SELECT value FROM config_values WHERE key_name = 'day_counter'"
);
$current_day_result = mysql_fetch_row($current_day_query);
$current_day = reset($current_day_result);
$table_name = 'table_' . $current_day;
// code
$result = mysql_query( "SELECT id,name,address,phone,hours FROM $table_name" )
// more code
And last, don't forget to increment/reset the counter before you finish:
if ($current_day == 23) {
$next_day = 0;
}
else {
$next_day = $current_day + 1;
}
$increment_query = mysql_query(
"UPDATE config_values SET value = $next_day WHERE key_name = 'day_counter'"
);
Upvotes: 2