PHP - Counting Total Different Elements In A Column
I've been searching around for this for a while and nothing seems to be working properly.
On my website I track who is online at the time (within the past 15 minutes). My database contains important information like ip addresses and page views during the visit. It also contains a column for two digit country codes (US, NL, CA, GB, etc.) It works great. But I'd like to display to my visitors how many countries people are visiting from at the time. I might have ten people from the US, three from England, and one from Canada. I'd like it to say we have visitors from 3 countries. My hope this could be accomplished with a simple query. Here's the code I have ended up with. But it's not working. Each visitor has one row in the table. "cCode" is the column name and "onlinenow" is the table name. I would have changed the names in my code below but I wanted to post my code exactly as is. Code: [Select] $cDiff = mysql_query("SELECT cCode, COUNT(cCode) AS number FROM onlinenow"); I also tried.... Code: [Select] $cDiff = mysql_query("SELECT cCode, COUNT(DISTINCT cCode) AS number FROM onlinenow"); It results in returning "Resource id#30" for the value of cDiff. What am I doing wrong. I am self taught and please forgive me if I didn't use the correct lingo. Thank You In Advance. Similar TutorialsI have a table with "votes" column. I basically want to retrieve the sum of all the votes columns associated with a user. This is the full code. It gives me "NULL" output. $count_votes = $db->prepare("SELECT SUM(votes) as total FROM entries WHERE user_id = :user_id"); $count_votes->bindParam(':user_id', $user_id); $count_votes->execute(); $t_votes = $count_votes->fetchColumn(); $get_votes = $t_votes['total']; echo $get_votes;
But if I remove the user_id bind paramter, it gives me the results. But I need that parameter to show which user has how many votes. $count_votes = $db->prepare("SELECT SUM(votes) as total FROM entries"); $count_votes->execute(); $t_votes = $count_votes->fetchColumn(); $get_votes = $t_votes['total']; echo $get_votes;
What am I doing wrong? Hi, I have downloads that are available for users. They can click the links to download the file. Once they do this, I have a query that updates the number of downloads that file has received. I need to create a query that adds up all the rows Total Downloads. So for example: i have 3 rows: Name - Total Downloads First - 2 Second - 23 Third - 7 Using the query for these rows would give me an amount of 32. Any idea how to do this? I don't want to have to create a new column to find the total number of downloads. Any other ways? I get all the results displayed but how can I echo the total value for for all rows found for under column 'duration' which only contains numbers (seconds).
$query = "SELECT * FROM asterisk_cdr WHERE calldate LIKE '%$calldate%' AND channel LIKE '%$channel%' AND duration"; $result = mysqli_query($dbcon, $query) or die('Error getting data'); $num_rows = mysqli_num_rows($result); This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=330840.0 Hello I am very new to php and programming and I need a grand total of "$Total = odbc_result($result, "total");" but not sure of how to create it. I've a simple Inset List (jQuery Mobile) of several items with a Flip switch, Drop down, check box and Slider elements. I have wrapped this Inset List inside a HTML Form. <code> <div data-role="fieldcontain"> <ul data-role="listview" data-inset="true"> <li data-role="list-divider"> Inhouse capabilities </li> <li> <label for="flip-b">Project Manager</label> <select name="slider" id="flip-b" data-role="slider"> <option value="no">No</option> <option value="yes">Yes</option> </select> </li> <li> <label for="flip-b">Site Supervisor</label> <select name="slider" id="flip-b" data-role="slider"> <option value="no">No</option> <option value="yes">Yes</option> </select> </li> <li><label for="flip-b">Architect</label> <select name="slider" id="flip-b" data-role="slider"> <option value="no">No</option> <option value="yes">Yes</option> </select> </li> <li><label for="flip-b">Builderworks</label> <select name="slider" id="flip-b" data-role="slider"> <option value="no">No</option> <option value="yes">Yes</option> </select> </li> <li><label for="flip-b">Electrical</label> <select name="slider" id="flip-b" data-role="slider"> <option value="no">No</option> <option value="yes">Yes</option> </select> </li> <li><label for="flip-b">Mechanical</label> <select name="slider" id="flip-b" data-role="slider"> <option value="no">No</option> <option value="yes">Yes</option> </select> </li> <li><label for="flip-b">Hydraulics</label> <select name="slider" id="flip-b" data-role="slider"> <option value="no">No</option> <option value="yes">Yes</option> </select> </li> <li><label for="flip-b">Joinery</label> <select name="slider" id="flip-b" data-role="slider"> <option value="no">No</option> <option value="yes">Yes</option> </select> </li> <li> <label for="select-choice-1" class="select">Main Location:</label> <select name="select-choice-1" id="select-choice-1"> <option value="NSW">NSW</option> <option value="ACT">ACT</option> <option value="VIC">VIC</option> <option value="SA">SA</option> <option value="QLD">QLD</option> <option value="NT">NT</option> <option value="WA">WA</option> <option value="TAS">TAS</option> </select> </li> <!-- Select Menus: Main Location--> <li> <div data-role="fieldcontain"> <fieldset data-role="controlgroup"> <legend>CBA Reference</legend> <input type="checkbox" name="checkbox-1a" id="checkbox-1a" class="custom" /> <label for="checkbox-1a">Commercial</label> <input type="checkbox" name="checkbox-2a" id="checkbox-2a" class="custom" /> <label for="checkbox-2a">Retail</label> </fieldset> </div> </li> </ul> </div> <!-- End of Field Contain div tag --> <a href="#additionalinfo" data-role="button" data-inline="true" data-iconpos="left" data-theme="b">Additional Info</a> <a href="./search2.html" rel="external" data-ajax="false" data-role="button" data-inline="true" data-icon="search" data-iconpos="left" data-theme="e">Search</a> </code> I want to know, how I can include only the elements the user selected in my SQL query. Lets say, User wants to find the Electrical workers in QLD, then he will select Yes for Electrical and QLD as Main Location and the other items he would not touch. Now, how can I include only those two values in the select clause. Say, Select Electrical, Main Location from table where Main location='QLD'. I used POST method to gather users input. I use PHP, MySQL for this project. Any ideas on how to achieve this. Regards Sandeep <html> <head> <title>Sum Html Textbox Values using jQuery/JavaScript</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> <script> $(function() { $(document).on('blur keyup', '.add, .sub', function(e) { var sum = 0; $('.add, .sub').each(function(i) { if (!isNaN(this.value) && this.value.length != 0) { if ($(this).hasClass('add')) { sum += parseFloat(this.value); } else { sum -= parseFloat(this.value); } } }); $('#total').text(sum.toFixed(2)); }) }) </script> </head> <body> <form method="post" action="calc.php"> Addition: <br/> Field 1: <input type="text" class="add" name="1" value="7500"/><br/> Field 2: <input type="text" class="add" name="2" value=""/><br/> Field 3: <input type="text" class="add" name="3" value=""/><br/> Field 4: <input type="text" class="add" name="4" value=""/><br/><br/> Total Addition: <br/><br/> Substraction: <br/> Field 1: <input type="text" class="sub" name="5" value=""/><br/> Field 2: <input type="text" class="sub" name="6" value=""/><br/> Field 3: <input type="text" class="sub" name="7" value=""/><br/> Field 4: <input type="text" class="sub" name="8" value=""/><br/><br/> Total Substraction: <br/><br/> Grand Total: <div id="total"></div> <input type="submit" value="submit" /> </form> </body> </html> Hello all, thanks for everything you do! I used a php from generator to make the form seen on this page: http://www.firstpresgreenville.org/forms/retirementform/retirementform-alt.html I need to be able to click on the check boxes and get a dollar amount total in the total field....So if you checked "single", the total field would read $40, and if you checked on "couple", the field would read $50...simple as that... Is there anyway to make that happen? This is the code I am working with now.... <?php # This block must be placed at the very top of page. # -------------------------------------------------- require_once( dirname(__FILE__).'/form.lib.php' ); phpfmg_display_form(); # -------------------------------------------------- function phpfmg_form( $sErr = false ){ $style=" class='form_text' "; ?> <form name="frmFormMail" action='' method='post' enctype='multipart/form-data' onsubmit='return fmgHandler.onsubmit();'> <input type='hidden' name='formmail_submit' value='Y'> <div id='err_required' class="form_error" style='display:none;'> <label class='form_error_title'>Please check the required fields</label> </div> <ol class='phpfmg_form' > <li class='field_block' id='field_0_div'><div class='col_label'> <label class='form_field'>Full Name(s)</label> <label class='form_required' >*</label> </div> <div class='col_field'> <input type="text" name="field_0" id="field_0" value="<?php phpfmg_hsc("field_0"); ?>" class='text_box'> <div id='field_0_tip' class='instruction'></div> </div> </li> <li class='field_block' id='field_1_div'><div class='col_label'> <label class='form_field'>Street Address</label> <label class='form_required' >*</label> </div> <div class='col_field'> <input type="text" name="field_1" id="field_1" value="<?php phpfmg_hsc("field_1"); ?>" class='text_box'> <div id='field_1_tip' class='instruction'></div> </div> </li> <li class='field_block' id='field_2_div'><div class='col_label'> <label class='form_field'>City</label> <label class='form_required' >*</label> </div> <div class='col_field'> <input type="text" name="field_2" id="field_2" value="<?php phpfmg_hsc("field_2"); ?>" class='text_box'> <div id='field_2_tip' class='instruction'></div> </div> </li> <li class='field_block' id='field_3_div'><div class='col_label'> <label class='form_field'>State</label> <label class='form_required' >*</label> </div> <div class='col_field'> <input type="text" name="field_3" id="field_3" value="<?php phpfmg_hsc("field_3"); ?>" class='text_box'> <div id='field_3_tip' class='instruction'></div> </div> </li> <li class='field_block' id='field_4_div'><div class='col_label'> <label class='form_field'>Zip Code</label> <label class='form_required' >*</label> </div> <div class='col_field'> <input type="text" name="field_4" id="field_4" value="<?php phpfmg_hsc("field_4"); ?>" class='text_box'> <div id='field_4_tip' class='instruction'></div> </div> </li> <li class='field_block' id='field_5_div'><div class='col_label'> <label class='form_field'>Phone</label> <label class='form_required' >*</label> </div> <div class='col_field'> <input type="text" name="field_5" id="field_5" value="<?php phpfmg_hsc("field_5"); ?>" class='text_box'> <div id='field_5_tip' class='instruction'>(###) ###-####</div> </div> </li> <li class='field_block' id='field_6_div'><div class='col_label'> <label class='form_field'>Email</label> <label class='form_required' >*</label> </div> <div class='col_field'> <input type="text" name="field_6" id="field_6" value="<?php phpfmg_hsc("field_6"); ?>" class='text_box'> <div id='field_6_tip' class='instruction'></div> </div> </li> <li class='field_block' id='field_7_div'><div class='col_label'> <label class='form_field'>Number of Attendees</label> <label class='form_required' >*</label> </div> <div class='col_field'> <input type="text" name="field_7" id="field_7" value="<?php phpfmg_hsc("field_7"); ?>" class='text_box'> <div id='field_7_tip' class='instruction'></div> </div> </li> <li class='field_block' id='field_8_div'><div class='col_label'> <label class='form_field'>Single or Couple?</label> <label class='form_required' >*</label> </div> <div class='col_field'> <?php phpfmg_checkboxes( 'field_8', "Single|Couple" );?> <div id='field_8_tip' class='instruction'></div> </div> </li> <li class='field_block' id='field_9_div'><div class='col_label'> <label class='form_field'>Amount Due</label> <label class='form_required' >*</label> </div> <div class='col_field'> <input type="text" name="field_9" id="field_9" value="<?php phpfmg_hsc("field_9"); ?>" class='text_box'> <div id='field_9_tip' class='instruction'>$40/ single or $50/couple (Includes one book and lunch)</div> </div> </li> <li class='field_block' id='field_10_div'><div class='col_label'> <label class='form_field'>Additional Comments</label> <label class='form_required' > </label> </div> <div class='col_field'> <textarea name="field_10" id="field_10" rows=4 cols=25 class='text_area'><?php phpfmg_hsc("field_10"); ?></textarea> <div id='field_10_tip' class='instruction'></div> </div> </li> <?php phpfmg_dependent_dropdown( 'field_11' );?> <li> <div class='col_label'> </div> <div class='form_submit_block col_field'> <input type='submit' value='Submit' class='form_button'> <span id='phpfmg_processing' style='display:none;'> <img id='phpfmg_processing_gif' src='<?php echo PHPFMG_ADMIN_URL . '?mod=image&func=processing' ;?>' border=0 alt='Processing...'> <label id='phpfmg_processing_dots'></label> </span> </div> </li> </ol> </form> <?php phpfmg_javascript($sErr); } # end of form function phpfmg_form_css(){ ?> Thanks so much! 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)); My Php Buddies, I have mysql tbl columns these:
id: Now, I want to display their row data by excluded a few columns. Want to exclude these columns: date_&_time account_activation_code account_activation_status id_verification_video_file_url password
So, the User's (eg. your's) homepage inside his account should display labels like these where labels match the column names but the underscores are removed and each words' first chars CAPITALISED:
Id: 1
For your convenience only PART 1 works. Need help on Part 2 My attempted code:
PART 1 <?php // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // Query to get columns from table $query = $conn->query("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'members' AND TABLE_NAME = 'users'"); while($row = $query->fetch_assoc()){ $result[] = $row; } // Array of all column names $columnArr = array_column($result, 'COLUMN_NAME'); foreach ($columnArr as $value) { echo "<b>$value</b>: ";?><br><?php } ?> PART 2 <?php //Display User Account Details echo "<h3>User: <a href=\"user.php?user=$user\">$user</a> Details</h3>";?><br> <?php $excluded_columns = array("date_&_time","account_activation_code","account_activation_status","id_verification_video_file_url","password"); foreach ($excluded_columns as $value2) { echo "Excluded Column: <b>$value2</b><br>"; } foreach ($columnArr as $value) { if($value != "$value2") { $label = str_replace("_"," ","$value"); $label = ucwords("$label"); //echo "<b>$label</b>: "; echo "$_SESSION[$value]";?><br><?php echo "<b>$label</b>: "; echo "${$value}";?><br><?php } } ?> PROBLEM: Columns from the excluded list still get displayed. Edited November 19, 2018 by phpsaneI 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? 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>"; }}}}}} ?> 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'] 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 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 ? "; 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? 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 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? |