我目前有这样的页面,www.page.com / bars.php?monday,www.page.com / bars?php?Tuesday等。根据设置的日期,我会显示不同的内容。我是这样做的:
if (isset($_GET['monday']) && empty($_GET['monday'])) {
$query = "SELECT * FROM `bars_mon`";
$result = mysql_query($query) or die("no query");
$objects = array();
while($row = mysql_fetch_assoc($result)) {
$objects[] = $row;
}
foreach($objects as $object) {
echo
'<div class="bars-loc-cont">
<a href="' . $object['link'] . '"><img src="' . $object['img_link'] . '" /></a>
<div class="bars-info">
<a href="' . $object['link'] . '"><p class="bars-title">' . $object['title'] . '</p></a>
<p class="bars-line-1">' . $object['line_1'] . '</p>
<p class="bars-line-2">' . $object['line_2'] . '</p>
<p class="bars-line-3">' . $object['line_3'] . '</p>
<p class="bars-line-4">' . $object['line_4'] . '</p>
<p class="bars-line-5">' . $object['line_5'] . '</p>
</div>
</div>';
}
} else if (isset($_GET['tuesday']) && empty($_GET['tuesday'])) {
$query = "SELECT * FROM `bars_tues`";
$result = mysql_query($query) or die("no query");
$objects = array();
while($row = mysql_fetch_assoc($result)) {
$objects[] = $row;
}
foreach($objects as $object) {
echo
'<div class="bars-loc-cont">
<a href="' . $object['link'] . '"><img src="' . $object['img_link'] . '" /></a>
<div class="bars-info">
<a href="' . $object['link'] . '"><p class="bars-title">' . $object['title'] . '</p></a>
<p class="bars-line-1">' . $object['line_1'] . '</p>
<p class="bars-line-2">' . $object['line_2'] . '</p>
<p class="bars-line-3">' . $object['line_3'] . '</p>
<p class="bars-line-4">' . $object['line_4'] . '</p>
<p class="bars-line-5">' . $object['line_5'] . '</p>
</div>
</div>';
}
} else if (isset($_GET['wednesday']) && empty($_GET['wednesday'])) {
$query = "SELECT * FROM `bars_wed`";
$result = mysql_query($query) or die("no query");
$objects = array();
while($row = mysql_fetch_assoc($result)) {
$objects[] = $row;
}
foreach($objects as $object) {
echo
'<div class="bars-loc-cont">
<a href="' . $object['link'] . '"><img src="' . $object['img_link'] . '" /></a>
<div class="bars-info">
<a href="' . $object['link'] . '"><p class="bars-title">' . $object['title'] . '</p></a>
<p class="bars-line-1">' . $object['line_1'] . '</p>
<p class="bars-line-2">' . $object['line_2'] . '</p>
<p class="bars-line-3">' . $object['line_3'] . '</p>
<p class="bars-line-4">' . $object['line_4'] . '</p>
<p class="bars-line-5">' . $object['line_5'] . '</p>
</div>
</div>';
}
} else if (isset($_GET['thursday']) && empty($_GET['thursday'])) {
$query = "SELECT * FROM `bars_thurs`";
$result = mysql_query($query) or die("no query");
$objects = array();
while($row = mysql_fetch_assoc($result)) {
$objects[] = $row;
}
foreach($objects as $object) {
echo
'<div class="bars-loc-cont">
<a href="' . $object['link'] . '"><img src="' . $object['img_link'] . '" /></a>
<div class="bars-info">
<a href="' . $object['link'] . '"><p class="bars-title">' . $object['title'] . '</p></a>
<p class="bars-line-1">' . $object['line_1'] . '</p>
<p class="bars-line-2">' . $object['line_2'] . '</p>
<p class="bars-line-3">' . $object['line_3'] . '</p>
<p class="bars-line-4">' . $object['line_4'] . '</p>
<p class="bars-line-5">' . $object['line_5'] . '</p>
</div>
</div>';
}
} else if (isset($_GET['friday']) && empty($_GET['friday'])) {
$query = "SELECT * FROM `bars_fri`";
$result = mysql_query($query) or die("no query");
$objects = array();
while($row = mysql_fetch_assoc($result)) {
$objects[] = $row;
}
foreach($objects as $object) {
echo
'<div class="bars-loc-cont">
<a href="' . $object['link'] . '"><img src="' . $object['img_link'] . '" /></a>
<div class="bars-info">
<a href="' . $object['link'] . '"><p class="bars-title">' . $object['title'] . '</p></a>
<p class="bars-line-1">' . $object['line_1'] . '</p>
<p class="bars-line-2">' . $object['line_2'] . '</p>
<p class="bars-line-3">' . $object['line_3'] . '</p>
<p class="bars-line-4">' . $object['line_4'] . '</p>
<p class="bars-line-5">' . $object['line_5'] . '</p>
</div>
</div>';
}
} else if (isset($_GET['saturday']) && empty($_GET['saturday'])) {
$query = "SELECT * FROM `bars_sat`";
$result = mysql_query($query) or die("no query");
$objects = array();
while($row = mysql_fetch_assoc($result)) {
$objects[] = $row;
}
foreach($objects as $object) {
echo
'<div class="bars-loc-cont">
<a href="' . $object['link'] . '"><img src="' . $object['img_link'] . '" /></a>
<div class="bars-info">
<a href="' . $object['link'] . '"><p class="bars-title">' . $object['title'] . '</p></a>
<p class="bars-line-1">' . $object['line_1'] . '</p>
<p class="bars-line-2">' . $object['line_2'] . '</p>
<p class="bars-line-3">' . $object['line_3'] . '</p>
<p class="bars-line-4">' . $object['line_4'] . '</p>
<p class="bars-line-5">' . $object['line_5'] . '</p>
</div>
</div>';
}
} else if (isset($_GET['sunday']) && empty($_GET['sunday'])) {
$query = "SELECT * FROM `bars_sun`";
$result = mysql_query($query) or die("no query");
$objects = array();
while($row = mysql_fetch_assoc($result)) {
$objects[] = $row;
}
foreach($objects as $object) {
echo
'<div class="bars-loc-cont">
<a href="' . $object['link'] . '"><img src="' . $object['img_link'] . '" /></a>
<div class="bars-info">
<a href="' . $object['link'] . '"><p class="bars-title">' . $object['title'] . '</p></a>
<p class="bars-line-1">' . $object['line_1'] . '</p>
<p class="bars-line-2">' . $object['line_2'] . '</p>
<p class="bars-line-3">' . $object['line_3'] . '</p>
<p class="bars-line-4">' . $object['line_4'] . '</p>
<p class="bars-line-5">' . $object['line_5'] . '</p>
</div>
</div>';
}
}
我想知道的是,当用户只访问www.page.com/bars.php时,我该怎么做才能让我展示一下这样的日子呢?如果我选择一天,我想隐藏bars.php内容并仅显示bars.php?&#39; day&#39;内容。还有,有什么办法可以缩短这段代码吗?似乎会有更短的方式来做到这一点。
答案 0 :(得分:2)
网址应该是www.page.com/bars.php?day=tuesday
<?php
$day = (isset($_GET['day']) && !empty($_GET['day']))?$_GET['day']:'';
switch ($day) {
case "monday":
$query = "SELECT * FROM `bars_mon`";
break;
case "tuesday":
$query = "SELECT * FROM `bars_tues`";
break;
case "wednesday":
$query = "SELECT * FROM `bars_wed`";
break;
case "thursday":
$query = "SELECT * FROM `bars_thurs`";
break;
case "friday":
$query = "SELECT * FROM `bars_fri`";
break;
case "saturday":
$query = "SELECT * FROM `bars_sat`";
break;
case "sunday":
$query = "SELECT * FROM `bars_sun`";
break;
default:
$query = false;
}
if($query) {
$result = mysql_query($query) or die("no query");
$objects = array();
while($row = mysql_fetch_assoc($result)) {
$objects[] = $row;
}
foreach($objects as $object) {
echo
'<div class="bars-loc-cont">
<a href="' . $object['link'] . '"><img src="' . $object['img_link'] . '" /></a>
<div class="bars-info">
<a href="' . $object['link'] . '"><p class="bars-title">' . $object['title'] . '</p></a>
<p class="bars-line-1">' . $object['line_1'] . '</p>
<p class="bars-line-2">' . $object['line_2'] . '</p>
<p class="bars-line-3">' . $object['line_3'] . '</p>
<p class="bars-line-4">' . $object['line_4'] . '</p>
<p class="bars-line-5">' . $object['line_5'] . '</p>
</div>
</div>';
}
} else {
echo 'your other code here';
}
?>
答案 1 :(得分:1)
您需要使用array_keys从$_GET
获取密钥。
您将获得第一天的价值。
<强>步骤:强>
1)检查$_GET
是否为空。
2)使用array_keys获取数组的键。
3)创建一个允许的工作日数组。
4)创建另一个多维天数和数据库表数组。
5)其中key为day,value为数据库表名。
6)如果一切正常,请使用day
键获取数据库表的名称。
7)这将是数据库表名。
8)如果它不是空的,则完成其余的工作。
<强>代码:强>
<?php
$days = isset($_GET) && ! empty($_GET) ? array_keys($_GET) : array();
$day = (current($days)) ? strtolower(current($days)) : '';
$allowedDays = array('monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'); // Only allowed days.
if (! in_array($day, $allowedDays)) {
die('incorrect day');
}
$dbTables = array();
$dbTables['monday'] = 'bars_mon';
$dbTables['tuesday'] = 'bars_tues';
$dbTables['wednesday'] = 'bars_wed';
$dbTables['thursday'] = 'bars_thurs';
$dbTables['friday'] = 'bars_fri';
$dbTables['saturday'] = 'bars_sat';
$dbTables['sunday'] = 'bars_sun';
$tableName = isset($dbTables[$day]) ? $dbTables[$day] : '';
if (! empty($tableName)) {
$query = "SELECT * FROM `" . $tableName . "`";
// REST OF THE CODE...
}
?>
答案 2 :(得分:0)
这是一个更简单的方法,它允许默认:
使用?day=mon
进行呼叫,例如
$days = array(
"mon", "tue", "wed", "thu", "fri", "sat", "sun",
);
$day = "mon";
if (isset($_GET['day']) && in_array($_GET['day'], $days)) {
$day = $_GET['day'];
}
$query = "SELECT * FROM `bars_$day`";
$result = mysqli_query($query) or die("no query");
$objects = array();
while($row = mysqli_fetch_assoc($result)) {
$objects[] = $row;
}
foreach($objects as $object) {
echo
'<div class="bars-loc-cont">
<a href="' . $object['link'] . '"><img src="' . $object['img_link'] . '" /></a>
<div class="bars-info">
<a href="' . $object['link'] . '"><p class="bars-title">' . $object['title'] . '</p></a>
<p class="bars-line-1">' . $object['line_1'] . '</p>
<p class="bars-line-2">' . $object['line_2'] . '</p>
<p class="bars-line-3">' . $object['line_3'] . '</p>
<p class="bars-line-4">' . $object['line_4'] . '</p>
<p class="bars-line-5">' . $object['line_5'] . '</p>
</div>
</div>';
}
未来的一些提示:
die()
更好 - 您希望为用户提供好的错误消息,而不是原始文本答案 3 :(得分:0)
我认为你必须通过星期一或任何其他日子,如:
www.page.com/bars.php?day=monday
then
you can access like: $_GET['day']
答案 4 :(得分:0)
在页面开头,检查if (empty($_GET)) {
// no data passed by get, Set your get to your current day or any other day
$current_day = date('l');
$_GET[$current_day] = /**set it to what you want*/;
}
是否为空
log (5.5)
答案 5 :(得分:0)
你应该这样做:
<?php
# Check if weekday is passed in GET paramater in index `on`
# if not set then get current weekday using PHP date function
# to show current day records instead showing empty page
$day = (isset($_GET['on']) && !empty($_GET['on'])) ? $_GET['on'] : strtolower(date('l'));
$weekdays_and_db_tables = array(
'sunday' => 'sun',
'monday' => 'mon',
'tuesday' => 'tues',
'wednesday' => 'wed',
'thursday' => 'thurs',
'friday' => 'fri',
'saturday' => 'sat',
);
# Now check if weekdays is correct
if (!isset($weekdays_and_db_tables[$day]) or $weekdays_and_db_tables[$day] == '') {
die('Invalid weekday!');
}
# SQL Injection can cause here, so beware
# you should migrate all table in one and filter records using an extra column `weekday`
$query = printf("SELECT * FROM `bars_%s`", $weekdays_and_db_tables[$day]);
# instead separate table for each weekdays migrate in one and do:
$query_x = printf("SELECT * FROM `bars_weekdays` WHERE `weekday` = '%s'", $weekdays_and_db_tables[$day]);
$result = mysqli_query($query) or die("no query");
while($object = mysqli_fetch_assoc($result)) {
echo <<<HTML
<div class="bars-loc-cont">
<a href="{$object['link']}"><img src="{$object['img_link']}" /></a>
<div class="bars-info">
<a href="{$object['link']}"><p class="bars-title">{$object['title']}</p></a>
<p class="bars-line-1">{$object['line_1']}</p>
<p class="bars-line-2">{$object['line_2']}</p>
<p class="bars-line-3">{$object['line_3']}</p>
<p class="bars-line-4">{$object['line_4']}</p>
<p class="bars-line-5">{$object['line_5']}</p>
</div>
</div>
HTML;
}