PHP - Check Array Against Db
Hi all!
I'm developing a crude mailing list thingymajig (completely legit, I promise!!). At the moment, I have single email subscription in way of a single field form. The email is entered, checked against the database, and subscribed if it is not there. If it is there, an alert is displayed saying 'email already subscribed'. Great. Now I'm taking things further with a text field and coding hich can extract email addresses from a block of text. No, this isn't so I can harvest email addresses illegitimately, it's because I get asked to add chunks of addresses at a time, and at the moment I have to do this manually, one-by-one. I have the code working to extract the email addresses from the text to an array, but now I'm stuck at the duplication check. I think what I'm asking is: how would it be possible to check each address in the array against the database and highlight any duplicates (I'll ultimately be looking to colour the duplicates and new addresses differently, show the number of new addresses to be added, and have a final 'do it' button which will add them...but I can get there on my own once I learn how to check for dupes!) Hopefully someone can give me a nudge in the right direction Many thanks!! Similar TutorialsI have an array created to control the down states of the my buttons in my navigation. Which is working well. This array will have quite a few strings in it before I'm done. I then want to conditionally load a CSS file for all the files in that array with the exception of two, is this possible? In plain in english I want to say If in array apart from these values then load my css file. My array (will have more values when complete) Code: [Select] $otherTitles = array('Product Selector', 'Avon Range', 'Knightsbridge Range', 'Kingston Range' ); My code to load a css file Code: [Select] <?php if (in_array($title, $otherTitles)) { ?> <link rel="stylesheet" type="text/css" href="homepage.css"/> <?php } ?> I want all titles in the otherTitles array to get this CSS file apart from two Product Selector and Avon Range. Thanks Richard I am trying to compare an array containing required questions to an array holding survey responses. When a user submits a survey, I take the $_POST array and write the values to a new array called $surveyResponseArray. How can I determine if $surveyResponseArray exists?
Edited February 16 by SaranacLake Is there any simple way to check if all values in an array are same with an simple if condition Something like if( ...some code...) { //yes, all values are same } else { //nope, all are not same } my PHP Version is 5.2.9-2 Any help would be appreciated. Basically, i want to check if values in an array are equals to 1. I have the form / script below that works to add multiple rows of data with one query. One thing I did not think about is what if the first set of form fields are filled and the second is not. Currently the script insert an empty row. I'm sure how I would go about checking for an empty row and if there is an empty row ignore it and do not put it in the DB. Can someone point me in the right direction? Code: [Select] <?php // Begin the script for this page if (isset($_POST['submit'])) { //Assign each array to a variable $id = $_POST['id']; $store = $_POST['store']; $item = $_POST['item']; $itemprice = $_POST['itemprice']; $itemnumber = $_POST['itemnumber']; $couponvalue = $_POST['couponvalue']; $couponsused = $_POST['couponsused']; $limit = count($id); $values = array(); // initialize an empty array to hold the values for($i=0;$i<$limit;$i++){ $store[$i] = check_input($store[$i]); $item[$i] = check_input($item[$i]); $itemprice[$i] = check_input($itemprice[$i]); $itemnumber[$i] = check_input($itemnumber[$i]); $couponvalue[$i] = check_input($couponvalue[$i]); $couponsused[$i] = check_input($couponsused[$i]); $values[$i] = "( '{$id[$i]}', '{$store[$i]}', '{$item[$i]}', '{$itemprice[$i]}', '{$itemnumber[$i]}', '{$couponvalue[$i]}', '{$couponsused[$i]}')"; // build the array of values for the query string } $query = "INSERT INTO `item` (user_id, store, item, itemprice, itemnumber, couponvalue, couponsused) VALUES " . implode( ', ', $values ); // Form the query string and add the implod()ed values if (!mysql_query($query,$link)){ die('Error: ' . mysql_error()); } else { $added = "Your items have been added."; } } ?> <div class="pageContent"> <div id="main"> <div class="container"> <?php echo $added; if($_SESSION['id']){ echo '<form action="" method="post">'; echo '<table cellpadding= "4">'; echo '<tr>'; echo '<input type="hidden" name="id[]" id="id[]" value='.$_SESSION['id'].' />'; echo '<td><DIV CLASS="p2"><b>Store</b></div>'; echo '<input type="text" name="store[]" id="store[]" size="15" maxlength="255"/></td>'; echo '<td><DIV CLASS="p2"><b>Item</b></div>'; echo '<input type="text" name="item[]" id="item[]" size="15" maxlength="255"/></td>'; echo '<td><DIV CLASS="p2"><b>Item Price</b></div>'; echo '<input type="text" name="itemprice[]" id="itemprice[]" size="5" maxlength="255"/></td>'; echo '<td><DIV CLASS="p2"><b>Item #</b></div>'; echo '<select name="itemnumber[]" id="itemnumber[]"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option><option value="14">14</option><option value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option value="19">19</option><option value="20">20</option></select></td>'; echo '<td><DIV CLASS="p2"><b>Coupon Value</b></div>'; echo '<input type="text" name="couponvalue[]" id="couponvalue[]" size="10" maxlength="255"/></td>'; echo '<td><DIV CLASS="p2"><b># Coupons</b></div>'; echo '<select style="width: 60px;" name="couponsused[]" id="couponsused[]"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option><option value="14">14</option><option value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option value="19">19</option><option value="20">20</option></select></td>'; echo '<td><DIV CLASS="p2"><b>Doubled Coupon</b></div>'; echo '<select style="width: 75px;" name="doubledcoupon[]" id="doubledcoupon[]"><option value="1">Yes</option><option value="0">No</option></td>'; echo '</tr>'; echo '<tr>'; echo '<input type="hidden" name="id[]" id="id[]" value="'.$_SESSION['id'].'" />'; echo '<td><input type="text" name="store[]" id="store[]" size="15" maxlength="255"/></td>'; echo '<td><input type="text" name="item[]" id="item[]" size="15" maxlength="255"/></td>'; echo '<td><input type="text" name="itemprice[]" id="itemprice[]" size="5" maxlength="255"/></td>'; echo '<td><select name="itemnumber[]" id="itemnumber[]"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option><option value="14">14</option><option value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option value="19">19</option><option value="20">20</option></select></td>'; echo '<td><input type="text" name="couponvalue[]" id="couponvalue[]" size="10" maxlength="255"/></td>'; echo '<td><select style="width: 60px;" name="couponsused[]" id="couponsused[]"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option><option value="14">14</option><option value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option value="19">19</option><option value="20">20</option></select></td>'; echo '<td><select style="width: 75px;" name="doubledcoupon[]" id="doubledcoupon[]"><option value="1">Yes</option><option value="0">No</option></td>'; echo'</tr>'; echo'<tr>'; echo '<input type="submit" name="submit" value="Submit Item">'; echo'</tr>'; echo '</table>'; echo'</form>'; } else { echo '<h1>Please, <a href="index.php">login</a> and come back later!</h1>'; } ?> </div> </div> </div> Can someone please help me figure this out: I have a normal text file with 3 lines in it, every line contain one date i.e 12112011 (12.11-2011). I put the content of that file in an array, and want to compair the dates in the text file against the date generated from the loop. Problem is, i can only get a match for the last date listed in the text file no matter what.. If i have 4 dates in the file, it only style the last date. Thanks! --- CODE SNIPPET --- $day_num = 1; $days_in_month = 30; $month = 11; $year = 2011; //Access the file containing already taken dates and load them in an array: $date_array = file("test.txt"); //count up the days, untill we've done all of them in the month and check them against the dates in the text file: while ($day_num <= $days_in_month) { // Set the variable $date to be checked against the content of the array: $date = $day_num . $month . $year; if (in_array($date, $date_array)) { echo "<td class='color_green'>$day_num</td>"; //print_r($date_array); } else { echo "<td>$day_num</td><br />"; } $day_num++; } Hi, I've checked the PHP manual and Googled this but can't find anything that assists me. Does anybody know if there exists a function issubset($childarray, $parentarray) that returns true if $childarray is a subset of $parentarray? That is, if all the elements in $childarray are included in $parentarray. Many thanks I would like to check multiple rows from array with elseif. It's working if i add it manually, but i would like it to check all of the rows in a single elseif. Code(First if is nothing, just a example). Row is a array filled with numbers like: 3-3, 2-2 etc. Code: [Select] if("2-2" == $row["0"]){ echo BLUE; }elseif($dnumber == $row["0"]){ echo RED; }else{ echo WHITE; } Hey all. I have a page that dynamically creates form input fields called "menuitem[]". So on the page that checks the values I need to check if any of those fields were not filed in. My syntax is wrong any help would be great. test.php foreach($_POST['menuitem'] as $key => $value) { if(!isset($value) || $value = "") { echo "Please fill in all values. You left key " . $key . "empty."; } } Nothing gets echoed when I leave the fields empty so I'm doing something wrong here. For example for my Skill Tree:
array (size=9) 0 => array (size=2) 'Skill_Id' => string '1' (length=1) 'Skill_Points' => string '0' (length=1) 1 => array (size=2) 'Skill_Id' => string '2' (length=1) 'Skill_Points' => string '0' (length=1) 2 => array (size=2) 'Skill_Id' => string '3' (length=1) 'Skill_Points' => string '0' (length=1) 3 => array (size=2) 'Skill_Id' => string '4' (length=1) 'Skill_Points' => string '1' (length=1) 4 => array (size=2) 'Skill_Id' => string '5' (length=1) 'Skill_Points' => string '1' (length=1) 5 => array (size=2) 'Skill_Id' => string '6' (length=1) 'Skill_Points' => string '1' (length=1) 6 => array (size=2) 'Skill_Id' => string '7' (length=1) 'Skill_Points' => string '0' (length=1) 7 => array (size=2) 'Skill_Id' => string '8' (length=1) 'Skill_Points' => string '1' (length=1) 8 => array (size=2) 'Skill_Id' => string '9' (length=1)For the Skill_Id, it must be in order when checking server side. 1 through 9. (or 9 can be changed by me whenever). So, I have: $skillrange = range(1,$amountofskills); Which will output: array (size=9) 0 => int 1 1 => int 2 2 => int 3 3 => int 4 4 => int 5 5 => int 6 6 => int 7 7 => int 8 8 => int 9But, how do I check this order from my original array? I need to check My Skill_ID values in order from my original array and make sure they are 1 through 9. (So a user cannot tamper the data) Edit: I've come up with this solution since making this thread: $array1 = $skillidarray; $array2 = $skillrange; if (!empty(array_diff_assoc($array1, $array2))){ echo "Your skill tree data is out of order, please report this to an administrator."; }Something like this would work right? Edited by Monkuar, 11 October 2014 - 03:59 PM. After doing a mysql_query and pulling data into an array like: Code: [Select] $polquery = mysql_query("SELECT * FROM disclosures WHERE transid = '$transid'"); while($something = mysql_fetch_array($polquery){ $status = $something['complete']; } I need to cycle through the array $status to see if all values = "accept", I'm not sure how to do this... or just forgot. Maybe just do a while($status == "accept")? Hey Guys, I need some assistance, hopefully there is just a simple command that I am not aware of or an easy answer to this. Suppose I have an array with x amount of values... $arr = array("111111" => 10, "222222" => 20); And i have a query.. mysql_query("SELECT * FROM table WHERE table.zip = "111111" "); Suppose I want to modify the query, so it can check if table.zip exists anywhere in $arr, regardless of length of array. And Select the value that the array location refers to. So I need it to check if "111111" is anywhere in the array, and then take the value 10. The example I am applying this to is that I have a database full of business addresses/zip codes and a database full of every zip code in country. So I have a script to get every zip within x miles, and the distance from it, but need to be able to apply it when I query my business addresses to see if it exists within x miles. Thanks! Greg I want to check if a string has any of words listed in array; Code: [Select] $array=array('word1', 'word2', 'word3'); $string="This is a sentence containing word2"; Now I want to make an 'if else' to check if the string contains any of the words listed in the array. Thanks Hi, A user selects week and a room to book. I put these into an array so I have now have an array of room numbers and dates. I don't want them to book seperate holidays for one accommodation, I have mangaged to stop them booking a two week stay and they an extra week. But I need a way to stop them booking two or three seperate two week holidays. So basically I need to just give an error if the dates they have selected are not consecutive. I have tried many variations of adding and subtracting 7 days and comparing that in the array using a loop based on the room ids being the same. I know the code below doesn't work but it gives an idea of how I been trying. Also assume that the array has aan accomID and date seperated by a comma. function search_break_in_consec_weeks($accomCountValidCheck, $accomID, $date){ $allConsec = false; foreach ($accomCountValidCheck as $key=>$value){ $varArray = explode(', ', $value); $varDate = $varArray[1]; $varAccomID = $varArray[0]; if($accomID == $varAccomID && date("Y-m-d", strtotime($date)) == date("Y-m-d", strtotime($varDate ."+7 days")) || date("Y-m-d", strtotime($date)) == date("Y-m-d", strtotime($varDate ."-7 days"))){ $allConsec = true; break; }else{ $allConsec = false; } } return $countStray; } foreach($accomCountValidCheck as $accomVal){ $varArray = explode(', ', $accomVal); $varAccomID = $varArray[0]; $varDate= $varArray[1]; $valReturnConsec = search_break_in_consec_weeks($accomCountValidCheck, $varAccomID, $varDate); if($valReturnConsec){echo '<b>'.$varAccomID.'</b>'. ' consec<br />';} if($valReturnConsec == false){echo $varAccomID .'not consec<br />';} } Hey, Im trying make a way to check an input to make sure it only consists of "1,2,3,4,5,6,7,8,9,0 and . " I thought i could explode the number and check it to an array that has "1,2,3,4,5,6,7,8,9,0 and . " I havent worked with arrays before so i dont quite know how they work or to go about this? Any ideas cause im sure im not the first to ask for something like this Thanks Code: [Select] $arr = array('../', 'j/', 'AcidBB/', 'fileBOMB/', 'OurHome', 'up'); foreach ($arr as $value) { // echo "Value: $value<br /><br />"; if (is_dir($value)) { echo "<tr> <td>{$value}</td> <td><font color='#66FF33'>Exists</font></td></tr><br />"; } else { echo "<tr> <td>{$value}</td> <td><font color='#FF0000'>Does Not Exist</font></td></tr><br />"; } } ~~~ Solved after posting. Sorry. ~~~ Ok i have an Array of directories i need to check before the script installs itself, I know how to check for directories but i dont know how to scan each array in the list without calling each one seperately in Code: [Select] If() { } Else If { } Else If { } And so on and so on... My Question is, If a For Statement is neaded, How would i go about checking each string in the Array List? Code: [Select] Array ( [Comment] => Array ( [post_id] => 1 [name] => cc [email] => [body] => spam ) ) How can i check if email is empty or not? Is there any php built in function? Should i use foreach? i am trying to check if the array element (which would be a file) is null or not. when i run it, uploading one file, it says "Please select some files to upload first!" X 3. what am i doing wrong? my code: http://pastebin.com/m0k3fEd2 As the title says, how can i check if multiple arrays exist inside an array or not? Any help is greatly appreciated!
Hi guys, Could someone help me please? How do I check the following data foreach [id] and check to see if it the whole array has a certain ID in it? I can access individual data like this: $array['data'][0]['id']; but I'm not sure of the best way to check the array for a particular ID. Any ideas? Thanks, RJP1 Code: [Select] Array ( [data] => Array ( [0] => Array ( [name] => sdajskdhsajd [category] => Application [id] => 323635478887920 [created_time] => 878978978 ) [1] => Array ( [name] => askdjakdjasd [category] => Application [id] => 36741566509099181 [created_time] => 2011-02-11T15:41:14+0000 ) [2] => Array ( [name] => asdasdasdasds [category] => Interest [id] => 1150572090878508639 [created_time] => 2011-02-11T15:38:40+0000 ) I have a web server which contains a public site and a private site. I want to be able to test for public or private using the page title and return the appropriate header which contains a different banner, css etc. Firstly - the syntax below seems to be incorrect because it doesnt work correctly. More importantly how do I add all the values to an array and than iterate through it to check for a matching page title? Code: [Select] public function publicSite() { if ($this->getTitle()== 'Home' || 'About Us'||'Registration' || 'Sitemap' || 'Contact Us' || 'Useful Links' || 'Feedback') { return true; } return false; } Then later on I would have: Code: [Select] if ($this->publicSite()) { require($ROOT.'interface/pages/publicheader.php'); } else { require($ROOT.'interface/pages/privateheader.php'); } Thanks heaps! |