PHP - View Counter Keeps Counting One Hit As 2
Hope this is clear and concise! I have a view article page and every time you land on the page then I have a query that runs an update using sql. Now the problem is everytime the script is run it counts one hit as 2. For example if an article has been viewed 4 times. Next time you land on the page it shows the page has been seen 6 times instead of 5. Here is my code below for the view_article page:
Code: [Select] <body bgcolor="#000000"> <div id="site_div" align="center"> <div id="wrapper_div"> <!-- header --> <?php include 'header.php'; ?> <!-- header --> <div id="main_nav"> <div id="link1_div"><p class="link1_class"><a href="index.html" class="link1_class">All Articles</a></p></div> <div id="link2_div"><p class="link2_class"><a href="index.html" class="link2_class">Reviews</a></p></div> <div id="link3_div"><p class="link2_class"><a href="index.html" class="link2_class">Previews</a></p></div> <div id="link4_div"><p class="link2_class"><a href="buys.php?lightbox[width]=700&lightbox[height]=400&lightbox[modal]=true" class="link2_class lightbox">Videos</a></p></div> <div id="link5_div"><p class="link2_class"><a href="rents.php?lightbox[width]=700&lightbox[height]=400&lightbox[modal]=true" class="link2_class lightbox">News</a></p></div> <div id="link6_div"><p class="link2_class"><a href="index.html" class="link2_class">Podcasts</a></p></div> <div id="link7_div"><p class="link2_class"><a href="index.html" class="link2_class">Contact Us</a></p></div> <div id="link8_div"><p class="link2_class"><a href="index.html" class="link2_class"></a></p></div> </div> <div id="sub_nav"> <div id="sub_nav_div"> <p class="filter_by_class">FILTER BY: <a href="index.php" style="color:#21a8f7; text-decoration:underline">All</a> <a href="xbox360.php">Xbox 360</a> <a href="#">PS3</a> <a href="#">Wii</a> <a href="#">PC</a> <a href="#">PSP</a> <a href="#">DS</a><a href="#">Mobile</a><a href="#">Older Platforms</a></p></div> </div> </div> <div id="content_div"> <div id="content_spacer"> </div> <!-- TRACK PAGE <span class="posthilit">VIEWS</span> --> <?php //DATABASE CONNECTION include_once 'dbc.php'; $id = $_GET['id']; mysql_query("UPDATE articles SET `article_views` = `article_views`+1 WHERE idarticles='$id'"); ?> // TrACK PAGE <span class="posthilit">VIEWS</span> // MAIN CONTENT if(isset($_GET['id']) && is_numeric($_GET['id']) ) { $query = "SELECT * FROM articles WHERE idarticles= {$_GET['id']}"; if($r = mysql_query($query)) { $row = mysql_fetch_array($r); echo " <div id=\"main_article_div\"> <div id=\"top_blue_content_div\"> {$row['article_type']}</div> <div id=\"image_main_content_div\"><img src=\"{$row['article_big_image']}\" width=\"200\" height=\"112\" alt=\"main_article_image\" /></div> <div id=\"content_area\"><p class=\"main_content_title_class\">{$row['article_title']} <div id=\"addthisspacer\"> <!-- AddThis Button BEGIN --> <div class=\"addthis_toolbox addthis_default_style \"> <a class=\"addthis_button_facebook_like\" fb:like:layout=\"button_count\"></a> <a class=\"addthis_button_tweet\"></a> <a class=\"addthis_counter addthis_pill_style\"></a> </div> <script type=\"text/javascript\" src=\"http://s7.addthis.com/js/250/addthis_widget.js#username=xa-4d1d4fb34dc87309\"></script> <!-- AddThis Button END --> </div> </p> </div> </div> "; } } ?> <!-- MAIN CONTENT ENDS --> </div> <div id="lower_content_div"> <!-- THIS IS WHERE THE MAIN ARTICLE/COMMENTS STARTS --> <?php //include 'dbc.php'; if(isset($_GET['id']) && is_numeric($_GET['id']) ) { $query = "SELECT * FROM articles WHERE idarticles= {$_GET['id']}"; if($r = mysql_query($query)) { $row = mysql_fetch_array($r); echo " <div id=\"left_lower_content\" class=\"break-word\"> {$row['actual_article']} </div> "; } } ?> <!-- THIS IS THE END OF THE MAIN ARTICLE/COMMENTS QUERY --> </div> <div id="footer_div"> <div id="footer_1"></div> <div id="actual_footer"> <div id="top_footer_div"> <div id="top_footer_div_left"><a href="login.php?lightbox[width]=396&lightbox[height]=200&lightbox[modal]=true"><img src="images/rent-or-buy-2-logo_small2.png" width="260" height="39" alt="small_logo" class="small_logo_class" /></a></div> <div id="top_footer_div_right"> <div id="login_register_div_2"> <p class="logintext"><a href=""><img src="images/rss_icon.png" width="16" height="16" alt="rss_icon" /></a></p><p class="logintext"><a href="http://www.twitter.com"><img src="images/twitter_icon.png" width="16" height="16" alt="twitter_icon" /></a></p><p class="logintext"><a href="http://www.youtube.com"><img src="images/youtube_icon.png" width="38" height="16" alt="youtube_icon" /></a></p><p class="logintext"><a href="http://www.facebook.com"><img src="images/facebook_icon.png" width="16" height="16" alt="facebook_icon" /></a></p> </div> </div> <div id="search_div2"> <form action="#" method="post" name="search"> <input type="text" value="Search!" class="formimage" /> <INPUT TYPE="IMAGE" src="images/search_bar_button.jpg" BORDER="0" style="padding:0" class="formbutton"> </form> </div> </div> <div id="actual_footer_bottom_section"> <div id="actual_footer_bottom_section_top"></div> <div id="actual_footer_bottom_section_top_links"><a href="index.php">All Articles</a> | <a href="#">Reviews</a> | <a href="#">Previews</a> | <a href="#">"Buys"</a> | <a href="#">"Rents"</a> | <a href="#">Videos</a> | <a href="#">News</a> | <a href="#">Games</a> </div> <div id="actual_footer_bottom_section_bottom"> <p class="actual_footer_bottom_section_paragraph">Design By: Felix Henriques | Created By: Felix Henriques | Copyright 2010 Buy Or Rent Games</p></div> <div id="actual_footer_bottom_section_top"></div> </div> </div> </div> </div> </div> </body> </html> Similar TutorialsI may not be a PHP wizard, but a view counter seems pretty damn simple and easy to implement. Unless you leave it in my hands... It's a pretty simple problem, so I'm hoping it'll be a simple solution. Every time you load a game on my website, at the top of the php "playgame.php" is a function call: $objGlobal->update_gameplay($gameid); In my functions.class.php is the function declaration: function update_gameplay() { $args = func_get_args(); $getsql = mysql_query("select * from games where gameid = $args[0]"); if($getsql) { $resultgame = mysql_fetch_array($getsql, MYSQL_ASSOC); $timesplayed = $resultgame['timesplayed'] + 1; $updatesql = "update games set timesplayed = '$timesplayed', last_played = now() where gameid = $args[0]"; if(mysql_query($updatesql)) return true; else return false; } } Yet, when I load the playgame page, it adds 1,2 or 3!!! So this means the function is being called 1, 2 or 3 times, but how can that be if it is at the VERY top of the page? Try it yourself on my website, www.gpstudios.com. Click on a game and reload. For some reason, the newer games always add 1, but other older/more popular games add 1, 2 or 3 I've searched for the string "update" and the string "timesplayed" in every file using a program called FileSeek and I can say for certain that the function must be being called multiple times for some reason. What's weirder is that my game reviews (http://www.gpstudios.com/reviews.php) are also having their view counter incremented by 1 or 2 on a page load. Please help me solve this quick. Thanks, -Tom I have a table in my database named order, and i want to count the quantity sold. i have the basic query already setup like: Code: [Select] $sql = "SELECT SQL_CALC_FOUND_ROWS deal.id, deal.title, deal.min_qty, deal.start_date, deal.end_date, deal.status, deal.secondary, count(o.id) as orders_cnt FROM deals as deal LEFT JOIN orders as o on o.d_id = deal.id WHERE start_date >= '".$beg."' AND start_date <= '".$end."' GROUP BY o.d_id, deal.id ORDER BY start_date LIMIT ".AwsPager::getCurrentIndex().", ".AwsPager::getLimit(); Which would theoretically work, but in the orders table, there is a column named quantity, and its those numbers that i need to add together and store in the array as orders_cnt.. any clue how i can do that? Hi im trying to make it so the time says how many hows left, not how many hours past. i have the right idea but it shows a negative number under "$time" $hours = number_format(($diffrence / 60 / 60)); $time = number_format(($hours - 24)); ............. else if(empty($password)){ $errorMsg[]="please enter password"; } else { try { $select_stmt=$db->prepare("SELECT * FROM tbl_user WHERE username=:uname OR email=:uemail"); $select_stmt->execute(array(':uname'=>$username, ':uemail'=>$email)); $row=$select_stmt->fetch(PDO::FETCH_ASSOC); if($select_stmt->rowCount() > 0) { if($username==$row["username"] OR $email==$row["email"]) { if(password_verify($password, $row["password"])) { $_SESSION["user_login"] = $row["user_id"]; //---------------- $pdo_statement=$db->prepare("update tbl_user set logins=7 where user_id=2"); $result = $pdo_statement->execute(); //---------------- $loginMsg = "Successfully Login..."; header("refresh:2; welcome.php"); } else hey guys iam starting to learn during this crysis php :-). I got stucked with this pdo style..... i like to count logins, i tryed and figured out i can use the id... but my question is how can replace this "where user_id=2" to get the specific ID for the current user login in? the count function i i know how to make the... first i want to know how to write the syntax of this pdo...... thank you :-) Hiya peeps, I have a mysql database with a record of all the images a customer uploads on my site, these images are displayed on there account. I can get this to work but the problem I'm facing is I need a minimum of 5 images displayed so if they only upload 2 images for instance I need to detect that and add my "No Image Uploaded" image. But if they have uploaded 5 or more I need to just let them be displayed without adding the "No Image Uploaded" picture. Does anyone have any ideas as I'm very stuck! Many thanks James. I'm trying to get a total number from the db where the month is the same and the ip is different. This is a stats script that someone else wrote, and I'm just trying to get a month-to-month total. Right now, it pulls the information out and displays the month with how many different ips came that month, the problem is, it lists like this: June - 41 - 41 June - 1 - 1 June - 1 - 1 June - 1 - 1 June - 1 - 1 June - 1 - 1 June - 9 - 9 June - 2 - 2 June - 2 - 2 June - 2 - 2 June - 1 - 1 June - 3 - 3 June - 2 - 2 June - 4 - 4 June - 1 - 1 June - 13 - 13 June - 154 - 154 what I need to do is grab all those numbers and give a total for that month, in this case, June. This is my query Code: [Select] $query = "SELECT date, COUNT(referrer)'referrer' FROM app_analytics WHERE appid = $userid AND date BETWEEN '" .$y_month . "' AND '" . $t_month . "' GROUP BY referrer ORDER BY date"; Can anybody help out? Thanks in advance Hello, I'm creating a program that analyzes information that is exported by another program. That program exports it in this way: Value1 Value2 Value3 Value4 Value1 Value2... In order to insert them in the database, I first have to know how many lines are inserted. They get send through HTML post. I already tried: $lines = explode("\n", $string); $number_lines = count($lines); But this always results in 1. How do I fix this? Regards, Chris I have a page that displays a list of retail locations (est. 100-150 locations). I have my layout setup to fit 3 columns side by side. Ex: <div class="col"></div> <div class="col"></div> <div class="col"></div> .. with the "col" class having a specific width and float: left. Basically, I want to print out the locations in the column and when a column reaches a certain number of locations, it closes that div and starts a new one and continues printing the rest of the locations. How would I go about making sure that once a column has reached a certain number of locations, it closes the div and starts the new one? I have an array. When I print it it looks like this: Quote Array ( [1] => Array ( [Cushion] => Cushion 1 [Fabric] => f-111111 [FabricPrice] => 0 [Fill] => Fiber [Button] => none [ContWelt] => none [ContWeltFabric] => none [Zipper] => N [Quantity] => 2 [WeltSize] => [SKU] => c-111111 [Edge] => Knife [Cap] => N [Straps] => N [Hinged] => N [Type] => Boxed Button [Price] => 54.25 [Total] => 108.5 ) [2] => Array ( [Cushion] => Cushion 1 [Fabric] => f-111111 [FabricPrice] => 0 [Fill] => Fiber [Button] => none [ContWelt] => none [ContWeltFabric] => none [Zipper] => N [Quantity] => 4 [WeltSize] => [SKU] => c-111111 [Edge] => Knife [Cap] => N [Straps] => N [Hinged] => N [Type] => Boxed Button [Price] => 54.25 [Total] => 217 ) ) Now when I use count() I get a result of 01 when I should get 02 when I use count( ,1) I get a result of 381. I am Using this wrong? the array is stored in $_SESSION['cushArray'] I know how to count the files: <?php $dir = $_SERVER[DOCUMENT_ROOT]."/api/sigs/cache/".$i[id]."/"; $files = scandir($dir); $result = count($files); printf("$result Signatures Generated"); ?> But i'm trying to make it so it only counts the files that were modified recently (That Day). I am getting the result I expected with ORDER BY/GROUP BY queries WITH a troubling EXCEPTION: the data is being IN ORDER from 0 thru 9, but 10 (which in this test sample is my LARGEST quantity) is being inserted between the ONE and 2 quantity. Not sure if this will occur with ALL teen values, as well as hundreds. The column is a SMALLINT field (not sure if that makes a difference)> Is there a solution for this situation? Hello I have a table that inserts a new row with data when a member views a page. I wish to count all the rows for each member, and then be able to show what the cuurent members "position" is eg. what members has the highest row count. Example. counting the rows for member_A returns 1000 rows, the number of rows for member_B returns 1500 rows. How can I display for member_A that he is in "second" place? ok my code is below but i want to count how many $sname or ['member_name'] there is as if there is 4 or more i want it to add a marquee to it have that sorted but im having trouble getting it to count can you help me? $query = "SELECT * FROM `ibf_members` WHERE member_group_id = '4' or member_group_id = '6' or member_group_id = '17' or member_group_id = '18' or mgroup_others = '4' or mgroup_others = '6' or mgroup_others = '17' or mgroup_others = '18' "; $result99 = mysql_db_query("pbf99_backup", $query); $staff1 = mysql_num_rows($result99); if ($result99) { while ($r = mysql_fetch_array($result99)) { // Begin while $sid = $r["member_id"]; $smgid = $r["member_group_id"]; $query2 = "SELECT * FROM `ibf_sessions` WHERE member_id = '$sid'"; $result2 = mysql_db_query("pbf99_backup", $query2); if ($result2) { while ($r2 = mysql_fetch_array($result2)) { // Begin while $sname = $r2["member_name"]; $query3 = "SELECT * FROM `ibf_groups` where g_id = '$smgid'"; $result3 = mysql_db_query("pbf99_backup", $query3); if ($result3) { while ($r3 = mysql_fetch_array($result3)) { // Begin while $ssuffix = $r3["suffix"]; $sprefix = $r3["prefix"]; echo "<a href='$host$sid' target='_parent'>$sprefix$sname$ssuffix$stafff</a><br><br>"; }}}}}} ?> One more question to ask. I'm trying to do a count of total logins that have happened in the last 5 months including the current one so far. My users_logins table has the following structu users_id, session_id, login_date(datetime), last_activity(datetime). What the end result is going to be is: <tr> <td></td> <th scope="col"><?php echo date("M", strtotime("-4 month")); ?></th> <th scope="col"><?php echo date("M", strtotime("-3 month")); ?></th> <th scope="col"><?php echo date("M", strtotime("-2 month")); ?></th> <th scope="col"><?php echo date("M", strtotime("-1 month")); ?></th> <th scope="col"><?php echo date("M"); ?></th> </tr> <tr> <th scope="row">Logins</th> <td>94</td> <td>53</td> <td>124</td> <td>92</td> <td>105</td> </tr> Where the first td would be May and the last td would be September. Do I have to do this in 4 separate queries. If so here's what I have for the first query. I'm not sure what woudl go for the WHERE. $four_months_ago_query = "SELCT COUNT(date_login) FROM users_logins WHERE ? "; I have the following code. Basically I'm trying to count the amount of colours returned in the $colour variable. What I have is always returning 1, any ideas why? Code: [Select] $colour = $db->query("SELECT * FROM colours WHERE prod_id = ".$row['prod_id']." AND LENGTH(col_image_1) > 0 ORDER BY col_rank ASC LIMIT 1"); $colourcount = mysql_num_rows($colour); if($db->numrows($colour)) { $colour = $db->fetchrow($colour); } else { unset($colour); } Code: [Select] echo $colourcount; Hi all What I am trying to achieve is a for each loop for the following output Code: [Select] Array ( [result] => success [source] => getDirectory [success] => Array ( [0] => /root/mc/world/data/ [1] => /root/mc/world/level.dat [2] => /root/mc/world/level.dat_old [3] => /root/mc/world/players/ [4] => /root/mc/world/players/herghost.dat [5] => /root/mc/world/region/ [6] => /root/mc/world/region/r.-1.0.mcr [7] => /root/mc/world/region/r.0.0.mcr [8] => /root/mc/world/session.lock [9] => /root/mc/world/uid.dat ) ) How would I go about saving each path from the [success] output? Basically I wish to loop through the array and delete the files. Many Thanks Alright more on my affiliate page which you guys helped me build last night. Thanks again I want to count the number of times one of the affiliates is clicked. So each one has to have its own impression number. I have my array set up like so: - i see that impression is now 0 and needs to be changed to a variable etc like $aff_blah1_impressions // affiliates array for affiliates page $affiliates = array( array( "title" => "aff_blah1", "image" => "http://aff_blah1.net/images/aff_blah1.png", "href" => "http://aff_blah1.net/", "start_date" => "1/1/2011", "impressions" => "0" ), array( "title" => "aff_blah2", "image" => "http://aff_blah2.net/images/aff_blah2.png", "href" => "http://aff_blah2.net/", "start_date" => "1/1/2011", "impressions" => "0" ), array( "title" => "aff_blah3", "image" => "http://aff_blah3.net/images/aff_blah3.png", "href" => "http://aff_blah3.net/", "start_date" => "1/1/2011", "impressions" => "0" ), ); // affiliates array end I have seen some examples but they all use a text file. Is there any way to have it stored in the database? I was thinking it would somehow be in the link with like a onClick (which is java) that would run a php function. What i am trying to figure out is the best way to store it and have only one function for impressions made on each affiliate. Any keyword search terms or links would be very helpful! Can also contact through AIM: MadLittleMods@gmail.com Hi Everyone, I've been studying the topics at this site and others on ways to count uploaded images; and what I've tried hasn't worked. I have seen this question posed and answered many times and most of the answers are something akin to this: Code: [Select] $count = count($_FILES['userfile'] ['name']); echo "the number of photos is ".$count; The explanation usually states that "count($_FILES['userfile'])" will simply count the number of elements in the array and return 5. But including ['name'] will return the number of files (images) uploaded. I will allow up to five photos to be uploaded (less is OK too) and want to count them in order to iterate through the proper number of loops to filter and process the photos. I've tested both of the above and get 5 (elements) when I count the ['userfile']; but get 0 (zero) when I test "['userfile'] ['name']". Here's the html code: Code: [Select] <!-- html here to input form data --> <label></label><input type="hidden" name="MAX_FILE_SIZE" value="500000000" /><br /> <label for="userfile">Upload photo 1</label> <input type="file" name="userfile1" id="userfile1" /><br /> <label for="userfile">Upload photo 2</label> <input type="file" name="userfile2" id="userfile2" /><br /> <label for="userfile">Upload photo 3</label> <input type="file" name="userfile3" id="userfile3" /><br /> <label for="userfile">Upload photo 4</label> <input type="file" name="userfile4" id="userfile4" /><br /> <label for="userfile">Upload photo 5</label> <input type="file" name="userfile5" id="userfile5" /><br /><br /> <label></label><input type="submit" name="userfile" value="Send Ad" /> </form> I've been testing the uploads with two files (jpg) and have tried to come up with an alternative that can iterate through the file arrays and determine how many photos I have. The code I've been testing is as follows: Code: [Select] $num = 1; $count = 0; while ($num <=5) { foreach ($_FILES['userfile'.$num] as $upload){ if ($upload ['error'] != 4){ } $count ++; } $num++; } echo "the number of photos is ".$count; I'm using "['error'] !=4" because some times people won't have 5 photos to upload. The result I get is 25 or 5 (files) x 5 (elements) for a total of 25. Does anyone know the proper way of doing this? Thanks for you input! Cheers, Rick Hi guys, How would I write a small script in php to count the unique values in the below array : the below code if part of cubecart 3.20 shopping cart, I need to count the unique number of Seller_ID fields within this array in the basket to be able to calculate shipping according to the number of sellers. How would I to this ? please any help is appreciated. Code: [Select] if(isset($_GET['act']) && $_GET['act']=="step4"){ // set live vars for order inv and its the last step $view_cart->assign("QUAN_DISABLED","disabled"); $view_cart->assign("TEXT_BOX_CLASS","textboxDisabled"); $basket = $cart->setVar($productId,"productId","invArray",$i); $basket = $cart->setVar($product[0]['name'],"name","invArray",$i); $basket = $cart->setVar($product[0]['productCode'],"productCode","invArray",$i); $basket = $cart->setVar($plainOpts,"prodOptions","invArray",$i); $basket = $cart->setVar(sprintf("%.2f",$price*$quantity),"price","invArray",$i); $basket = $cart->setVar($quantity,"quantity","invArray",$i); $basket = $cart->setVar($product[0]['digital'],"digital","invArray",$i); $basket = $cart->setVar($product[0]['Prod_Seller'],"Prod_Seller","invArray",$i); $basket = $cart->setVar($product[0]['Seller_ID'],"Seller_ID","invArray",$i); } else { $basket = $cart->unsetVar("invArray"); } Suppose I have a multidimensional array... $AnArray[1][$j] I'd like to count how many $j So if $AnArray = array(0 => array('orange', 'banana', 'apple'), 1 => array('carrot', 'collard', 'pea','tuna')); The count I'm after would be 4... |