dongxianrang9269 2013-07-30 17:41
浏览 60
已采纳

回显空数组值会返回“未定义索引”错误

I think this is a relatively simple question to answer. Here is the basic layout of what I want to do.

I have an array

$polarity_array

Values of the array are HTML tags which display an image.

But some values of these arrays are empty, because I performed a function which UNSETS any array value equal to "No mod in this slot" (A dropdown box selection in a form when they submit something)

I then have a heredoc, echoing my HTML page which contains the values of these arrays like such:

{$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']}

But since some values AREN'T set (and the unset values depend on what the user submitted initially), I get "undefined index" errors.

Is there a better way to do this?

    <?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"> &nbsp;$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;

?>
  • 写回答

3条回答 默认 最新

  • dqe9657 2013-07-30 17:43
    关注

    You must check if the index exists, like this:

    if(isset($polarity_array['image_polarity1'])) {
        echo $polarity_array['image_polarity1'];
    } else {
        echo "This slot is empty.";
    }
    

    I'm not sure what those curly brackets in your example mean, though.

    Even better, use array to loop through the items:

    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.";
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图