tags:

views:

56

answers:

5

Hi guys,

I'm working towards sending an XML request to a URL using cURL in PHP.

First I just wanted to make sure my request is sending the right data so here is a snippet from my code. I will add the curl statements in later once I know i'm sending the right data.

here is my code so far:

$format = 'Y-m-j G:i:s'; 

$date = date ( $format );
$d = date ( $format, strtotime ( '-90 days' ) );

$sql = mysql_query("SELECT * FROM recurringPayments WHERE lastpmt <= '$d'");

$num_rows = mysql_num_rows($sql); 
echo $num_rows . " results found";

echo "<table style=\"border:1px solid green;\">
            <tr bgcolor=\"#bdd73b\">
            <th>ID</th>
            <th>Company Name</th>
            <th>Annual Subscription</th>
            <th>Package</th>
            <th>Cost</th>
            <th>Payer Ref</th>
            <th>Payment Ref</th>
            <th>Last Payment Date</th>
            </tr>";

            while ($row = mysql_fetch_array($sql))
            {
                echo "<tr>";
                echo "<td>" . $row['ID'] . "</td>";
                echo "<td>" . $row['compName'] . "</td>";
                echo "<td>" . $row['annualSub'] . "</td>";
                echo "<td>" . $row['package'] . "</td>";
                echo "<td>" . $row['cost'] . "</td>";               
                echo "<td>" . $row['payerref'] . "</td>";
                echo "<td>" . $row['pmtref'] . "</td>";
                echo "<td>" . $row['lastpmt'] . "</td>";
            }
            echo "</table>";        
while ($row = mysql_fetch_array($sql))
{
$xml_data ='<request type="receipt-in" timestamp="20030520151742">'.
       '<merchantid>test</merchantid>'.
           '<account>internet</account>'.
       '<orderid>transaction01</orderid>'.
       '<amount currency="EUR">'.$row['cost'].'</amount>'.
           '<payerref>'.$row['payerref'].'</payerref>'.
           '<paymentmethod>'.$row['pmtref'].'</paymentmethod>'.
           '<autosettle flag="1" />'.
           '<md5hash />'.
       '<sha1hash>c81377ac77b6c0a8ca4152e00cc173d01c3d98eb</sha1hash'.  
       '</request>';
}

 echo $xml_data;

When I try to echo $xml_data, I get the following error message:

Notice: Undefined variable: xml_data in C:\wamp\www\Internal\paymentDue.php on line 63

It was my logic that I would be able to output the XML as I output the table, however I could be (probably am) wrong. Any guidance is appreciated.

Thanks.

PS:

I also just realise while I'm posting this, that if I use the while loop in the current context, $xml_data will be overwritten each time it loops. Any help with this would be great too.

+3  A: 

You're looping through your resultset from first to last to build the html table, then trying to loop through it again for your xml, even though you've already passed the end of the set. Build both in the same loop.

Set xml_data to '' before the loop, then use xml_data .= to build it up (or even use SimpleXML or XMLWriter rather than building it as a string)

Mark Baker
Hi Mark, thanks for your reply. This seemed to do the trick. I have one quick question about it. I can't see the XML tags, should I? I just wonder for later on when I am implementing a curl request into it. Will this matter? thanks :)
TaraWalsh
The XML tags won't be visible in a web browser... use the browser's "view source" option to see them
Mark Baker
that's great Mark. thanks :)
TaraWalsh
+1  A: 

your second loop loops through a mysql_query that was already looped so the loop pointer is at the end meaning this code never gets run. Since it hasn't been run $xml_data is unset and when you try to echo it at the end you get a notice.

while ($row = mysql_fetch_array($sql))
{
$xml_data ='<request type="receipt-in" timestamp="20030520151742">'.
       '<merchantid>test</merchantid>'.
           '<account>internet</account>'.
       '<orderid>transaction01</orderid>'.
       '<amount currency="EUR">'.$row['cost'].'</amount>'.
           '<payerref>'.$row['payerref'].'</payerref>'.
           '<paymentmethod>'.$row['pmtref'].'</paymentmethod>'.
           '<autosettle flag="1" />'.
           '<md5hash />'.
       '<sha1hash>c81377ac77b6c0a8ca4152e00cc173d01c3d98eb</sha1hash'.  
       '</request>';
}

Try adding

mysql_data_seek($sql, 0);

before your second while loop

Galen
A: 

You're moving the result set point forward when you're iterating for the initial print. When you want to output the XML, the pointer as it the end of the result set, thus you either need to reset it or build the XML while you're printing.

For the latter, try this:

