我认为这是一个相对简单的问题。这是我想做的基本布局。
我有一个数组
$polarity_array
数组的值是显示图像的 HTML 标记。
但是这些数组的某些值是空的,因为我执行了一个函数,它取消设置任何等于“此插槽中没有 mod”的数组值(提交内容时表单中的下拉框选择)
然后我有一个heredoc,回显我的HTML页面,其中包含这些数组的值,如下所示:
{$polarity_array['image_polarity1']}
{$polarity_array['image_polarity2']}
{$polarity_array['image_polarity3']}
{$polarity_array['image_polarity4']}
{$polarity_array['image_polarity5']}
{$polarity_array['image_polarity6']}
{$polarity_array['image_polarity7']}
{$polarity_array['image_polarity8']}
但是由于某些值没有设置(并且未设置的值取决于用户最初提交的内容),我得到“未定义的索引”错误。
有一个更好的方法吗?
<?php
include_once 'header.php';
require_once 'login_builds.php';
include_once 'functions.php';
//Connect to server
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
//Select the database for build-related pages
mysql_select_db($db_database)
or die("Unable to select database: " . mysql_error());
//Retrieve page variables from GET array for inital querying, decode them
$buildname = $_GET['buildname'];
$author = $_GET['author'];
$pageitem = sanitizeString(urldecode($buildname));
$pageauthor = sanitizeString(urldecode($author));
//Set variables for array query and object image retrieval
$query_info = "SELECT * FROM weapons WHERE buildname='$pageitem' AND author='$pageauthor'";
$info_result = mysql_query($query_info);
$info_array = mysql_fetch_array($info_result);
//Remove entries with no mod, and no polarity
/*
foreach($info_array as $key => $string)
{
if($string == "n" || $string == "No mod in this slot")
{
unset($info_array[$key]);
}
}
*/
$page_id = $info_array['id'];
$page_author = $info_array['author'];
$page_buildname = $info_array['buildname'];
$page_weapon = $info_array['weapon'];
$page_mod1 = $info_array['mod1'];
$page_mod2 = $info_array['mod2'];
$page_mod3 = $info_array['mod3'];
$page_mod4 = $info_array['mod4'];
$page_mod5 = $info_array['mod5'];
$page_mod6 = $info_array['mod6'];
$page_mod7 = $info_array['mod7'];
$page_mod8 = $info_array['mod8'];
$page_polarity1 = $info_array['polarity1'];
$page_polarity2 = $info_array['polarity2'];
$page_polarity3 = $info_array['polarity3'];
$page_polarity4 = $info_array['polarity4'];
$page_polarity5 = $info_array['polarity5'];
$page_polarity6 = $info_array['polarity6'];
$page_polarity7 = $info_array['polarity7'];
$page_polarity8 = $info_array['polarity8'];
$page_category = $info_array['category'];
$page_description = $info_array['description'];
$page_date = $info_array['date'];
$page_hidden = $info_array['hidden'];
//Check if the accessing user is the same as the page creator. If not, check if page is hidden. If page is hidden, redirect to index.php.
if($_SESSION['username'] != $page_author)
{
if($page_hidden == y)
{
header("Location: index.php");
}
}
//Retrieve Page Main Image
$page_main_image = convertImageMainPageWeapon($page_weapon);
//Set up mod and polarity associative arrays
$mod_array = array(
"image_mod1" => "$page_mod1",
"image_mod2" => "$page_mod2",
"image_mod3" => "$page_mod3",
"image_mod4" => "$page_mod4",
"image_mod5" => "$page_mod5",
"image_mod6" => "$page_mod6",
"image_mod7" => "$page_mod7",
"image_mod8" => "$page_mod8"
);
$polarity_array = array(
"image_polarity1" => "$page_polarity1",
"image_polarity2" => "$page_polarity2",
"image_polarity3" => "$page_polarity3",
"image_polarity4" => "$page_polarity4",
"image_polarity5" => "$page_polarity5",
"image_polarity6" => "$page_polarity6",
"image_polarity7" => "$page_polarity7",
"image_polarity8" => "$page_polarity8"
);
foreach($mod_array as $key => $string)
{
if($string == "No mod in this slot")
{
unset($mod_array[$key]);
}
}
foreach($polarity_array as $key => $string)
{
if($string == "n")
{
unset($polarity_array[$key]);
}
}
foreach($mod_array as &$string)
{
if($string != "")
{
$string = convertImageMod($string);
}
}
foreach($polarity_array as &$string)
{
if($string != "")
{
$string = convertImagePolarity($string);
}
}
echo<<<_END
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="en-us" http-equiv="Content-Language" />
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Weapon Build Creator</title>
<link href="styles/main.css" rel="stylesheet" type="text/css" />
</head>
<body style="background-image: url('images/bg.jpg')">
<div id="form" style="form">
<div class="newsdiv">
<br />
<p class="title">$page_buildname <br />
(ID: $page_id)</p>
<p class="regular_text">Author: $page_author<br />
Weapon: $page_weapon</p>
<img class="center_image" src="$page_main_image"></img><br />
<p class="mod_text"> MODS AND POLARITIES</p>
<p class="regular_text"></p>
<?php
for($i=1; $i<=8; $i++) {
if(isset($polarity_array['image_polarity' . $i])) {
echo $polarity_array['image_polarity' . $i];
} else {
echo "This slot is empty.";
}
}
?>
<p class="regular_text"> Description:</p>
<div class="description_text_div"> $page_description<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
</div>
<p class="regular_text">*comment section currently disabled for beta testing*</p>
</div>
<form action = "authenticate.php" class="regular_text" method="post">
<div class="auto-style1">
<p class="warning_text">
<br />
<br /></p>
</div>
</form>
</div>
</div>
</body>
</html>
_END;
?>