0

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.

Here is what I'm trying to do:

I have a URL which contains two variables $buildname, and $author.

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:

ID, Author, Buildname, Weapon, Mod1, Mod2 .. Mod 8, Polarity 1, Polarity 2.. Polarity 8, hidden.

I then want to check each value, and see if it is equal to a string such as

"No mod in this slot"

and replace it with

""

So that I can later use array_filter to get rid of that section of the array.

My problem(s) I'm encountering are the following

  1. 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

    foreach($info_array as $string) { echo $string; }

I get the results

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

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.

Why is it duplicating like this?

Continuing...

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:

foreach($info_array as &$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");
    } 
}

I would then filter the array to get rid of any empty values.

However, this code isn't performing properly.

If I echo the array after I run the loop, all values are the same.

What the heck am I doing wrong here?? This is my full code:

    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 &$string)
{
    if($string != "")
    {
    $string = convertImageMod($string);
    }
}

foreach($polarity_array as &$string)
{
    if($string != "")
    {
    $string = convertImagePolarity($string);
    }
}

EDIT: Code fixed. The variables are now 'unset' but I receive "undefined index errors"

Thanks!

2
  • 1
    PSA: The mysql_* functions are deprecated in PHP 5.5. It is not recommended for writing new code as it will prevent you from upgrading in the future. Instead, use either MySQLi or PDO and be a better PHP Developer. Commented Jul 30, 2013 at 15:26
  • Try actually using what str_replace returns $something = str_replace('n','',$string); Commented Jul 30, 2013 at 15:30

2 Answers 2

1

Instead of putting empty values into your array and then later using array_filter, why not just remove the array elements:

foreach($info_array as $key => $string)
{
    if($string == "n" || $string == "No mod in this slot")
    {
        unset($info_array[$key]);
    }
}
Sign up to request clarification or add additional context in comments.

9 Comments

$string = str_replace("n", "", $string);
@TobiasKun - Thanks. I got in a hurry because lunch is being served.
No problem at all. Enjoy your meal ^^
This works, but when it runs it returns notices: Notice: Undefined index: polarity8 in C:\web\view_build.php on line 72 ... Shouldnt my array_filter take care of these empty values and delete the row of the array which contains an empty value?
Yeah, I'm using the new code. It works very well and I like this as an idea, however, I get these errors: Notice: Undefined index: mod1 in C:\web\view_build.php on line 44 Notice: Undefined index: mod2 in C:\web\view_build.php on line 45 Notice: Undefined index: mod3 in C:\web\view_build.php on line 46 etc. Later in my code I reference the $info_array because I want to turn each of the strings into image tags. But with nothing set, it sees "undefined index" See my code -- I'm about to update it
|
0

Try this

foreach($info_array as &$string)
{
    if($string == "n")
    {
        $string = str_replace("n", "", $string);
    }
    if($string == "No mod in this slot")
    {
        $string = str_replace("No mod in this slot", "", $string);
    } 
}

[edit]removed quotes surrounding $string in str_replace, added variable assignment, skimmed too much originally and didn't notice this wasn't already done.

1 Comment

This won't do anything else then the original script because str_replace returns the changed string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.