$xmlData = '';
while ($row = mysql_fetch_array($sql))
{
    echo "<tr>";
    echo "<td>" . $row['ID'] . "</td>";
    echo "<td>" . $row['compName'] . "</td>";
    echo "<td>" . $row['annualSub'] . "</td>";
    echo "<td>" . $row['package'] . "</td>";
    echo "<td>" . $row['cost'] . "</td>";               
    echo "<td>" . $row['payerref'] . "</td>";
    echo "<td>" . $row['pmtref'] . "</td>";
    echo "<td>" . $row['lastpmt'] . "</td>";

    $xmlData .= '<request type="receipt-in" timestamp="20030520151742">'.
   '<merchantid>test</merchantid>'.
       '<account>internet</account>'.
   '<orderid>transaction01</orderid>'.
   '<amount currency="EUR">'.$row['cost'].'</amount>'.
       '<payerref>'.$row['payerref'].'</payerref>'.
       '<paymentmethod>'.$row['pmtref'].'</paymentmethod>'.
       '<autosettle flag="1" />'.
       '<md5hash />'.
   '<sha1hash>c81377ac77b6c0a8ca4152e00cc173d01c3d98eb</sha1hash'.  
   '</request>';
}

echo "</table>";
Denis 'Alpheus' Čahuk
+2  A: 

Try this:

   <?php

    $format = 'Y-m-j G:i:s'; 

    $date = date ( $format );
    $d = date ( $format, strtotime ( '-90 days' ) );

    $sql = mysql_query("SELECT * FROM recurringPayments WHERE lastpmt <= '$d'");

    $num_rows = mysql_num_rows($sql); 
    echo $num_rows . " results found";

    $xml_data = "";

    echo "<table style=\"border:1px solid green;\">
                <tr bgcolor=\"#bdd73b\">
                <th>ID</th>
                <th>Company Name</th>
                <th>Annual Subscription</th>
                <th>Package</th>
                <th>Cost</th>
                <th>Payer Ref</th>
                <th>Payment Ref</th>
                <th>Last Payment Date</th>
                </tr>";

                while ($row = mysql_fetch_array($sql))
                {
                    echo "<tr>";
                    echo "<td>" . $row['ID'] . "</td>";
                    echo "<td>" . $row['compName'] . "</td>";
                    echo "<td>" . $row['annualSub'] . "</td>";
                    echo "<td>" . $row['package'] . "</td>";
                    echo "<td>" . $row['cost'] . "</td>";               
                    echo "<td>" . $row['payerref'] . "</td>";
                    echo "<td>" . $row['pmtref'] . "</td>";
                    echo "<td>" . $row['lastpmt'] . "</td>";

                    $xml_data .='<request type="receipt-in" timestamp="20030520151742">'.
                           '<merchantid>test</merchantid>'.
                               '<account>internet</account>'.
                           '<orderid>transaction01</orderid>'.
                           '<amount currency="EUR">'.$row['cost'].'</amount>'.
                               '<payerref>'.$row['payerref'].'</payerref>'.
                               '<paymentmethod>'.$row['pmtref'].'</paymentmethod>'.
                               '<autosettle flag="1" />'.
                               '<md5hash />'.
                           '<sha1hash>c81377ac77b6c0a8ca4152e00cc173d01c3d98eb</sha1hash'.  
                           '</request>';


                }
                echo "</table>";        


     echo $xml_data;

There's no point in looping twice, and looping is causing the cursor to move to the end. This should build up your xml_data variable during the initial loop. Also notice that I've declared $xml_data as an empty string outside of the loop, and each time we loop I'm just appending to the string using .=

Mikee
A: 
$format = 'Y-m-j G:i:s'; 

$date = date ( $format );
$d = date ( $format, strtotime ( '-90 days' ) );

$sql = mysql_query("SELECT * FROM recurringPayments WHERE lastpmt <= '$d'");

$num_rows = mysql_num_rows($sql); 
echo $num_rows . " results found";

$xml_data = ''; // init empty

echo "<table style=\"border:1px solid green;\">
            <tr bgcolor=\"#bdd73b\">
            <th>ID</th>
            <th>Company Name</th>
            <th>Annual Subscription</th>
            <th>Package</th>
            <th>Cost</th>
            <th>Payer Ref</th>
            <th>Payment Ref</th>
            <th>Last Payment Date</th>
            </tr>";

while ($row = mysql_fetch_array($sql))
{
   echo "<tr>";
   echo "<td>" . $row['ID'] . "</td>";
   echo "<td>" . $row['compName'] . "</td>";
   echo "<td>" . $row['annualSub'] . "</td>";
   echo "<td>" . $row['package'] . "</td>";
   echo "<td>" . $row['cost'] . "</td>";               
   echo "<td>" . $row['payerref'] . "</td>";
   echo "<td>" . $row['pmtref'] . "</td>";
   echo "<td>" . $row['lastpmt'] . "</td>";

   $xml_data .='<request type="receipt-in" timestamp="20030520151742">'.
                       '<merchantid>test</merchantid>'.
                           '<account>internet</account>'.
                       '<orderid>transaction01</orderid>'.
                       '<amount currency="EUR">'.$row['cost'].'</amount>'.
                           '<payerref>'.$row['payerref'].'</payerref>'.
                           '<paymentmethod>'.$row['pmtref'].'</paymentmethod>'.
                           '<autosettle flag="1" />'.
                           '<md5hash />'.
                       '<sha1hash>c81377ac77b6c0a8ca4152e00cc173d01c3d98eb</sha1hash'.  
                       '</request>';

}
echo "</table>";        

echo $xml_data;
Yanick Rochon