-2

I am currently working on a little project and I only just realised that the way I thought it would work, won't in fact work as JavaScript variables can't be sent to PHP that easily.

I have a Database full of members and each member has an id from 0 to x (lets just say x = 10) and I wanted to use a JavaScript for loop to iterate through and create everything easily.

Basically, the end product should be the following (except where it says id = 0 it would be different for each entry)

<tr>
    <td><?php echo mysql_fetch_assoc(mysql_query("SELECT name FROM squad WHERE id = 0"))['name'] ?></td>
    <td><?php switch(mysql_fetch_assoc(mysql_query("SELECT rank FROM squad WHERE id = 0"))['rank']){case 3:echo 'Lieutenant';break;case 2:echo 'Soldier';break;default:echo 'Private';} ?></td>
    <td><?php echo number_format(mysql_fetch_assoc(mysql_query("SELECT power FROM squad WHERE id = 0"))['power']); ?></td>
    <td><input type="number" name="gained0" value="<?php echo mysql_fetch_assoc(mysql_query("SELECT power FROM squad WHERE id = 0"))['power']; ?>"></td>
    <td><input type="number" name="rank0" value="<?php echo mysql_fetch_assoc(mysql_query("SELECT rank FROM squad WHERE id = 0"))['rank'] ?>"></td>
</tr>

Obviously I can't type in a JavaScript variable into PHP so how would I be able to loop through and create the above for all entries?

Spedwards
  • 4,167
  • 16
  • 49
  • 106
  • you're completely mixing up client side and server side code. I suggest ditching javascript completely for a while, try to do everything in php, and come back to javascript once you have a better idea of how the server side works. – serakfalcon Aug 06 '14 at 08:28

3 Answers3

0

My god that's an awful approach have a look in to AJAX http://en.wikipedia.org/wiki/Ajax_%28programming%29 how it is used ... You will need to implement some kind of API's with simple get requests.

With AJAX basically you can call URL-s from javascript and obtain the data that is returned from the page let that be text,html,json etc..

Here you can find a great explanation on how AJAX works How does AJAX work? read through it you can find free tutorials on youtube also and after you got the idea rethink your code.

Community
  • 1
  • 1
Csak Zoli
  • 408
  • 1
  • 4
  • 11
0

If you want to use javascript, I think you should use AJAX (google it). Best practice would be using only PHP and learning the difference between server side and client side. Good luck!

Onovar
  • 729
  • 1
  • 7
  • 19
0

Managed to get it done with just PHP. The switch I had to think about but it works as far as I can tell.

<?php
    for ($i=0; $i<=10; $i++) {
        $r = "";
        switch(mysql_fetch_assoc(mysql_query("SELECT rank FROM squad WHERE id = $i"))["rank"]){case 3:$r="Lieutenant";break;case 2:$r="Soldier";break;default:$r="Private";}
        echo "<tr>" .
            "<td>" . mysql_fetch_assoc(mysql_query("SELECT name FROM squad WHERE id = $i"))["name"] . "</td>" .
            "<td>$r</td>" .
            "<td>" . number_format(mysql_fetch_assoc(mysql_query("SELECT power FROM squad WHERE id = $i"))["power"]) . "</td>" .
            "<td><input type=\"number\" name=\"gained$i\" value=\"" . mysql_fetch_assoc(mysql_query("SELECT power FROM squad WHERE id = $i"))["power"] . "\"></td>" .
            "<td><input type=\"number\" name=\"rank$i\" value=\"" . mysql_fetch_assoc(mysql_query("SELECT rank FROM squad WHERE id = $i"))["rank"] . "\"></td>" .
        "</tr>";
    }
?>
Spedwards
  • 4,167
  • 16
  • 49
  • 106