PHP - Count() Question
I need to add a condition to a while() statement I have. It should run when $counter is less than the number of records in my $products array which has 5 products. I set $counter and $product_no to 0.
I need to use the count() function to get the number of records. Does that make sense? Do ya'll need more info. Any help is Greatly appreciated. Ive referenced and searched but cant figure out how to incorporate it. Im still learning. Peace, Skrappy Similar TutorialsHi All, The following code works but for some reason it is leaving 1 result off of each page when displaying results from DB. I assume there is something wrong with the count but I cannot find the problem. Thank you for any help. Code: [Select] <?php $page = $_GET['page']; $result2 = mysql_query("SELECT * FROM vendors WHERE vendor_type_id = '$vdrt' AND active ='Yes' ORDER BY RAND()"); $total_results = mysql_num_rows($result2); if (empty($total_results)) {echo "Sorry, but we do not have any results for that search at this time. <br/><br/><a href='index.php'>Please try your search again</a>";} else { $total_pages = ceil($total_results / $results_limit); // $results_limit is set to 7 in config file if (empty($page)) { $page = "1"; } $offset = ($page - 1) * $results_limit; $result = mysql_query("SELECT * FROM vendors WHERE vendor_type_id = '$vdrt' AND active ='Yes' LIMIT $offset, $results_limit") or die ("$DatabaseError"); $row = mysql_fetch_assoc($result); $vendor_type_id = $row['vendor_type_id']; $num = mysql_numrows($result); if ($num == 0) : print "<br><br><p>Sorry, no vendors could be found.</p><br><br>"; elseif ($num > 0) : echo "Page - "; if ($page != 1) { echo "<a href=$PHP_SELF?city=Orlando&state=FL&vdrt=$vendor_type_id&page=1><< First</a> "; $prevpage = $page - 1; } if ($page == $total_pages) { $to = $total_pages; } elseif ($page == $total_pages-1) { $to = $page+1; } elseif ($page == $total_pages-2) { $to = $page+2; } else { $to = $page+3; } if ($page == 1 || $page == 2 || $page == 3) { $from = 1; } else { $from = $page-3; } for ($i = $from; $i <= $to; $i++) { if ($i == $total_results) $to=$total_results; if ($i != $page) { echo "<a href=$PHP_SELF?city=Orlando&state=FL&vdrt=$vendor_type_id&showold=yes&page=$i>$i</a>"; } else { echo "<b><span class='current-page'>[$i]</span></b>"; } if ($i != $total_pages) echo " "; } if ($page != $total_pages) { $nextpage = $page + 1; echo " <a href=$PHP_SELF?city=Orlando&state=FL&vdrt=$vendor_type_id&page=$total_pages>Last >></a>"; } if ($num == 0) { echo "<br><br>";} else {echo "<br><br>";} $i = 0; while ($i < $num) : while ($row = mysql_fetch_assoc($result)) { $vdrid = $row['vendor_id']; $vendor_name = $row['vendor_name']; $vendor_name_enc = urlencode($row['vendor_name']); $vendor_city = $row['vendor_city']; $vendor_state = $row['vendor_state']; $vendor_phone = $row['vendor_phone']; $vendor_overview = $row['vendor_overview']; // Trim overview if(strlen($vendor_overview) > 385) {$trimed_vendor_overview = substr($vendor_overview,0,385) . "..."; } else { $trimed_vendor_overview = $vendor_overview; } echo "<strong><a href='listing.php?$vendor_name_enc-$vendor_city-$vendor_state-&vdrid=$vdrid'>$vendor_name</a> </strong> - $vendor_city, $vendor_state $vendor_phone<br/>"; echo "$trimed_vendor_overview<br/><br/>"; } $i++; endwhile; endif; } ?> SELECT COUNT(*) obviously counts all records matching the WHERE clause, however... Field1 / Field2 1 / A / A 1 / B 1 / A I want to count all records where Field2 is "A", but if Field1 is set to "1" then that record counts as half. So above would return a result of 2 (one and two halfs = 2) I'm sure SQL can do this without the need to perfrom PHP calculations. Hope this makes sense. Thanks in advance. My table has 250 rows, but COUNT only seems to see 162 of them. In phpMyAdmin, for instance, there is a notice in the browse tab: "Showing rows 0-161 (162 total, Query took 0.0030 sec)". On one of my php pages, I get the same 162 result from this attempt to count the number of entries to my auto-incrementing "id" column :
$result = mysql_query('SELECT COUNT(id) AS id_count FROM MyTable)';Can anyone suggest the kinds of things I should check/adjust to understand why about 90 rows are not being counted? Many thanks in advance for any help anyone can offer. Hi guys, Sorry - I know this is a basic question but I've been out of the game for a year now and need to jog my memory. I am trying to count the number of rows per group. e.g: $sql = "SELECT *, COUNT(id) FROM image GROUP BY city"; $result = mysql_query($sql); $row = mysql_fetch_assoc($result); echo $row['COUNT(id)']; So if there are, for example, 5 New York's in the table, 3 London's and 2 Sydney's I want the echo to display 5, 3, 2 Should I use a loop for this? Cheers Hi guys, I need your help. I am trying to insert the rows in the mysql database as I input the values in the url bar which it would be like this: Code: [Select] www.mysite.com/testupdate.php?user=tester&pass=test&user1=tester&email=me@shitmail.com&ip=myisp However i have got a error which i don't know how to fix it. Error: Column count doesn't match value count at row 1 <?php session_start(); define('DB_HOST', 'localhost'); define('DB_USER', 'mydbusername'); define('DB_PASSWORD', 'mydbpassword'); define('DB_DATABASE', 'mydbname'); $errmsg_arr = array(); $errflag = false; $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } function clean($var){ return mysql_real_escape_string(strip_tags($var)); } $username = clean($_GET['user']); $password = clean($_GET['pass']); $adduser = clean($_GET['user1']); $email = clean($_GET['email']); $IP = clean($_GET['ip']); if($username == '') { $errmsg_arr[] = 'username is missing'; $errflag = true; } if($password == '') { $errmsg_arr[] = 'PASSWORD is missing'; $errflag = true; } if($errflag) { $_SESSION['ERRMSG_ARR'] = $errmsg_arr; echo implode('<br />',$errmsg_arr); } else { $sql = "INSERT INTO `members` (`username`,`email`,`IP`) VALUES ('$adduser','$email','$IP')"; if (!mysql_query($sql,$link)) { die('Error: ' . mysql_error()); } echo "The information have been updated."; } ?> Here's the name of the columns i have got in my database: Code: [Select] username IP I have input the correct columns names, so I can't correct the problem I am getting. Please can you help? This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=331562.0 Why am I getting this error when there are 3 Fields with 3 values? Column count doesn't match value count at row 1 Code: [Select] $sql5="INSERT INTO participants (participant_name, team_no, sport) VALUES ('".implode("','$_POST[team]'),('",$_POST['participant_name'])."','$_POST[team]','$sport')"; Anyone can help me? <?php $tbl_name="menu"; $kategorije = mysql_query("SELECT * FROM $tbl_name WHERE Left(menu_name, 1) BETWEEN 'A' AND 'M' ORDER BY menu_name ASC"); if (!$kategorije) { die("Database query failed: " . mysql_error()); } while ($row=mysql_fetch_array( $kategorije )) { echo "<div id=lista-header><h4>{$row["menu_name"]}</h4></div>"; $id_sub=$row['id_menu']; $podkategorije = mysql_query("SELECT * FROM submenu WHERE id_menu=$id_sub ORDER BY sub_name ASC", $connection); if (!$podkategorije) { die("Database query failed: " . mysql_error()); } echo "<ul class=\"pod\">"; while ($pod=mysql_fetch_array( $podkategorije )) { echo "<li><a href=index.php?=podkate?".$pod["id_sub"]." class=black>{$pod["sub_name"]}</a><hr size=1 align=left width=100px color=#cccccc></li>"; } echo "</ul>"; } ?> In this way, I get list with categories and hes subcategories. How to count how many subcategories have each categories, and count how many articles have each categories? Example (I wanna get this kind of look): Categories name (3) subcategoriesname (2) subcategoriesname (4) subcategoriesname (7) Categories name (5) subcategoriesname (1) subcategoriesname (14) subcategoriesname (9) subcategoriesname (2) subcategoriesname ( Categories name (2) subcategoriesname (28) subcategoriesname (17) Where the numbers represent how many categories and sub-items have articles Hello,
I am trying to get the report of my sales table. I want to get total number of leads for each month. And then i have to use this data to create graph using google graph.
But i am not getting how eactly i can get this.
Here is my code
<?php if(isset($_POST['submit'])) { $type = $_POST['type']; $sql="select * from leads where lead_customer='".$type."'"; $query=mysql_query($sql); while ($row = mysql_fetch_array($query)) { list($year,$month,$day)=explode("-", $row['last_modified']); $l = $row['last_modified']; $count=mysql_num_rows($month); echo $count; $myurl[] = "['".$month."', ".$count."]"; } print_r($myurl); echo implode(",", $myurl); } ?>But for $count, it doesn't show any values. below is my database. Capture.PNG 18.16KB 0 downloads Please suggest me returns -30. how can i remove the minus so it just returns 30 Code: [Select] $days = (strtotime(date("Y-m-d")) - strtotime($info['expiredate'])) / (60 * 60 * 24) echo $days Hey All, I need to count and display the number of rows I have. <?php //declare the SQL statement that will query the database $query = " SELECT COUNT(id) FROM connectvisits WHERE staffid = '$staffid' "; //execute the SQL query and return records $result = mssql_query($query); $thismonth = mssql_num_rows($result); echo "Total Meetings This Month: "; echo $thismonth; echo "<br />"; ?> For some reason I keep getting a result of 1. Everything else I try gives me a "Resource ID" number. Any help would be apprecaited. Maybe I have the completely wrong code. Thanks! Hello, I'm using the following code to count the amount of SQL results and divide it. Works great as is but I want to alter it so it works with categories. $amount = mysql_query('SELECT COUNT(`car_id`) AS num FROM `tbl_cars`') or die(mysql_error()); $amount_row = mysql_fetch_assoc($amount); I tried adding... WHERE car_cat = '".$cat."' which causes an error. Why doesn't this work, and how can I get the amount of results as a variable? hey i'm just woundering how i would go abouts on echoing unread messages sent to a user i want to beable to see how many unread messages i have Hi, I'm new to the forum, I have a slight issue with my script. Basically, I need to figure out a script that will allow me to do the following: - The user defines x amount of ours to count down (ie. 16 hours, 8 hours, 4 hours) - The script then starts the count down of the x hours - I need the script to count between 08:00 - 20:00 after 20:00 it needs to stop, then start again at 08:00 - The script also need to stop throughout the weekend (ie counts until 20:00 on friday, stops saturday & sunday, then starts 08:00 on monday) Can this be done? If so, any pointers or help? Many thanks, Lewis. $count = 0; foreach($myArray as $date => $values){ if($count >= 20) break; echo 'blablabla' ; $count++;} i am using $date as a key, and i want to start counter if $date is bigger then 20 php operates in order so i guess i have to put something before $count command so it must be something like this : if ($date >=20) {$count = 0;} foreach($myArray as $date => $values){ if($count >= 20) break; echo 'blablabla' ; $count++;} all suggestions are welcome. I am trying to echo the count for blog posts that have not been review yet, but I get this error. Notice: Undefined index: status but I feel like I have defined my status in my query. Plus my query is working as I have checked it in phpmyadmin Here is my function: Code: [Select] function unapproved_post_counter() { $sql = "SELECT COUNT(`approved`) AS `status` FROM `blog_posts` WHERE `approved` = 0 "; $result = mysql_query($sql); $data = array(); while (($row = mysql_fetch_assoc($result)) !== false) { $data[] = $row; } return $data; } Here is how I am echoing it. Code: [Select] $count = unapproved_post_counter(); <h1>Blog<span class="blogadds f_right"><?php echo $count['status']; ?> New Articles</span> print_r($count) tells me this... Array ( => Array ( [status] => 2 ) ) $count=0; $numb=50; foreach ($sepkeys as $dbkey) { for ($page=10;$page<=$numb;$page=$page + 10) { // the if block $count=$count+1; } } I am trying to maintain a separate a count for each key number in the above code. Eg: key- 574, it searches from pages 10-50 and increments the count by 1. The problem that I have is the count is continuous. After searching for the first key and moves on to the next key and them I need the count to start from the beginning rather than being continuous. Eg: key-874 : count = 22, in my case the next key 875 : the count is 23 I need to make it 1. I removed the if block and several lines because the code is too long. Can someone please suggest me a way how to do it Hi fellas, having a bit of trouble getting a count from the database where the field is not empty? $count= "SELECT COUNT(unid) FROM table"; $result2 = mysql_query($count) or die("Select Failed!"); $counter = mysql_fetch_array($result2); echo $counter[0]; From what i have read, this should get all the not null results from the field, but it gets all of them. Some are empty, the ones i want have various id numbers in them. Is there a way to get just the numbers where there is an id number present? I need some help. I have this script that pulls timestamps from the database. What I want to do is create a count of how many of those timestamps that were returned were from the morning(6am-12pm), Afternoon (12pm-6pm), Evening (6pm-12am), or Night (12am-6am). How do I do I parse the timestamps to do that and display the count. Thanks! Code: [Select] <?php /** * @author William Morris * @copyright 2010 */ include('inc/db.php'); //function to show date and time based on timestamp function showTIME($timestamp){ $date = date('F j Y h:i:s A', $timestamp); return $date; } $startdate = "2010-07-01"; $enddate = "2010-08-05"; //query to pull time stamps in between dates //convert dates into timestamp //$startdate = strtotime($_POST['startdate']); //$enddate = strtotime($_POST['enddate']); //convert dates into morning timestamp $startdate1 = strtotime($startdate."06:00:00"); $enddate1 = strtotime($enddate."11:59:59"); $sql = mysqli_query($conn, "SELECT event_time FROM event WHERE event = 'Registration' AND event_time BETWEEN '$startdate1' AND '$enddate1'"); while($row = mysqli_fetch_array($sql)){ echo showTIME($row['event_time'])."<br>"; } ?> |