-1

I'm working on a jQuery drop/drag game and pulling the information from a csv file such as image, year and text which is working:

$rows = file('Book1.csv');
$len = count($rows);
$rand = array();
while (count($rand) < 5) {
    $r = rand(0, $len);
    if (!in_array($r, $rand)) {
        $rand[] = $r;
    }
}
echo 'var cardDeck = [';
foreach ($rand as $r) {
    $csv = $rows[$r];
    $data = str_getcsv($csv);
    echo "{\n";
    echo "'image':'".$data[1]."',\n"; 
    echo "'year':'".$data[0]."',\n"; 
    echo "'hint':'".$data[3]."',\n"; 
    echo "'caption':'".$data[2]."'"; 
    echo "\n},\n";
}
echo "];";

But where I'm stuck at is that I'm trying to create 5 new arrays using a for loop that increments the variable name and also set the year values into the arrays that has already been pulled from the csv

foreach ($rand as $r) {
    $csv = $rows[$r];
    $data = str_getcsv($csv);
    for ($i = 1; $i < 5; $i++) {
        $varToEcho = "correct_dropzone$i"; // become correct_dropzone1,correct_dropzone2,etc.
        echo "var ".$varToEcho." = new Array('".$data[0]."');\n"; 
    }   
}

What I'm looking for:

var correct_dropzone1 = new Array('1845');
var correct_dropzone2 = new Array('1835');
var correct_dropzone3 = new Array('1586');
var correct_dropzone4 = new Array('1912');
var correct_dropzone5 = new Array('1969');

But my output is:

var correct_dropzone1 = new Array('1845');
var correct_dropzone2 = new Array('1845');
var correct_dropzone3 = new Array('1845');
var correct_dropzone4 = new Array('1845');
var correct_dropzone1 = new Array('1835');
var correct_dropzone2 = new Array('1835');
var correct_dropzone3 = new Array('1835');
var correct_dropzone4 = new Array('1835');
var correct_dropzone1 = new Array('1586');
var correct_dropzone2 = new Array('1586');
var correct_dropzone3 = new Array('1586');
var correct_dropzone4 = new Array('1586');
var correct_dropzone1 = new Array('1912');
var correct_dropzone2 = new Array('1912');
var correct_dropzone3 = new Array('1912');
var correct_dropzone4 = new Array('1912');
var correct_dropzone1 = new Array('1822');
var correct_dropzone2 = new Array('1822');
var correct_dropzone3 = new Array('1822');
var correct_dropzone4 = new Array('1822');
  • 1
    drop the `for ($i = 1; $i < 5; $i++) {`loop, and instead use a variable `$i` in the `foreach()` loop that increases (e.g. in this case from 1 to 4). – jibsteroos Nov 05 '19 at 21:34
  • 3
    Don't try to create JavaScript objects by echoing each part. Create a PHP variable containing the whole thing, then use `echo json_encode($variable);` to convert it to JS syntax. – Barmar Nov 05 '19 at 21:37
  • @jibsteroos I should create another foreach and nest it inside or add on to the existing? – Shanice Morgan Nov 05 '19 at 21:44
  • @Barmar I never did that before. Could you point in the right direction like with an example or webpage? – Shanice Morgan Nov 05 '19 at 21:45

1 Answers1

1

You don't need nested loops, just one loop. Use the index of the loop as to create the number suffix of the variable.

foreach ($rand as $i => $r) {
    $csv = $rows[$r];
    $data = str_getcsv($csv);
    $varToEcho = "correct_dropzone" . ($i + 1);
    $valToEcho = json_encode(array($data[0]));
    echo "var $varToEcho = $valToEcho;\n";
}

I've also shown how you can use json_encode() to convert a PHP value to a JavaScript literal, rather than constructing it by hand.

Also, variables with numeric suffixes is usually a clue that you should be using an array rather than separate variables. You could make correct_dropzone an array.

$dropzones = array();
foreach ($rand as $r) {
    $csv = $rows[$r];
    $data = str_getcsv($csv);
    $dropzones[] = array($data[0]);
}
echo "var correct_dropzones = " . json_encode($dropzones) . ";";

Do similarly with cardDeck:

$cardDeck = array();
foreach ($rand as $r) {
    $csv = $rows[$r];
    $data = str_getcsv($csv);
    $cardDeck[] = array(
        'image' => $data[1],
        'year' => $data[0],
        'hint' => $data[3],
        'caption' => $data[2]
    );
}
echo "var cardDeck = " . json_encode($cardDeck) . ";";

You can use the same loop for both variables.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks so much for the help. Now I need to learn more about json_encode because I didn't know that was possible – Shanice Morgan Nov 05 '19 at 21:47
  • I used your recommendation for cardDeck which is great and works the same but when I check the code on chrome debugger there's slashes in the array. Just curious – Shanice Morgan Nov 05 '19 at 21:56
  • 1
    You mean `\/` in the image URLs? See https://stackoverflow.com/questions/10210338/json-encode-escaping-forward-slashes – Barmar Nov 05 '19 at 22:15