Current location: Hot Scripts Forums » Programming Languages » PHP » Select one row of an array where item number is ..?


Select one row of an array where item number is ..?

Reply
  #1 (permalink)  
Old 12-30-06, 07:14 AM
Oskare100 Oskare100 is offline
Newbie Coder
 
Join Date: Apr 2005
Posts: 86
Thanks: 0
Thanked 0 Times in 0 Posts
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
            
}
Best Regards
Oskar R

Last edited by Oskare100; 12-30-06 at 07:18 AM.
Reply With Quote
  #2 (permalink)  
Old 12-30-06, 01:57 PM
grafman grafman is offline
Coding Addict
 
Join Date: Dec 2006
Posts: 278
Thanks: 0
Thanked 0 Times in 0 Posts
Some answers to your post

$itemNodes is an array of objects. The objects are instances of the php DomDocument class that is a subset of the DOM XML tools.

http://us3.php.net/DomDocument_get_elements_by_tagname

http://us3.php.net/manual/en/ref.domxml.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.

Last edited by grafman; 12-30-06 at 02:00 PM.
Reply With Quote
  #3 (permalink)  
Old 12-30-06, 02:27 PM
Oskare100 Oskare100 is offline
Newbie Coder
 
Join Date: Apr 2005
Posts: 86
Thanks: 0
Thanked 0 Times in 0 Posts
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...

Thanks,
/Oskar

Last edited by Oskare100; 12-30-06 at 02:31 PM.
Reply With Quote
  #4 (permalink)  
Old 12-30-06, 02:46 PM
grafman grafman is offline
Coding Addict
 
Join Date: Dec 2006
Posts: 278
Thanks: 0
Thanked 0 Times in 0 Posts
Oskar,

You would just follow what you've been doing in the script that you already have.

Use the function that I gave you to call the object that has the id you are looking for '123123'.

Then use the code you already have

$myItem = getItem($objectArray,'123123');

$price = $myItem->get_elements_by_tagname('CurrentPrice');

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.

Last edited by grafman; 12-30-06 at 02:50 PM.
Reply With Quote
  #5 (permalink)  
Old 12-30-06, 04:50 PM
Oskare100 Oskare100 is offline
Newbie Coder
 
Join Date: Apr 2005
Posts: 86
Thanks: 0
Thanked 0 Times in 0 Posts
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);
    
    
///Build the request Xml string
    
$requestXmlBody '<?xml version="1.0" encoding="utf-8"?>';
    
$requestXmlBody .= '<GetItemTransactionsRequest xmlns="urn:ebay:apis:eBLBaseComponents">';
    
$requestXmlBody .= "<RequesterCredentials><eBayAuthToken>$userToken</eBayAuthToken></RequesterCredentials>";
    
$requestXmlBody .= '<ItemID>110013560883</ItemID>';
    
$requestXmlBody .= "<ModTimeFrom>2006-12-01 00:00:00</ModTimeFrom>";
    
$requestXmlBody .= "<ModTimeTo>2006-12-31 00:00:00</ModTimeTo>";
    
$requestXmlBody .= '</GetItemTransactionsRequest>';
    
    
$responseXml sendHttpRequest($requestXmlBody$serverUrl$headers);
    if(
stristr($responseXml'HTTP 404') || $responseXml == '')
        die(
'<P>Error sending request');
    
    
//Xml string is parsed and creates a DOM Document object
    
$responseDoc domxml_open_mem($responseXml);
    
    
    
    
//get any error nodes
    
$errors $responseDoc->get_elements_by_tagname('Errors');
    
    
//if there are error nodes
    
if(count($errors) > 0)
    {
        echo 
'<P><B>eBay returned the following error(s):</B>';
        
//display each error
        //Get error code, ShortMesaage and LongMessage
        
$code $errors[0]->get_elements_by_tagname('ErrorCode');
        
$shortMsg $errors[0]->get_elements_by_tagname('ShortMessage');
        
$longMsg $errors[0]->get_elements_by_tagname('LongMessage');
        
//Display code and shortmessage
        
echo '<P>'$code[0]->get_content(), ' : 'str_replace(">""&gt;"str_replace("<""&lt;"$shortMsg[0]->get_content()));
        
//if there is a long message (ie ErrorLevel=1), display it
        
if(count($longMsg) > 0)
            echo 
'<BR>'str_replace(">""&gt;"str_replace("<""&lt;"$longMsg[0]->get_content()));

    }
    else 
//no errors
    
{
    echo 
"no errors";
 function 
findId($objectArray,$id)
{
  foreach(
$objectArray as $searchItem)
  {
    if(
$id == $searchItem->get_elements_by_tagname('UserID'))
      return 
$searchItem;
    else
      return 
0;
  }
}

//Then call it with:

if(($myItem findId($itemNodes,'theventbuy') == 0)
  die(
"Record Id not found");
else
  
$transaction_id $myItem->get_elements_by_tagname('TransactionID');  
  echo 
"Transaction ID: $transaction_id"
    
}
?>
Reply With Quote
  #6 (permalink)  
Old 12-30-06, 05:35 PM
grafman grafman is offline
Coding Addict
 
Join Date: Dec 2006
Posts: 278
Thanks: 0
Thanked 0 Times in 0 Posts
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.

Anyway your syntax won't work. Things like this

[php]

<?php require_once('variables.php') ?>
<?php require_once('functions.php') ?>
<?php

//uneccessary syntax

<?php
require_once('variables.php');
require_once('functions.php');

//The rest of you code
// Then

?>

[\php]

The function is set in the wrong place.

for long strings try this:

PHP Code:



$x 
= <<<END
asdf
adsf
adsf
asdf
END;

echo 
$x
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.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to select a transaction of an array and check it on regular basis Oskare100 PHP 0 12-28-06 05:27 PM
Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' Dr. Forensics PHP 3 07-15-06 03:54 PM
Search item in array lcwei81 PHP 4 03-24-06 07:59 AM
linking to iframe not working :( j0d JavaScript 5 01-19-04 08:14 PM
Need Epinions-lite system in PHP & MYSQL wali001 Job Offers & Assistance 4 01-12-04 06:02 AM


All times are GMT -5. The time now is 05:51 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.