Hi

I am writing a site that will include an audio dictionary.

The dictionary will have a working search function.

This is what I have so far:

When a search is made, one or more results is returned.

The user can click on each result to get taken to a unique record page for the search result he clicked on.

This will include some sentences where he can hear the word being used in context.

Currently I have got to the stage where the word can be clicked on to open its unique page;

if (mysql_num_rows($result) < 1) {

echo "<br><br><h2>Oh dear. We didn't find anything. Sorry, we did look though.</h2>";

}else {
         
echo '<table align="center" cellspacing="8" cellpadding="8" width="85%"><tr><td align="left"><b>Word</b></td></tr>';

echo "<br><br><h3>This is what we found:</h3><br>";

while ($r=mysql_fetch_array($result, MYSQLI_ASSOC))

echo '<tr><td align="left"><a href="word.php?w=' . $r['word'] . '">' . $r['word'] . '</a></td></tr>';

}

It's OK to print out the word too in the word.php;

echo $_GET['w'];

This is the problem;

What has so for eluded me is getting the sentence to appear on the unique word page.

I'm hoping I've been missing something reasonably straight forward.

Could someone help me with that please?

Thanks in advance - do please say if what I've written is unclear.

Say the sentence is $row. You could use an if statement like this one on the top of the page (or wherever you wanna display you result):

if(isset($_GET['w'])){ //show the next div only if there\'s GET parameter
echo '<div id="sentence">This is the sentence : . $row['sentence'] .</div>';
}
else
{
echo 'No sentence!';
}

Basically, you can control a page with IF/ELSE statements, unless you wanna go advanced and use AJAX (interesting if you have some spare time).

Thanks for that, much appreciated! That's just the kind of thing I'm after.

Something needs a little finessing though (elsewhere in the site) as it returns a white page, even with error reporting on.

Any ideas?

after or before you put my code ?

After.

print_r($_GET);

returns 'word' but not 'sentence'. I wonder if everything is being fetched from the db?

Well, here's a hint:
Use ANOTHER query especialy for the sentence; something like: "SELECT sentence FROM table WHERE word = '$word'"

Thanks.

This is what I currently use - is this query flawed for its purpose?

$result = mysql_query("SELECT word,sentence1 FROM pro_words WHERE word LIKE '%$search%'");

Perhaps I'm not getting quite the right answer as I'm not asking quite the right question.

I'll try what you suggest - I appreciate your help.

The query is Ok.

So it's probably not the query then.

Error reporting is on in php.ini so am a bit baffled by this white page.

can you post ALL the code ?

here it is:

<?php

session_start();

if(isset($_SESSION['first_name']))

  {

include ('includes/header_loggedin.html');

echo '<br><br><h3>You have chosen the word </h3><br>';

//insert word

echo $_GET['w'];

#print_r($_GET);

echo '<h3>Here are some examples of its modern use </h3>';

if (isset($_GET['w'])){ //show the next div only if there\'s GET parameter
echo '<div id="sentence1">This is the sentence : . $row['sentence1'] .</div>';
}
else
{
echo 'Content coming soon!';
}

echo '<h3>Use the player to listen and practice </h3>';


echo '<h3>Click on a sentence to listen to it. </h3>';

//insert player

echo '<h3>Listen to the instructions to get the most out of our player! </h3>';

//insert player with instructions

echo '<br><h2><a href="search.php">Back To Search</a><br></h2>';

// Check for an image name in the URL:

}else{

include ('includes/header.html');

       echo '<h2><p>If you want to access this site, please register or log-in.</p></h2>';

   }

?>

I meant, with the SQL part.

here it is;

<?php
  session_start();
    // check session variable

  if (isset($_SESSION['first_name']))
  {
include ('includes/header_loggedin.html');
  
  //connect to mysql
$link = mysql_connect('localhost', 'zzz', 'xxx');
if (!$link) {
    die('Not connected : ' . mysql_error());
}

// access the required db

$db_selected = mysql_select_db('proguide', $link);
if (!$db_selected) {
    die ('Can\'t use proguide : ' . mysql_error());
}

//specify type of search

$search=$_POST["search"];

//get the mysql and store them in $result

$result = mysql_query("SELECT word,sentence1 FROM pro_words WHERE word = '$word' LIKE '%$search%'");
			        
//get the db content that is specified above

if (mysql_num_rows($result) < 1) {

echo "<br><br><h2>Oh dear. We didn't find anything. Sorry - we did look though.</h2>";

}else {
         
echo '<table align="center" cellspacing="8" cellpadding="8" width="85%"><tr><td align="left"><b>Word</b></td></tr>';

echo "<br><br><h3>This is what we found:</h3><br>";

while ($r=mysql_fetch_array($result, MYSQL_ASSOC))

echo '<tr><td align="left"><a href="word.php?w=' . $r['word'] . '">' . $r['word'] . '</a></td></tr>';

}

#$_GET["word"]=$r['word'];
#$_GET["sentence1"]=$r['sentence1'];

echo "Click on any word to use it.";

echo '</table>'; // Close the table.      

   }

	else
 
   {
    echo '<h1><p>You are not logged in.</p></h1>';
    echo '<h2><p>If you want to see this page, please register or log-in.</p></h2>';
    echo '<a href="index.php">Take Me Back</a><br>';
    echo '<a href="register.php">Register</a><br>';
   }
  
 
    echo '<br><a href="search.php">Search again?</a><br>';
    
?>
$result = mysql_query("SELECT word,sentence1 FROM pro_words WHERE word = '$word' LIKE '%$search%'");

Something is VERY wrong here ... ! Where do you get your $_POST (and what is it? a word, sentence ..??) ?

I'll look into this and see how I could improve it.

The search function was slightly adapted from a book I have, but was maybe not designed for the purpose I have been trying to use it.

A proper SQL query is written like:

SELECT word, sentence1 FROM pro_words WHERE word LIKE '%$search%'

you can't mix both "=" and LIKE for the same field !

definitely.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.