Jump to content

Help with idea


herghost

Recommended Posts

Hi all

 

I am looking for some help on how to achieve something. Basically I want a 50x50 square on a page where each 'square' can have its color changed. Once each colour has been changed I need to be able to count how many squares have there colour changed and save the new 50x50 square to a database.

 

How do I go about achieving the square to start with!

 

Cheers

Link to comment
Share on other sites

Javascript is what you want.

 

Have a table, and on click, call a JS function that changes the bgcolor of that cell

 

Alternately, have a bunch of 50x50 gifs of different colors, and swap the src of that image onclick.

Link to comment
Share on other sites

Works a little slow right now, so I decided to take this one up on my own. Here's a complete script with database dump that'll track a user's choice.

I decided to go with a text column, so the actual grid count can be variable, and you don't have to deal with 2500 rows/columns per user.

 

<?php 

// This would obviously be generated from a login form or something
$userID = 1;
$db = new mysqli( 'localhost', 'root', '', 'db' );
$cells = 2500;
$cols = 50;

if( !empty($_POST['color']) && is_array($_POST['color']) ) {
$data = $db->escape_string( implode('|',$_POST['color']) );
$q = 'REPLACE INTO `grid` (`userid`,`data`) VALUES ("'.$userID.'","'.$data.'")';
if( !$db->query($q) ) $msg = 'Update query error';
}

?>
<html>
<head>
<script type="text/javascript">
	function changeColor( cell ) {
		switch (cell.style.backgroundColor) {
			case 'blue':
				cell.style.backgroundColor = 'yellow';
				break;
			case 'yellow':
				cell.style.backgroundColor = 'green';
				break;
			case 'green':
				cell.style.backgroundColor = 'white';
				break;
			default:
				cell.style.backgroundColor = 'blue';
		}
	}
	function changeMouse( cell ) {
		cell.style.cursor='pointer';
	}
	function submitGrid() {
		var form = document.getElementById('gridForm');
		var table = document.getElementById('grid');
		form.elements[0].disabled = true;
		form.elements[0].value = 'Please wait...';
		for ( var i = 0, row; row = table.rows[i]; i++ ) {
			var cells = row.getElementsByTagName('td');
			for( var j = 0; j < cells.length; j++ ) {
				var color, input = document.createElement('input');
				if( cells[j].style.backgroundColor == '' ) {
					color = 'white';
				} else {
					color = cells[j].style.backgroundColor;
				}
				input.setAttribute( 'type', 'hidden' );
				input.setAttribute( 'name', 'color[]' );
				input.setAttribute( 'value', color );
				form.appendChild(input);
			}
		}
		document.forms['gridForm'].submit();
	}
</script>
<style type="text/css">
	#grid td {
		width: 10px;
		height: 10px;
		border: 1px solid black;
		font-size: .1 em;
	}
</style>
</head>
<body>

<table id="grid"><tr>
<?php

$q = 'SELECT `data` FROM `grid` WHERE `userid` = "'.$userID.'"';
$c = array();
if( ($r = $db->query($q)) === FALSE ) $msg = 'Select query error';
else {
$row = $r->fetch_assoc();
$c = explode('|',$row['data']);
$r->free();
}
for( $i = 1; $i <= $cells; $i++ ) {
echo '<td ';
echo ( !empty($c[$i-1]) ) ? 'style="background-color:'.$c[$i-1].'" ' : '';
echo 'onmouseover="changeMouse(this)" onclick="changeColor(this)"> </td>';
if( $i % $cols == 0 && $i != $cells )
	echo '</tr><tr>';
}
?>
</tr></table>
<form id="gridForm" method="post" action="#">
<input type="button" value="Submit Grid" onclick="submitGrid()">
</form>
<?php if( !empty($msg) ) echo $msg; ?>
</body>
</html>
<?php 

/* DATABASE DUMP

CREATE TABLE IF NOT EXISTS `grid` (
  `userid` int(11) NOT NULL,
  `data` text NOT NULL,
  UNIQUE KEY `userid` (`userid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

*/

?>

Link to comment
Share on other sites

Hi xyph, wow, thanks! Thats definitely above and beyond what I expected to get!

 

Thanks for getting me started, I will see what I can do with it from here, if I get stuck with some simple stuff ill post on here, however if it goes way over my knowledge are you available for some freelance work?

Link to comment
Share on other sites

Always available for work, as long as your timeline isn't insanely strict... my job here take priority, but I don't mind working through the evening/weekends, especially with the summer we've been having here :D

 

PM me for that kind of stuff though. If you need more help, I can try to explain. Sorry the example isn't commented.

 

Glad I could help. Seemed like a fun challenge :)

Link to comment
Share on other sites

That's you attempting to select the text in the cell with a double-click

 

This _can_ be disabled, but each browser does this slightly differently from what I understand. I like to avoid removing functionality anyways

 

Regardless, this was just a proof of concept. If I were coding this for production, I would probably have the user drag a color from a palette on to a square, or use a blank 1x1 GIF rather than a   inside the cells.

 

Now that I think about it, you can probably just have an empty cell. I like to avoid empty cells, as the HTML spec doesn't say they need to be drawn, but I suppose once you style them, it's forced out. Removing the   seems to have fixed the issue.

 

Edit - I take that back, IE9 won't draw the borders without the  

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.