Jump to content

Display search result


marky83

Recommended Posts

Hi!

 

I have a SQL database with information about albums and track (music).

 

This is where the user inputs a search term(entersearch.php):

<form name="form" action="displaysearchalbum.php" method="get">
  <input type="text" name="getsearch" />
  <input type="submit" name="Submit" value="search album" />
</form>
<br />
<h2>Search for track</h2>
<form name="form" action="displaysearchtrack.php" method="get">
  <input type="text" name="getsearch2" />
  <input type="submit" name="submit2" value="search track" />
</form>

 

And this is where the search result is displayed with links to a pages with more information about the album the user is interested in.

 

<?php

  // Get the search variable from URL
  $var = @$_GET['getsearch'] ;
  $trimmed = trim($var); //trim whitespace from the stored variable

// rows to return
$limit=20; 

// check for an empty string and display a message.
if ($trimmed == "")
  {
  echo "<p>Please enter a search...</p>";
  exit;
  }

// check for a search parameter
if (!isset($var))
  {
  echo "<p>We dont seem to have a search parameter!</p>";
  exit;
  }

//connect to your database
mysql_connect("xxx","xxx","xxx"); //(host, username, password)

//specify database
mysql_select_db("xxx") or die("Unable to select database"); //select which database we're using

// Build SQL Query  
$result = mysql_query("SELECT albumid, albumname FROM album WHERE albumname like '%$trimmed%'");
//$query = "SELECT albumname FROM album WHERE albumname='$trimmed'"; 

while ($row = mysql_fetch_array($result)) {
    
echo '<a href="albuminfo.php?albumid=' . $row[0] . '">' . $row[1] . '</a>';
echo "<p></p>";
}

?>

 

My problem is that I don't know how to transfer the information from displaysearchalbum.php to albuminfo.php about which album the information is going to be displayed. Preferably I would have a variable in albuminfo.php which contains the albumid in interest. I am a novice in php but think that the code I have so far written is working. Any ideas about necessary changes in the code I wrote or what kind of code I need for albuminfo.php would be helpful. I have not yet written any code for albuminfo.php because I am not quite sure how to.

 

 

Link to comment
Share on other sites

It's looking good so far, you're almost there.

 

This line "echo '<a href="albuminfo.php?albumid=' . $row[0] . '">' . $row[1] . '</a>';" makes the 'albumid' available to albuminfo.php.

So on albuminfo.php you just have to query the database to return all the data for that albumid, e.g. with a query like:

 

$result = mysql_query("SELECT * FROM album WHERE albumid = '" . $_GET["albumid"] . "'");

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.