skip to Main Content

I’m implementing a vary simple search engine to ebay items using eBay APIs for a university project.
I have a simple index.php file containing a search box :

<form action="MySample1.php" method="POST">
    <input type="text" name="query" size="90" maxlength="255" />
    <input type="submit" value="Cerca" /> 
</FORM>

and a sample.php file for displaying the results. The sample.php file implements a function findItemsByKeywordsResults just as following:

<?php
session_start();
error_reporting(E_ALL);  // Turn on all errors, warnings and notices for easier debugging

// API request variables
$endpoint = 'http://svcs.ebay.com/services/search/FindingService/v1';  // URL to call
$s_endpoint = "http://open.api.ebay.com/shopping?";  // Shopping URL to call
$m_endpoint = 'http://svcs.ebay.com/MerchandisingService?';  // Merchandising URL to call
$version = '1.0.0';  // API version supported
$appid = 'ed-7877-4e90-a4b4-bc6d897f71c3';  // Generated via My Account to ebay developer
$globalid = 'EBAY-US';  // Global ID of the eBay site you want to search (e.g., EBAY-DE)
$responseEncoding = 'XML';  // Type of response we want back
$cellColor = "bgcolor="#dfefff"";  // Light blue background used for selected items
$_SESSION['query'] = $_POST['query'];

function findItemsByKeywordsResults($selectedItemID = '', $cellColor = '') {
    $query= $_SESSION['query'];
    $safequery = urlencode( $query);  // Make the query URL-friendly
    global $endpoint;
    global $appid;
    global $responseEncoding;
    global $version;
    global $globalid;
    global $sort;

    $results = "<FORM ACTION="" . $_SERVER['PHP_SELF'] . "" METHOD="POST"> n";
    $results .= "<FIELDSET>n<LEGEND>Sort by: </LEGEND>n";
    $results .= "<SELECT NAME="Order" onchange="this.form.submit();"> n";
    $results .= "<OPTION VALUE="BestMatch" SELECTED="selected"> Best Match </OPTION> n";
    $results .= "<OPTION VALUE="BidCountFewest" > Bid Count Ascending </OPTION>  n";
    $results .= "<OPTION VALUE="BidCountMost"> Bid Count Descending </OPTION>  n";
    $results .= "<OPTION VALUE="PricePlusShippingLowest" > Price + Shipping Ascending </OPTION>  n";
    $results .= "<OPTION VALUE="PricePlusShippingHighest"> Price + Shipping Descending </OPTION>  n";
    $results .= "</SELECT> n </FIELDSET> n";
    $results .= "</FORM> n";  

    // Construct the findItemsByKeywords HTTP GET call
    $apicall = "$endpoint?";
    $apicall .= "OPERATION-NAME=findItemsByKeywords";
    $apicall .= "&SERVICE-VERSION=$version";
    $apicall .= "&SECURITY-APPNAME=$appid";
    $apicall .= "&GLOBAL-ID=$globalid";
    $apicall .= "&sortOrder=$sort";
    $apicall .= "&keywords=$safequery";
    $apicall .= "&paginationInput.entriesPerPage=10";

    // Load the call and capture the document returned by eBay API
    $resp = simplexml_load_file($apicall);

    // Check to see if the request was successful, else print an error
    if ($resp->ack == "Success") {
        $results .= '';
        // If the response was loaded, parse it and build links

        $results .= "<h2>These are the results for  <i>". $_SESSION['Query'] ."</i> </h2>n";
        $results .= "<!-- start table in findItemsByKeywordsResults --> n";
        $results .= "<table width="60%" cellpadding="5" border="1">";

        //for each item node build a table cell and append it to $results
        foreach($resp->searchResult->item as $item){
            // Set the cell color blue for the selected most watched item
            if ($selectedItemID == $item->itemId) {
                $thisCellColor = $cellColor;
            } else {
                $thisCellColor = '';
            }

            // Determine which price to display
            if ($item->sellingStatus->currentPrice) {
                $price = $item->sellingStatus->currentPrice;
            } else {
                $price = $item->listingInfo->buyItNowPrice;
            }

            // For each item, create a cell with imageURL, viewItemURL, watchCount, currentPrice
            $results .= "<tr> n<td $thisCellColor valign="top"> n";
            $results .= "<img src="$item->galleryURL"></td> n";
            $results .= "<td $thisCellColor valign="top"> <p><a href="" . $item->viewItemURL . "">" . $item->title . "</a></p>n";
            $results .= 'Shipped to: <b>' . $item->shippingInfo->shipToLocations . "</b><br> n";
            $results .= 'Current price: <b>$' . $price . "</b><br><br> n";
            $results .= "<FORM ACTION="" . $_SERVER['PHP_SELF'] . "" METHOD="POST"> n";
            $results .= "<INPUT TYPE="hidden" NAME="Selection" VALUE="$item->itemId"> n";
            $results .= "<INPUT TYPE="submit" NAME="$item->itemId" ";
            $results .= "VALUE="Get Details and Similar Items"> n";
            $results .= "</FORM> n";
            $results .= "</td> </tr> nn";
        }
        $results .= "</tr></table> n<!-- finish table --> n";
    }
    // If the response does not indicate 'Success,' print an error
    else {
        $results  = "<h3>Oops! The request was not successful. Make sure you are using a valid ";
        $results .= "AppID for the Production environment.</h3>";   
    }
    return $results;
}//end of the function

well, initially I obtain the right results. the problem come when i try to reorder the results using the Sort By form and reload the page. The error I find is :

Notice: Undefined index: query in C:xampphtdocsGettingStartedSample.php on line 23

Can someone of you help me to solve this problem??

2

Answers


  1. instead of the below code:

    $_SESSION['query'] = $_POST['query'];
    

    use the following code:

    $_SESSION['query'] = isset($_POST['query']) ? $_POST['query'] : (isset($_SESSION['query']) ?$_SESSION['query'] : '' );
    
    Login or Signup to reply.
  2. $_POST['query']; is not set, you are submitting the form without filling query
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search