Select one row of an array where item number is ..?
Hello,
When I call the Ebays XML API I get an array or something similar back (see the code sample), is it possible to "select item_id where item_id = 123123" or something similar as in MySQL? If, How can I do that? Also, I need to attach a PHP variables to the row I selected as for example $starttime, $endtime and so on.
Here is the code sample (the end of the scritpt);
PHP Code:
<?php
//stores the alternationg background colour of the rows
$bgColor = "#FFFFFF";
//go through each result
foreach($itemNodes as $item)
{
//get the required nodes from the results
$itemID = $item->get_elements_by_tagname('ItemID');
$title = $item->get_elements_by_tagname('Title');
$price = $item->get_elements_by_tagname('CurrentPrice');
$startTime = $item->get_elements_by_tagname('StartTime');
$endTime = $item->get_elements_by_tagname('EndTime');
$link = $item->get_elements_by_tagname('ViewItemURL');
//display the result in a table row
?>
<TR bgcolor="<?php echo $bgColor ?>">
<TD><?php echo $itemID[0]->get_content(); ?></TD>
<!-- Display the Title as a link to the item on eBay -->
<TD><A href="<?php echo $link[0]->get_content(); ?>" target="_blank">
<?php echo $title[2]->get_content(); ?>
</A>
</TD>
<TD><?php echo $price[0]->get_content(); ?></TD>
<TD><?php echo $startTime[0]->get_content(); ?> GMT</TD>
<TD><?php echo $endTime[0]->get_content(); ?> GMT</TD>
</TR>
<?php
//alternate the background colours
$bgColor = $bgColor == "#FFFFFF" ? "#EEEEEE" : "#FFFFFF";
} ?>
</TABLE>
<?php
}
To do the select you want you can write a function that walks through the itemNodes array and calls $item->get_elements_by_tagname('ItemID'); for each one and compares it to the value you are looking for.
PHP Code:
function findId($objectArray,$id)
{
foreach($objectArray as $searchItem)
{
if($id == $searchItem->get_elements_by_tagname('ItemID'))
return $searchItem;
else
return 0;
}
}
//Then call it with:
if(($myItem = findId($itemNodes,'123123') == 0)
die("Record Id not found");
else
//<do something with $myItem>
Because I'm not sure what you are doing with this I can't answer for sure, however, I don't think you need to "attach" your start and end time variables to anything unless you are using them for something later in the code. If you do need to put the times into some form that will carry on with each "$item" object look at the php API. There are many ways to do this depending on what you are trying to accomplish.
A couple of suggestions.
1. Rather than switch in and out of php and html like you have done just use php's syntax for printing "here documents"
print <<<ENDOFTEXTTOPRINT
...
...
a bunch of text
...
...
ENDOFTEXTTOPRINT;
much cleaner and easier to put out to a templating engine as well.
2. Look into a template engine like smarty http://smarty.php.net or PEAR's templates. There are literally thousands of them out there and they make doing this sort of thing very easy once you learn how the templates work.
Hello,
OK, I'll take a look at those things. Really thanks, great. Just one more question. If I want to select/add a variable for example the "CurrentPrice" where ID = 123123, how can I do that with the code in your post? For example assign CurrentPrice where ID = 123123 to $currentprice.
(The script I'm writing will have to find the "row" where item = item and then select the transaction ID of that "row")
Again, thanks for your reply. Just need to know that to be able to use it...
Then you can do something like apply a markup percentage
$price = $price + ($price * .25);
or just set the price to something else and not call the object method at all
$price = 39.95;
If you want to change the value of CurrentPrice in the object itself then you'll need to look at the reference pages that I showed you and find the method (function) that does that for you. Something like set_element_by_tagname() I would guess.
Hello,
I've now tried several times but I don't get anything back, just an empty page..
here is the code I've tried with;
PHP Code:
<?php require_once('variables.php') ?>
<?php require_once('functions.php') ?>
<?php
echo "no errors";
//SiteID must also be set in the Request's XML
//SiteID = 0 (US) - UK = 3, Canada = 2, Australia = 15, ....
//SiteID Indicates the eBay site to associate the call with
$siteID = 0;
//the call being made:
$verb = 'GeteBayOfficialTime';
//Regulates versioning of the XML interface for the API
$compatabilityLevel = 433;
//get an array of strings containing the required headers
$headers = buildEbayHeaders($devID, $appID, $certID, $compatabilityLevel, $siteID, $verb);
I'd recommend a php tutorial. It doesn't appear to be your first programming language, however, its clear that you aren't familiar with it enough to make it work for you. You program in it like you do ColdFusion work.
If you are interested I can most likely fix this for you, however, I don't do work like that for free. There are some other really good folks that come here that can do it for you too. Submit a post to the request forum.