Jump to content

JAMerlino

New Members
  • Posts

    1
  • Joined

  • Last visited

JAMerlino's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Hi all, I am new here and have a question. I have created a search tool for a database table which I am currently searching through the Description column in the table but would like to add the Title, and the Category columns as well. My script is below. it searches, creates the table adn displays adn paginates perfectly, but as soon as I try to add more variables it crashes. It seems to be in the $construct variable I am building to fetch adn display the results. Any assistance would be great. I would like to be able to, in index.php search feild, put Burger and it would look through Title, Catagory, adn Description within the table and display the results. This is the Index.php script <html> <head> <title>RECIPE SEARCH</title> </head> <body> <form action='search.php' method='GET'> <center> <h1>CHEF JAMIE RECIPE SEARCH TOOL</h1><br/> <h4>Currently searching through recipe ingredients.</h4> <input type='text' size='50' name='search'></br></br> <input type='submit' name='submit' value='Search source code' ></br></br></br> </center> </form> </body> </html> And this is the search.php page <?php $button = $_GET ['submit']; $search = $_GET ['search']; if(strlen($search)<=1) echo "Search term too short"; else{ echo "You searched for <b>$search</b> <hr size='1'></br>"; mysql_connect("localhost","xxxx","xxxxxx!"); mysql_select_db("chefjami_CJG2"); $search_exploded = explode (" ", $search); foreach($search_exploded as $search_each) { $x++; if($x==1) $construct .="Description LIKE '%$search_each%'"; else $construct .="AND Description LIKE '%$search_each%'"; } $constructs ="SELECT * FROM recipes WHERE $construct"; $run = mysql_query($constructs); $foundnum = mysql_num_rows($run); if ($foundnum==0) echo "Sorry, there are no matching result for <b>$search</b>.</br></br>1. Try more general words. for example: If you want to search 'how to create a website' then use general keyword like 'create' 'website'</br>2. Try different words with similar meaning</br>3. Please check your spelling. </br>4. <a href='index.php'>Go back to search</a>"; else { echo "$foundnum results found ! <a href='index.php'>Go back to search</a><p>"; $per_page = 1; $start = $_GET['start']; $max_pages = ceil($foundnum / $per_page); if(!$start) $start=0; $getquery = mysql_query("SELECT * FROM recipes WHERE $construct LIMIT $start, $per_page"); while($runrows = mysql_fetch_assoc($getquery)) { $Title = $runrows ['Title']; $Description = $runrows['Description']; $Catagory = $runrows ['Catagory']; echo " <b><h3>$Title</h3></b><br> $Description<br> <strong>Category: $Catagory</strong><p> <hr> "; } //Pagination Starts echo "<center>"; $prev = $start - $per_page; $next = $start + $per_page; $adjacents = 3; $last = $max_pages - 1; if($max_pages > 1) { //previous button if (!($start<=0)) echo " <a href='search.php?search=$search&submit=Search+source+code&start=$prev'>Prev</a> "; //pages if ($max_pages < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up { $i = 0; for ($counter = 1; $counter <= $max_pages; $counter++) { if ($i == $start){ echo " <a href='search.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> "; } else { echo " <a href='search.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> "; } $i = $i + $per_page; } } elseif($max_pages > 5 + ($adjacents * 2)) //enough pages to hide some { //close to beginning; only hide later pages if(($start/$per_page) < 1 + ($adjacents * 2)) { $i = 0; for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++) { if ($i == $start){ echo " <a href='search.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> "; } else { echo " <a href='search.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> "; } $i = $i + $per_page; } } //in middle; hide some front and some back elseif($max_pages - ($adjacents * 2) > ($start / $per_page) && ($start / $per_page) > ($adjacents * 2)) { echo " <a href='search.php?search=$search&submit=Search+source+code&start=0'>1</a> "; echo " <a href='search.php?search=$search&submit=Search+source+code&start=$per_page'>2</a> .... "; $i = $start; for ($counter = ($start/$per_page)+1; $counter < ($start / $per_page) + $adjacents + 2; $counter++) { if ($i == $start){ echo " <a href='search.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> "; } else { echo " <a href='search.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> "; } $i = $i + $per_page; } } //close to end; only hide early pages else { echo " <a href='search.php?search=$search&submit=Search+source+code&start=0'>1</a> "; echo " <a href='search.php?search=$search&submit=Search+source+code&start=$per_page'>2</a> .... "; $i = $start; for ($counter = ($start / $per_page) + 1; $counter <= $max_pages; $counter++) { if ($i == $start){ echo " <a href='search.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> "; } else { echo " <a href='search.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> "; } $i = $i + $per_page; } } } //next button if (!($start >=$foundnum-$per_page)) echo " <a href='search.php?search=$search&submit=Search+source+code&start=$next'>Next</a> "; } echo "</center>"; } } ?>
×
×
  • 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.