Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

mysql and random comparison

Codes here !

Moderators: macek, egami, gesf

mysql and random comparison

Postby smithandson » Tue Jul 17, 2012 8:50 am

Hi.
I have a table like the one below in mysql:
id word sentance answer explanation
1 happy I am sorry for your unhappines unhappiness
2 consume I am a consumer consumer
3



First I am trying to do is create the variable ‘$answer’ which captures in the random row generate , the content in the column "answer" of that row for exampls ‘unhappiness’


To generate the random I am using the code below:
<?php

$con = mysql_connect ("localhost", "root", "root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("clients", $con);
$range_result = mysql_query( " SELECT MAX(`wd_id`) AS max_id , MIN(`wd_id`) AS min_id FROM `words` ");
$range_row = mysql_fetch_object( $range_result );
$random = mt_rand( $range_row->min_id , $range_row->max_id );
$result = mysql_query( " SELECT * FROM `words` WHERE `wd_id` >= $random LIMIT 0,1 ");

mysql_close($con);
?>

To echo the random the random answer I am using the following code.

<?php

while($row = mysql_fetch_array($result))
{
echo "<table border ='0' width='600px'>";
echo "<tr>";
echo "<td width='100px'> <font color='black'> Clue word: </font></td>";
echo "<td width='100px'>" . $row['wd'] . "</td>";
echo "<td width='150px'> <font color='black'>Context sentence: </font></td>";
echo "<td width='200px'>" . $row['st'] . "</td>";
echo "<td>" . $row ['wd_id'] . "</td>";
echo "<td>" . $row ['answer'] . "</td>";
echo "</tr>";
}
echo"</table>";

?>

I also have a text input box and a submit button.
I want to compare the word typed in the input box with the random word.
If they are the same I want to echo the word in green
If not I want to echo the typed word in red
The code I have is as below:

To compare and green or red:
<?php

$input = $_POST['1'];

if (isset ($_POST['SUBMIT'])){

if ($input == $answer)

echo "<font color='green'>" . $answer . "</font> ";
else echo "<font color='red'>" . $input . "</font>";
echo $explanation;
echo $related;
echo $pron;

}
?>

the problem I have is that
when I try comparing the input from the form to the variable $answer, it doesn't compare.

Basically because I are not able to define to variable $anwer with the information I want it to have.

I need the the code that defines the variable $answer (which would be content in the columna "answer" of the random row generated by the variable $result).
Any help would be very welcome

Many thanks
smithandson
New php-forum User
New php-forum User
 
Posts: 2
Joined: Mon Jul 16, 2012 12:42 pm

Re: mysql and random comparison

Postby johnj » Wed Jul 18, 2012 8:09 am

First of all , do not use words like "input, system, file , answer, question, date etc etc" as variable names.

second, if $input cannot be compared with $answer, then display both the variables and verify that they have a value.
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm


Return to mySQL & php coding

Who is online

Users browsing this forum: Google [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.