Note that there are some explanatory texts on larger screens.

plurals
  1. POFiltering an array retrieved from MySQL after replacing strings
    primarykey
    data
    text
    <p>I've been having a lot of trouble getting this code to work and I was wondering if you guys could take a look at it and maybe see what I'm doing wrong.</p> <p>Here is what I'm trying to do:</p> <p>I have a URL which contains two variables $buildname, and $author.</p> <p>These two variables are inserted into a query for Mysql to retrieve a unique row, where it then fetches an array of information for that row. For example, each row would contain information such as:</p> <p>ID, Author, Buildname, Weapon, Mod1, Mod2 .. Mod 8, Polarity 1, Polarity 2.. Polarity 8, hidden.</p> <p>I then want to check each value, and see if it is equal to a string such as </p> <p>"No mod in this slot"</p> <p>and replace it with</p> <p>""</p> <p>So that I can later use array_filter to get rid of that section of the array.</p> <p>My problem(s) I'm encountering are the following</p> <ol> <li><p>When I fetch the array, and perform a foreach loop which echos each element of the array, it is in duplicate. For example, if I perform</p> <p>foreach($info_array as $string) { echo $string; }</p></li> </ol> <p>I get the results</p> <p>66SteelyDanSteelyDanAcrid BabiesAcrid BabiesAcridAcridNo mod in this slotNo mod in this slotNo mod in this slotNo mod in this slotNo mod in this slotNo mod in this slot</p> <p>Where the ID is 6, the author is SteelyDan, the buildname is Acrid Babies, the first mod is No mod in this slot... and so on.</p> <p>Why is it duplicating like this?</p> <p>Continuing...</p> <p>My code to replace these values "n" (which would appear as a polarity value) and "No mod in this slot" (Which would appear as a mod value) is the following:</p> <pre><code>foreach($info_array as &amp;$string) { if($string == "n") { str_replace("n","","n"); } if($string == "No mod in this slot") { str_replace("No mod in this slot","","No mod in this slot"); } } </code></pre> <p>I would then filter the array to get rid of any empty values.</p> <p>However, this code isn't performing properly.</p> <p>If I echo the array after I run the loop, all values are the same.</p> <p>What the heck am I doing wrong here?? This is my full code:</p> <pre><code> 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 &amp;$string) { if($string != "") { $string = convertImageMod($string); } } foreach($polarity_array as &amp;$string) { if($string != "") { $string = convertImagePolarity($string); } } </code></pre> <p>EDIT: Code fixed. The variables are now 'unset' but I receive "undefined index errors"</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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