Looping Data

Hi Guys,

I’m getting a bit muddled with a loop here lol, what i’m trying to do is loop out the last 10 poular products from mysql and query amazon:


<?php
      // Start the table
      print "<table width=\\"90%\\" align=\\"center\\" border=\\"0\\" cellpadding=\\"2\\" cellspacing=\\"2\\" />";
      
      // Credentials
      $PublicKey   = "";
      $PrivateKey  = "";
      
      // Get the stored keywords for this category
      $qI = mysql_query("SELECT * FROM `fcs_products_viewed` LIMIT 10");
      
      while ($aI = mysql_fetch_array($qI))
      {

      $pID = $aI['product_unique_id'];

      // Prarameters we need to send to the function
      $parameters = array(
      'Operation'       => 'ItemLookup',
    //'SellerId'        => '***',
    //'Keywords'        => 'potter',
    //'SearchIndex'     => 'DVD',  //Books for example.
    //"$searchparameter"=> $searchparameterdata    ,
    //'ItemPage'        => 1,  //which page?
    //'Service'         => 'AWSECommerceService'   ,  //this makes it version 4
    //'SubscriptionId'  => $subscriptionid         ,  //is this correct?
    //'AWSAccessKeyId'  => $awsaccesskeyid         ,  //is this correct?
      'ItemId'          => $pID,
    //'ResponseGroup'   => 'Medium'                ,  //Small, Medium or Large
      'ResponseGroup'   => 'Medium'  //Small, Medium, Large or SellerListing
      );
      
      } // End while loop

      // Execute the function to return our URL
      $url = amazon_signed_request("co.uk", $parameters, $PublicKey, $PrivateKey);
      //print $url;
      // Load the results and get the contents    
      $response = simplexml_load_file($url);
  
      // Pagination numbers
      $totalPages = $response->Items->TotalPages;
      //print $totalPages;
      // Loop through the xml nodes
      foreach ($response->Items->Item as $item) 
      {     
      
        // We need a function to check what ID number we need
        //$identificationNumber = amazon_id_number($catName);
        // All products so far seem to have an ASIN number which will be the identifier
      
        $title       = $item->ItemAttributes->Title;
        $price       = $item->OfferSummary->LowestNewPrice->FormattedPrice;
        $asin        = $item->ASIN;
        $img_src     = $item->SmallImage->URL;
        
        // Cut the title string  if it's mega long
        if (strlen($title) > 100)
        {
          $title = substr($title, 0, 99);
        }
        
        // Clean symbols from the titles
        $titlesCleaned = amazon_strip_symbols($title);

        // Replace that stupid symbol
        $newprice = str_replace("Â", " ", $price);
          
        //print $itemID ;                   
        $amazonDisplay = "<form action=\\"cart.php?add=Add\\" method=\\"POST\\" name=\\"fcsProductBox\\" />
                    <input type=\\"hidden\\" name=\\"fcsCartTriggered\\" value=\\"1\\" /> 
                    <input type=\\"hidden\\" name=\\"fcsCartProductId\\" value=\\"$asin\\" />
                    <input type=\\"hidden\\" name=\\"fcsCartProductQT\\" value=\\"1\\" />
                    <input type=\\"hidden\\" name=\\"fcsCartVendor\\" value=\\"amazon\\" />
                    <table class=\\"prod\\">
                     <tr>
                      <td valign=\\"top\\" class=\\"pic\\"><img src=\\"$img_src\\" border=\\"0\\" alt=\\"$titlesCleaned\\"></td>
                       <td class=\\"desc1\\">
                       <div>
                        <h3>".amazon_friendly_url($titlesCleaned, $asin)."</h3>
                        <p><b>".amazon_friendly_url($titlesCleaned, $asin)."</b></p>
                       <p>" . word_wrap_amazon_product_description($asin) . "</p>
                    </div>
                   </td>
                   </tr>
                    <tr>
                     <td class=\\"price\\">$newprice</td>
                     <td class=\\"add-inf\\"><input type=\\"image\\" img src=\\"img/img-add-to-cart.png\\" title=\\"Add this item to your shopping cart\\"></td>
                      </tr>
                         </table>
                            </form>";   
           
    if ($x == 0){    
      print("<tr>\
<td valign=\\"center\\" align=\\"center\\">$amazonDisplay</td>\
");    
    } elseif ($x == 2) {       
      print("<td valign=\\"center\\" align=\\"center\\">$amazonDisplay</td></tr>\
");         
    } else {     
      print("<td valign=\\"center\\" align=\\"center\\">$amazonDisplay</td>\
");    
    } 
 
     // Use some basic math to determine how many to show per line
     $x++; $x = $x % 2; 

    }

      print "</tr></table>\
";
  
 //}
?>

i have a while() loop to get the last 10 products, but i need to use a foreach() at the same time to get the detaisl from amazon the way i have it now only loops 1 item.

any help would be appreciated

thanks guys

Graham

Got it moved the end of the while loop after the </form>.

cheers mate

Graham

Hi Mate,

Oh yeah the deliberate mistake hehe :slight_smile:

i have now done:


