Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6695651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:14:51+00:00 2026-05-26T06:14:51+00:00

function countBrand($brand_id, $brand_name) { $sql = SELECT brand FROM coupons WHERE expire >= CURRENT_DATE

  • 0
function countBrand($brand_id, $brand_name) {
    $sql = "SELECT brand FROM coupons WHERE expire >= CURRENT_DATE AND brand='".$brand_id."'";
    $result = mysql_query($sql);
    $row = mysql_fetch_array($result);
    echo '<li>'.$brand_name.'</li>';
}

function brandCount() {
    $sql = "SELECT DISTINCT brand,brand_id,brand_name FROM coupons,coupons_brand WHERE brand=brand_id AND expire >= CURRENT_DATE ORDER BY brand_name";
    $result = mysql_query($sql);
    $row = mysql_fetch_array($result);
    $num = mysql_num_rows($result);

    echo '<h3>'.$num.' Brands</h3>';

    echo '<ul>';

    $i = 0;
    while ($i < $num) {
        $brand_id = mysql_result($result, $i, "brand_id");
        $brand_name = mysql_result($result, $i, "brand_name");
        countBrand($brand_id, $brand_name);
        $i++;
    }

    echo '</ul>';
}

It works perfectly and gives me the results I am looking for. I am not as strong with sql statements as I would like to be. Is there a way I could do this that would be more efficient, it seems very slow.

Basically, it counts how many brands have coupons, then coupons how many coupons each brand has..

I also, on the same page, do this for categories. There are a few thousand categories and maybe 20,000 coupons.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:14:51+00:00Added an answer on May 26, 2026 at 6:14 am

    there are a few php optimisations you could do, but they probably wont save you much time, compared to adding an index to mysql on the correct columns

    i have commented some php optimisations below, maybe of interest to you anyway

    given the simple nature of the functions, it is not necessary to have 2 functions, and that would save the timecost of calling countBrand(), (although it is a pretty minimal time saving)

       function countBrand($brand_id, $brand_name) {
            $sql = "SELECT brand FROM coupons WHERE expire >= CURRENT_DATE AND brand='".$brand_id."'";
            $result = mysql_query($sql) || die (mysql_error()); // always check for errors
    
            list($brand) = mysql_fetch_row($result);
    
    // fetch row, returns a more concise array then mysql_fetch_array
    //        $row = mysql_fetch_array($result);
    
            // use commas rather then . when concatenating echo statements
            // dots force concatenation before output
            echo '<li>',$brand,'</li>'; 
    
        }
    
        function brandCount() {
            $sql = "SELECT DISTINCT brand,brand_id,brand_name FROM coupons,coupons_brand WHERE brand=brand_id AND expire >= CURRENT_DATE ORDER BY brand_name";
            $result = mysql_query($sql) || die(mysql_error()); // always check for errors
    
        //    $row = mysql_fetch_array($result); // not sure why this is needed
            $num = mysql_num_rows($result); 
    
            // use commas rather then . when concatenating echo statements
            // dots force concatenation before output
            echo '<h3>',$num,' Brands</h3>';
    
            echo '<ul>';
    
          // fetch all cols at once, rather then using lots of separate calls to mysql_result
          // use mysql_fetch_row as it returns just the ordered values (vs mysql_fetch_assoc, and mysql_fetch_array)
          // 
           while(list($brand, $brand_id, $brand_name) == mysql_fetch_row($result)) {
                countBrand($brand_id, $brand_name);
           }
    
          // replaced with while loop above
          //  $i = 0;
          //  while ($i < $num) {
          //      $brand_id = mysql_result($result, $i, "brand_id");
          //      $brand_name = mysql_result($result, $i, "brand_name");
         //       countBrand($brand_id, $brand_name);
          //      $i++;
            }
    
            echo '</ul>';
        }
    

    those enhancements will only give you a minor speed increase
    the biggest speed increase you will get is if you call the database less.

    currently you select each brand, and then go back and count each brand individually
    without knowing the structure of you tables this sql is difficult for me to write, so it is a guess, but it should point you in the right direction

    SELECT brand, brand_id, brand_name, COUNT(*) as brandcount
    FROM coupons
    JOIN coupons_brand ON brand=brand_id 
    WHERE expire >= CURRENT_DATE 
    GROUP BY brand, brand_id, brand_name
    ORDER BY brand_name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

function get_total_adults() { $sql = SELECT SUM(number_adults_attending) as number_of_adults FROM is_nfo_rsvp; $result = mysql_query($sql)
function toltalRetailerComm($userId) { //print_r ($shoppeId); $sql = SELECT shoppe_id FROM atm_super_shoppe WHERE user_id='$userId'; $query
function(char* name) { char sql[50]; sprintf(sql, select %s;, name); } What's the best way
function GetMe(id) { DB.transaction( function (transaction) { transaction.executeSql(SELECT * FROM users WHERE id =
function selected() { var selObj = window.getSelection(); } This function returns selected text from
function dowork() { $(.wrappedElement).removeClass(wrappedElement); $(.wrappedElementout).removeClass(wrappedElementout); var a=$(div#DepPart select option:selected).val(); $(div#TestPart select option[value^=a]).addClass(wrappedElement); $(div#TestPart select
function returnsAnArray () { return array ('test'); } echo returnsAnArray ()[0]; generates a syntax
Function FillAdminAccount() As Boolean FillAdminAccount = True Try SQLconn.ConnectionString = connect timeout=9999999; & _
function Submit_click() { if (!bValidateFields()) return; } function bValidateFields() { /// <summary>Validation rules</summary> ///
function main() { Hello(); } function Hello() { // How do you find out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.