Jump to content

selecting boxes in grid randomly


AhmadAli90

Recommended Posts

Hello, how can i randomly select 5-10 boxes and color those selected differently from this entire grid?
code done so far:
 

<!DOCTYPE HTML>
<html>
	<head>
		<title>Arrays -1- </title>

		<style>

		table {
			width: 200px;
			height:200px
			margin: 15px;
			border-collapse: collapse;
			border: 2px solid black;
		}
		tr,td{
			
			height: 50px;
		}
		td{
			border: 1px solid gray;
			width: 100px;
			vertical-align: middle;
			text-align: center;
		}

		.paint{
			z-index:3;
			background-color: pink;
		}


		</style>

	</head>
	<body>

	<?php
ini_set('max_execution_time', 0);
	$quest = array (

		"TL" => array ( array(2,2,2,2),
						array ( 2,2,2,2),
						array ( 2,2,2,2) ,
						array ( 2,2,2,2) 
						) ,

		"TR" => array ( array(2,2,2,2),
						array ( 2,2,2,2),
						array ( 2,2,2,2) ,
						array ( 2,2,2,2) 
						) ,

		"BL" => array (array(2,2,2,2),
						array ( 2,2,2,2),
						array ( 2,2,2,2) ,
						array ( 2,2,2,2) 
						) ,

		"BR" => array (array(2,2,2,2),
						array ( 2,2,2,2),
						array ( 2,2,2,2) ,
						array ( 2,2,2,2) 
						) 

			);
			
			

	echo "<table>";
	$con = 1;
	
	
	
		
			/*$ccounter=0;
			do{
			foreach($quest as $keyOut=>&$dataOut){
				foreach($dataOut as $keyMid=>&$dataMid){
					foreach($dataMid as $key=>&$data){
					
					$color = rand(1,2);
					if($color == 1)
						$ccounter++;
					
					$data=$color;
					
				}
			}
			}
			}while(!($ccounter>=5&&$ccounter<=10));
	*/
	
	$count=0;
	foreach($quest as $keyOut=>&$dataOut){
		if($con==1){
			echo "<tr>
					<td>";
		}else{
			echo "<td>";
		}
			echo "<table>";
		
			foreach($dataOut as $keyMid=>&$dataMid){
				echo "<tr>";
			foreach($dataMid as $key=>&$data){
				
				
				
				if($data==2)
					//if($clr==2)
					echo "<td>$data</td>";
				else
				{echo "<td class=\"paint\">$data</td>";
					
				}
			}
				echo "</tr>";
		}
			echo "</table>";
		if($con ==1){
			echo "</td>";
			$con = 2;
		}else{
			echo "</td>
				</tr>";
			$con = 1;
		}
	}

	echo "</table>";
	
	$tl=$quest["TL"];
	
	echo "<table>";
	foreach($tl as $keyO=>$dataO){
		echo "<tr>";
				foreach($dataO as $keyM=>$dataM){
					
					if($dataM==2)
					//if($clr==2)
					echo "<td></td>";
				else
				{echo "<td class=\"paint\"></td>";
					
				}
					
				}
			echo "</tr>";	
	}
	
	

	?>

	</body>
</html>

 

Link to comment
Share on other sites


<?php
 
ini_set('max_execution_time', 0);

//Define the number of grids per row - used to dynamically open/close TRs
$gridsPerRow = 2;
 
$quest = array (
 
    "TL" => array ( array(2,2,2,2),
                    array ( 2,2,2,2),
                    array ( 2,2,2,2) ,
                    array ( 2,2,2,2)
                    ) ,
 
    "TR" => array ( array(2,2,2,2),
                    array ( 2,2,2,2),
                    array ( 2,2,2,2) ,
                    array ( 2,2,2,2)
                    ) ,
 
    "BL" => array (array(2,2,2,2),
                    array ( 2,2,2,2),
                    array ( 2,2,2,2) ,
                    array ( 2,2,2,2)
                    ) ,
 
    "BR" => array (array(2,2,2,2),
                    array ( 2,2,2,2),
                    array ( 2,2,2,2) ,
                    array ( 2,2,2,2)
                    )
 
        );
 
function outputGrid($gridArray)
{
    //Get count of total elements
    $totalElements = 0;
    foreach($gridArray as $dataArray)
    {
        $totalElements += count($dataArray);
    }
 
    //Create an array of the total count of elements
    $elementIndexesAry = range(1, $totalElements);
    //Randomize the list
    shuffle($elementIndexesAry);
    //Pick 5-10 elements
    $randIndexes = array_slice($elementIndexesAry, 0, rand(5, 10));
 
    $gridOutput = "<table>\n";
    $index = 0;
    foreach($gridArray as $dataArray)
    {
        $gridOutput .= "<tr>\n";
        foreach($dataArray as $data)
        {
            //Increae the index
            $index++;
            //Determine the class based on current index
            $class = (in_array($index, $randIndexes)) ? "paint" : "";
            //Output the cell
            $gridOutput .= "<td class=\"{$class}\">{$data}</td>\n";
        }
        $gridOutput .= "</tr>\n";
    }
    $gridOutput .= "</table>\n";
    return $gridOutput;
}
 
