Jump to content

easy fix but I don't know how.. help please lol :P


Mike088

Recommended Posts

Basically I am making a search query using the ebay API... uhh I don't think you will need to have any knowledge of the API to help here as all I need to do is fix something up regarding the variables I have set.

 

So when I first load the page I am getting an error about an undefined variable ($query). What I have worked out is basically that is because the variable = 'the word submitted into the form' and obviously when it first loads there is no word submitted... how do I go about making it not try and load that variable until the search is executed through the form input? Here is my code:

 

<?php

if(isset($_POST['submit'])) {

$query =  trim($_POST["word"]); }

error_reporting(E_ALL);  // turn on all errors, warnings and notices for easier debugging

$SafeQuery = urlencode($query);

$endpoint = 'http://open.api.ebay.com/shopping';  // URL to call
$responseEncoding = 'XML';   // Format of the response

// Construct the FindItems call
$apicall = "MY APP ID AND SUCH YOU DONT NEED TO SEE";
// Load the call and capture the document returned by the Shopping API
$resp = simplexml_load_file($apicall);

// Check to see if the response was loaded, else print an error
if ($resp) {
        $results = '';

    // If the response was loaded, parse it and build links
    foreach($resp->Item as $item) {
        $link  = $item->ViewItemURLForNaturalSearch;
        $title = $item->Title;

                // For each result node, build a link and append it to $results
                $results .= "<a href=\"$link\">$title</a><br/>";
        }
}
// If there was no response, print an error
else {
        $results = "Oops! Must not have gotten the response!";
}

?>

<html>
<head>
<title>eBay Search Results For <?php print $query ?></title>
</head>

<body>

<form action="Working.php" method="post">
Enter Your Word: <input type="text" name="word" />
<input type="submit" value="Submit" name="submit"/>
</form>

<h1>eBay Search Results</h1>

<?php echo $results;?>


</body>
</html>

 

I am pretty much a noob lol so I give you permission to laugh at me with my attempt to do this kind of stuff :D

 

Any help much appreciated.

Link to comment
Share on other sites

You probably aren't getting an error, but rather a notice.  These are harmless, however they usually point out small "good practices" in your code that need to be fixed.  The one you are encountering is when you use a variable that hasn't been defined before.

 

You can turn off notices by doing this:

 

error_reporting(E_ALL ^ E_NOTICE);

 

or you can define a default value for query...at the top of your page, put something like:

 

<?php

$query = "Unknown";

// ... rest of code

 

or you can have a check where you print the value of $query...

 

<?php print (empty($query) ? "unknown" : $query); ?>

Link to comment
Share on other sites

<?php

if(isset($_POST['submit'])) {

$query =  trim($_POST["word"]); 

error_reporting(E_ALL);  // turn on all errors, warnings and notices for easier debugging

$SafeQuery = urlencode($query);

$endpoint = 'http://open.api.ebay.com/shopping';  // URL to call
$responseEncoding = 'XML';   // Format of the response

// Construct the FindItems call
$apicall = "MY APP ID AND SUCH YOU DONT NEED TO SEE";
// Load the call and capture the document returned by the Shopping API
$resp = simplexml_load_file($apicall);

}//end of if(isset($_POST['submit'];

?>

 

Moving that bracket lower down should fix the problem as it will not try to process the code with $query in until the search has been submitted..

Link to comment
Share on other sites

Thanks, new it would be something simple like that (probably just need some more coffee). To be honest I don't know why I didn't think about the fundamental fact that php executes from top to bottom lol  ::)

 

EDIT: Uhh this is SOLVED but somethings up with the 'Topic Solved' button giving me a database error.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.