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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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>
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.