Cannot use a scalar value as an array in on line 66(not understanding why)

For the past month, I have trying to create a database that stores English and Japanese words. I then wanted to use the data populated by myself to quiz me on my progress. I have been working on this script to get it to pull random questions and display in a multiple choice format. I have getting this error as I am trying to create a new array which will hold 4 questions.
[php]
function WrongAnswer(){
static $counter;
$counter =0;
$username = “saori”;
$password = “”;
$hostname = “Jamal-pc”;
$conn = new PDO(‘mysql:host=Jamal-PC;dbname=japanesewords’,$username,$password);
$sql =‘Select EnglishWord from japanesedefinition;’;
$stmt = $conn->prepare($sql);
$stmt ->execute();
$result = $stmt->fetchAll(PDO::FETCH_OBJ);
print_r($result);
$sizeofArray = sizeof($result);

//make this while loop store this into a new array
//also be sure to add the answer after the loop enters
//then shuffle the array
//the methods will call this function and give out the questions
$storeanswer = array ();
if($counter==0){

for($x=0;$x<3;$x++){
$storeanswer = rand(0,$sizeofArray);
$answer = $result[$storeanswer]->EnglishWord;
$storeanswer[$x]=$answer;
}
$counter++;
}

}
}
[/php]
I did a var_dump and the $answer is a string, so why can’t I just add them into a new array. I wanted the array to hold my random questions.

Am I missing something basic here?

Thanks in advance.

In line 20 you set $storeanswer to be an array
In line 24 you set $storeanswer to an integer
In line 26 you use it as an array

How do you want the $storeanswer array to look after the loop?

Also, in line 23 you don’t need to add the $x, this will do exactly the same as you’re doing now:
[php]$storeanswer[]=$answer;[/php]

I didn’t realize that I was using the same variable name multiple times. I feel foolish. After changing the variable name, it worked perfectly.

To answer your question, I just wanted to have a basic array, so that I can have control of the questions that will be displayed as a radio button. Near my radio buttons in my HTML section, I will have methods that call the the function and randomly pull a string from the array and display it to the user in the form of a question.

Another question, why does it appear that using arrays slows down the compiler? Is there a way to speed the overall process up?

Thanks.

Well every code you add will add execution time, but looping a small array (less than 10000 entries) should’t take much time (milliseconds).

Try to make a test script to benchmark arrays with some loops, I can run it as well so you get an execution time comparison.

Another quick question, so I am using a random number generator to pull from my database, but I didn’t anticipate that the random number generator would possibly generate the same number twice. How could I use a random function, but make it only generate a number once? Should I put a few logic test in the form of IF statements, and do another random number generate until I get 3 unique values? How would you go about solving that problem?

Try something like this.

I changed sizeof($result) to use count instead since that’s the real function (sizeof is just an alias)

I create a range (an array of values from 0 to count($result)-1. The reason for this is that arrays starts with the index 0, so if you run from 0 to count($result) you’re going 1 array key above what is actually available.

I then just did what you wrote in the comment, shuffle the array and thereby getting the numbers in random order.

And as said, when assigning $storeanswer[$x] the $x is pretty redundant so I just removed it. I also did it in one line as there was no real reason to assign the englishword to an $answer variable before adding it to the array.

[php]function WrongAnswer() {
static $counter;
$counter = 0;
$username = “saori”;
$password = “”;
$hostname = “Jamal-pc”;
$conn = new PDO(‘mysql:host=Jamal-PC;dbname=japanesewords’, $username, $password);
$sql = ‘Select EnglishWord from japanesedefinition;’;
$stmt = $conn->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_OBJ);
$sizeofArray = count($result);

//make this while loop store this into a new array
//also be sure to add the answer after the loop enters
//then shuffle the array
//the methods will call this function and give out the questions
$storeRange = range(0,$sizeofArray-1);
shuffle($storeRange);
$storeanswer = array();
if ($counter == 0) {
for ($x = 0; $x < 3; $x++) {
$storeanswer[] = $result[$storeRange[$x]]->EnglishWord;
}
$counter++;
}
}[/php]

Thank you so much a few tweaks and now the script works perfectly.

After seeing your code, I got very disappointed in myself for not figuring it out. Do you think I am approaching PHP from the right angle? I thought just getting my feet wet by building something useful. It’s just really frustrating getting stuck on something so trivial. Any words of encouragement?

Just looking at the way you wrote the code so quickly is something I hope to eventually achieve for myself.

Thanks again, you have been so helpful once again.

Just keep coding, beeing a good coder requires experience. Also keep in mind there are always several solutions to a problem in programming.

If I were to give advise it would be that most likely what you’re trying to achieve (function wise) has been done already. If it is in the php library (rand, range, shuffle) then you’re pretty much always better off using the built in functions instead of re-inventing the wheel :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service