Hi there, I'm trying to create a simple DVD shopping cart. I have
implemented a way to browse by genre and am trying to implement a way to
search by selecting title, director and actor and typing some text in the
text field for it to search the database.
As trying to implement the title, director and actor searches seemed to
complicated to try together, I am trying to make the title one work first
and then move on to the other two from there (2 more if statements
hopefully). Any help or advice would be great.
When I try to search with the code below, it just brings up all of the DVDs
again (I'm also not sure why the whole list of DVDs appears when the page is
first loaded).
Here is my code:
<?php
// this section creates a session, connects to the db and handles the two
buttons being pressed.
session_start ();
require ('mysql.php');
mysql_connect ($host, $user, $passwd);
mysql_select_db ($dbName);
if (isset ($_GET['add']))
$_SESSION['trolley'][] = $_GET['add'];
else if ($_GET['op'] === 'clear')
$_SESSION['trolley'] = "";
?>
<html>
<head>
<title>PHP & MySQL</title>
</head>
<center>
<body><P>
</P>
<form action="browse_search.php" method=get>
<select name="search_in">
<option value="title">title</option>
<option value="director">director</option>
<option value="actor">actor</option>
</select>
<input type="text" size="16" maxlength="24" name="search_text" />
<input type="submit"/>
</form
</center>
<table>
<?php
$getdvds_query = "";
//this section gets a query from the db using genre selected by the user,
and outputs DVDs based
//on selected genre
$getgenres_query = mysql_query ('SELECT * FROM genre');
while ($genre = mysql_fetch_assoc ($getgenres_query))
echo "<a href=\"$PHP_SELF?genrename=$genre[name]\">$genre[name]</a><BR>";
echo "<BR>";
if ($search_in == "title") {
$getdvds_query = mysql_query ("SELECT title, duration, rel, descr, genre,
stock, price FROM dvd where title LIKE '%$search_text%'");
}
$getdvds_query = mysql_query ("SELECT title, duration, rel, descr, genre,
stock, price FROM dvd where genre LIKE '%$genrename%'");
echo "<table bgcolor=\"ddeeff\"><thead><tr>";
for ( $i = 0 ; $i < mysql_num_fields($getdvds_query) ; $i++ ) {
echo "<th bgcolor=\"abcdef\">" . mysql_field_name($getdvds_query,$i) .
"</th>\n";
}
if (mysql_num_rows ($getdvds_query))
{
for ($id = 0; $dvd[] = mysql_fetch_assoc ($getdvds_query); $id++)
{
echo "<tr>\n";
echo "<td>{$dvd[$id][title]}</td>\n";
echo "<td>{$dvd[$id][duration]}</td>\n";
echo "<td>{$dvd[$id][rel]}</td>\n";
echo "<td>{$dvd[$id][descr]}</td>\n";
echo "<td>{$dvd[$id][genre]}</td>\n";
echo "<td>{$dvd[$id][stock]}</td>\n";
echo "<td>{$dvd[$id][price]}</td>\n";
echo "<td><a href=\"$PHP_SELF?add=$id\">add to basket</a></td>";
echo "</tr>\n";
}
}
?>
</table>
<a href="browse_search.php?op=clear">empty trolley</a><br /><br />
<?php
//this section outputs basket contents
if (!empty ($_SESSION['trolley']))
{
echo 'Trolley contents:<br />';
foreach ($_SESSION['trolley'] as $trolley_item)
echo $dvd[$trolley_item]['title']."<br />";
}
else
echo 'Trolley is empty!';
?>
</body>
</html>