0

I have a set of plays that are stored in an array while the user is playing. The user has a board in the screen where I want to print the plays (words) he/she did. I tried passing to an array and then print in the html which didn't work so I asked one of my teacher how I could do it.

He told me to create a function to print the output and the html (I've never done this type of function before) where the array $row (or any other name) is passed internaly to the function, and then I should call the function inside the loop where the array $row is passed as a parameter.

I tried to do what he told me, so I did the following:

PHP

$sqlplays="Select word from plays where usertype=2 and idgame=$idgame";
$result=mysqli_query($link, $sqlplays);


for($i=0;$i<10 && ($row=mysqli_fetch_assoc($result)) ;$i++){
   print($row);

  //rest of the code
}

Java Script

 <script>
 function print(row){
     var aword=[];
      aword=jogadas;

    for (var i = 1; i <= 10 ;){
        $('.word').append(aword[i]).show();
    }   
}
</script>

HTML

   <div id="row" class="esq">
        <span class="attempt">1 :</span>
        <span class="word"> //Where it should print </span>
    </div> 

When the user makes the first play the page shows an error saying: "`Error: Call to undefined function print() in (...) "

I don't understand why is this happening, can someone tell me what is the problem? And also, I've never done functions to print html before, if you could tell me if the function is printing correctly I would appreciate.

Thank you in advance.

Rinzler786
  • 109
  • 1
  • 11
  • Check out the differences between server side and client side languages: https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming – Gary Thomas Dec 24 '18 at 12:09
  • `.append()` behaves differently than `.innerHTML` and `.innerText`, take a look at those items – Akhil Aravind Dec 24 '18 at 12:11
  • You are defining print function in java script and calling it from php. What is that?? – Sitepose Dec 24 '18 at 16:00

1 Answers1

-1

You can try this to understand the logic.

<?php

$sqlplays="SELECT word FROM plays WHERE usertype=2 AND idgame=$idgame";
$result=mysqli_query($link, $sqlplays);
$output = "";

//define function here, it appends each word to the output variable
function printword($w){
    $output .= "<span class='attempt'>1 :</span><span class='word'>".$w."</span><br>";
}

// with while & for loop iterate through 10 elements to repeat the function which appends to $output.
while($row=mysqli_fetch_assoc($result)) {
  for($i=0;$i<10;$i++){
     printword($row['word']);
    //rest of the code
  }
}


?>
<!-- HTML to print the output -->
   <div id="row" class="esq">
        <?php echo $output; ?>
    </div>
Sitepose
  • 304
  • 1
  • 13