Is this script OK?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DavidPr
    New Member
    • Mar 2007
    • 155

    Is this script OK?

    [PHP]<?php
    $query = "SELECT viewed FROM $j where id='$file'";
    $result = mysql_query($qu ery);
    while($row = mysql_fetch_arr ay($result, MYSQL_ASSOC))
    {
    $viewed = $row['viewed'];
    }

    if ($viewed == '0')
    {
    $addone = '1';
    }
    else
    {
    $addone = $viewed + 1;
    }

    mysql_query("UP DATE $j SET viewed='$addone ' WHERE id=$file");
    ?>[/PHP]

    I want to know how many times an ad is viewed on my website. So I want the number stored in the row - $viewed - to increase by one each time the ad it's associated with is viewed. The default value is "0" so when the ad is viewed for the first time "0" is changed to "1" and then it increases by one each time it's viewed.

    I just want to make sure that I have this right, because I wonder if sometimes the number isn't being increased by 2, 3 or more.
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Looks fine to me.

    The checking of whether the 'viewed' is 0 is pointless, though.

    You can increment a number, instead of using '+ 1', by doing $var++ or ++$var.

    Comment

    • DavidPr
      New Member
      • Mar 2007
      • 155

      #3
      The checking of whether the 'viewed' is 0 is pointless, though.
      Yes, I guess it is.

      You can increment a number, instead of using '+ 1', by doing $var++ or ++$var.
      So all I really need to do is implement the update query.
      [PHP]mysql_query("UP DATE $j SET viewed='$viewed +1' WHERE id=$file");[/PHP]How would I write the update query above to change the +1 to $var++?

      Comment

      • DavidPr
        New Member
        • Mar 2007
        • 155

        #4
        How about this:
        [PHP]function increment(&$vie wed, $amount = 1)
        {
        $viewed = $viewed +$amount;
        }[/PHP]

        And then the update process:
        [PHP]<?php
        $query = "SELECT viewed FROM $j where id='$file'";
        $result = mysql_query($qu ery);
        while($row = mysql_fetch_arr ay($result, MYSQL_ASSOC))
        {
        $viewed = $row['viewed'];
        }

        increment($view ed);
        $viewed = $viewed;

        mysql_query("UP DATE $j SET viewed='$viewed ' WHERE id=$file");
        ?>[/PHP]

        By creating the increment function I can use it on other pages where I want to increase a database entry by one.

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          For simplicity's sake, you could just do (using $var++):

          [php]
          mysql_query("UP DATE $j SET viewed='$viewed ++' WHERE id=$file");
          [/php]

          Comment

          • DavidPr
            New Member
            • Mar 2007
            • 155

            #6
            Yes, that does simplify things. Thank you.

            Comment

            Working...