0

I have a two dimensional array in PHP that prints the index of the array instead of the value.

Here's where I set up the array.

for($i = 1;$i <= $numOfCriteria;$i++)
            {
                for($j = 1;$j <= $numOfScores;$j++)
                {
                    $description[$i][$j] = mysql_real_escape_string($_POST['descriptionPosition'.$i.$j]);
                }
            }

Here's the SQL query.

for($i = 1;$i <= $numOfCriteria;$i++)
            {
                for($j = 1;$j <= $numOfScores;$j++)
                {
                    $this->Instructor->query("INSERT INTO Criteria_Description (description,CriteriaID,ScoreID) VALUES (\"$description[$i][$j]\",\"$criteriaID[$i]\", \"$scoreID[$j]\")");
                }
            }

This is what it puts in the database.

            Array[1]        
            Array[2]        
            Array[1]        
            Array[2]        

Thanks for any replies in advance.

2
  • Alax will you please show me what $_POST array contains? Commented Apr 27, 2012 at 7:51
  • $_POST contains strings. Commented Apr 27, 2012 at 7:53

3 Answers 3

2

That is because the parser of PHP doesn't recognize multiple array indexes inside your string correctly.

If you try this:

<?php
$foo = array(array(array("bar")));
echo "wrong: \"$foo[0][0][0]\"" . PHP_EOL;
echo "right: \"{$foo[0][0][0]}\"" . PHP_EOL;

You'll realize:

wrong: "Array[0][0]"
right: "bar"

To fix this, as illustrated above, use the "{$var}" syntax. The curly braces around your variable expression ensure that the parser handles it correctly:

$this->Instructor->query("INSERT INTO Criteria_Description (description,CriteriaID,ScoreID) VALUES (\"{$description[$i][$j]}\",\"{$criteriaID[$i]}\", \"{$scoreID[$j]}\")");
Sign up to request clarification or add additional context in comments.

2 Comments

That did it! Thanks so much. What does that syntax do specifically?
@AlexOulapour it defines everything inside as a variable expression. Also, it makes your code more readable. otherwise PHP can not know what you mean. "on $date[yesterday] i was lazy" can mean two things: 1. insert variable $foo before 'yesterday' or 2. search for a variable expression $date[yesterday]. Default behaviour is 1.
0
for($i = 1;$i <= $numOfCriteria;$i++)
            {
                for($j = 1;$j <= $numOfScores;$j++)
                {
                    $this->Instructor->query("INSERT INTO Criteria_Description (description,CriteriaID,ScoreID) VALUES ('".$description[$i][$j]."','".$criteriaID[$i]."', '".$scoreID[$j]."')");
                }
            }

Using arrays inline in a string does not work. It only works with strings,numericals etc.

For objects and arrays you will hve to split it up.

For readability I used ' instead of \"

Regards, STEFAN

Comments

0

A. Don't use double quotes " for SQL use single quote '

B. It not efficient duplication SQL Statement in a loop

C. \"$criteriaID[$i]\" is an invalid way to access array variable in PHP

D. You only need 1 loop not 2

$sql = "INSERT INTO Criteria_Description (description,CriteriaID,ScoreID) VALUES ('%d','%s', '%d')";
for($i = 1; $i <= $numOfCriteria; $i ++) {
    for($j = 1; $j <= $numOfScores; $j ++) {
        $this->Instructor->query ( sprintf ( $sql, mysql_real_escape_string ( $_POST ['descriptionPosition' . $i . $j] ), $criteriaID [$i], $scoreID [$j] ) );

    }
}

Comments

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.