SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: loading data

  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2002
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    loading data

    I am extracting data from mysql. I am looping thru multiple product categories and retrieving many products along with prices. I am print this data out to a table in html. My problem is that the page is taking 20-40 seconds to build. My question is how do I create a simple 'preloader' to alert user that data is loading. I would like to have this preloader displayed until entire page is loaded. Any ideas?

    Here is link to page: http://www.noeink.com/cg/test/index.php

    Below is the code I am using for the looping and printing. Maybe I am not doing it as effciently as possible.

    Many thanks!

    PHP Code:
     //get all product categories 
    $getCat = @mysql_query("SELECT * FROM tblPSCats ORDER BY TitleSort ");
    if (!
    $getCat) {
    echo 
    "<p>Error performing query: " mysql_error() . "</p>";
    exit();

    $numCats mysql_num_rows($getCat);
     
     
    // loop thru all product categories 
    // <$numCats-1> is used because there is an extra category that isn't used at the end of list
    for ($a=0$a<($numCats-1); $a++) {     
         
    $TitleDesciption =mysql_result($getCat,$a,"TitleDesciption"); 
         
    $TitleIndex =mysql_result($getCat,$a,"TitleIndex"); 
     
     
         
    $getProduct = @mysql_query("SELECT * FROM tblPSLines WHERE TitleIndex = '$TitleIndex' ORDER BY TitleLineSort");
         if (!
    $getProduct) {
         echo 
    "<p>1Error performing query: " mysql_error() . "</p>";
         exit();
         } 
         
    //number of products in category
         
    $numProducts mysql_num_rows($getProduct);
     
         
    // determine if any products are listed under current category, if not, skip to next category
         
    if ($numProducts 0) {
         echo 
    "<tr><td height='50' colspan='6' align='center'><h2>" $TitleDesciption "</h2></td></tr>"
             echo 
    "<tr><td width='50%' valign='top'><table width='350' border='0' cellspacing='0' cellpadding='3'>";
     
         
    // variables for keeping track of rows printed
         
    $numRows round(($numProducts/2), 0);
         
    $numRows2 round($numProducts0);
         
    $rowCount 0
     
         for (
    $b=0$b<$numProducts$b++) { 
     
         
    $ItemNumber =mysql_result($getProduct,$b,"ItemNumber"); 
         
    $Description =mysql_result($getProduct,$b,"Description");
     
         
    $getPrice = @mysql_query("SELECT * FROM IMB_PriceCode WHERE ItemNumber = '$ItemNumber' AND CustomerNumber = '$cn'");
         if (!
    $getPrice) {
            echo 
    "<p>2Error performing query: " mysql_error() . "</p>";
            exit();
         } 
         
    $numPrice mysql_num_rows($getPrice);
     
         if (
    $numPrice == 0) {
            
    $getPrice = @mysql_query("SELECT * FROM IM1_InventoryMasterfile WHERE ItemNumber = '$ItemNumber'");
            if (!
    $getPrice) {
             echo 
    "<p>3Error performing query: " mysql_error() . "</p>";
             exit();
            } 
     
            
    $price mysql_result($getPrice,0,"SuggestedRetailPrice"); 
         } else {
     
            
    $price mysql_result($getPrice,0,"DiscountMarkupPriceRate1"); 
         } 
     
         
    $getPricePer = @mysql_query("SELECT StdPrice,SalesUMFactor,SalesUM,PurchaseUM FROM IM1_InventoryMasterfile WHERE ItemNumber = '$ItemNumber'");
         if (!
    $getPricePer) {
            echo 
    "<p>4Error performing query: " mysql_error() . "</p>";
            exit();
         } 
         
    $retail mysql_result($getPricePer,0,"StdPrice");
         
    $factor mysql_result($getPricePer,0,"SalesUMFactor");
         
    $pricePerUnit mysql_result($getPricePer,0,"SalesUM");
         
    $saleUnit mysql_result($getPricePer,0,"PurchaseUM");
     
         
    // determine individual units of sale
         
    switch ($saleUnit) {
         case 
    "C":
             
    $saleUnit "/case";
             break;
         case 
    "EACH":
             
    $saleUnit "/each";
             break;        
         case 
    "EA":
             
    $saleUnit "/each";
             break;
         case 
    "E":
             
    $saleUnit "/each";
             break;
         case 
    "LB":
             
    $saleUnit "/lb";
             break;
         case 
    "L":
             
    $saleUnit "/lb";
             break;
         }
     
         switch (
    $pricePerUnit) {
         case 
    "C":
             
    $pricePerUnit "/case";
             break;
         case 
    "EACH":
             
    $pricePerUnit "/each";
             break;        
         case 
    "EA":
             
    $pricePerUnit "/each";
             break;
         case 
    "E":
             
    $pricePerUnit "/each";
             break;
         case 
    "LB":
             
    $pricePerUnit "/lb";
             break;
         case 
    "L":
             
    $pricePerUnit "/lb";
             break;
         }
     
     
         
    // determine price per item
         
    $pricePer $retail $factor;
     
         
    $pricePer sprintf("%01.2f"$pricePer);
         
    $price sprintf("%01.2f"$price);         
     
         
    //print table and data
         
    echo "<tr><td width='300' class='verdana10Black'>" $Description "</td><td width='75' class='verdana10Black'>" $price $saleUnit "</td><td width='75' class='verdana10Black'>"$pricePer $pricePerUnit ."</td></tr>";
         
    $rowCount ++;
         if (
    $rowCount == $numRows) {
            echo 
    "</table></td><td width='50%' valign='top'><table width='350' border='0' cellspacing='0' cellpadding='3'>";
         } else if (
    $rowCount == $numRows2) {
            echo 
    "</table></td></tr>";     
         }
         }
     
         }
     
     
     

    NOEINK
    <Think Without Ink>

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    first of all only select the fields you need from your database. ie don't use * use the actual field names.
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    St Petersburg, Russia
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can echo '*' from time to time to show how your script is progressing.
    you can also use Output Control Functions (see PHP documentation).

  4. #4
    SitePoint Enthusiast
    Join Date
    Oct 2002
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have modified script getiing data that I need. Still not much difference.

    you can echo '*' from time to time to show how your script is progressing.
    Nothing is getting echo'd until all is done, how would I do this?

    Thanks again
    NOEINK
    <Think Without Ink>

  5. #5
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    > My problem is that the page is taking 20-40 seconds to build.

    Yes, this is a serious problem...

    > My question is how do I create a simple 'preloader' to alert user that data is loading.

    This is a wrong question.

    > Maybe I am not doing it as effciently as possible.

    And this one is correct. Think about optimizing your code, not about 'preloader'.

    You're using three mysql queries within loop, so if you have 10 categories, you make 1 + 3 * 10 = 31 queries per page. This is too many. Dont know your db stucture, but seems you can achieve the same result with one single query (look for JOIN in mysql docs).

  6. #6
    SitePoint Addict Fire59's Avatar
    Join Date
    Aug 2003
    Location
    mn
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about only select 25 products at a time and then have a next 20 or prev 20 button. This might be too much work for now but in the long run it would be a benifit.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •