Jump to content

awjudd

Staff Alumni
  • Posts

    422
  • Joined

  • Last visited

  • Days Won

    1

Posts posted by awjudd

  1. $_POST[foo] and $_POST['foo'] both work because PHP will automatically upconvert single words of text that it doesn't know as a constant to a string.

     

    That said, using $_POST[foo] is slower because not only does it cause notices to be thrown but it has to check for any definitions before it can assume that it is a string.

     

    Sample:

    <?php
        $arr = array ();
    
        for ( $x = 0; $x < 1000000; $x++ )
        {
            $arr [ foo ] = 'bar';
        }
    ?>

     

    Time of execution:

    $ time php test.php 
    
    real    0m1.641s
    user    0m1.424s
    sys     0m0.044s

     

    Versus:

     

    <?php
        $arr = array ();
    
        for ( $x = 0; $x < 1000000; $x++ )
        {
            $arr [ 'foo' ] = 'bar';
        }
    ?>

     

    Run Time:

    $ time php test.php 
    
    real    0m0.467s
    user    0m0.292s
    sys     0m0.052s

     

    Yes, this is a highly exaggerated case but it is a decent example of how it is bad ;)

     

    ~judda

  2. Not in the case where we are trying to see if something changes ...

     

    <?php
    $sql=mysql_query("SELECT items.itemname, cat.category FROM items,cat WHERE cat.catid=items.catid");
    $curr = '';
    while($res=mysql_fetch_array($sql))
    {
        if ( $curr != $res [ 'category' ] )
        {
            echo $res['category'] . "<br>";
            $curr = $res [ 'category' ];
        }
            echo $res['itemname'] . "<br>";
    }

     

    Something like that would work.

     

    ~judda

  3. How do you keep track of it? ... in a variable?

    $prevCategory = $res['category'];

     

    The sorting in that manner should probably as you said put the things followed by a rank you would need a simple table mapping the orders, or you could do it in PHP (less efficient) using the array sorting functions.

     

    ~judda

  4. Question 1: what i need is for it to display the category name only once...then each item under that correct category..then display the next category and its respected items etc etc.

     

    Keep track of the current header and use an if statement if it is different then print it, otherwise don't.

     

    Question 2: I would order by the category and then the itemname (ORDER BY category, itemname)

     

    ~judda

  5. ASP.NET has lots of stuff already built-in for you ... so that can be both helpful and detrimental to your learning ...

     

    For example, ASP.NET has their membership provider so that in a matter of about 10 lines of code, you can have a complete login script done with user roles and everything.

     

    Whereas to do something similar in PHP, you need to do it manually.

     

    ~judda

  6. if ( !isset ( $_POST['wholesale'] ) ) { $wholesale = '.'; }

     

    That should remove the notice.  That said, you shouldn't be ignoring the notices because there is a chance it could cause your script to do something you don't expect.  And iirc, the throwing of notices slows the execution of the scripts a bit.

     

    ~judda

  7. Why are your expressions within the count a string?

     

    COUNT('CASE games.competition WHEN 3 THEN goals.goal_id ELSE NULL END CASE') AS gls3,

     

    It shouldn't be in quotes ... it should be:

    COUNT(CASE games.competition WHEN 3 THEN goals.goal_id ELSE NULL END CASE) AS gls3,

     

    ~judda

  8. How about having a conditional statement within your COUNTs?

     

    i.e.

     

    SELECT
    COUNT( CASE games.competition WHEN 1 THEN goals.goal_id ELSE NULL END CASE ) AS gls1, /* NULL shouldn't be counted */
    COUNT( CASE games.competition WHEN 2 THEN goals.goal_id ELSE NULL END CASE ) AS gls2,
    goals.scorer,
    players.surname,
    games.competition
    FROM goals
    INNER JOIN players ON goals.scorer = players.player_id
    INNER JOIN games ON goals.match = games.match_id
    GROUP BY goals.scorer

     

    Etc? ... or group it by competition id, then you don't need the conditional, and it would return the counts for each of the competitions, even if there were more than what you originally expected.

     

    Does that make sense?

     

    ~judda

  9. There is no need to order by the two queries.  Try a similar fashion to what I have in my third post with the union bit being within a subquery.  For example:

     

    SELECT SeatPosition FROM (
    SELECT SeatPosition AS SeatOffset,SeatPosition FROM users 
       WHERE TableID='2' AND
       SeatPosition > $MyCurrentSeatPosition 
    
       UNION
    
       SELECT SeatPosition + 8 AS SeatOffset, SeatPosition FROM users WHERE TableID='2' 
       AND SeatPosition <= $MyCurrentSeatPosition LIMIT 1 ) ORDER BY SeatPosition

     

    The other thing I noticed in your original query is that you wrote last post, you have two things being returned, whereas the second query only returns one.  Which breaks what is required for a union (both tables must return the same number of columns).

     

    ~judda

×
×
  • 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.