Jump to content

Trouble in creating a Word Scramble Game in PHP using mysql


ravi1988

Recommended Posts

I am creating a word scramble game in PHP .But I am having difficulty in doing it. What Iam planning to do is to get the jumbled words from the database and display it as tile format or individual buttons . The words are scrambled and I have to do dragging it so that it matched the word from mysql. First how to get individual letters from mysql and that too scrambled.

 

Individual letters , I know it is str_split($words) so that the letter is split into letters. Please help me on this.

It's hard to help without seeing your code attempts. That's how it works here. Oh - and if your reference to 'mysql' means that you are using the MySQL extension to access your database, stop now and go read up on the mysqlI or PDO extensions and use one of them for your database accessing. See the manual for why this is if you don't know.

<?php

function get_random_word(PDO $pdo) {
$sql = 'SELECT word FROM words WHRE id = (SELECT FLOOR(MIN(id) + RAND() * (MAX(id) - MIN(id))) FROM words)';
$stmt = $pdo->query($sql);
if (0 === $stmt->rowCount()) {
return '';
}
return $stmt->fetchColumn();
}

$pdo = new PDO('dsn', 'user', 'pass');
$letters = str_split(get_random_word($pdo)); ?>
<html>
<head><title>Word Game</title></head>
<body>
<div class="letters">
<span class="letter"><?php implode('</span><span class="letter">', $letters); ?></span>
</div>
<script src="//code.jquery.com/jquery-2.1.3.min.js"></script>
<script>
$(function() {
var $dragging = null;

$(document.body).on('mousemove', function(e) {
if ($dragging) {
$dragging.offset({
top: e.pageY,
left: e.pageX
});
}
}).on('mousedown', 'span.letter', function(e) {
$dragging = $(e.target);
}).on('mouseup', function(e) { $dragging = null; });
});
</script>
</body>
</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.