Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1086977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:52:50+00:00 2026-05-16T22:52:50+00:00

I want to generate a grid where people can select one or more cells

  • 0

I want to generate a grid where people can select one or more cells and then save them into the database in this format (row,cell):

18,27 18,28 19,27 19,28

Then I want to generate the grid and at the same time mark the cells that were selected. I have managed to do this (see below) but it does not feel right. Also I need to get the color for the field from the database so that the cell looks different for each user.

This is how I generate the grid and populate it:

include 'connect.php';

$result = mysql_query("SELECT cus_pixels FROM customers");

while($row = mysql_fetch_array($result)) {

    $string .= $row['cus_pixels']." ";

}

$pixels = explode(' ', $string);

$a = 0;

while ($a <= 40) {

    echo "<tr>";

    $b = 0;

    while ($b <= 60) {

        if (in_array($a.','.$b, $pixels)) {

            echo '<td class="occupied"></td>';

        } else {

            echo '<td class="pixel"></td>';

        }

        $b += 1;
    }

    echo "</tr>";

$a += 1;

}

How can I both generate the grid, populate the cells that are occupied and color them individually. I am really stuck here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:52:51+00:00Added an answer on May 16, 2026 at 10:52 pm

    You could set the table class or id to have the "pixel" properties by default. Then you would only need to specify the "occupied" classes.

    ex) <table id="gridTable">..<td> instead of <table>..<td class="pixel">

    If you can use jquery (javascript), then you could use it together with CSS selectors to change the classes of your "occupied" cells only. Instead of looping through all (60×40 = 2400) cells, you would then only have to mark the occupied cells.

    Addition:
    Now that I think about it more, the javascript solution might be too slow/trouble then it
    is worth. You could do something like the following, keeping in mind that it is more pseudocode then
    an exact answer and untested.

    <?php 
     $results = fromDB;
     //ex) ['i' => 3, 'j' => 28, 'color' => 'red']
    ?>
    <script>
      $(document).ready(function() {
      <?php
        foreach ($results as $r) {
          $i = $r['i'];
          $j = $r['j'];
          $color = $r['color'];
    
          echo "$('#gridTable tr:eq($j) td:eq($i)').setClass('occupied')";
          //  OR
          //echo "$('#gridTable tr:eq($j) td:eq($i)').css('background','$color')";
        
        }
      ?>
    
      });
    
    </script>
    

    In summary, the HTML will be static. <table id="gridTable">, <td></td>. PHP will be used to get data from the database. The PHP array of (i, j) row-column pairs will be looped through and echoed out into javascript (jquery) statements.

    This might be overkill for your needs, but should lend itself to some added extendability.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using this code in order to generate XML from my grid's cells values:
I want to generate 1M random (appearing) unique alphanumeric keys and store them in
Learning haskell and want a function to generate a 2D grid similar to how
I have this code I want to bind data to grid view without using
READ FIRST I re-wrote this to make it more readable. If you want to
I want to use the 960 grid system to generate the layout for my
I previously asked this question but need more help. Read this, then I will
I'm using the code in this answer to generate a grid of buttons. When
I'm using libnoise to generate Perlin noise on a 1024x1024 terrain grid. I want
Let's say I have a class of 30 students and want generate every possible

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.