Jump to content

php and random sql search


smithandson

Recommended Posts

Hello people,

 

I need some help with some PHP coding, and hope someone can help me.

 

I have a mysql table within a mysql database.

for example

 

id  word          sentence                                      associated word

1  happy      there is a lot of unhappiness                  unhappiness

2  consume  I am a consumer                                  consumer

3

 

 

I want to choose a random element from the associated word column

 

I want to make this random element a php variable  $word1 (for example)

 

elsewhere I want to have an text input box with a submit button

I want to make what is typed a php variable, for example $word 2

 

I then want to compare $word1 and $word2

 

If they are the same, I want to echo $word2 in green

If not, I want to echo $word2 in red with an explanation

 

I hope my description is not too confusing

 

many thanks

Link to comment
Share on other sites

thank you very much for such a fast response

 

What I have till now is as 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 ");

 

$answer = mysql_query("SELECT answer FROM words ORDER BY RAND() LIMIT 1");

 

while($row = mysql_fetch_array($answer))

  {

 

echo $row['answer'];

 

echo "<br />";

 

}

 

mysql_close($con);

?>

 

<?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>";

 

  ?>

 

<?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 $explanation1;

 

echo $related = $row['related'];

 

echo $pron = $row['pron'];

 

 

}

 

?>

 

Please advise

 

kind regards

Link to comment
Share on other sites

Hi,

I can generate the random element with SQL, but only as echo

 

elsewhere I can do the php comparison and echo green or red depending on whether the answer is right or wrong.

 

I am having problems establishing the random output of the sql as a php variable, with which to do the comparison.

 

many thanks

Link to comment
Share on other sites

Hi

 

I think I need to be a bit clearer

 

I am starting with the following table

 

 

id word         sentance                                     answer               explanation

1 happy I am sorry for your unhappines     unhappiness

2 consume I am a consumer                     consumer

3

 

 

From this table I am  trying to  create the variable $answer which captures in the random row generated , the content in the column "answer" of that row. (for example 'unhappiness)

 

the code I have for this operation is:

 

 

<?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 this random answer, the code I have is

 

 

<?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 then have a text input box with a submit button

a word is typed in this input box which I wish to compare with the random answer previously generated and if they are the same, echo in green if not echo in red

 

the code for that is

 

 

<?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 you try comparing the input from the form to the variable $answer, it doesn't compare.

 

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

 

I need 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 appreciated

 

many thanks

 

 

 

 

Link to comment
Share on other sites

Thank you very much for your help JESIROSE

 

you have been very kind.

 

I will be away for a bit and as such will not be able to try your solution.

 

As soon as I get it working I will give you some feedback.

 

thank you once againg, and take care

 

regards

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.