PHP - Count Return On If Statement?
Is it possible to count how many returns there are on a if statement?
Where you can see Code: [Select] echo 'yes'; it will echo this twice according to my condition, all I want is the the value 2 that it has returned twice... ? Code: [Select] while($row = mysql_fetch_array($bookings)) { if(time() > $row['returnby'] && $row['returned']==0) { $books_overdue = count($row['returned']); } if(time() > $row['returnby'] && $row['returned']==1) { $returned_ontime = count($row['returned']); } if(time() <= $row['returnby']) { echo 'yes'; } } thanks for help. Similar TutorialsHi I'm using the below code <?php //Getting failed orders for Online $data=mysqli_query($mysqli,"SELECT Error, COUNT(*) as total FROM db WHERE Department='Online' AND Date BETWEEN '$Fromdate' AND '$Todate' GROUP BY Date"); ?> var totalOnline=[<?php while($info=mysqli_fetch_array($data)){ echo $info['total'].',';} /* ?>]; Now some of these doesn't return anything as it should, but is there a way to return 0 instead? $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 have a table with 5 records with the following "id_number" for each record in ASC order: 2, 6, 74, 86,87 There is one other field called "tag_number" and for each record in ASC order here is the data: 50670, 50077, 1234, 1235, 1236 I have a text field and if I search for a spastic "tag_number" I want to return the record but count and display what record like this: Record 3 of 5 The above 3 of 5 means I searched for "1234". I'm not sure what loop I need and how to count the records, any help would be great! Here is what I'm thinking: $id_number = 74; $result = db_query("select * from table order by id_number ASC"); $num_rows = mysql_num_rows($result); //this will provide how many records are in the table $result = db_query("select * from table where id_number = '$id_number' order by id_number ASC"); for ($i = 1; $i == $num_rows; $i = $i + 1) { if ($result['id_number'] == $id_number) { I'm trying to work out a count of how many of what assets are in a certain location: there should be 13 different categories (asset types) in a location and i want the individual count of each category. here's what i have - which seems to give me a total count of all assets and locations together plus one asset description and one location..... almost right. Code: [Select] $con = mysql_connect('localhost','root',''); if(! $con) die(mysql_error()); mysql_select_db("test",$con); $result = mysql_query("SELECT DISTINCT COUNT(`Asset Description`) AS TOTALAD, LOCATION, `Asset Description` AS AD FROM master WHERE LOCATION like 'NC%'"); while($row = mysql_fetch_array($result)) { echo $row["LOCATION"]; echo "<br />"; echo $row["TOTALAD"]; echo "<br />"; echo $row["AD"]; echo "<br />"; } mysql_close($con); 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 I'm having issues with the following: Code: [Select] <?php session_start(); $_SESSION['username']=$_POST['username']; $_SESSION['password']=$_POST['password']; if($_SESSION['username']=="username" && $_SESSION['password']=="password"){ if($_GET['product']=="add"){ $content.=' <p><label>Product Name:</label> <input type="text" name="product_name" size="30" /> <label>Product Price:</label> <input type="text" name="product_price" size="5" /> </p> <p><label>Product Category:</label> <input type="text" name="product_category" size="30" /></p> <p><label>Product Link:</label> <input type="text" name="product_link" size="30" /></p> <p><label>Product Image:</label> <input type="text" name="product_image" size="30" /></p> <p><label>Product Tag:</label> <input type="text" name="product_tag" size="30" /></p> <p><label>Product Keywords:</label> <input type="text" name="keyword" size="30" /></p> <p><label>Product Features:</label><br /> <textarea name="product_features" rows="10" cols="60"></textarea> </p> <p><label>Product Pros:</label><br /> <textarea name="product_pros" rows="5" cols="30"></textarea> </p> <p><label>Product Cons:</label><br /> <textarea name="product_cons" rows="5" cols="30"></textarea> </p> <p><label>Product Description:</label><br /> <textarea name="product_description" rows="10" cols="60"></textarea> </p> <p><label>Product Notes:</label><br /> <textarea name="product_notes" rows="5" cols="30"></textarea> </p> '; $logout='<div><a href="./acp_admincp.php?log-out">Log-Out</a></div>'; } elseif($_GET['product']=="view"){ } else{ $content.=' <a href="./admincp.php?product=add">Add New Product</a> <br /> <a href="./admincp.php?product=view">View Products</a> '; } } elseif(isset($_GET['log-out'])){ session_start(); session_unset(); session_destroy(); header("Location: ./admincp.php"); } else{ $content=' <form action="./admincp.php" method="post"> <p><label>Username:</label> <input type="text" name="username" size="30" />'; $content.='</p> <p><label>Password:</label> <input type="password" name="password" /></p>'; $content.='<p><input type="submit" value="Submit" name="Submit" /></p> </form>'; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <base href="http://ghosthuntersportal.com/" /> <title>Ghost Hunter's Portal - Admin Control Panel</title> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <meta name="verify-v1" content="" /> <meta name="keywords" content="ghost, hunters, hunter, ghosts, spirit, spirits, paranormal, investigation, investigator, investigators, k2, emf, meter, kii" /> <meta name="description" content="Ghost Hunters Potal. Parnormal research equipment store." /> <meta name="author" content="Andrew McCarrick" /> <meta name="robots" content="index, follow" <link rel="stylesheet" type="text/css" href="style.css" /> </head> <body> <img src="./logo.png" alt="Ghost Hunter's Portal Admin Control Panel" /> <br /> <div style="color: #AA44AA; font-size: 26px; margin-top: -30px; margin-left: 125px;">Admin Control Panel</div> <?php echo $logout; echo $content; ?> </body> </html> I can log-in, and get to the page with the two links on it. However, once I click one of the links it falls back to the log-in page, and it ends up being a never ending loop. It's doing this: Log-In --> Page with links ---> Log-In page again Should be doing this: Log-In --> Page with links --> Add Product page or View Products page I can never get into the the actual sub page. Just to be clear, the address bar actually shows product=add or product=view, but it still shows the log-in page. Hello Everyone, I have to change the if statements to a switch statement, in this game. Can anyone help? Thanks. var checkKeyPressed = function (e) { // Press key A or key D to make dog run to the left or right // The running speed is specified by the step variable, 10 by default. // If you replace 10 with a larger integer, the dog will run faster. //press A, dog runs left if (e.keyCode == "65") { if (prex[0] > 5) { prex[0] = prex[0] - step; } } //press D, dog runs right if (e.keyCode == "68") { if (prex[0] < right) { prex[0] = prex[0] + step; } } }; 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 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 First of all i am very weak about OOP and classes in php. I am learning now but i stuck with very silly problem. My codes are below. <?php class myTestClass { function __construct() { $this->OldName("This is Old Name"); } function OldName($VeryOld) { if($VeryOld == "This is Old Name") $this->NewName(); else $VeryOld = "Something Wrong"; return $VeryOld; } function NewName() { echo "This is Brand New Name"; } } $i = new myTestClass(); ?> if i send value to OldName "This is Old Name" then codes works fine. But if i send "This is Old Nameeee" then does not appear anything. what i want to do is print "Something Wrong" text if i enter different value. I have no idea what return do and how it do? How to retrieve/print "Something Wrong"? i can do that with echo but i want to transfer result of return to another function. Really appreciate for any help/idea. Hi Everyone,
My name is Raoul Grosser, live in The Netherlands.
I'm a front/backend developer working with AngularJS and Bootstrap and Symphone2.
I've been writing PHP code for over 4 years now my focus is on opensource projects.
If you have any question, please let me know!
return (int) $value VS return $value
What is the purpose of using int in a bracket? return (int) $value
Which is the best and safest method?
Thank you
Hey all. My site has a config database table that has a column showlogin to set whether or not login functionality is enabled. I'm playing around with MySQLi, Classes, and Returns. My return is always 1. Where am I going wrong? test.php <?php // Start Session session_start(); // Production Settings ini_set('display_errors', 1); mysqli_report(MYSQLI_REPORT_ALL); include 'db.php'; $mysqli = new mysqli($host, $user, $pass, $db); class styles { function showlogin($show) { global $mysqli; $select = $mysqli->query("SELECT showlogin FROM config LIMIT 1"); $value = $select->fetch_object(); if ($value->showlogin = 1) { return 1; } return 0; } } $login = new styles(); echo $login->showlogin($show); ?> |