PHP - How Do I Compare Strings?
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 Similar TutorialsHi I am trying to search an array that comes from a power shell script, the array of strings that returns is ever changing as it receives variables from the script. Therefore I need to use preg_grep to search for the word "not" I am also using another array which inverts this I then need to compare these new two arrays to the original and separate the results $working and not working into a table. I would be most grateful for any advice/solutions. The preg_grep aren't displaying anything.
<?php
//phpinfo();
ini_set('display_errors', 'On');
error_reporting(0);//E_ALL
echo '<html>
<style>
body
{
font-family:Calibri,Helvetica,sans-serif;
font-size:100%;
}
</style>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript">
function uploadJS(){
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ""); };
var textAreaValue=document.getElementById(\'ServerList\').value;
var trimmedTextAreaValue=textAreaValue.trim();
if(trimmedTextAreaValue!="") {
document.myForm.submit();
} else{
alert("Server List Text Area is Empty");
}
};
var input=document.getElementById(\'fileSelect\').value;
if(input!="") {
document.myForm.submit();
} else{
alert("You have not selected a file, please select one to proceed.");
}
var handleFileSelect = function(e) {
var files = e.target.files;
if(files.length === 1) {
document.forms.myForm.filecsv.value = files[0].name;
}
}
};
</script>
</head>';
#Upload Code
$target = "D:\Web\Upload/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). "
has been uploaded";
}
else {
}
if ($uploaded_size > 150000)
{ echo "Your file is too large.<br>";
$ok=0;
}
if ($uploaded_type =="text/php")
{
echo "No PHP files<br>";
$ok=0;
}
if (!($uploaded_type=="text/csv")) { echo "<br>";
$ok=0;
}
$target = "upload/"; $target = $target . basename( $_FILES['uploaded']['name']) ; $ok=1; //This is our size condition
if ($uploaded_size > 350000)
{
echo "Your file is too large.<br>";
$ok=0;
}
//This is our limit file type condition
if ($uploaded_type =="text/php")
{ echo "No PHP files<br>";
$ok=0;
}
//Here we check that $ok was not set to 0 by an error
if ($ok==0)
{
}
//If everything is ok we try to upload it
else {
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
} else
{
}
}
#Start of scripts to check Servers in textbox
if(isset($_POST['ServerList']))
{
//$arry=explode( "\r\n", $_POST['ServerList'] );
$txttrim = trim($_POST['ServerList']);
//$textAr = explode("\n", $text);
//$textAr1 = array_filter($text, 'trim'); // remove any extra \r characters left behind
$txtarea = explode("\n",$txttrim);
$txtarea = array_filter($txtarea,'trim');
foreach ($txtarea as $line => $servername)
{
//$line => $servername;
##check if querying itself....
}
echo '<pre>';
$target="D:/Web/Upload/"; $target= $target . basename( $_FILES['uploaded']['name']) ;
$csv = $target;
//$filename= $_FILES['uploaded']['name'];
$output=shell_exec("powershell -Command D:/Web/scripts/PHPfwrules.ps1 $csv < NUL");
echo '<h6>';
print_r($output);
$nomatch=preg_grep('/^not/i',$output);
echo $nomatch;
$match=preg_grep("/not/",$output,PREG_GREP_INVERT);
echo $match;
if ($working1= array_intersect($output,$match))
{
echo"<link rel=stylesheet type=text/css href=style.css>
<table class=results>
<tr>
<th>Server Name</th>
<th>Working</th>
</tr>
<tr>
<td>".$servername."</td>
<td>".$working1."</td>
</tr></table>
";
}
else if ($notworking1= array_intersect($output,$nomatch))
{
echo"<link rel=stylesheet type=text/css href=style.css>
<table class=results>
<tr>
<th>Server Name</th>
<th>Not Working</th>
</tr>
<tr>
<td>".$servername."</td>
<td>".$notworking1."</td>
</tr></table>
";
}
}
echo' <link rel=stylesheet href=dhtmlwindow.css type=text/css />
<link rel=stylesheet type=text/css href=style.css>
<script src=js/dhtmlwindow.js></script>
<hr />
<table class=results>
<tr>
<th>Server Name</th>
<th>Working</th>
<th> Not Working</th>
</tr>
<tr>
<td>'.$servername.'</td>
<td>'.$working1.'</td>
<td>'.$notworking1.'</td>
<td></td>
';
echo '</pre>';
echo '
<h3>Firewall Implementation </h3>
<h4>Please enter the server below, you can only select one server at a time. </h4>
<!--The form-->
<form action="fw2.php" method="post" name="myForm" id="myForm" enctype="multipart/form-data">
<textarea name=ServerList id=ServerList>
</textarea>
<h5>Please select a CSV file.</h5>
<input name="uploaded" type="file" /><br />
<input type="submit" value="Submit" />
<br>
</html> '
;
?>
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 ? 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? 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. 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! 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 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
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 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. 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! 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? 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! 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? I have the time time now viewed as 17:11. The time $Start_Time is 17:00. The time $End_time is 18:00. Can I use this to check if time is in between starttime and endtime? Or do I have to convert it to other formats before it understand that I'm comparing it as times and not just numbers? if($Start_Time < $Now_time && End_Time > $Now_time){ } hi i am am making a leave calendar ...i want to compare dates and if the matches the background will be changed . i am try to do is i got two loops first for 12 months and seconds for numbers of days in that month and i am using $num = cal_days_in_month(CAL_GREGORIAN, $Month, $cYear) this will give be numbers of days in that month... then there is a my sql query which will select that dates and other data from database .. now problem i am have is that numbers of days are not increment it show date like 2011-1-1 ,2011-1-1 and so on instead of 2011-1-1, 2011-1-2.... please help me ..here <<pre lang="xml">table > <?php for($Month=01;$Month <= 12;) { ?> <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 FROM `hs_hr_leave` WHERE employee_id = $emplid "; $res = mysql_query($sql); for($i=1;$i<=$num;$i++) { $dates = date("$cYear-$Month-$i"); //echo $dates."<br/>"; while ($row = mysql_fetch_row($res, MYSQL_NUM)) { $time2=date("Y-n-j",strtotime ($row[0])).'<br/>'; echo "<td bgcolor='red'>".$dates."--".$time2."</td>"; //echo "<td>".$row[1]."</td><br/>"; //echo "<td>".$row[1]."</td><br/>"; //echo "<td>".$row[2]."</td><br/>"; } echo "<td width='26' height='20px' align='center' valign='middle' >".$i."</td>"; } //echo "<br/>"; $Month++; } ?> </tr> </tabl Hi! I have two similar tables on two different databases and i need to check if there are any entries on the table1, for example, that there aren't on the second table and print if there are i need to print them. Any suggestions? Thank you... Hi Everyone, I am working on developing a blog/comment system for my website. I have 3 tables, the first a blog post table with a row/primary key of id that is auto incremented as each new article is created via a form. The second table is a comments table with a row of article_id and the third table is a category table with an article_id row as well. From the main blog page I have links that will take users to each individual blog post using $_GET like so: Code: [Select] http://www.mydomain.com/blog/article/?id={$row['id']} From here I would like to be able to display the comment for that particular article below the article and also have a sidebar with links to blog categories which will then display each post according to whether they are in that category or not. My question is two-fold: First, what is the best way to pull the id field from the blog/post table, form, or url and enter it into the article_id rows of the second two tables? Should I use a hidden field, the URL id via $_GET, using a join, or what? Secondly: What is the best way to compare and display those rows so that the appropriate comments are being shown for the correct article, etc. This is my first time doing this so I am grateful for any help or suggestions. Thanks, kaiman Guys,
How can I compare two values in same table.
servis.kalkulacija_stavke ( id BIGINT(255) NOT NULL AUTO_INCREMENT, id_kalkulacija INT(255) NOT NULL, id_cjenika INT(255) NOT NULL, vrijeme TIMESTAMP DEFAULT CURRENT_TIMESTAMP, kataloski_broj VARCHAR(15) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL, kategorija_artikla VARCHAR(10) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL, grupa_proizvoda VARCHAR(10) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL, podgrupa_proizvoda VARCHAR(15) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL, cijena_EUR DECIMAL(20, 6) NOT NULL, cijena_KN DECIMAL(20, 6) NOT NULL, carina DECIMAL(20, 6) NOT NULL, spediter DECIMAL(20, 6) NOT NULL, banka DECIMAL(20, 6) NOT NULL, transport DECIMAL(20, 6) NOT NULL, nabavna_cijena DECIMAL(20, 6) NOT NULL, drezga_marza_po_grupi DECIMAL(20, 6) NOT NULL, drezga_zarada DECIMAL(20, 6) NOT NULL, neto_VPC DECIMAL(20, 6) NOT NULL, neto_MPC DECIMAL(20, 6) NOT NULL, trosak_firme DECIMAL(20, 6) NOT NULL, trosak_firme_p DECIMAL(20, 6) NOT NULL, diler_marza_po_grupi DECIMAL(20, 6) NOT NULL, preporucena_VPC DECIMAL(20, 6) NOT NULL, preporucena_MPC DECIMAL(20, 6) NOT NULL, zarada_diler_kn DECIMAL(20, 6) NOT NULL, zarada_diler_post DECIMAL(20, 6) NOT NULL, zarada_za_nas_kn DECIMAL(20, 6) NOT NULL, zarada_za_nas_post DECIMAL(20, 6) NOT NULL, brutto_zarada_za_nas_kn DECIMAL(20, 6) NOT NULL, brutto_zarada_za_nas_post DECIMAL(20, 6) NOT NULL, godina YEAR(4) NOT NULL, PRIMARY KEY (id), INDEX id (id), INDEX kat_br (kataloski_broj), INDEX kataloski_broj (kataloski_broj, kategorija_artikla, grupa_proizvoda, podgrupa_proizvoda) ) ENGINE = MYISAM AUTO_INCREMENT = 1119990 AVG_ROW_LENGTH = 264 CHARACTER SET utf8 COLLATE utf8_general_ci;I want to compare field nabavna_cijena in last year and this year. How can I achieve that? Edited by budimir, 15 December 2014 - 08:12 AM. I 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");} Hello. Assuming that I have two variables: $exURL=@$HTTP_REFERER; $goodURL = 'http://www.SomeSite/login/'; How can I check if the two URLs match each other? Regards |