$output = '';
 
 
$output .= "<table>";
$gridCount = 0;
foreach($quest as $keyOut=>$gridData)
{
    //Increase count
    $gridCount++;
 
    //If first record in row - open TR
    if($gridCount % $gridsPerRow == 1)
    {
        $output .= "<tr>";
    }
 
    $output .= "<td>\n";
    $output .= outputGrid($gridData);
    $output .= "</td>\n";
 
    //If last record in row - close TR
    if($gridCount % $gridsPerRow == 0)
    {
        $output .= "</tr>\n";
    }
}
$output .= "</table>";
 
$output .= outputGrid($quest["TL"]);
 
?>
<!DOCTYPE HTML>
<html>
    <head>
        <title>Arrays -1- </title>
 
        <style>
 
        table {
            width: 200px;
            height:200px
            margin: 15px;
            border-collapse: collapse;
            border: 2px solid black;
        }
        tr,td{
 
            height: 50px;
        }
        td{
            border: 1px solid gray;
            width: 100px;
            vertical-align: middle;
            text-align: center;
        }
 
        .paint{
            z-index:3;
            background-color: pink;
        }
 
 
        </style>
 
    </head>
    <body>
    <?php echo $output; ?>
    </body>
</html>
Edited by Psycho
Link to comment
Share on other sites

Thank you Sir, this works fine but there's only one issue, i need it to select 5-10 boxes from the whole grid, not for each and every 4*4 box, how can i do that?

 

<?php
 
ini_set('max_execution_time', 0);

//Define the number of grids per row - used to dynamically open/close TRs
$gridsPerRow = 2;
 
$quest = array (
 
    "TL" => array ( array(2,2,2,2),
                    array ( 2,2,2,2),
                    array ( 2,2,2,2) ,
                    array ( 2,2,2,2)
                    ) ,
 
    "TR" => array ( array(2,2,2,2),
                    array ( 2,2,2,2),
                    array ( 2,2,2,2) ,
                    array ( 2,2,2,2)
                    ) ,
 
    "BL" => array (array(2,2,2,2),
                    array ( 2,2,2,2),
                    array ( 2,2,2,2) ,
                    array ( 2,2,2,2)
                    ) ,
 
    "BR" => array (array(2,2,2,2),
                    array ( 2,2,2,2),
                    array ( 2,2,2,2) ,
                    array ( 2,2,2,2)
                    )
 
        );
 
function outputGrid($gridArray)
{
    //Get count of total elements
    $totalElements = 0;
    foreach($gridArray as $dataArray)
    {
        $totalElements += count($dataArray);
    }
 
    //Create an array of the total count of elements
    $elementIndexesAry = range(1, $totalElements);
    //Randomize the list
    shuffle($elementIndexesAry);
    //Pick 5-10 elements
    $randIndexes = array_slice($elementIndexesAry, 0, rand(5, 10));
 
    $gridOutput = "<table>\n";
    $index = 0;
    foreach($gridArray as $dataArray)
    {
        $gridOutput .= "<tr>\n";
        foreach($dataArray as $data)
        {
            //Increae the index
            $index++;
            //Determine the class based on current index
            $class = (in_array($index, $randIndexes)) ? "paint" : "";
            //Output the cell
            $gridOutput .= "<td class=\"{$class}\">{$data}</td>\n";
        }
        $gridOutput .= "</tr>\n";
    }
    $gridOutput .= "</table>\n";
    return $gridOutput;
}
 
$output = '';
 
 
$output .= "<table>";
$gridCount = 0;
foreach($quest as $keyOut=>$gridData)
{
    //Increase count
    $gridCount++;
 
    //If first record in row - open TR
    if($gridCount % $gridsPerRow == 1)
    {
        $output .= "<tr>";
    }
 
    $output .= "<td>\n";
    $output .= outputGrid($gridData);
    $output .= "</td>\n";
 
    //If last record in row - close TR
    if($gridCount % $gridsPerRow == 0)
    {
        $output .= "</tr>\n";
    }
}
$output .= "</table>";
 
$output .= outputGrid($quest["TL"]);
 
?>
<!DOCTYPE HTML>
<html>
    <head>
        <title>Arrays -1- </title>
 
        <style>
 
        table {
            width: 200px;
            height:200px
            margin: 15px;
            border-collapse: collapse;
            border: 2px solid black;
        }
        tr,td{
 
            height: 50px;
        }
        td{
            border: 1px solid gray;
            width: 100px;
            vertical-align: middle;
            text-align: center;
        }
 
        .paint{
            z-index:3;
            background-color: pink;
        }
 
 
        </style>
 
    </head>
    <body>
    <?php echo $output; ?>
    </body>
</html>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.