473,324 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

PHP search (basic level)

Hi everyone,
I'm very new to PHP and have been following Kevin Yank's "How to build a db driven website using php & mysql" 3rd ed. and am working through Chapter 6, where a basic search page is created. What I would like to be able to do is, in the results, display not only the joketext (as done in the book), but also the author's name (which is located in a different table in the db), as well as the date it was submitted (in the same db as joketext), possibly a link to another site and extra info like the joke category.

In general, I would like to be able to understand how to adjust the php code to add data related to the results of any given search. The information is already related through the database, how do I use it in the results of a search?

I've used the same names of variables and tables as used in the book.

Thank you in advance for any help you can offer! I really appreciate it.

Below is the code for the part of the code I believe I need to change followed by the code for whole page (2 main sections "search" and "results").

Note: the only variable currently displaying in the results in $joketext.

/////////// EXCERPT : PARTS I THINK I NEED TO CHANGE //////////////
<code>

PART 1 // The basic SELECT statement

$select = 'SELECT DISTINCT id, joketext';
$from = ' FROM joke';
$where = ' WHERE 1=1';

PART 2 / results section

while ($joke = mysql_fetch_array($jokes)) {
$id = $joke['id'];
$joketext = htmlspecialchars($joke['joketext']);
echo "<li id=\"jump\">
<article class=\"entry\">
<header>
<h3 class=\"entry-title\"><a href=''>variable title</a></h3>
</header>
<div class=\"entry-content\">
<p>$joketext</p></div>
<footer class=\"entry-info\">
<abbr class=\"published\" title=\"2011-09-22T14:07:00-07:00\">Sept. 22, 2011</abbr>
</footer>
</article>
</li>";

</code>
//////////////////////////// MAIN CODE ///////////////////////
<code>
<html>
<body>
.
<header></header>
.
<section id="search">

<?php

$dbcnx = @mysql_connect('localhost', 'root', 'password');

if (!$dbcnx) {
exit('<p>Unable to connect to the ' . 'database server at this time.</p>');
}

if (!@mysql_select_db('ijdb')) {
exit('<p>Unable to locate the joke ' . 'database at this time.</p>');
}

$authors = @mysql_query('SELECT id, name FROM author');
if (!$authors) {
exit('<p>Unable to obtain author list from the database.</p>');
}

$cats = @mysql_query('SELECT id, name FROM category');
if (!$cats) {
exit( '<p>Unable to obtain category list from the database.</p>');
}

$themes = @mysql_query('SELECT id, name FROM theme');
if (!$themes) {
exit( '<p>Unable to obtain category list from the database.</p>');
}

$geofoci = @mysql_query('SELECT id, name FROM geofocus');
if (!$geofoci) {
exit( '<p>Unable to obtain category list from the database.</p>');
}

?>

<form class="searchField" name="input" action="main_search.php#jump" method="post">
<input type="text" name="searchtext">
<input type="submit" value="Search">
<ul>
<li>
<label><select name="aid" size="1">
<option selected value="">Any Author</option>
<?php
while ($author = mysql_fetch_array($authors)) {
$aid = $author['id'];
$aname = htmlspecialchars($author['name']);
echo "<option value='$aid'>$aname</option>\n";
}
?>
</select></label>
</li>
<li>
<label><select name="cid" size="1">
<option selected value="">Any Category</option>
<?php
while ($cat = mysql_fetch_array($cats)) {
$cid = $cat['id'];
$cname = htmlspecialchars($cat['name']);
echo "<option value='$cid'>$cname</option>\n";
}
?>
</select></label>
</li>
<li>
<label><select name="tid" size="1">
<option selected value="">Any Theme</option>
<?php
while ($theme = mysql_fetch_array($themes)) {
$tid = $theme['id'];
$tname = htmlspecialchars($theme['name']);
echo "<option value='$tid'>$tname</option>\n";
}
?>
</select></label>
</li>
<li>
<label><select name="gfid" size="1">
<option selected value="">Any Region</option>
<?php
while ($geofocus = mysql_fetch_array($geofoci)) {
$gfid = $geofocus['id'];
$gfname = htmlspecialchars($geofocus['name']);
echo "<option value='$gfid'>$gfname</option>\n";
}
?>
</select></label>
</li>
<li><a href="">Closing Date</a></li>
</ul>
</form>
</section>

<section id="results">
<?php

$dbcnx = @mysql_connect('localhost', 'root', 'password');

if (!$dbcnx) {
exit('<p>Unable to connect to the ' . 'database server at this time.</p>');
}

if (!@mysql_select_db('ijdb')) {
exit('<p>Unable to locate the joke ' . 'database at this time.</p>');
}

// The basic SELECT statement

$select = 'SELECT DISTINCT id, joketext';
$from = ' FROM joke';
$where = ' WHERE 1=1';

$aid = $_POST['aid'];
if ($aid != '') { // An author is selected
$where .= " AND authorid='$aid'";
}

$cid = $_POST['cid'];
if ($cid != '') { // A category is selected
$from .= ', jokecategory';
$where .= " AND joke.id=jokecategory.jokeid AND categoryid='$cid'";
}

$tid = $_POST['tid'];
if ($tid != '') { // A theme is selected
$from .= ', joketheme';
$where .= " AND joke.id=joketheme.jokeid AND themeid='$tid'";
}

$gfid = $_POST['gfid'];
if ($gfid != '') { // A region is selected
$from .= ', jokegeofocus';
$where .= " AND joke.id=jokegeofocus.jokeid AND geofocusid='$gfid'";
}

$searchtext = $_POST['searchtext'];
if ($searchtext != '') { // Some search text was specified
$where .= " AND joketext LIKE '%$searchtext%'";
}

?>

<ol id="results-list">
[COLOR="#FF0000"]
<?php
$jokes = @mysql_query($select . $from . $where);
if (!$jokes) {
echo '</table>'; exit('<p>Error retrieving jokes from database!<br />'.
'Error: ' . mysql_error() . '</p>');
}

while ($joke = mysql_fetch_array($jokes)) {
$id = $joke['id'];
$joketext = htmlspecialchars($joke['joketext']);
echo "<li id=\"jump\">
<article class=\"entry\">
<header>
<h3 class=\"entry-title\"><a href=''>variable title</a></h3>
</header>
<div class=\"entry-content\">
<p>$joketext</p></div>
<footer class=\"entry-info\">
<abbr class=\"published\" title=\"2011-09-22T14:07:00-07:00\">Sept. 22, 2011</abbr>
</footer>
</article>
</li>";
}

?>
[/COLOR]
</ol>
</section>
.
<footer></footer>
.
.
</body>
</html>

</code>
Oct 27 '11 #1
0 1178

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Orange Free | last post by:
I want to create a program that will ask a user a series of questions and then generate a Microsoft Word document whose content is dictated by the answers. I am not a professional programmer, and...
16
by: Stanley Sinclair | last post by:
Bear with me. I am being very calm; took a Valium. I have waited two weeks to write this, because every time I wrote it before the message was, at best, nasty. I need to use the services of...
56
by: Dave Vandervies | last post by:
I just fixed a bug that some of the correctness pedants around here may find useful as ammunition. The problem was that some code would, very occasionally, die with a segmentation violation...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
1
by: Brian | last post by:
Hello I am trying to implement 'Search' (Basic Keyword search) functonality on a site. Home page has a search text box and a 'GO' button. When users will type in a keyword and presses 'GO', URL...
4
by: RoadRunner | last post by:
Hi, I have a question. I am asked to produce a global search of a given corporate name, in more that one database. Each database has different table names and different field names in the tables....
3
by: GISDude | last post by:
Hi all. I am trying to create a little script(I think) that will basically use a search cursor. I am a GIS(geographic information systems) Analyst and in our software(ESRI ARCGIS 9.1) ESRI has...
2
by: darab | last post by:
will anybody be kind enough to send me this game by using basic level programming.I will be very thankful to him for this kindness. My alternative email adress is <email removed>
1
by: Brij kishor | last post by:
In java, how can we traverse a binary search tree level by level ?
4
by: Tony Johansson | last post by:
Hello! I know this might not be the appropriate forum for xml question. But xml is so important for .NET I hope my basic question can be answered in this forum. I just wonder does every xml...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.