Jump to content

HuggieBear

Members
  • Posts

    1,899
  • Joined

  • Last visited

Posts posted by HuggieBear

  1. You can't pass in additional arguments to this function as it's a callback of uasort(), but you could use global variables.  See the example below.  I've added two new variables, sort order and sort key.  I've also added quantity to the product array as a new key to sort on.

    <?php
    
    // Setup product array (You already have yours)
    $products = array(
                    'Bike' => array('price' => '199.99','quantity' => '1'),
                    'Apple' => array('price' => '0.87','quantity' => '2'),
                    'Car' => array('price' => '5999.00','quantity' => '3')
                );
                
    // *NEW* Specify either ASC or DESC
    $sortorder = 'DESC';
    
    // *NEW* Specify the key you'd like the array sorted on
    $sortkey = 'quantity';
                
    // Comparison function *NOTE* You now change the sort order with the global variable, not by swaping the return values around
    function compare($x, $y){
        global $sortorder;
        global $sortkey;
        
        if ( $x[$sortkey] == $y[$sortkey] ){
            $return = 0;
        }
        if ($sortorder == 'ASC'){
           return ($x[$sortkey] < $y[$sortkey]) ? -1 : 1;
        }
        else {
           return ($x[$sortkey] < $y[$sortkey]) ? 1 : -1;
        }
    }
    
    uasort($products, 'compare');
    
    // Output the products
    echo '<pre>';
    print_r($products);
    echo '</pre>';
    ?>

  2. No, it will work, but as far as I'm aware it can't be done in the same page as you need to output different header types.  It's easier if just the image path is stored in the database.

     

    I think you'll need a page that displays the html, with the image source links pointing to something like getimage.php?imageid=1

  3. This should get you started

    // Set secret word
    $word = 'mysecretword';
    
    // How many letters must they provide
    $required_letters = 3;
    
    // Populate array with letters keyed on their position
    $letters = array();
    while (count($letters) < $required_letters){
        $k = rand(1, strlen($word));
        $letters[$k] = substr($word, $k-1, 1);
    }
    
    // Sort if you want them to enter the letters in order
    ksort($letters);
    
    // Print array
    echo '<pre>';
    print_r($letters);
    echo '</pre>';

  4. Try this for returning the award

    // Query the database for active awards
    $res = mysql_query("SELECT code, award FROM awards WHERE active = '1'");
    
    // Populate the codes array
    $codes = array();
    while ($row = mysql_fetch_assoc($res)){
       $codes[$row['code']] = $row['award'];
    }
    
    // Hardcoded userid, yours probably comes from a form or something
    $userid = '1';
    
    // Query the database for user awards
    $res = mysql_query("SELECT * FROM log WHERE userid = '$userid'");
    
    // Print the award details (I'm assuming this will only be a single row)
    $row = mysql_fetch_assoc($res);
    foreach ($codes as $code => $award){
       if ($row[$code] == 1){
          echo $award . "<br />\n";
       }
    }

    As for the id, you can just use a simple count variable.  Do you need the id to output as an HTML id, or do you just need it for layout of the awards?

  5. It's not very good database design.  What's the reason for not wanting multiple rows for each user?

     

    Like I said, it can be done the way you want but I wouldn't advise it:

    // Query the database for active awards
    $res = mysql_query("SELECT code FROM awards WHERE active = '1'");
    
    // Populate the codes array
    $codes = array();
    while ($row = mysql_fetch_row($res)){
       $codes[] = $row[0];
    }
    
    // Hardcoded userid, yours probably comes from a form or something
    $userid = '1';
    
    // Query the database for user awards
    $res = mysql_query("SELECT * FROM log WHERE userid = '$userid'");
    
    // Print the award details (I'm assuming this will only be a single row)
    $row = mysql_fetch_assoc($res);
    foreach ($codes as $award){
       if ($row[$award] == 1){
          echo $award . "<br />\n";
       }
    }

    This is untested, but I expect it to be pretty close to the mark.

  6. Can you change the database structure?  If so, that would be better.

     

    awards

    awardid

    award

    code

    active

     

    userawards

    userid

    awardid

     

    You have a row in the userawards table for each award that each user has.  Then a simple join could retrieve the results you're after.  If you can't change the structure then it can still be done, but changing the structure would be better.

  7. It makes perfect sense, but I'm sure there's a better way to do it.  Here's an interim solution:

    <?php
    // Declare totals as an array
    $totals = array();
    
    // This is the format of your array (I assume posted from your form) notice some duplicates
    $submitted[] = array('1', '12113');
    $submitted[] = array('2', '21200');
    $submitted[] = array('4', '10200');
    $submitted[] = array('1', 'yyyy');
    $submitted[] = array('20', 'xxxxx');
    $submitted[] = array('5', '21200');
    $submitted[] = array('7', '21200');
    
    // This sticks it into a new array keyed on product id with quantity as a value
    foreach ($submitted as $k => $a){
        if (!array_key_exists($submitted[$k][1], $totals)){
            $totals[$submitted[$k][1]] = $submitted[$k][0];
        }
        else {
            $totals[$submitted[$k][1]] = $totals[$submitted[$k][1]] + $submitted[$k][0];
        }
    }
    
    // Print the array and total quantities
    echo '<pre>';
    print_r($totals);
    echo '</pre>';
    
    ?>

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.