Note that there are some explanatory texts on larger screens.

plurals
  1. POEchoing empty array values returns "undefined index" error
    text
    copied!<p>I think this is a relatively simple question to answer. Here is the basic layout of what I want to do.</p> <p>I have an array</p> <pre><code>$polarity_array </code></pre> <p>Values of the array are HTML tags which display an image.</p> <p>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)</p> <p>I then have a heredoc, echoing my HTML page which contains the values of these arrays like such:</p> <pre><code>{$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']} </code></pre> <p>But since some values AREN'T set (and the unset values depend on what the user submitted initially), I get "undefined index" errors.</p> <p>Is there a better way to do this?</p> <pre><code> &lt;?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 =&gt; $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" =&gt; "$page_mod1", "image_mod2" =&gt; "$page_mod2", "image_mod3" =&gt; "$page_mod3", "image_mod4" =&gt; "$page_mod4", "image_mod5" =&gt; "$page_mod5", "image_mod6" =&gt; "$page_mod6", "image_mod7" =&gt; "$page_mod7", "image_mod8" =&gt; "$page_mod8" ); $polarity_array = array( "image_polarity1" =&gt; "$page_polarity1", "image_polarity2" =&gt; "$page_polarity2", "image_polarity3" =&gt; "$page_polarity3", "image_polarity4" =&gt; "$page_polarity4", "image_polarity5" =&gt; "$page_polarity5", "image_polarity6" =&gt; "$page_polarity6", "image_polarity7" =&gt; "$page_polarity7", "image_polarity8" =&gt; "$page_polarity8" ); foreach($mod_array as $key =&gt; $string) { if($string == "No mod in this slot") { unset($mod_array[$key]); } } foreach($polarity_array as $key =&gt; $string) { if($string == "n") { unset($polarity_array[$key]); } } foreach($mod_array as &amp;$string) { if($string != "") { $string = convertImageMod($string); } } foreach($polarity_array as &amp;$string) { if($string != "") { $string = convertImagePolarity($string); } } echo&lt;&lt;&lt;_END &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;meta content="en-us" http-equiv="Content-Language" /&gt; &lt;meta content="text/html; charset=utf-8" http-equiv="Content-Type" /&gt; &lt;title&gt;Weapon Build Creator&lt;/title&gt; &lt;link href="styles/main.css" rel="stylesheet" type="text/css" /&gt; &lt;/head&gt; &lt;body style="background-image: url('images/bg.jpg')"&gt; &lt;div id="form" style="form"&gt; &lt;div class="newsdiv"&gt; &lt;br /&gt; &lt;p class="title"&gt;$page_buildname &lt;br /&gt; (ID: $page_id)&lt;/p&gt; &lt;p class="regular_text"&gt;Author: $page_author&lt;br /&gt; Weapon: $page_weapon&lt;/p&gt; &lt;img class="center_image" src="$page_main_image"&gt;&lt;/img&gt;&lt;br /&gt; &lt;p class="mod_text"&gt; MODS AND POLARITIES&lt;/p&gt; &lt;p class="regular_text"&gt;&lt;/p&gt; &lt;?php for($i=1; $i&lt;=8; $i++) { if(isset($polarity_array['image_polarity' . $i])) { echo $polarity_array['image_polarity' . $i]; } else { echo "This slot is empty."; } } ?&gt; &lt;p class="regular_text"&gt; Description:&lt;/p&gt; &lt;div class="description_text_div"&gt; &amp;nbsp;$page_description&lt;br /&gt; &lt;br /&gt; &lt;br /&gt; &lt;br /&gt; &lt;br /&gt; &lt;br /&gt; &lt;br /&gt; &lt;br /&gt; &lt;br /&gt; &lt;/div&gt; &lt;p class="regular_text"&gt;*comment section currently disabled for beta testing*&lt;/p&gt; &lt;/div&gt; &lt;form action = "authenticate.php" class="regular_text" method="post"&gt; &lt;div class="auto-style1"&gt; &lt;p class="warning_text"&gt; &lt;br /&gt; &lt;br /&gt;&lt;/p&gt; &lt;/div&gt; &lt;/form&gt; &lt;/div&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; _END; ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload