Author Topic: adding an add to cart button to a search engine  (Read 376 times)

0 Members and 1 Guest are viewing this topic.

Offline huntrguy102Topic starter

  • Irregular
  • Posts: 27
    • View Profile
adding an add to cart button to a search engine
« on: February 19, 2010, 11:27:03 AM »
So I have a search engine that searches my database of books. http://chulainnlibros.com/cart/searching.html(they are spanish books so search something like "el" or "la"). So what I was wondering was, is there a way to add an add to cart button (like this one on my site http://chulainnlibros.com/cart/index.php). thanks
« Last Edit: February 19, 2010, 11:29:41 AM by huntrguy102 »

Offline jskywalker

  • Enthusiast
  • Posts: 289
  • Gender: Male
    • View Profile
Re: adding an add to cart button to a search engine
« Reply #1 on: February 20, 2010, 07:26:12 AM »
i think i dont understand the question, because you have the example in the index.php?

echo '<a href="http://chulainnlibros.com/cart/cart.php?action=add&id=1">Add to cart</a>'

Offline huntrguy102Topic starter

  • Irregular
  • Posts: 27
    • View Profile
Re: adding an add to cart button to a search engine
« Reply #2 on: February 22, 2010, 10:56:21 AM »
well here is the code from the index php



// Include MySQL class
require_once('inc/mysql.class.php');
// Include database connection
require_once('inc/global.inc.php');
// Include functions
require_once('inc/functions.inc.php');
// Start the session
session_start();


// find out how many rows are in the table 
$sql "SELECT COUNT(*) FROM books";
$result mysql_query($sql$conn) or trigger_error("SQL"E_USER_ERROR);
$r mysql_fetch_row($result);
$numrows $r[0];


// number of rows to show per page
$rowsperpage 10;
// find out total pages
$totalpages ceil($numrows $rowsperpage);

// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
   
// cast var as int
   
$currentpage = (int) $_GET['currentpage'];
} else {
   
// default page num
   
$currentpage 1;
// end if

// if current page is greater than total pages...
if ($currentpage $totalpages) {
   
// set current page to last page
   
$currentpage $totalpages;
// end if
// if current page is less than first page...
if ($currentpage 1) {
   
// set current page to first page
   
$currentpage 1;
// end if

// the offset of the list, based on current page 
$offset = ($currentpage 1) * $rowsperpage;

// get the info from the db 
$sql "SELECT id, title, author, price, Genre FROM books LIMIT $offset$rowsperpage";
$result mysql_query($sql$conn) or trigger_error("SQL"E_USER_ERROR);

$output[] = '<ul>';
// while there are rows to be fetched...
while ($row mysql_fetch_assoc($result)) {
   
// echo data
   
	
$output[] = '<li>"'.$row['title'].'" by '.$row['author'].': $'.$row['price'].': '.$row['Genre'].'<br /><a href="cart.php?action=add&id='.$row['id'].'">Add to cart</a></li>';
// end while

$output[] = '</ul>';
echo 
join('',$output);
/******  build the pagination links ******/
// range of num links to show
$range 3;

// if not on page 1, don't show back links
if ($currentpage 1) {
   
// show << link to go back to page 1
   
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'>First</a> ";
   
// get previous page num
   
$prevpage $currentpage 1;
   
// show < link to go back to 1 page
   
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'>Prev</a> ";
// end if 

// loop to show links to range of pages around current page
for ($x = ($currentpage $range); $x < (($currentpage $range) + 1); $x++) {
   
// if it's a valid page number...
   
if (($x 0) && ($x <= $totalpages)) {
      
// if we're on current page...
      
if ($x == $currentpage) {
         
// 'highlight' it but don't make a link
         
echo " [<b>$x</b>] ";
      
// if not current page...
      
} else {
         
// make it a link
         
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
      } 
// end else
   
// end if 
// end for
                 
// if not on last page, show forward and last page links        
if ($currentpage != $totalpages) {
   
// get next page
   
$nextpage $currentpage 1;
    
// echo forward link for next page 
   
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>Next</a> ";
   
// echo forward link for lastpage
   
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>Last</a> ";
// end if
/****** end build pagination links ******/
?>



and I just dont know how to get the add to cart button from here and put it into the search.

here is the code for the search page:





<?php

  
// Get the search variable from URL

  
$var = @$_GET['q'] ;
  
$trimmed trim($var); //trim whitespace from the stored variable

// rows to return
$limit=10

// 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 ** EDIT REQUIRED HERE **
mysql_connect("amarroquin.db.5021301.hostedresource.com","amarroquin","Chulainnl1br0s"); //(host, username, password)

//specify database ** EDIT REQUIRED HERE **
mysql_select_db("amarroquin") or die("Unable to select database"); //select which database we're using

// Build SQL Query  
$query "SELECT * FROM books WHERE title OR author LIKE \"%$trimmed%\"  
  order by title"
// EDIT HERE and specify your table and field names for the SQL query

 
$numresults=mysql_query($query);
 
$numrows=mysql_num_rows($numresults);

// If we have no results, offer a google search as an alternative

if ($numrows == 0)
  {
  echo 
"<h4>Results</h4>";
  echo 
"<p>Sorry, your search: &quot;" $trimmed "&quot; returned zero results</p>";

// google
 
echo "<p><a href=\"http://www.google.com/search?q=" 
  
$trimmed "\" target=\"_blank\" title=\"Look up 
  " 
$trimmed " on Google\">Click here</a> to try the 
  search on google</p>"
;
  }

// next determine if s has been passed to script, if not use 0
  
if (empty($s)) {
  
$s=0;
  }

// get results
  
$query .= " limit $s,$limit";
  
$result mysql_query($query) or die("Couldn't execute query");

// display what the person searched for
echo "<p>You searched for: &quot;" $var "&quot;</p>";

// begin to show results set
echo $title;
echo 
$author;
$count $s ;

// now you can display the results returned
  
while ($rowmysql_fetch_array($result)) {
  
$title $row["title"];
  
$author $row["author"];
  
  echo 
"$count.)&nbsp;$title<br/>" ;
  echo 
"author";
  
$count++ ;
  }

$currPage = (($s/$limit) + 1);

//break before paging
  
echo "<br />";

  
// next we need to do the links to other results
  
if ($s>=1) { // bypass PREV link if s is 0
  
$prevs=($s-$limit);
  print 
"&nbsp;<a href=\"$PHP_SELF?s=$prevs&q=$var\">&lt;&lt; 
  Prev 10</a>&nbsp&nbsp;"
;
  }

// calculate number of pages needing links
  
$pages=intval($numrows/$limit);

// $pages now contains int of pages needed unless there is a remainder from division

  
if ($numrows%$limit) {
  
// has remainder so add one page
  
$pages++;
  }

// check to see if last page
  
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

  
// not last page so give NEXT link
  
$news=$s+$limit;

  echo 
"&nbsp;<a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 &gt;&gt;</a>";
  }

$a $s + ($limit) ;
  if (
$a $numrows) { $a $numrows ; }
  
$b $s ;
  echo 
"<p>Showing results $b to $a of $numrows</p>";
  
?>




Offline huntrguy102Topic starter

  • Irregular
  • Posts: 27
    • View Profile
Re: adding an add to cart button to a search engine
« Reply #3 on: February 22, 2010, 03:32:41 PM »
well i figured it out. It had nothing to do with what I was talking about.