PHP - How To Compare Variable Against Another?
I run an article directory. I was wondering how I could check an article that is currently pending review against published articles in our database. What I am looking to do is to place the content of the body in the pending article into a variable. That much is easy and I know how to do that. Then I would like to use that variable to compare against other content in our database and have it output a percentage of how close it is to any other article already published in our database. Any direction our guidance with this would be so greatly appreciated.
Thanks! Similar TutorialsI typically only use the first two solutions. Is there anything wrong with the third? Is there any more streamlined way to do it?
function f1($x){return $x*1;} function f2($x){return $x*1;} $x=2;$y=1; if(f1($x)>f2($y)){echo(f2($y)."\n");} // or $f2_y=f2($y); if(f1($x)>$f2_y){echo($f2_y."\n");} // or if(f1($x)>$f2_y=f2($y)){echo($f2_y."\n");} Hi again everyone, I am trying to compare a php variable to the value of a row in mysql, but keep getting the following error: Code: [Select] Wrong perameter count for mysql_result() What syntax would I use to accomplish this? Here is my code: // connects to server and selects database. include ("../includes/dbconnect.inc.php"); // table name $table_name = "availability"; // split up date into 3 separate fields $date = "12/25/2010"; $date_split = explode("/", $date); $month = $date_split[0]; $day = $date_split[1]; $year = $date_split[2]; // check if earth room is already reserved $taken = "Reserved"; $sql = "SELECT earth_room FROM $table_name WHERE month='$month' AND day='$day' AND year='$year' LIMIT 1"; $result = mysql_query($sql) or trigger_error("A mysql error has occurred!"); $row = mysql_result($result); if($row == $taken){ echo "Room Already Reserved"; } else{ echo "Room Available"; } Thanks for the help, kaiman Hi, I want to compare the tables from 2 mysql databases in a table. This is what I have: for($i=0;$i<$maxCount;$i++) { echo "<tr>"; // start row //fist data square if ($i < count($db1)) echo "<td>{$db1[$i]}</td>"; else echo "<td></td>"; echo "<td></td>"; // middle //last if ($i < count($db2)) echo "<td>{$db2[$i]}</td>"; else echo "<td></td>"; echo "</tr>"; // end row } that will put make a nice table for me to see. What I'd like to do next is have all identical sql tables lined up. So for e.g I have 2 arrays of table names that will look like this: Databse 1 Database 2 table1 table1 table2 table3 table4 table4 does that make sense? Also is there a more elegant way of scripting what I have? Can php be used to get two blog entries and compare them side by side, with a choice one which one to use. A bit like wikipedia has ? Not sure if there is a better way to do this, but I want to check whether the value in a Sticky Field is the same as what is in the Database. If what is in the Sticky Field is *exactly* what is in the Database, then the User didn't update that field, and so I would NOT do an UPDATE. Seem reasonable? Here is where I grab the value out of the Form... Code: [Select] // **************************** // Process Each Form Field. * // **************************** foreach($_POST['answerArray'] as $questionID => $response){ // Copy trimmed Form array to PHP array. $answerArray[$questionID] = trim($response); And here is where I grab the existing value in the Database... Code: [Select] // ******************** // Check for Answer. * // ******************** // Build query. $q1 = "SELECT response FROM bio_answer WHERE member_id=? AND question_id=?"; // Prepare statement. $stmt1 = mysqli_prepare($dbc, $q1); // Bind variable to query. mysqli_stmt_bind_param($stmt1, 'ii', $memberID, $questionID); // Execute query. mysqli_stmt_execute($stmt1); // Store results. mysqli_stmt_store_result($stmt1); // Check # of Records Returned. if (mysqli_stmt_num_rows($stmt1)==1){ // Answer Found. // Bind result-set to variable. mysqli_stmt_bind_result($stmt1, $currResponse); // Fetch record. mysqli_stmt_fetch($stmt1); So I guess I want to compare $answerArray[$questionID] against $currResponse and see if there are any differences?! What is the best way to do that? Any better ideas of how to check to see if the User made any changes to a field so I only do an UPDATE if there was actually a change? Thanks, Debbie Hi everyone! I've been looking for a solution, but i just can't find it anywhere... Ok, so the problem is: i have a database which looks something like this: user_id product_id score 14 . 235 . 79 23 . 235 . 32 53 . 665 . 21 14 . 235 . 90 5 . 675 . 45 This is step-by-step of what i need to do: 1. Select user - user_id =343 2. For user_id =343 select all products that he gave score to: product_id = 43 - score = 99 product_id = 12 - score = 56 product_id = 68 - score = 32 product_id = 124 - score = 67 3. Find all users that voted for the same products e.g.: for product_id = 43: user_id = 125 voted 93 points, user_id = 23 voted 56 points. 4. Calculate the difference between the score of user, so: for product_id = 43: user_id = 125 voted 93 points (99 - 93 =6 points of diff.) user_id = 23 voted 56 points (99 - 56 =43 points of diff.) return the results.... That's basically what i need to do. I still don't know the number of the users, or product, but it doesn't really matter - i just need to make it work. Ok, hope someone can help! I have this code that compares date times but im not sure if its correct. if("2008-08-10 00:00:00.0" > strtotime("now")) { echo "here"; } The code echo's here, but it shouldn't because the time is less than the current time. Any ideas? Hi, I am trying to write an SQL to XML converter, so so far I have all the records inputted into the db tables, and now I want to append a child, but I don't know how to compare current row's field value with the next row below it, specifically I want to check if a current row is parent of the next row. I have already got the XML to SQL function working (some bugs but for the most part it's working) So for eg: <?php $query=mysql_query("SELECT * FROM edge"); while($row=mysql_fetch_assoc($query) ) { if($row['target'] is parent of the next row), then append the next row as child of current row } ?> Table edge is one of the tables that hold target node/source(aka: parent) nodes. Any help much appreciated! Hi! I have a table called "Categories" Here is the structu id | Categories | ParentCategories 1 | Rings | Parent Category 2 | Pendants | Parent Category 3 | Ladies Rings | Rings 4 | Ladies Pendants | Rings 5 | Gents Rings | Gents 6 | Necklaces | Parent Category But Necklaces is one of those who is not going to have any subcategories or you can say, it is going to be a category by itself. So how can I construct a php statement to get data out of MySQL to determine that Necklaces is a Category which does not have subcategories. Please help ! Thank you ! - Xeirus. I thought I was able to determine which DateInterval is greater the same as done with DateTime, however, either this is not true or I am doing something wrong. Please advise. Thanks! private function compareDateInterval(\DateInterval $dateInterval1, \DateInterval $dateInterval2):bool { $greaterThan = $dateInterval1 > $dateInterval2?'true':'false'; echo("test1: dateInterval1 > dateInterval2: $greaterThan\n"); $dateTimeNow = new \DateTimeImmutable; $dateTime1 = $dateTimeNow->add($dateInterval1); $dateTime2 = $dateTimeNow->add($dateInterval2); $greaterThan = $dateTime1 > $dateTime2?'true':'false'; echo("test2: dateTime1 > dateTime2: $greaterThan\n"); $timestampNow = $dateTimeNow->getTimestamp(); $timestamp1 = $dateTime1->getTimestamp(); $timestamp2 = $dateTime2->getTimestamp(); $greaterThan = $timestamp1 > $timestamp2?'true':'false'; echo("test3: timestamp1 > timestamp2: $greaterThan\n"); echo('$dateTimeNow => '.json_encode($dateTimeNow).PHP_EOL); echo('$dateInterval1 => '.json_encode($dateInterval1).PHP_EOL); echo('$dateInterval2 => '.json_encode($dateInterval2).PHP_EOL); echo("timestampNow: $timestampNow, timestamp1: $timestamp1, timestamp2: $timestamp2\n"); } <b>Warning</b>: Cannot compare DateInterval objects in <b>/var/www/app/src/Tools/PointTransitionHistory.php</b> on line <b>136</b><br /> test1: dateInterval1 > dateInterval2: false test2: dateTime1 > dateTime2: true test3: timestamp1 > timestamp2: true $dateTimeNow => {"date":"2020-08-23 15:06:24.454702","timezone_type":3,"timezone":"UTC"} $dateInterval1 => {"y":0,"m":0,"d":2,"h":14,"i":0,"s":0,"f":0,"weekday":0,"weekday_behavior":0,"first_last_day_of":0,"invert":1,"days":2,"special_type":0,"special_amount":0,"have_weekday_relative":0,"have_special_relative":0} $dateInterval2 => {"y":0,"m":0,"d":11,"h":15,"i":0,"s":0,"f":0,"weekday":0,"weekday_behavior":0,"first_last_day_of":0,"invert":1,"days":11,"special_type":0,"special_amount":0,"have_weekday_relative":0,"have_special_relative":0} timestampNow: 1598195184, timestamp1: 1597971984, timestamp2: 1597190784
Hi! What should the code be if I get an IP address to a postback url and I want to find that member in my database who has that IP-address? The postback comes like this http://yoursite.com/postback.php?ip=xxx Please help I know the forum is supposed to be used to help with any code I have come up with but here's to taking a shot anyway: I am in need of creating a php script that will allow me to input two different XML files and upon pressing "go" will compare these two files and save only the differences to a new xml file. Thing is, I have no idea where to start. Are there any php functions that exist that I can use to do this? If not, cn someone help me create one? Would this be better handled in javascript? If so, how can I do so using a method similar to the above? Any kind of help would be greatly appreciated. how to compare date with current date let say $date="2010-10-08 2:00 PM" how can i compare $date with current date and get the result in days, hours, minutes and seconds thanks I need to compare several arrays...to narrow down a bunch of mysql selects. I can't figure out which of the array_diff, array_unique, array_merge options there are. I need to get an array of umpires (baseball), then compare that to the array of umpires that can work a given level, then compare that to the array of umpires that can work at that school, and finally compare that to the array of umpires who have asked for the day off. In the code below, all of the selects work...the echo's that are commented out printed out what I expected them to. I'm having a problem with the array comparisons at the end of this code: Code: [Select] <?php //connect to database $dbc = mysql_pconnect($host, $username, $password); mysql_select_db($db,$dbc); //get info of game $sql = "SELECT sport,home,day FROM games WHERE `game_id` = $_GET[game]"; $rs = mysql_query($sql,$dbc); while($row = mysql_fetch_array($rs)) { $game_array = $row['sport']; $home_team_array = $row['home']; $game_day_array = $row['day']; } //list of all umpires in association $sql = "SELECT ump_id FROM ump_names WHERE '$_SESSION[association_id]' = `association_id`"; $rs = mysql_query($sql,$dbc); while($row = mysql_fetch_array($rs)) { $name_array[] = $row['ump_id']; // echo "<br /><br />All Ump ID's Array = "; // foreach($name_array as $name_array_final) { // echo $name_array_final . "<br />"; // } } //get bad schools list $sql = "SELECT ump_id FROM bad_school WHERE '$_SESSION[association_id]' = `association_id` AND `school` = '$home_team_array' and `sport` = '$game_array'"; $rs = mysql_query($sql,$dbc); while($row = mysql_fetch_array($rs)) { $bad_school_array[] = $row['ump_id']; // echo "<br /><br />Bad School Array = "; // foreach($bad_school_array as $bad_school_array_final) { // echo $bad_school_array_final . ", "; // } } //get official list at this game's level $sql = "SELECT ump_id FROM ump_names WHERE `association_id` = '$_SESSION[association_id]' AND `$game_array` = '1'"; $rs = mysql_query($sql,$dbc); while($row = mysql_fetch_array($rs)) { $official_level_array[] = $row['ump_id']; // echo "<br /><br />Officials that can work this level of game = "; // foreach($official_level_array as $official_level_array_final) { // echo $official_level_array_final . ", "; // } } //get vacation list $sql = "SELECT ump_id FROM days_off WHERE '$_SESSION[association_id]' = `association_id` AND `day` = '$game_day_array'"; $rs = mysql_query($sql,$dbc); while($row = mysql_fetch_array($rs)) { $days_off_array[] = $row['ump_id']; // echo "<br /><br />Officials that are on vacation this day = "; // foreach($days_off_array as $days_off_array_final) { // echo $days_off_array_final . ", "; // } } //$first_step = array_merge($official_level_array, $name_array); $first_step = array_merge($official_level_array, $name_array); $first_step_part_2=array_unique($first_step); //$second_step = array_merge($bad_school_array, $first_step); $second_step = array_merge($bad_school_array, $first_step_part_2); $second_step_part_2=array_unique($second_step); //$third_step = array_merge($days_off_array, $second_step); $third_step = array_merge($days_off_array, $second_step_part_2); $third_step_part_2=array_unique($third_step); echo "These umpires are available for this game:<br />"; foreach ($third_step as $iamavailable) { echo "$iamavailable<br />"; } ?> This prints out: Quote 2 1 2 4 5 6 10 14 15 3 7 8 9 11 12 13 16 17 It should only print out 4, 5, 6, 10, 14, 15. What I want to do is: compare $official_level_array and $name_array...only keep those that are in $official_level_array. Then, compare that result with $bad_school_array...removing those in the $bad_school_array...and keeping the ones that are left. Then, compare that result, with $days_off_array...removing those in the $days_off_array...and keeping the ones that are left. Print out what's left... Where am I going wrong? Let's say for example I have 3 ID's in ONE database row like this: name: values: page ids 17 23 32 And I have a list of page titles with the ID's of the pages inserted into the value field of a checkbox. [] page title 1 [] page title 2 [] page title 3 [] page title 4 [] page title 5 [] page title 6 What I Want to Do! I want to fetch the page ID's from the database and I want a way to see if there match any page ID's from the list with the page ID's from the database row. Based on that I want to run an if statement. How can I do that? The string of ID's from the database would be the haystack, and the ID's from the list would be the needles, and every time one ID from the haystack matches with an ID from the list then that page title should be automatically marked as checked with the help of an if statement. This is how the list gets listed, with a foreach loop: <?php $pages = get_pages(); foreach ($pages as $pagg) { $option = '<input type="checkbox" name="exclude[]" value="'. $pagg->ID .'">'; $option .= $pagg->post_title . "<br />"; echo $option; } ?> Now I need to compare the ID in the value field with the ID string (separated by spaces) in the database row, and when a match occurs then execute a case. I know there are functions like str_replace and similar, but I'm not sure if I can do that with them, what would be a fitting function for my case? I am building a site that needs to update member accounts on a weekly basis. At the moment I have a cron job set for 12:01 am on Monday, that resets five database fields for all of my members. The first field is the current week, the second field is the previous week, the third field is from two weeks ago, the fourth field is from three weeks ago, and the fifth field is from four weeks ago. Obviously the cron job resets the first field to 0, and the data from this field is moved to field two (last week), and so on and so forth, and the data from the fifth field is just removed altogether. However, this is really not preferred, because if/when I get 1000+ users, the cron job will take up a crapload of CPU as many mysql queries need to be executed for each account. So, I am trying to implement a script that checks the last login date for a user, and if they have have not logged in since before the latest Monday, the Monday before that, the Monday before that, or the Monday before that, etc. I have never really worked with dates, so I don't even know how to begin. The last user login is stored as a DATA datatype in a mysql database, and is in the following format: YYYY-MM-DD. Thanks in advance! Best method to for the following? Code: [Select] // The following $str1 and $str2 should say they are identical $str1 = "abcDEF"; $str2 = "EcFabD"; // The following $str1 and $str2 should say they are NOT identical because of the "a" "A" case difference. $str1 = "abcDEF"; $str2 = "EcFAbD"; I'm comparing bitvector strings. "ABC" is different than "abc". "abc" is the same as "cba". What's the best php function or method I should use? Thank you. Hello Guys, I am trying to figure this out.. I have a page that has several check boxes that when checked the values ends up as an array Like this 1, 3, 9, 6, That's what it looks like in the database. I want to compare the values (numbers) submitted with the values in my database table called benefits Here is the function I wrote this function and I want to check the values of the submitted values but I am just not sure how to compare the values, especially with a comma in the array. Code: [Select] $benefits2 = $_POST['benefits']; function check_benefits ($benefits2,$member_id,$description,$ip){ $queryb = mysql_query("SELECT * FROM benefits WHERE b_id = $benefits2"); while ($row = mysql_fetch_array($queryb)) { $benefit_list = $row['b_id']; } if (isset($benefits2)&&( $benefit_list!== $benefits2)) { Do something here! } } Thanks for your help! So what I'm wanting to do is log changes made by users, to the database... I was thinking on the lines of first doing a query of the database row that is going to be affected, prior to it being affected, putting that row result in an array variable, then allowing the update to happen and re-querying the database row and putting the results in a separate array, to then compare to the original database row array variable... So I guess, (A): Does this make sense to do this, this way? and (B): How do you compare 2 identical arrays, then extract the value from both where the particular element does not match, e.g. has been updated? |