&lt;?php
      // Start the table
      print "&lt;table width=\\"90&#37;\\" align=\\"center\\" border=\\"0\\" cellpadding=\\"2\\" cellspacing=\\"2\\" /&gt;";
      
      // Credentials
      $PublicKey   = "";
      $PrivateKey  = "";
      
      // Get the stored keywords for this category
      $qI = mysql_query("SELECT * FROM `fcs_products_viewed` ORDER BY `product_views` DESC LIMIT 10");
      
      while ($aI = mysql_fetch_array($qI))
      {

      $pID = $aI['product_unique_id'];

      // Prarameters we need to send to the function
      $parameters = array(
      'Operation'       =&gt; 'ItemLookup',
    //'SellerId'        =&gt; '***',
    //'Keywords'        =&gt; 'potter',
    //'SearchIndex'     =&gt; 'DVD',  //Books for example.
    //"$searchparameter"=&gt; $searchparameterdata    ,
    //'ItemPage'        =&gt; 1,  //which page?
    //'Service'         =&gt; 'AWSECommerceService'   ,  //this makes it version 4
    //'SubscriptionId'  =&gt; $subscriptionid         ,  //is this correct?
    //'AWSAccessKeyId'  =&gt; $awsaccesskeyid         ,  //is this correct?
      'ItemId'          =&gt; $pID,
    //'ResponseGroup'   =&gt; 'Medium'                ,  //Small, Medium or Large
      'ResponseGroup'   =&gt; 'Medium'  //Small, Medium, Large or SellerListing
      );

      // Execute the function to return our URL
      $url = amazon_signed_request("co.uk", $parameters, $PublicKey, $PrivateKey);
      //print $url;
      // Load the results and get the contents    
      $response = simplexml_load_file($url);
  
      // Pagination numbers
      $totalPages = $response-&gt;Items-&gt;TotalPages;
      //print $totalPages;
      // Loop through the xml nodes
      foreach ($response-&gt;Items-&gt;Item as $item) 
      {     
      
        // We need a function to check what ID number we need
        //$identificationNumber = amazon_id_number($catName);
        // All products so far seem to have an ASIN number which will be the identifier
      
        $title       = $item-&gt;ItemAttributes-&gt;Title;
        $price       = $item-&gt;OfferSummary-&gt;LowestNewPrice-&gt;FormattedPrice;
        $asin        = $item-&gt;ASIN;
        $img_src     = $item-&gt;SmallImage-&gt;URL;
        
        // Cut the title string  if it's mega long
        if (strlen($title) &gt; 100)
        {
          $title = substr($title, 0, 99);
        }
        
        // Clean symbols from the titles
        $titlesCleaned = amazon_strip_symbols($title);

        // Replace that stupid symbol
        $newprice = str_replace("&#194;", " ", $price);
          
        //print $itemID ;                   
        $amazonDisplay = "&lt;form action=\\"cart.php?add=Add\\" method=\\"POST\\" name=\\"fcsProductBox\\" /&gt;
                    &lt;input type=\\"hidden\\" name=\\"fcsCartTriggered\\" value=\\"1\\" /&gt; 
                    &lt;input type=\\"hidden\\" name=\\"fcsCartProductId\\" value=\\"$asin\\" /&gt;
                    &lt;input type=\\"hidden\\" name=\\"fcsCartProductQT\\" value=\\"1\\" /&gt;
                    &lt;input type=\\"hidden\\" name=\\"fcsCartVendor\\" value=\\"amazon\\" /&gt;
                    &lt;table class=\\"prod\\"&gt;
                     &lt;tr&gt;
                      &lt;td valign=\\"top\\" class=\\"pic\\"&gt;&lt;img src=\\"$img_src\\" border=\\"0\\" alt=\\"$titlesCleaned\\"&gt;&lt;/td&gt;
                       &lt;td class=\\"desc1\\"&gt;
                       &lt;div&gt;
                        &lt;h3&gt;".amazon_friendly_url($titlesCleaned, $asin)."&lt;/h3&gt;
                        &lt;p&gt;&lt;b&gt;".amazon_friendly_url($titlesCleaned, $asin)."&lt;/b&gt;&lt;/p&gt;
                       &lt;p&gt;" . word_wrap_amazon_product_description($asin) . "&lt;/p&gt;
                    &lt;/div&gt;
                   &lt;/td&gt;
                   &lt;/tr&gt;
                    &lt;tr&gt;
                     &lt;td class=\\"price\\"&gt;$newprice&lt;/td&gt;
                     &lt;td class=\\"add-inf\\"&gt;&lt;input type=\\"image\\" img src=\\"img/img-add-to-cart.png\\" title=\\"Add this item to your shopping cart\\"&gt;&lt;/td&gt;
                      &lt;/tr&gt;
                         &lt;/table&gt;
                            &lt;/form&gt;";   
           
    if ($x == 0){    
      print("&lt;tr&gt;\
&lt;td valign=\\"center\\" align=\\"center\\"&gt;$amazonDisplay&lt;/td&gt;\
");    
    } elseif ($x == 2) {       
      print("&lt;td valign=\\"center\\" align=\\"center\\"&gt;$amazonDisplay&lt;/td&gt;&lt;/tr&gt;\
");         
    } else {     
      print("&lt;td valign=\\"center\\" align=\\"center\\"&gt;$amazonDisplay&lt;/td&gt;\
");    
    } 
 
     // Use some basic math to determine how many to show per line
     $x++; $x = $x % 2; 

    }

      print "&lt;/tr&gt;&lt;/table&gt;\
";
  
} // End while loop
?&gt;

which works but it throws the table out of whack.

cheers

Graham

Well #1 unless your table is set to some very strange key, you need an ORDER BY clause in your table query.

Move your “End While” close brace to the bottom of the file and roll it.