PHP - Comparing Row Values
Hi. I have a MySQL database with 4 different fields. id, customer, case and status.
No problems of getting them to show up on my page but.. I would like to change the row bgcolor depending of field value on "customer". For example row color would be white until customer changes, then color yellow until customer changes again and back to white etc.. Hope you get the point. Is this possible? Thanks. Similar Tutorialsi've google but no luck on finding what i need....is there a way in javascript to compare 2 variables ie.
var him = "jack"; var her = "jill"; if (him == her){ alert('same name'); }thank you Hi im not sure if this can be done or not but im trying to do a site without using mysql and i want to be able to compare 3 values and depending on the values have them aranged lowest to highest... for example: Apple = 8 Pear = 3 Bannana = 5 so the results would be displayed like... Pear with a total of 3 bannana with a total of 5 Apple with a total of 8 Is this possible using just PHP or will i need to use Mysql as well... Thank you Chris Hi there i have 2 queries one for each table that calculates values in one or more fields. The two tables are different in structure so i have had to calculate them differently: | Ship table | Class |Planet table | Class 1 Class 2 Class 3 Class 4 Depending on what type of ship is added (Class 1-4 ship) its corresponding Class is determined. The planet table is different though as it can have any number of Class 1 - 4 ships. So with help ive put the Ship query results into an array and echoed the results. The Planet query is not in an array as such as i could acheive the same results using the SUM() function. Both work fine and the outputted results look like this: Ship query 1 Class 1 4 Class 2 3 Class 3 1 Class 4 Planet query 1 Class 1 2 Class 2 4 Class 3 13 Class 4 What i really need to do now is compare each queries Class and if the planets class 1-4 < ships class 1-4 then echo the difference. For example the results would be: Class 1 = 0 echo no Class 1 needed Class 2 = - 2 echo no Class 2 needed Class 3 = +1 echo 1 Class 3 needed Class 4 = +12 echo 12 class 4 needed Im sorry if it seems longwinded or if ive not explained my self very well but im finding it difficult. Heres the code: Code: [Select] <?php $colname_resultp = "-1"; if (isset($_SESSION['MM_Username'])) { $colname_resultp = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']); } mysql_select_db($database_swb, $swb); $query_resultp = sprintf("SELECT SUM(Class1) As Class1_totla, SUM(Class2) As Class2_totla, SUM(Class3) As Class3_totla, SUM(Class4) As Class4_totla FROM planet WHERE PlayerName = %s", GetSQLValueString($colname_resultp, "text")); $resultp = mysql_query($query_resultp, $swb) or die(mysql_error()); $row_resultp = mysql_fetch_assoc($resultp); $totalRows_resultp = mysql_num_rows($resultp); $colname_Recordset1 = "-1"; if (isset($_SESSION['MM_Username'])) { $colname_Recordset1 = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']); } mysql_select_db($database_swb, $swb); $query = sprintf("SELECT Class FROM ships WHERE PlayerName = %s ORDER BY Class ASC", GetSQLValueString($colname_Recordset1, "text")); $result = mysql_query($query, $swb) or die(mysql_error()); $number_of_results = array(); while($row = mysql_fetch_array($result)){ if (isset($number_of_results[$row['Class']])) { $number_of_results[$row['Class']]++; } else { $number_of_results[$row['Class']] = 1; } } foreach ($number_of_results as $class => $number_of_class) { echo $number_of_class . " Class " . $class; echo "<br />"; } <?php do { ?> <?php echo $row_resultp['Class1_totla']; echo ' Class 1'; echo'<br>'; echo $row_resultp['Class2_totla']; echo ' Class 2'; echo'<br>'; echo $row_resultp['Class3_totla']; echo ' Class 3'; echo'<br>'; echo $row_resultp['Class4_totla']; echo ' Class 4'; ?> <?php } while ($row_resultp = mysql_fetch_assoc($resultp)); mysql_free_result($result); mysql_free_result($resultp); ?> If you could please help that would be ace. Thanks for listening. Hi guys,
I wonder if you can offer any advice?
I have two user database tables that are about 70% identical. The problem is that with one of them, the phone number for each user has been cut short by two digits (it was an error on my part early on in the database design).
What I need to do is to get the values from both tables and put them into separate arrays then compare the arrays in a loop. If, say, the first five digits of the phone number in each row of the first table match one entry in the second table then update the second table with the phone number (the full length one).
I'm about puzzled about how to approach this, I've still got a lot to learn about array functions, it seems like it should be fairly simple but I'm a bit lost as to where to begin.
Any advice would be massively appreciated!
hey guys, i have my code set up to run substrings through a db to check for matches...even when there is a match it wont insert correctly Code: [Select] $result = mysql_query("select * from friends where userid=$userid"); while($row = mysql_fetch_assoc($result)) { $first_name = $row['first_name']; $last_name = $row['last_name']; $strpos_1 = strpos($friend_1, ' '); $substr_1 = substr($friend_1, 0, $strpos_1); $substr_2 = substr($friend_1, $strpos_1); if($substr_1 == $first_name && $substr_2 == $last_name) { $sql = "insert into fav_friends values('', '$userid', '$friend_1', '$friend_2', '$friend_3', '$friend_4', '$friend_5', '$friend_6')"; $query = mysql_query($sql); } } and i don't receive any errors, any thoughts? This has kept me busy for a couple of hours. I have two arrays that I would like to compare against each other to see matches and otherwise. Simple enough, right? foreach ($array1 as $value){ if (in_array($value, $array2)) { echo $value."-FOUND<br />"; } else { echo $value."-NOT!<br />"; } } Using print_r I can CLEARLY see matches: Code: [Select] array1 = Array ( [0] => CHARLES [1] => TOM [2] => DICK [3] => HARRY) array2 = Array ( [0] => HARRY [1] => DICK [2] => TOM [3] => CHARLES) The only difference I can see is the keys - but why should that matter for what I'm trying to do? Both arrays were converted to uppercase. I have done variations of search_array, array_intersect and array_unique - but absolutely nothing is working. 3 hours later and I'm no further then I started off. WTF? Hi, The main idea is that I have a client running on certain desktops. Each 5th minute the client sends and update to the web-server, increasing "tick" by one. Tick start out as 0, so after 5 minutes ticket should be greater than 5. If tick is not greater than 0 then do <code omitted> I Guess I would need some kind of cronjob to complete this, but i'd love to avoid sessions & cookies at all costs, as the same server is checking the same tick but for different accounts.. Any suggestions are highly appreciated ^.^ im having problem comparing timestamps in my where clause for some strange reason...my query works perfect but when adding WHERE it doesn't bring up a result although it should...I've been playing about with it for almost a day now...and it doesn't make sense
unix_timestamp values start_date_time 1406411505 end_date_time 1407275505 now 1406461573my query SELECT i.item_id, i.title, i.price, @start_date_time := CONVERT_TZ(i.start_date_time, '+00:00', u.time_zone), @end_date_time := DATE_ADD(@start_date_time, INTERVAL 10 DAY), @end_date_time AS `end_date_time` FROM items i LEFT JOIN sub_categories sc ON sc.sub_category_id = i.sub_category_id LEFT JOIN categories c ON c.category_id = sc.category_id JOIN users u ON username = 'Destramic' WHERE UNIX_TIMESTAMP(@start_date_time) < UNIX_TIMESTAMP(NOW()) AND UNIX_TIMESTAMP(@end_date_time) >= UNIX_TIMESTAMP(NOW())if anyone can diagnose my problem I'd it would be much appreciated I am writing a program which checks the results for a student. If the results are same as the ones inside the table, then correct message is printed. If the results are not the same then the right answer should be printed as well as wrong message and the answer the user selected. I am using checkbox to let the user choose the answers and this is saved on to the database. I need to retrieve this and compare this with the original table. my tables are Answer: ,Ans_ID,Ans_Answer1, Ans_Answer2, Ans_Answer3, Ans_Answer4,Que_ID Question:Que_ID, Que_Answer1, Que_Answer2, Que_Answer3, Que_Answer4 Answer table is what the user enters and the question table is the one with the correct answer. Code: [Select] SELECT Ans_ID, IF(answer.Ans_Answer1 = question.Que_Answer1 AND answer.Ans_Answer1 = question.Que_Answer1 AND answer.Ans_Answer1 = question.Que_Answer1 AND answer.Ans_Answer1 = question.Que_Answer1, "Correct","Wrong") AS result FROM answer LEFT JOIN question ON answer_id = question_id can someone give me ideas, not asking for you to write the code for me. just help me out as I am stuckk Hello
I have two arrays of mysql values $a1, $b1
I am trying to loop through each $a value, no problem,
but I want to see if each element in $a = one of the values in $b
so:
$a = array(1, 2, 3, 4, 5, 6); $b = array(4, 6); $y = ''; foreach ($a1 as $a){ foreach ($b1 as $b){ if ($a[val] == $b[val]){ $y = '*'; } } echo $y.$a.'<br />'; }I was kinna hoping it would give me 1 2 3 *4 5 *6 Its most definitely not though... Im sure Im flawed in my logic but have coded so much other crap I just can't see the answer! ty for the look/help. Hello I am having a problem getting the difference between two arrays. The first array $list has all codes that can be linked to a specific id The second array $exist has the values select and linked to a specfic id. Both arrays are storing integers array $list should have 110,111,112,113,114,115. Array $exist gets 110, 114 115 from the database the difference should be 111 & 112 I think the problem is with array $exist that is getting its values from the database. Could the problem be that the arrys are storing data differently? How do I resolve this problem? /**==========ARRAY 1=======**/ /**All values for this type in array for comparison against**/ Code: [Select] $list = array('110','111','112','113','114','115'); /**view values in array $list**/ Code: [Select] var_dump($list); array(6) { [0]=> string(3) "110" [1]=> string(3) "111" [2]=> string(3) "112" [3]=> string(3) "113" [4]=> string(3) "114" [5]=> string(3) "115" } /**==========ARRAY 2=======**/ /**Get stored types for specific id **/ Code: [Select] $exist = array();//create array //store values in array $query = "SELECT type FROM contact WHERE id ='$id' AND deleted = 'N' ORDER BY type"; $result = mysqli_query ($mysqli, $query); while($row = mysqli_fetch_array($result)) { $exist[] = $row; } /**View values in array for specific id in array $exist**/ Code: [Select] var_dump($exist); array(3){ [0]=>array(2){ [0]=>string(3) "110" ["contact_type"]=> string(3) "110"} [1]=> array(2){[0]=> string(3) "114" ["contact_type"]=> string(3) "114"} [2]=> array(2){ [0]=> string(3) "115"["contact_type"]=> string(3) "115"}} /**==========RESULT=======**/ /**Get the difference between all possible type and stored values linked to a specific id **/ Code: [Select] $difference = array_diff($list, $exist); /**viewthe difference between the two arrays**/ Code: [Select] var_dump($difference); array(6) { [0]=> string(3) "110" [1]=> string(3) "111" [2]=> string(3) "112" [3]=> string(3) "113" [4]=> string(3) "114" [5]=> string(3) "115" } Hey I have a quick question. I am trying to have a part of this PHP script compare two numbers. There is a randomly generated two-digit number named $random and There is another two digit number named $number. I want to make it so the PHP checks to see if $number has any of the same charcters as $random. I don't know how to do this though. I suspect that somehow both numbers need to be broken apart somehow. Any advice would be appreciated. Let me know if I should explain it more clearly Thanks a ton! +-----------+----------------------------------+ | username | password | +-----------+----------------------------------+ | user | Hash | +-----------+----------------------------------+ This is what I have set up in my database(obviously I'm not going to put the real user or hash). I'm trying to md5 what the user inputs for a password and compare it to the database, but I keep getting failed login attempts. Code: [Select] <?php error_reporting(-1); ini_set('display_errors', 1); $host = "localhost"; $dbuser = "****"; $pass = "****"; $databasename = "login"; mysql_connect($host,$dbuser,$pass)or die('Could not connect: '.mysql_error()); mysql_select_db($databasename) or die(mysql_error()); if (isset($_POST["user"]) && isset($_POST["pwd"])) { $admin = mysql_query("SELECT username FROM members"); $pass = mysql_query("SELECT password FROM members"); if ($_POST["user"] == $admin && md5($_POST["pwd"]) == $pass) { session_register("admin"); session_register("pass"); header('Location:https://deadnode.com/output.php'); } else { echo"<center><h1>Failed login attempt.</h1></center>"; } } ?> How in PHP am i able to select a date from a table and compare that date with a period of 2 months? In sql we use something like Quote select @2_months_later_date =dateadd(mm,2,getdate()) how would that be done in php? Hello everyone, I'm currently working on a large inventory management screen; the website has multiple "distributors" with their own inventory of products. Each distributor has the potential to carry the entire catalog of SKU's so right now, I have the stock/sku/dealer ID/product ID in one table with the product data/ID in another and am only inserting new rows if they are carrying the product. However they want to manage all of the inventory at once so I need to determine which IDs are update queries and which are insert queries. I'm trying to achieve this by comparing an array of the submitted data against an array of their existing inventory. If they don't have an entry for the submitted data key, we need to insert a new row for it to track their inventory. I just can't figure out how to get the keys to line up. Here is what I have; I tried to put it all into obviously defined variables for this forum. Code: (php) [Select] // assign our submitted values to an array $submitted_data = $_POST; // remove the SKUs with 0 or no inventory submitted $submitted_data = array_filter($submitted_data); // pull the distributor id out of the array before the insertion loop $distributors_inventory_distributor_ID = array_pop($submitted_data); // pull all existing inventory for this distributor to see if we are adding new inventory // or updating old inventory $existing_Inventory_Result = mysql_query("SELECT distributors_inventory_product_ID FROM distributors_inventory WHERE distributors_inventory_distributor_ID = $distributors_inventory_distributor_ID ORDER BY distributors_inventory_product_ID ASC", $db); // verify there is a result $existing_Inventory_num_results = mysql_num_rows($existing_Inventory_Result); if ($existing_Inventory_num_results > 0){ while($existing_Inventory_row = mysql_fetch_assoc($existing_Inventory_Result)){ // put existing inventory into an array $existing_Inventory[] = $existing_Inventory_row['distributors_inventory_product_ID']; } } $update_Inventory_Array = array_diff($submitted_data, $existing_Inventory); // print the array [DEBUG ONLY] echo '<h1>$update_Inventory_Array:</h1>'; print_r($update_Inventory_Array); echo '<hr/>'; echo '<h1>$submitted_data:</h1>'; print_r($submitted_data); echo '<hr/>'; echo '<h1>$existing_Inventory:</h1>'; print_r($existing_Inventory); That is outputting this: Quote $update_Inventory_Array: Array ( [963] => 5 [979] => 2 [982] => 2 [974] => 1 [32] => 5 ) $submitted_data: Array ( [963] => 5 [979] => 2 [982] => 2 [974] => 1 [32] => 5 ) $existing_Inventory: Array ( => 32 [1] => 963 [2] => 974 [3] => 979 ) I'd like it to be: Quote $update_Inventory_Array: Array ( [963] => 5 [979] => 2 [974] => 1 [32] => 5 ) $submitted_data: Array ( [982] => 2 ) $existing_Inventory: Array ( [32] => 5 [963] => 1 [974] => 1 [979] => 2 ) Can anyone suggest how to do this? I think if I could make the existing_Inventory array line up with my submitted_data array I could sort the rest out but I'm not sure how to get the keys/data to line up properly. Well I'm hoping someone will have some fun with this. My database has a list of times for a given date, and a schedule for each time. I'm trying to change the input backgrounds for rows between 2 different times. Like this: 8:00 enter some data 8:10 enter some data 8:20 enter some data etc. What I want is to change the background color of any input field between say 9:00 and 10:00, but leave everything else white. I can change the color, just can't figure out how to compare the times. Here's part of the code: Code: [Select] while($row = mysql_fetch_array( $result )) { $opentimeID = date("g:i",strtotime(1530)); //not sure if these statements are correct. $closetimeID = date("g:i",strtotime(1730)); if (strtotime($row['Time']) //...not sure how to finish this statement to compare the time in the database row to $opentime and $closetime. { echo "<tr><td>"; echo date("g:i ", strtotime($row[Time])); echo "</td><td>"; echo "<input type='text' size='20' value= '$row[Player1]' name='player1[$i]' style ='background-color: #FFFF66;'>"; echo "</td><td>"; echo "<input type='text' size='20' value= '$row[Player2]' name='player2[$i]' style ='background-color: #FFFF66;'>"; echo "</td><td>"; echo "<input type='text' size='20' value= '$row[Player3]' name='player3[$i]' style ='background-color: #FFFF66;'>"; echo "</td><td>"; echo "<input type='text' size='20' value= '$row[Player4]' name='player4[$i]' style ='background-color: #FFFF66;'>"; echo "</td><td>"; echo "<input type='text' size='20' value= '$row[Player5]' name='player5[$i]' style ='background-color: #FFFF66;'>"; } else { echo "<tr>"; echo "<td>"; echo date("g:i ", strtotime($row[Time])); echo "</td><td>"; echo "<input type='text' size='20' value= '$row[Player1]' name='player1[$i]'>"; echo "</td><td>"; echo "<input type='text' size='20' value= '$row[Player2]' name='player2[$i]'>"; echo "</td><td>"; echo "<input type='text' size='20' value= '$row[Player3]' name='player3[$i]'>"; echo "</td><td>"; echo "<input type='text' size='20' value= '$row[Player4]' name='player4[$i]'>"; echo "</td><td>"; echo "<input type='text' size='20' value= '$row[Player5]' name='player5[$i]'>"; } Thanks for any advice! Hey guys, im having a slight issue with comparing mysql time stamp. I'm using the timestamp in mysql to show how many people are online with in 5 minutes. $sql = mysql_query("SELECT name,id FROM users WHERE DATE_SUB(NOW(), INTERVAL 5 MINUTE) <= last_active ORDER BY id ASC"); But I want to grab last_active from the data base and compare it like below. I dont know if strtotime just doesnt compare with mysql time stamp or what? if($last_active > strtotime('-5 minutes')) { echo "<td>Online</td>"; }else{ echo "<td>Offline</td>"; } Does anybody know how I would turn this into functioning code. if $_SERVER[REQUEST_TIME] is 15 minutes greater than $time_in_database; echo 'time is 15 minutes greater'; Thanks for any help hi i have to compare dates one from database and other from the loops ..if they match background color will change ... so far i have done very thing but the problem i am getting that in while loop i am getting one date to compare with all 365 days in the year ...which are coming from the loop.. i need help to solve this... <?php if (isset($emplistshow) && ($emplistshow == 'show')) { $monthNames = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); ?> <div class="outerbox" style=" float: left;"> <table colspan="5" rowspan="4"> <tr > <td width="18%" colspan="2"><strong>Month</strong></td> <td width="12%" colspan="2"><strong>Division</strong></td> <?php for($m=1;$m <=31;$m++) { ?> <td width="26" height="20px" align="center" valign="middle" ><?php echo $m; ?></td> <?php } ?> </tr> </table> </div> <div class="outerbox" style=" float: left;"> <table > <?php //$sql ="SELECT leave_date,leave_type_id,leave_request_it, FROM `hs_hr_leave` WHERE employee_id = $emplid "; //loop to print numbers if months in a given year for($Month=1;$Month <= 12;$Month++) { ?> <tr> <td width="18%" colspan="2"><strong><?php echo $monthNames[$Month-1]; ?></strong></td> <td width="12%" colspan="2"><strong>Division</strong></td> <?php $num = cal_days_in_month(CAL_GREGORIAN, $Month, $cYear); // 31 //$dates = date("$cYear-$Month-1"); $sql ="SELECT leave_date,leave_status,leave_comments,leave_type_id,emp_depment FROM `hs_hr_leave` WHERE employee_id = $emplid "; $res = mysql_query($sql); //loop to print numbers if days in a given month for($i=1;$i<=$num;$i++) { ///$dates = date("$cYear-$Month-$i"); $dates = date("Y-m-d", mktime(0, 0, 0, $Month, $i, $cYear)); $checkdate = strtotime($dates); //loop to print dates from database while ($row = mysql_fetch_array($res, MYSQL_NUM)) { //echo $time2=date("Y-n-j",strtotime ($row[0])).'<br/>'; //here you have to seprate the year month and day coming from db and pas it to mktime function as doing above ///like $dates = date("Y-n-d", mktime(0, 0, 0, $Month, $i, $cYear)); $status = $row[1]; $comments = $row[2]; $typeid = $row[3]; $depment = $row[4]; $dbD = date("d", strtotime($row[0])); $dbM = date("m", strtotime($row[0])); $dbY = date("Y", strtotime($row[0])); $dbuser = date("Y-m-d", mktime(0, 0, 0, $dbM, $dbD, $dbY)); $userDates = strtotime($dbuser); //echo $dbuser.'<br/>'; //echo $dates.'<br/>'; } //echo $dbuser.'<br/>'; //echo $checkdate ."==". $userDates.'<br/>'; if($checkdate == $userDates) { //echo $typeid ; $cSql = "SELECT leave_type_name,leave_color FROM hs_hr_leavetype WHERE leave_type_id = '$typeid' "; $cRes = mysql_query($cSql); while ($color = mysql_fetch_array($cRes, MYSQL_NUM)) { //echo $color[1]; //echo $color[0]; //echo $status."<br/>"; echo "<td width='26' title='".$comments."' height='20px' bgcolor='#".$color[1]."' align='center' valign='middle' >"; if($status == -1) { echo "R"; } elseif ($status == 0 ) { echo "C"; } elseif ($status == 1 ) { echo "PA"; } elseif ($status == 2 ) { echo "A"; } elseif ($status == 3 ) { echo "T"; } elseif ($status == 4 ) { echo "W"; } elseif ($status == 5 ) { echo "SH"; } echo "</td>"; } } else { echo "<td width='26' height='20px' align='center' valign='middle' >".$i."</td>"; } } } ?> </tr> </table> Hello (I think this is more of a php question than MySQL - sorry if I have got it wrong.) I need to know if items have been orphaned (because they lack a cat_id that actually exists). I have two MySQL tables that each have fields 'cat_id'. I have a Category table... cat_id / category 1 / fruit 2 / veg 3 / tools I have another product table cat_id / product 1 / apple 1 / Pear 2 / carrot 2 / cabbage 3 / screwdriver 1 / peach 8 / orphan I have entered the data into two arrays... $categories[] $products[] I have tried a number of combinations of foreach loops to try to determine if the products(cat_id) doesn't match the categories(cat_id) but I seem to be going around in circles. Is there perhaps a function that is available to do this? Or maybe just a straight thinking person that can point out my ineptitude? Thank you for any input. |