0

I'm using this PHP code to write a HTML table, but it takes time to load. I was thinking it would be a good idea to let JavaScript do the job, but the problem is that $width and $height are dynamic and is defined on the server-side. How do I get around that?

echo "<table>";

    for ($y = 0; $y < $height; $y++) {
        echo "<tr>";
        for ($x = 0; $x < $width; $x++) {
            echo '<td x="' . $x . ' y="' . $y . ' id="' . $x . '-' . $y . '"></td>';     //coordinates to be used for cropping later
        }
        echo '</tr>';
    }
    echo '</table>';
2
  • if you know AJAX, make a ajax call to yr server to get width and height dynamically i guess Commented Jan 25, 2014 at 17:40
  • Using AJAX would be overkill. Just echo the variables as @kevinAlbs suggests Commented Jan 25, 2014 at 17:43

3 Answers 3

1

I'm unsure if this is best practice, but you can echo from PHP directly into Javascript. For example:

<script>
var width = <?php echo $width; ?>;
var height = <?php echo $height; ?>;
//now build table here using the Javascript width and height variables
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

Put PHP variables in javascript

<script>

var height = <?php echo $height ?>;

</script>

Comments

0

If no paramount forms are intented to exist in your page, you can store them in hidden HTML tags as hidden inputs, like this:

echo ="<form action ='#' name='form'>
<input type='hidden' name='v_height' id='v_height' value='".$height."'>
<input type='hidden' name='v_width' id='v_width' value='".$width."'>
</form>";

And then in Javascript get those variables and iterate with them.

jHeight = parseInt(document.form.v_height.value);

And so with the width.

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.