PHP - Remove Duplicate Word
For some reason, this doesn't work:
<?php
$string = 'You have a code CODE code of ethics'; while (preg_match("/code\s+code/",$string)) { $string = preg_replace("/code\s+code/","code",$string); } print $string; ?> I don't want to replace all instances of duplicated words, rather, only when the word "code" is duplicated. Throughout my 100mb text file, the word "code" appears duplicated (with varying whitespace between). For example: From this: You have a code CODE code code of ethics. Code code Code green means everything is okay. What is the CODE code code code for the lock. To this: You have a code of ethics. Code green means everything is okay. What is the code for the lock. But for some reason, I cannot get my preg_replace to work? Similar Tutorials I have written code in php to connect and insert rows into a MSSQL database. i used odbc to connect database.user can enter his details through the form. after submitting the form the details are getting stored into a database. while inserting rows into a database am not trying to insert duplicate values . for this i have given if conditions.these conditions are able to notice the user cname and name exist in the database if the same name exist. but the else part after these conditions are not working i.e rows are not getting inserted. i put everything inside the while loop. how can i correct it? This is my php code. $connect = odbc_connect('ServerDB','sa', 'pwd'); //connects database $query2="select count(*) from company";//this is needer for loop through $result2=odbc_exec($connect,$query2); while(odbc_fetch_row($result2)); { $count=odbc_result($result2,1); echo "</br>","$count"; } $query1="select * from company"; $result1 = odbc_exec($connect, $query1); # fetch the data from the database while(odbc_fetch_row($result1)) { $compar[$count] = odbc_result($result1, 1); $namearray[$count] = odbc_result($result1, 2); if($compar[$count]==$_POST['cname']) { echo "<script> alert(\"cname Exists\") </script>"; } else if($namearray[$count]==$_POST['name']) { echo "<script> alert(\"Name Exists\") </script>"; } else { $query=("INSERT INTO company(cname,name) VALUES ('$_POST[cname]','$_POST[name]') "); $result = odbc_exec($connect, $query); echo "<script> alert(\"Row Inserted\") </script>"; } } is this possible? Hi, i got help earlier with the this code-- exploding a field and getting an array, then inserting the array into a multiselect box-- That part is easy and works fine. Now the explode function gives an array of words with duplicate words in it: I tried several ways most either give a blank option/value, or the array word.. here is the code: $query = "SELECT DISTINCT property_functionsexperience FROM #__users_profiles WHERE published = '1' ORDER BY property_functionsexperience ASC"; $functionsexperiencelistgeneral=doSelectSql($query); foreach($functionsexperiencelistgeneral as $words) $property_funcexperilist=$words->property_functionsexperience; $wording = explode(', ', $property_funcexperilist); foreach($wording as $funciex) $funcexi=array_unique($funciex); $funcexpList .= "<option value=\"".$funcexi.','."\">".$funcexi."</option>"; $funcexpList .= "</optgroup>"; } $funcexpList.="</select>"; $output['FUNCEXPLIST']=$funcexpList; this code is adapted slightly from the working original that gives the double words-- this code gives a blank, but i think its on the correct track!? for example, i want to search for an the string "example@example.com" and erase it. thanks in advance ! First problem fixed. My second problem is if the result is 0X.XX,0 or 0X.XX,1 I would like to remove the first 0 The X.XX are numbers, but the 0 is not always there. Can anyone help please Thanks I am inserting the two records below simultaneously (one after the other), but what I want to do write the second ONLY if the first isn't a duplicate. Help? Code: [Select] //FIRST INSERT mysql_query("INSERT INTO spam (id, scm_mem_id) VALUES('', '$social_mem_id' ) ON DUPLICATE KEY UPDATE scm_mem_id=$social_mem_id") or die(mysql_error()); //SECOND INSERT mysql_query("INSERT INTO sc_messages (smg_from, smg_to, smg_subject, smg_body, smg_sent_del, smg_postdate) VALUES ('$social_mem_id','1','$subject','$body','1','$time')") or die(mysql_error()); Code: [Select] foreach ($items as $product ) { echo $product; ?><input type="text" size="2" value="<?php \\display quantity here ?>" name="quantity" /><?php } $items is an array. It has multiple values, some duplicate. How do i count the duplicate values in the array and display in this foreach loop? i know array_count_values($product) but putting that in the foreach loop won't accomplish what i want. another thing. i can get the foreach loop to not display duplicates by doing this Code: [Select] foreach (array_unique($items) as $product ) { echo $product; ?><input type="text" size="2" value="<?php \\display quantity here ?>" name="quantity" /><?php } how would i accomplish both. basically i want it to display the quantity without displaying duplicate rows. aka shopping cart. Hi, I have a function that returns the value of a array if it matches $string, but it only returns Test 1 instead of returning Test 1, Test 2. Can you someone see what I'm doing wrong? And it has to return N if nothing found. Code: Code: [Select] function fes($string){ $fes['Test 1'] = '104'; $fes['Test 2'] = '104'; return in_array($string, $fes) ? array_search($string, $fes) : 'N'; } echo fes('104'); thanks, Cyto Hi, I have a multidimentional array which looks like this : Code: [Select] array(2) { ["HOKIT100E-041"]=> array(3) { [0]=> string(26) "Honda Cr 125-250 2000-2001" [1]=> string(5) "89.99" [2]=> string(1) "1" } ["WAFAXS"]=> array(3) { [0]=> string(27) "Wulf Flite-x Helmet - Green" [1]=> string(5) "65.94" [2]=> string(1) "1" } } howver when I do a foreach on it, the 1st array is being duplicated and I have no idea why this is the code I have : Code: [Select] foreach($basketArray as $key => $value) { echo$nvpstr = $nvpstr ."<br/><br/>&L_PAYMENTREQUEST_0_NUMBER$q=".$key." <br/>&L_PAYMENTREQUEST_0_NAME$q=".$value[0]." <br/>&L_PAYMENTREQUEST_0_AMT$q=".$value[1]." <br/>&L_PAYMENTREQUEST_0_QTY$q=".$value[2]; $q++; } this is what is output : Quote &L_PAYMENTREQUEST_0_NUMBER0=HOKIT100E-041 &L_PAYMENTREQUEST_0_NAME0=Honda Cr 125-250 2000-2001 &L_PAYMENTREQUEST_0_AMT0=89.99 &L_PAYMENTREQUEST_0_QTY0=1 &L_PAYMENTREQUEST_0_NUMBER0=HOKIT100E-041 &L_PAYMENTREQUEST_0_NAME0=Honda Cr 125-250 2000-2001 &L_PAYMENTREQUEST_0_AMT0=89.99 &L_PAYMENTREQUEST_0_QTY0=1 &L_PAYMENTREQUEST_0_NUMBER1=WAFAXS &L_PAYMENTREQUEST_0_NAME1=Wulf Flite-x Helmet - Green &L_PAYMENTREQUEST_0_AMT1=65.94 &L_PAYMENTREQUEST_0_QTY1=1 Have I made a schoolboy error? Thanks. Hey guys! I know, I know this problem is EVERYWHERE but i just dont understand! I have a solid knowlage of php but my SQL skills are low, so i dont know too much about Keys and stuff. But my error is: Duplicate entry '' for key 2. The thing that im working on at this section is logging in with facebook. The code that presents my error is: $sql = "SELECT * FROM users WHERE uid=".$uid; $fbid = mysql_query($sql); $num_rows = mysql_num_rows($fbid); if(mysql_num_rows($fbid) < 1) { echo "You are not logged in. "; mysql_query("INSERT INTO `users` (`uid`) VALUES ('".$uid."')") or die(mysql_error()); } else { mysql_query("UPDATE users SET logged = '1' WHERE uid=".$uid); //mysql_query("UPDATE users SET full_name = $me WHERE uid=".$uid); echo "Your Logged in "; echo $me['name']; ?> Continue to <a href="removed :)"> My Settings </a>. <? } Any help is welcome Hey folks, Sorry for being a pain in the ass. I am trying to submit data to my database via a form and when I click Submit, I get: Duplicate entry '' for key 1 I understand that it means I have a duplicate entry with the ID of 1 or something like that. I can't find where the issue is. Here is the form: <form actin="" id="settings" name="settings"> <table class="listing form" cellpadding="0" cellspacing="0"> <tr> <th class="full" colspan="2"><?php echo $lang_settings; ?></th> </tr> <tr> <th colspan="2"><?php echo $lang_settings_description; ?></th> </tr> <tr> <td><?php echo $lang_sitename; ?>: </td> <td><input type="text" name="sitename" value="<?php echo $site_name; ?>" width="172" /> <em>Site name for logo</em></td> </tr> <tr> <td><?php echo $lang_email; ?>: </td> <td><input type="text" name="email" value="<?php echo $site_email; ?>" width="172" /> <em>Your email address</em></td> </tr> <tr> <td><?php echo $lang_yourname; ?>: </td> <td><input type="text" name="name" value="<?php echo $your_name; ?>" width="172" /> <em>Your own name</em></td> </tr> <tr> <td><?php echo $lang_meta_description; ?>: </td> <td><input type="text" name="meta-description" value="<?php echo $description; ?>" width="172" /> <em>SEO</em></td> </tr> <tr> <td><?php echo $lang_keywords; ?>: </td> <td><input type="text" name="meta-keywords" value="<?php echo $keywords; ?>" width="172" /> <em>Separate with Commas</em></td> </tr> <tr> <td><input type="submit" class="button" name="submit" value="<?php echo $lang_button_savesettings; ?>"></td> </tr> </table> </form> Here is the Insert code: $insert = "INSERT INTO settings (site_name, description, keywords, email, name) VALUES ('$sitename', '$meta_description', '$meta_keywords', '$site_email', '$your_name')"; mysql_query($insert) or die(mysql_error()); Can anyone please tell me where I am going wrong here? Much appreciated. hi, i have some code that i need to update existing columns in a table, but i just cant seem to get it to work if anyone caan please help it would be appreciated. Quote // select the database mysql_select_db($database) or die ("Could not select database because ".mysql_error()); $insert = mysql_query("insert into $table values ('NULL', '".$_POST['email']."', '".$_POST['password']."', '".$_POST['name']."', '".$_POST['Gender']."', '".$_POST['Age']."', '".$_POST['Race']."', '".$_POST['SpecialtyOne']."', '".$_POST['Feet']."', '".$_POST['Weight']."', '".$_POST['Eyes']."', '".$_POST['Hair']."', '".$_POST['Strength']."', '".$_POST['Stamina']."', '".$_POST['Perception']."', '".$_POST['Agility']."', '".$_POST['Intelligence']."', '".$_POST['Tech']."', '".$_POST['Luck']."', '".$_POST['Appearance']."', '".$_POST['Background']."', '".$_POST['Weapons']."', '".$_POST['Armour']."', '".$_POST['Items']."', '".$_POST['Enhancements']."', '".$_POST['Skills']."')") or die("Could not insert data because ".mysql_error()); ?> <?php Hi there, I know I'm doing something stupid, but i've tried for hours to try and figure this out and I just cant seem to figure out why tutorials i've seen seem to work yet my own code doesnt. basically i've got two functions: getAll() and get($id) inside a class, calling getAll, returns an array with one of the same record as opposed to all the records (which is my intended result). Can anyone help? thanks. Code: [Select] class Test{ protected $_db; protected $_table = "table"; protected $_friends = array(); public function __construct($db) { $this->_db = $db; } public function getAll() { $db = $this->_db; $sql = "SELECT * FROM $this->_table"; $db->query($sql); while($data = $db->fetch_array()) { $this->_friends[$data['id']] = $this->get($data['id']); } return $this->_friends; } public function get($id) { $db = $this->_db; $sql = "SELECT * FROM $this->_table WHERE id='$id'"; $db->query($sql); $data = $db->fetch_array(); return (object)$data; } } $test = new Test(new mysqlConnClass()); $arrayOfAllAsObjects = $test->getAll(); Hey guys just wondering if this piece of code is correct because i get a mysql error saying Duplicate entry '*********' for key 2 this is the code i have and table structure. $sql = " INSERT INTO `IP_Address` VALUES(NULL,'".$ip."','".$date."','".$time."') ON DUPLICATE KEY UPDATE `date` = `".$date."`, `time` = `".$time."` "; Hi.. So im currently working on a script.. My script generates a "oid" based on timestamp. Ive made the "oid" field unique in my db, so if i do a quick refresh i get the message: Duplicate entry '1283195988' for key 'oid' Is there some way i can check if its a dublicate, and if it is + it with 1 or something? Please i ran into this problem help me : Code: [Select] <p>Posted by <a href=""> <?php if(isset($_GET['id'])) { $tpid=$_GET['id']; } else { $tpid=$_POST['id']; } include"header.php"; $sql="SELECT*FROM topics WHERE topicsid='$tpid'"; $result=mysql_query($sql) or die(mysql_error()); while($row=mysql_fetch_array($result)) { echo"{$row['topics_by']}"; echo"</a></p>"; echo"<p>"; echo"{$row['topics_subject']}"; echo" <p align='left'><img src='speakout_1.png'/>"; echo"{$row['topics_content']}"; echo"<p class='post-footer align-right'> <a href='' class='comments'>"; session_start(); if(isset($_SESSION['views'])) $_SESSION['views']=$_SESSION['views']+1; else $_SESSION['views']=1; echo"Comments:".$_SESSION['views']; echo"</a>"; echo"<span class='date'>"; echo"{$row['topics_date']}"; } ?></span> </p> </div> <h3>Comments:</h3> <table> <tr class="row-a"> <td class="first"></td> <td><?php include"header.php"; $sql="SELECT post_content,post_by FROM post WHERE topicsID='$tpid'"; $result=mysql_query($sql)or die(mysql_error()); while($row=mysql_fetch_array($result)) { echo"<strong>{$row['post_by']}</strong>: {$row['post_content']}"."</br>"; } ?></td> </tr> </table> <?php include"header.php"; if(isset($_POST['submit'])) { $comment=mysql_real_escape_string(trim($_POST['comment'])); $name=mysql_real_escape_string(trim($_POST['name'])); $hidden=$_POST['id']; if($comment!=='' && $name!=='') { $ins="INSERT INTO post(topicsID,post_content,post_by)VALUES('$hidden','$comment','$name')"; mysql_query($ins) or die(mysql_error()); } else { echo"you cannot post an empty field"; } } ?> <h3>Post your comments here</h3> <form action=''method='post'> <textarea name="comment" id="content" style="width:400px;height:50px;background-color:#D0F18F;color:#000000;font:15px/20px cursive;scrollbar-base-color:#638E0D;"></textarea> <br /> Name:<input type="text"name="name"/> <input class="button" type="submit"name="submit"value="submit" /> <input type="hidden"name="id"value='<?php echo "$tpid"; ?>'/> </p> </form> <br /> </div> Hi.... I want to know what syntax should i used for UPDATE or ON DUPLICATE KEY. Because now, I only have syntax for insert query.,I want to add syntax for update or DUPLICATE key. here is my code: Code: [Select] <?php include 'config.php'; $currentEmpID = $_SESSION['empID']; $DATE1 = $_GET['Regfirstinput']; $DATE2 = $_GET['Regsecondinput']; $smarty->assign('DATE1', $DATE1); $smarty->assign('DATE2', $DATE2); $result = mysql_query("INSERT INTO payroll.reg_att(EMP_NO, DATE_DTR, LOGIN, LOGOUT, TotalHours) SELECT EMP_NO, DATE(LOGIN), LOGIN, LOGOUT, TIMEDIFF(LOGOUT, LOGIN) FROM attendance.employee_attendance") or die(mysql_error()); $result = mysql_query("UPDATE payroll.reg_att SET Rendered = case when time_to_sec(time(TotalHours)) <= time_to_sec('03:00:00') then sec_to_time(time_to_sec('00:00:00')) when time_to_sec(time(LOGIN)) BETWEEN time_to_sec('20:35:00') AND time_to_sec('21:35:00') AND time_to_sec(time(LOGOUT)) BETWEEN time_to_sec('05:35:00') AND time_to_sec('06:35:00') then sec_to_time(time_to_sec('08:00:00')) when time_to_sec(time(LOGIN)) BETWEEN time_to_sec('04:35:00') AND time_to_sec('05:35:00') AND time_to_sec(time(LOGOUT)) BETWEEN time_to_sec('13:35:00') AND time_to_sec('14:35:00') then sec_to_time(time_to_sec('08:00:00')) when time_to_sec(time(LOGIN)) BETWEEN time_to_sec('12:35:00') AND time_to_sec('13:35:00') AND time_to_sec(time(LOGOUT)) BETWEEN time_to_sec('21:35:00') AND time_to_sec('22:35:00') then sec_to_time(time_to_sec('08:00:00')) when time_to_sec(time(LOGIN)) BETWEEN time_to_sec('06:00:00') AND time_to_sec('07:00:00') AND time_to_sec(time(LOGOUT)) BETWEEN time_to_sec('16:00:00') AND time_to_sec('16:59:00') then sec_to_time(time_to_sec('08:00:00')) when time_to_sec(time(LOGIN)) BETWEEN time_to_sec('07:01:00') AND time_to_sec('08:00:00') AND time_to_sec(time(LOGOUT)) BETWEEN time_to_sec('17:00:00') AND time_to_sec('17:59:00') then sec_to_time(time_to_sec('08:00:00')) when time_to_sec(time(LOGIN)) BETWEEN time_to_sec('07:01:00') AND time_to_sec('08:00:00') AND time_to_sec(time(LOGOUT)) BETWEEN time_to_sec('18:00:00') AND time_to_sec('18:59:00') then sec_to_time(time_to_sec('08:00:00')) when time_to_sec(time(LOGIN)) BETWEEN time_to_sec('21:35:00') AND time_to_sec('22:35:00') AND time_to_sec(time(LOGOUT)) BETWEEN time_to_sec('05:35:00') AND time_to_sec('07:35:00') then sec_to_time(time_to_sec(concat(substr(LOGIN, 1,10), ' ' , '08:00:00')) - (time_to_sec(LOGIN) - time_to_sec(concat(substr(LOGIN, 1,10), ' ' , '21:35:00')))) when time_to_sec(time(LOGIN)) BETWEEN time_to_sec('13:35:00') AND time_to_sec('15:35:00') AND time_to_sec(time(LOGOUT)) BETWEEN time_to_sec('21:35:00') AND time_to_sec('22:35:00') then sec_to_time(time_to_sec(concat(substr(LOGIN, 1,10), ' ' , '08:00:00')) - (time_to_sec(LOGIN) - time_to_sec(concat(substr(LOGIN, 1,10), ' ' , '13:35:00')))) when time_to_sec(time(LOGIN)) BETWEEN time_to_sec('05:35:00') AND time_to_sec('07:35:00') AND time_to_sec(time(LOGOUT)) BETWEEN time_to_sec('13:35:00') AND time_to_sec('15:35:00') then sec_to_time(time_to_sec(concat(substr(LOGIN, 1,10), ' ' , '08:00:00')) - (time_to_sec(LOGIN) - time_to_sec(concat(substr(LOGIN, 1,10), ' ' , '05:35:00')))) when time_to_sec(time(LOGIN)) BETWEEN time_to_sec('07:00:00') AND time_to_sec('07:59:00') AND time_to_sec(time(LOGOUT)) BETWEEN time_to_sec('16:00:00') AND time_to_sec('16:59:00') then sec_to_time(time_to_sec(concat(substr(LOGIN, 1,10), ' ' , '08:00:00')) - (time_to_sec(LOGIN) - time_to_sec(concat(substr(LOGIN, 1,10), ' ' , '07:00:00')))) when time_to_sec(time(LOGIN)) BETWEEN time_to_sec('08:00:00') AND time_to_sec('08:59:00') AND time_to_sec(time(LOGOUT)) BETWEEN time_to_sec('16:00:00') AND time_to_sec('16:59:00') then sec_to_time(time_to_sec(concat(substr(LOGIN, 1,10), ' ' , '08:00:00')) - (time_to_sec(LOGIN) - time_to_sec(concat(substr(LOGIN, 1,10), ' ' , '08:00:00')))) when time_to_sec(time(LOGIN)) BETWEEN time_to_sec('08:00:00') AND time_to_sec('08:59:00') AND time_to_sec(time(LOGOUT)) BETWEEN time_to_sec('17:00:00') AND time_to_sec('17:59:00') then sec_to_time(time_to_sec(concat(substr(LOGIN, 1,10), ' ' , '08:00:00')) - (time_to_sec(LOGIN) - time_to_sec(concat(substr(LOGIN, 1,10), ' ' , '08:00:00')))) when time_to_sec(time(LOGIN)) BETWEEN time_to_sec('08:00:00') AND time_to_sec('08:59:00') AND time_to_sec(time(LOGOUT)) BETWEEN time_to_sec('18:00:00') AND time_to_sec('18:59:00') then sec_to_time(time_to_sec(concat(substr(LOGIN, 1,10), ' ' , '08:00:00')) - (time_to_sec(LOGIN) - time_to_sec(concat(substr(LOGIN, 1,10), ' ' , '08:00:00')))) when time_to_sec(time(LOGIN)) BETWEEN time_to_sec('20:35:00') AND time_to_sec('21:35:00') AND time_to_sec(time(LOGOUT)) BETWEEN time_to_sec('03:35:00') AND time_to_sec('05:35:00') then sec_to_time(time_to_sec(concat(substr(LOGOUT, 1,10), ' ' , '08:00:00')) + (time_to_sec(LOGOUT) - time_to_sec(concat(substr(LOGOUT, 1,10), ' ' , '05:35:00')))) when time_to_sec(time(LOGIN)) BETWEEN time_to_sec('12:35:00') AND time_to_sec('13:35:00') AND time_to_sec(time(LOGOUT)) BETWEEN time_to_sec('18:01:00') AND time_to_sec('21:35:00') then sec_to_time(time_to_sec(concat(substr(LOGOUT, 1,10), ' ' , '08:00:00')) + (time_to_sec(LOGOUT) - time_to_sec(concat(substr(LOGOUT, 1,10), ' ' , '21:35:00')))) when time_to_sec(time(LOGIN)) BETWEEN time_to_sec('04:00:00') AND time_to_sec('05:35:00') AND time_to_sec(time(LOGOUT)) BETWEEN time_to_sec('09:01:00') AND time_to_sec('13:35:00') then sec_to_time(time_to_sec(concat(substr(LOGOUT, 1,10), ' ' , '08:00:00')) + (time_to_sec(LOGOUT) - time_to_sec(concat(substr(LOGOUT, 1,10), ' ' , '13:35:00')))) when time_to_sec(time(LOGIN)) BETWEEN time_to_sec('06:00:00') AND time_to_sec('07:00:00') AND time_to_sec(time(LOGOUT)) BETWEEN time_to_sec('13:01:00') AND time_to_sec('16:00:00') then sec_to_time(time_to_sec(concat(substr(LOGOUT, 1,10), ' ' , '08:00:00')) + (time_to_sec(LOGOUT) - time_to_sec(concat(substr(LOGOUT, 1,10), ' ' , '16:00:00')))) when time_to_sec(time(LOGIN)) BETWEEN time_to_sec('07:01:00') AND time_to_sec('08:00:00') AND time_to_sec(time(LOGOUT)) BETWEEN time_to_sec('13:01:00') AND time_to_sec('16:00:00') then sec_to_time(time_to_sec(concat(substr(LOGOUT, 1,10), ' ' , '08:00:00')) + (time_to_sec(LOGOUT) - time_to_sec(concat(substr(LOGOUT, 1,10), ' ' , '16:00:00')))) when time_to_sec(time(LOGIN)) BETWEEN time_to_sec('07:01:00') AND time_to_sec('08:00:00') AND time_to_sec(time(LOGOUT)) BETWEEN time_to_sec('14:01:00') AND time_to_sec('17:00:00') then sec_to_time(time_to_sec(concat(substr(LOGOUT, 1,10), ' ' , '08:00:00')) + (time_to_sec(LOGOUT) - time_to_sec(concat(substr(LOGOUT, 1,10), ' ' , '17:00:00')))) when time_to_sec(time(LOGIN)) BETWEEN time_to_sec('07:01:00') AND time_to_sec('08:00:00') AND time_to_sec(time(LOGOUT)) BETWEEN time_to_sec('15:01:00') AND time_to_sec('18:00:00') then sec_to_time(time_to_sec(concat(substr(LOGOUT, 1,10), ' ' , '08:00:00')) + (time_to_sec(LOGOUT) - time_to_sec(concat(substr(LOGOUT, 1,10), ' ' , '18:00:00')))) when time_to_sec(time(LOGIN)) BETWEEN time_to_sec('21:35:00') AND time_to_sec('22:35:00') AND time_to_sec(time(LOGOUT)) BETWEEN time_to_sec('03:35:00') AND time_to_sec('05:35:00') then sec_to_time(time_to_sec(concat(substr(LOGOUT, 1,10), ' ' , '08:00:00')) + (time_to_sec(LOGOUT) - time_to_sec(concat(substr(LOGOUT, 1,10), ' ' , '05:35:00'))) + time_to_sec(concat(substr(LOGIN, 1,10), ' ' , '08:00:00')) - (time_to_sec(LOGIN) - time_to_sec(concat(substr(LOGIN, 1,10), ' ' , '21:35:00'))) - time_to_sec('08:00:00')) when time_to_sec(time(LOGIN)) BETWEEN time_to_sec('13:35:00') AND time_to_sec('14:35:00') AND time_to_sec(time(LOGOUT)) BETWEEN time_to_sec('18:01:00') AND time_to_sec('21:35:00') then sec_to_time(time_to_sec(concat(substr(LOGOUT, 1,10), ' ' , '08:00:00')) + (time_to_sec(LOGOUT) - time_to_sec(concat(substr(LOGOUT, 1,10), ' ' , '21:35:00'))) + time_to_sec(concat(substr(LOGIN, 1,10), ' ' , '08:00:00')) - (time_to_sec(LOGIN) - time_to_sec(concat(substr(LOGIN, 1,10), ' ' , '13:35:00'))) - time_to_sec('08:00:00')) when time_to_sec(time(LOGIN)) BETWEEN time_to_sec('05:35:00') AND time_to_sec('06:35:00') AND time_to_sec(time(LOGOUT)) BETWEEN time_to_sec('10:01:00') AND time_to_sec('13:35:00') then sec_to_time(time_to_sec(concat(substr(LOGOUT, 1,10), ' ' , '08:00:00')) + (time_to_sec(LOGOUT) - time_to_sec(concat(substr(LOGOUT, 1,10), ' ' , '13:35:00'))) + time_to_sec(concat(substr(LOGIN, 1,10), ' ' , '08:00:00')) - (time_to_sec(LOGIN) - time_to_sec(concat(substr(LOGIN, 1,10), ' ' , '05:35:00'))) - time_to_sec('08:00:00')) when time_to_sec(time(LOGIN)) BETWEEN time_to_sec('07:00:00') AND time_to_sec('07:59:00') AND time_to_sec(time(LOGOUT)) BETWEEN time_to_sec('12:01:00') AND time_to_sec('16:00:00') then sec_to_time(time_to_sec(concat(substr(LOGOUT, 1,10), ' ' , '08:00:00')) + (time_to_sec(LOGOUT) - time_to_sec(concat(substr(LOGOUT, 1,10), ' ' , '16:00:00'))) + time_to_sec(concat(substr(LOGIN, 1,10), ' ' , '08:00:00')) - (time_to_sec(LOGIN) - time_to_sec(concat(substr(LOGIN, 1,10), ' ' , '07:00:00'))) - time_to_sec('08:00:00')) when time_to_sec(time(LOGIN)) BETWEEN time_to_sec('08:00:00') AND time_to_sec('08:59:00') AND time_to_sec(time(LOGOUT)) BETWEEN time_to_sec('12:01:00') AND time_to_sec('16:00:00') then sec_to_time(time_to_sec(concat(substr(LOGOUT, 1,10), ' ' , '08:00:00')) + (time_to_sec(LOGOUT) - time_to_sec(concat(substr(LOGOUT, 1,10), ' ' , '16:00:00'))) + time_to_sec(concat(substr(LOGIN, 1,10), ' ' , '08:00:00')) - (time_to_sec(LOGIN) - time_to_sec(concat(substr(LOGIN, 1,10), ' ' , '08:00:00'))) - time_to_sec('08:00:00')) when time_to_sec(time(LOGIN)) BETWEEN time_to_sec('08:00:00') AND time_to_sec('08:59:00') AND time_to_sec(time(LOGOUT)) BETWEEN time_to_sec('12:01:00') AND time_to_sec('17:00:00') then sec_to_time(time_to_sec(concat(substr(LOGOUT, 1,10), ' ' , '08:00:00')) + (time_to_sec(LOGOUT) - time_to_sec(concat(substr(LOGOUT, 1,10), ' ' , '17:00:00'))) + time_to_sec(concat(substr(LOGIN, 1,10), ' ' , '08:00:00')) - (time_to_sec(LOGIN) - time_to_sec(concat(substr(LOGIN, 1,10), ' ' , '08:00:00'))) - time_to_sec('08:00:00')) when time_to_sec(time(LOGIN)) BETWEEN time_to_sec('08:00:00') AND time_to_sec('08:59:00') AND time_to_sec(time(LOGOUT)) BETWEEN time_to_sec('12:01:00') AND time_to_sec('18:00:00') then sec_to_time(time_to_sec(concat(substr(LOGOUT, 1,10), ' ' , '08:00:00')) + (time_to_sec(LOGOUT) - time_to_sec(concat(substr(LOGOUT, 1,10), ' ' , '18:00:00'))) + time_to_sec(concat(substr(LOGIN, 1,10), ' ' , '08:00:00')) - (time_to_sec(LOGIN) - time_to_sec(concat(substr(LOGIN, 1,10), ' ' , '08:00:00'))) - time_to_sec('08:00:00')) END") or die(mysql_error()); $result = mysql_query("INSERT INTO payroll.reg_hours(EMP_NO, Hours) SELECT EMP_NO, sec_to_time(SUM(time_to_sec(Rendered))) FROM payroll.reg_att GROUP BY EMP_NO") or die(mysql_error()); $sql = "SELECT MIN(DATE(LOGIN)) AS FDATE, MAX(DATE(LOGIN)) AS LDATE FROM payroll.reg_att"; $rsDate = $conn2->Execute($sql); $FDATE = $rsDate->fields['FDATE']; $LDATE = $rsDate->fields['LDATE']; $smarty->assign('FDATE', $FDATE); $smarty->assign('LDATE', $LDATE); $smarty->display('header_att.tpl'); $smarty->display('RegAttendance.tpl'); $smarty->display('footer.tpl'); ?> I have a sorting functionality, the code is he <?php $select_category = $_REQUEST['sort_category']; $sort_date_var = $_REQUEST['sort_date']; $sort_submit = $_POST['sortSubmit']; // Connect to the database $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); if (($select_category == 'All') || (!isset($select_category)) && (!isset($sort_date_var))) { // Retrieve the chosen category from MySQL $query = "SELECT * FROM con"; $data = mysqli_query($dbc, $query); //Loop through the array of data while ($row = mysqli_fetch_array($data)) { echo "<table class='knuffixTable'>"; // Display the score data echo "<tr><td class='knuffix_name'>"; echo "<strong>" . htmlentities($row['name']) . "</strong><br /></td></tr>"; echo "<tr><td class='knuffix_contribution'><pre>" . $row['contribution'] . "</pre><br /></td></tr>"; echo "<tr><td class='knuffix_categoryDate'>" . $row['category'] . " | " . date('M d, Y', strtotime($row['contributed_date'])) . " </td></tr>"; echo "</table>"; } mysqli_close($dbc); } elseif (isset($select_category) && !isset($sort_date_var)) { // Retrieve the chosen category from MySQL $query = "SELECT * FROM con WHERE category = '$select_category'"; $data = mysqli_query($dbc, $query) or die (mysqli_error($dbc)); //Loop through the array of data while ($row = mysqli_fetch_array($data)) { echo "<table class='knuffixTable'>"; // Display the score data echo "<tr><td class='knuffix_name'>"; echo "<strong>" . htmlentities($row['name']) . "</strong><br /></td></tr>"; echo "<tr><td class='knuffix_contribution'><pre>" . $row['contribution'] . "</pre><br /></td></tr>"; echo "<tr><td class='knuffix_categoryDate'>" . htmlentities($row['category']) . " | " . date('M d, Y', strtotime($row['contributed_date'])) . " </td></tr>"; echo "</table>"; } mysqli_close($dbc); } elseif (!isset($select_category) && isset($sort_date_var)) { // Retrieve the chosen category from MySQL $query = "SELECT * FROM con ORDER BY contributed_date $sort_date_var"; $data = mysqli_query($dbc, $query) or die (mysqli_error($dbc)); //Loop through the array of data while ($row = mysqli_fetch_array($data)) { echo "<table class='knuffixTable'>"; // Display the score data echo "<tr><td class='knuffix_name'>"; echo "<strong>" . htmlentities($row['name']) . "</strong><br /></td></tr>"; echo "<tr><td class='knuffix_contribution'><pre>" . $row['contribution'] . "</pre><br /></td></tr>"; echo "<tr><td class='knuffix_categoryDate'>" . htmlentities($row['category']) . " | " . date('M d, Y', strtotime($row['contributed_date'])) . " </td></tr>"; echo "</table>"; } mysqli_close($dbc); } elseif (isset($select_category) && isset($sort_date_var)) { // Retrieve the chosen category from MySQL $query = "SELECT * FROM con WHERE category = '$select_category' ORDER BY contributed_date $sort_date_var"; $data = mysqli_query($dbc, $query) or die (mysqli_error($dbc)); //Loop through the array of data while ($row = mysqli_fetch_array($data)) { echo "<table class='knuffixTable'>"; // Display the score data echo "<tr><td class='knuffix_name'>"; echo "<strong>" . htmlentities($row['name']) . "</strong><br /></td></tr>"; echo "<tr><td class='knuffix_contribution'><pre>" . $row['contribution'] . "</pre><br /></td></tr>"; echo "<tr><td class='knuffix_categoryDate'>" . htmlentities($row['category']) . " | " . date('M d, Y', strtotime($row['contributed_date'])) . " </td></tr>"; echo "</table>"; } mysqli_close($dbc); } ?> With this code I'm able to: - showcase every category by DEFAULT when someone comes to the page. - sort only by category, - sort only by date, - sort by category AND date, The problem is as you can see I have a lot of duplicate code, if I for example want to change the table that is being printed out, I have to change it on all of them. I'd like to ask how I could avoid this duplicate code. Can I for example just have the table ONE TIME at a separate place and then insert the REFERENCE _after_ the query in each if statement? Like this: if (($select_category == 'All') || (!isset($select_category)) && (!isset($sort_date_var))) { // Retrieve the chosen category from MySQL $query = "SELECT * FROM con"; $data = mysqli_query($dbc, $query); table_here(); } elseif (isset($select_category) && !isset($sort_date_var)) { // Retrieve the chosen category from MySQL $query = "SELECT * FROM con WHERE category = '$select_category'"; $data = mysqli_query($dbc, $query) or die (mysqli_error($dbc)); table_here(); } I'm just coding since 2 month so I don't know how do it, maybe I can do it with functions? I've tried doing it with functions, but I got an error since the function doesn't contain any query statement and only the table because it was trying to fetch. it looked like this: function table_here () { //Loop through the array of data while ($row = mysqli_fetch_array($data)) { echo "<table class='knuffixTable'>"; // Display the score data echo "<tr><td class='knuffix_name'>"; echo "<strong>" . htmlentities($row['name']) . "</strong><br /></td></tr>"; echo "<tr><td class='knuffix_contribution'><pre>" . $row['contribution'] . "</pre><br /></td></tr>"; echo "<tr><td class='knuffix_categoryDate'>" . $row['category'] . " | " . date('M d, Y', strtotime($row['contributed_date'])) . " </td></tr>"; echo "</table>"; } mysqli_close($dbc); } Hi. I'm new to PHP and Wordpress, quite the beginner. I thought I finally had it all done on my site and I've stumbled on another issue. Basically I post a link and it spits it out. I sometimes post links that have the same domain ($content['host']) but I don't want these to be duplicated. Here's what I have: Code: [Select] <?php query_posts(''); ?> <?php while (have_posts()) : the_post(); ?> <li> <a href="http://<?php $content = parse_url(strip_tags(get_the_content())); echo $content['host']; ?>"> <?php $content = parse_url(strip_tags(get_the_content())); echo $content['host']; ?></a> </li> <?php endwhile;?> I've Googled around for a while, reading posts about how to prevent duplicate posts in multiple loops but can't adapt it to mine. I'd really appreciate any advice. Cheers, Craig. Hi I am getting this error message and don't know why. please help this is the message " Error: Duplicate entry '' for key 1 " Here is the code Code: [Select] <?php include'../DB-connection.php'; $sql="INSERT INTO Company(CompanyName,Address,Logo,PhoneNumber,ContactPerson) VALUES('".mysql_real_escape_string($_POST[CompanyName])."', '".mysql_real_escape_string($_POST[Address])."', '".mysql_real_escape_string($_POST[Logo])."', '".mysql_real_escape_string($_POST[PhoneNumber])."', '".mysql_real_escape_string($_POST[ContactPerson])."')"; if(!mysql_query($sql,$con)) { die(' Error: '. mysql_error()); } echo " <center>your compnay info added       <a href='User-Login.php'>Login to Post a job</a> </center>" ; mysql_close($con) ?> |