Hey,
I hope there is someone on here who will be able to help me out with this, i am making a keyword search to go into an exsisiting website i have so users can search for desktop wallpapers for their computers. It is all working fine apart form when the users use more than one keyword it seems to print unwanted db entries on the bottom without the proper formatting or anything.
For an example of what it does go to www.tazzyserver.co.uk/desktop.php
and try search for 'red glow'
My code is as follows:
PHP Code:
<?php session_start(); include('functions.php'); dbconnect(); // is the one accessing this page logged in or not? if (!isset($_SESSION['tsuser_loggedin']) || $_SESSION['tsuser_loggedin'] !== true) { // not logged in, move to login page $loggedin="false"; } else { $loggedin="true"; $username=$_COOKIE['username']; $userrank=$_COOKIE['userrank']; } $username=$_COOKIE['username']; $userrank=$_COOKIE['userrank']; $errormessage=$_REQUEST['errormessage']; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <link rel="stylesheet" href="style.css" type="text/css" /> <style type="text/css"> <!-- .style1 {font-weight: bold} --> </style> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>TazzyServer.co.uk</title> </head> <body> <?php if ($errormessage=='') { } else { echo "<div id='toperrorbar'><strong>".$errormessage."</strong></div>"; } ?> <div id="wrapper"> <div id="header"><img src="images/logo.jpg" alt="Tornado Designs" /> </div> <div id="main_content"> <div id="sidebar"> <div class="sideheader"><strong> Menu </strong></div> <div class="sidecontent"> <?php include ('navigation.inc'); ?> </div> <div class="sideheader"><strong> Members Area </strong></div> <div class="sidecontent"> <?php if (!isset($_SESSION['tsuser_loggedin'])) { // not logged in, redirect to index.php ?> <form action="login.php" method="post" enctype="multipart/form-data" name="login" id="login"> <label>Username <input name="username" type="text" class="form" id="username" /> </label> <br /> <label>Password <input name="userpass" type="password" class="form" id="userpass" /> </label> <br /> <label> <input name="login" type="submit" class="submitbutton style1" id="login" value="Login" /> </label> <br /> <a href="reminder.php">Forgotten Password?</a> </form> <?php } else { echo "<strong>Welcome </strong>".$username."!<br />"; echo "<strong>User Level: </strong>".$userrank."<br />"; if($userrank=='Admin') { echo "<a href='admin/admin.php'>Admin Home</a><br />"; } else if($userrank=='Member') { echo "<a href='members/member.php'>Members Home</a><br />"; } echo "<a href='logout.php'>Logout</a>"; } ?> </div> <div class="sideheader"><strong> Statistics</strong></div> <div class="sidecontent"><?php include('webstats.php'); ?> </div> <div class="sideheader"><strong>Who's Online? </strong></div> <div class="sidecontent"><?php include('whosonline.php'); ?> </div> </div> <div id="content"> <div class="mainheader"><strong>Welcome to TazzyServer.co.uk! </strong></div> <div class="maincontent"> <table width="100%" border="0"> <tr> <td class="tableheader"><div align='left'><strong>Keyword Search </strong></div></td> </tr> <tr> <td class="tablecontent"><form action="desktop.php?action=searchwallpapers" method="post" enctype="multipart/form-data" name="searchgames" id="searchgames"> <div align="left"> <input name="keywords" type="text" id="keywords"> <input name="search" type="submit" id="search" value="Search"> <br> Enter your keywords above and click 'Search' to search the wallpapers database! </div> </form></td> </tr> </table> <?php $action=$_REQUEST['action']; if ($action=='searchwallpapers') { // get wallpaper list and use pagination to display 10 per page $display=10; // Get the search variable from URL $keywords=$_REQUEST['keywords']; //trim whitespace from the stored variable $trimmed = trim($keywords); //separate key-phrases into keywords $trimmed_array = explode(" ",$trimmed); // determine how manay pages there are if(isset($_GET['np'])) { $num_pages=$_GET['np']; } else { foreach ($trimmed_array as $trimm){ // Count the number of pages $query="SELECT COUNT(*) FROM wallpapers WHERE wallpapername='".$keywords."' OR wallpapername LIKE '%$trimm%' ORDER BY wallpapername ASC"; $result=mysql_query($query); $row=mysql_fetch_array($result, MYSQL_NUM); $num_records=$row[0]; } // Calculate the number of pages if($num_records > $display) { // more than 1 page $num_pages = ceil($num_records / $display); } else { $num_pages=1; } }// end of np IF // determine the starting point in the database if(isset($_GET['s'])) { $start = $_GET['s']; } else { $start=0; } // table header echo'<table align="center" cellspacing="0" cellpadding="5" border="1"> <tr> <td class="mainheader" align="left"><strong>Wallpaper Name</strong></td> <td class="mainheader" align="left"><strong>Wallpaper Author</strong></td> <td class="mainheader" align="left"><strong>Actions</strong></td> </tr>'; // make the query $checkwallpapers="SELECT * FROM wallpapers WHERE wallpapername='".$keywords."' ORDER BY wallpapername ASC LIMIT $start, $display"; $result2=mysql_query($checkwallpapers); // Run the query $num_rows1=mysql_num_rows($result2); if($num_rows1>=1) { // fetch and print all records $bg='#FFFFCC';// set the background colour. while($wallpaper=mysql_fetch_array($result2, MYSQL_ASSOC)) { $bg=($bg=='#FFFFCC' ? '#FFFFCC' : '#FFFFFF'); // Switch background color. echo '<tr bgcolor="'.$bg.'" border="1"> <td align="left">'.$wallpaper['wallpapername'].'</td> <td align="left">'.$wallpaper['wallpaper_author'].'</td> <td align="left"><a href="viewwallpaper.php?wallpaperid='.$wallpaper['wallpaperid'].'">'.$wallpaper['wallpapername'].'</a></td> </tr>'; } } $partialdisplay=$display - $num_rows1; // Build SQL Query for each keyword entered foreach ($trimmed_array as $trimm){ // make the query $checkwallpapers3="SELECT * FROM wallpapers WHERE wallpapername LIKE '%$trimm%' ORDER BY wallpapername ASC LIMIT $start, $partialdisplay"; $result3=mysql_query($checkwallpapers3); // Run the query $num_rows3=mysql_num_rows($result3); if($num_rows3>=1) { // fetch and print all records $bg3='#CC99CC';// set the background colour. while($wallpaper3=mysql_fetch_array($result3, MYSQL_ASSOC)) { $bg3=($bg3=='#CC99CC' ? '#CC99CC' : '#FFFFFF'); // Switch background color. echo '<tr bgcolor="'.$bg3.'" border="1">'; echo '<td align="left">'.$wallpaper3['wallpapername'].'</td>'; echo '<td align="left">'.$wallpaper3['wallpaper_author'].'</td>'; echo '<td align="left"><a href="viewwallpaper.php?wallpaperid='.$wallpaper3['wallpaperid'].'">'.$wallpaper3['wallpapername'].'</a></td>'; echo '</tr>'; } } else { } echo '</table>'; } echo "<br />".$num_rows1." Exact Matches and ".$num_rows3." Partial Matches.<br />"; // pagination links if ($num_pages == 1) { echo "Page 1 of 1"; } else { echo '<br /><p>'; // determine what page the script is on $current_page=($start / $display) + 1; // if its not the first page make a previous button if ($current_page !=1) { echo '<a href="desktop.php?action=searchwallpapers&s='.($start - $display) . '&np=' .$num_pages.'">Previous </a>'; } // make all the numbered pages for ($i=1; $i <=$num_pages; $i++) { if($i !=$current_page) { echo ' | <a href="desktop.php?action=searchwallpapers&s='.(($display * ($i - 1))).'&np='.$num_pages.'">'.$i.'</a> | '; } else { echo $i.''; } } // if it's not the last page make a next button if($current_page != $num_pages) { echo '<a href="desktop.php?action=searchwallpapers&s='.($start + $display).'&np='.$num_pages.'"> Next</a>'; } echo '</p>'; echo '<p><a href="desktop.php">Start a New Search!</a>'; }// end of links section } ?> <div class="mainheader"><strong>Latest News </strong></div> <div class="maincontent"> <?php $getnews=mysql_query("SELECT * FROM articles ORDER BY postdate LIMIT 1"); if (mysql_num_rows($getnews)==0) { echo "There are currently no news articles, please try again later!"; } else { while ($news=mysql_fetch_array($getnews, MYSQL_ASSOC)) { //display news content here $getauthor=mysql_query("SELECT * FROM users WHERE userid='".$news['articleauthor']."'"); while($user=mysql_fetch_array($getauthor, MYSQL_ASSOC)) { $author="".$user['username'].""; } echo "<div align='left' style=' padding: 3px 3px 3px 3px; background-color: #333333; color: #FFFFFF; font-family: Geneva, Arial, Helvetica, sans-serif;'><strong>".$news['articlename']."</strong></div>"; echo "<div align='left' style=' border: 1px solid #333333; padding: 3px 3px 3px 3px; background-color: #FFFFFF; color: #000000; font-family: Geneva, Arial, Helvetica, sans-serif;'>".$news['articlecontent']."</div>"; $date = $news['postdate']; /*Convert date to a time stamp. Convert to desired formatted date.*/ // Parse the String into a new UNIX Timestamp $date_string = strtotime($date . " GMT"); $date_created = date("l dS F (g:ia)", $date_string); echo "<div align='left' style=' padding: 3px 3px 3px 3px; background-color: #333333; color: #FFFFFF; font-family: Geneva, Arial, Helvetica, sans-serif;'><strong>Posted By: </strong><a href='profile.php?userid=".$news['articleauthor']."' class='submitbutton'>".$author."</a></div>"; echo "<div align='left' style=' padding: 3px 3px 3px 3px; background-color: #333333; color: #FFFFFF; font-family: Geneva, Arial, Helvetica, sans-serif; margin-bottom: 5px;'><strong>News Posted On: </strong>".$date_created."</div>"; } } ?> </div> </div> <?php include('membersonline.php'); ?> </div> </div> <div id="footer"> All Content Copyright © Martin Laing 2008 </div> </div> </body> </html>
|