canabatz Posted December 1, 2011 Share Posted December 1, 2011 Hi ,im face little problem in duplicating rows. it's not easy task wht i need: i got this rows right now id----------position-------------A_Q_ID 1---------------1--------------------5----- 2---------------2--------------------5----- 3---------------3--------------------5----- 4---------------1--------------------6----- 5---------------2--------------------6----- 6---------------3--------------------6----- what i need is to duplicat all the rows and have them inserted after the rows i allready have like that: id------------position-------------A_Q_ID 7-----------------1--------------------7----- 8-----------------2--------------------7----- 9-----------------3--------------------7----- 10---------------1--------------------8----- 11---------------2--------------------8----- 12---------------3--------------------8----- i tryed few ways without success . any idea how to do that please? i cannot get the logic on doing it thanks Quote Link to comment https://forums.phpfreaks.com/topic/252205-duplicating-rows/ Share on other sites More sharing options...
trq Posted December 1, 2011 Share Posted December 1, 2011 First, why would you want duplicate data? Quote Link to comment https://forums.phpfreaks.com/topic/252205-duplicating-rows/#findComment-1292986 Share on other sites More sharing options...
canabatz Posted December 1, 2011 Author Share Posted December 1, 2011 thats a funny answer i felt bored so i ask this question i need it my friedn , im duplicating few tables and this one is my problem. im building a survey website ,and i want to build a ready made survey so people can duplicate ready made survey to there account. everything is working fine , only this table is hard... thanks Quote Link to comment https://forums.phpfreaks.com/topic/252205-duplicating-rows/#findComment-1292992 Share on other sites More sharing options...
trq Posted December 1, 2011 Share Posted December 1, 2011 That really doesn't answer the question. Data duplication is a sign of poor design. Without any valid reason as to why, I am very reluctant to let you know how. Quote Link to comment https://forums.phpfreaks.com/topic/252205-duplicating-rows/#findComment-1292997 Share on other sites More sharing options...
canabatz Posted December 1, 2011 Author Share Posted December 1, 2011 im building a survey website ,and i want to build a ready made survey so people can duplicate ready made survey to there account. that's not explaining what i need? you dont need to help if you dont know how to do it. people asking question when they need help. if you see some websites i develop then you wont say poor design , you dont know who you talking to!! thanks any way for your "help" Quote Link to comment https://forums.phpfreaks.com/topic/252205-duplicating-rows/#findComment-1292998 Share on other sites More sharing options...
litebearer Posted December 1, 2011 Share Posted December 1, 2011 1. People who help here do so out of courtesy and compassion 2. The particular person who has been attempting to help you DOES know what he is talking about 3. Rudeness does NOTHING to help you 4. When a question/request makes NO sense AND the person asking remains closed minded as to PROPER solutions, it makes it very very difficult for other to feel any compassion toward their situation. Perhaps an apology to Thorpe and the community as a whole is in order (and maybe an attitude adjustment) Quote Link to comment https://forums.phpfreaks.com/topic/252205-duplicating-rows/#findComment-1293000 Share on other sites More sharing options...
trq Posted December 1, 2011 Share Posted December 1, 2011 Thanks litebearer, but I do have a tendency to be pretty blunt. Mostly due to frustration. I know I shouldn't bother. To the op: Think about it. Why would you need multiple copies of the same survey stored? You don't. Simple. Duplicate data almost always points to poor design. A better question seeing as you don't seem to understand how to do it well is. "How could I link multiple surveys to multiple accounts" or similar. You basically need three tables. One to store all the questions, each row also needs a column linking it to the next table. The next table could store the name of the server and the id linking it to the previous table. You then use this same id to link each survey to a users account. Quote Link to comment https://forums.phpfreaks.com/topic/252205-duplicating-rows/#findComment-1293001 Share on other sites More sharing options...
canabatz Posted December 1, 2011 Author Share Posted December 1, 2011 Rudeness? it's you! i was asking a simple question to problem im trying to fix. you said that it is poor desing!! not me. dont judge people with what you know! probably you don't know everything. and please , if you want to help do that. Quote Link to comment https://forums.phpfreaks.com/topic/252205-duplicating-rows/#findComment-1293004 Share on other sites More sharing options...
trq Posted December 1, 2011 Share Posted December 1, 2011 Buddy, I have't said anything rude, if you read my last reply you will see I offer a proper solution to your problem. Quote Link to comment https://forums.phpfreaks.com/topic/252205-duplicating-rows/#findComment-1293006 Share on other sites More sharing options...
canabatz Posted December 1, 2011 Author Share Posted December 1, 2011 Now you speaking nice, thanks. what i need is a simple way to do it. there is 4 tables im copying data from . 3 tables are ok . 1 table making me problem. thanks Quote Link to comment https://forums.phpfreaks.com/topic/252205-duplicating-rows/#findComment-1293009 Share on other sites More sharing options...
trq Posted December 1, 2011 Share Posted December 1, 2011 I already explained how to do it. Where exactly are you stuck? Quote Link to comment https://forums.phpfreaks.com/topic/252205-duplicating-rows/#findComment-1293010 Share on other sites More sharing options...
canabatz Posted December 1, 2011 Author Share Posted December 1, 2011 im stuck here: i got this for example , this i s not the real table, the real table is very complicated. i can copy the all table and it's fine, but the A_Q_ID is my problem. what i try'd was to get the MAX(A_Q_ID) to add +1 to the duplicated data. but there is 3 in this example with same id. id----------position-------------A_Q_ID 1---------------1--------------------5----- 2---------------2--------------------5----- 3---------------3--------------------5----- 4---------------1--------------------6----- 5---------------2--------------------6----- 6---------------3--------------------6----- what i need to have after the copy is like that: result example: id------------position-------------A_Q_ID 7-----------------1--------------------7----- 8-----------------2--------------------7----- 9-----------------3--------------------7----- 10---------------1--------------------8----- 11---------------2--------------------8----- 12---------------3--------------------8----- how can i add +1 to the duplicated A_Q_ID for the result table in my example? thanks Quote Link to comment https://forums.phpfreaks.com/topic/252205-duplicating-rows/#findComment-1293017 Share on other sites More sharing options...
trq Posted December 1, 2011 Share Posted December 1, 2011 Did you actually read my reply? Why are you still trying to duplicate your data? Quote Link to comment https://forums.phpfreaks.com/topic/252205-duplicating-rows/#findComment-1293019 Share on other sites More sharing options...
canabatz Posted December 1, 2011 Author Share Posted December 1, 2011 i told you. im building a survey site. users can build surveys and send them to who they want. the client who i made this site for him ,he want to build Template survey's so the users can copy them to they account and modify them for what they need. what i did was to create one account and build few templates and add a link to see surveys example . on every example survey i will add a copy button so register users can copy the survey to they account. each survey are using 3 different tables. 1 survey 2 survey question 3 survey answer im stuck with the survey question. normaly when you add new question it gives it a question ID. in my example ,a question have 3 answer options. so you have question position for the 3 answer ,and question id is the same. what i need is to duplicate survey with more questions . so if A_Q_ID have 1 1 1 2 2 2 the copy i need is 3 3 3 4 4 4. thats it ,sorry for my bad explanation and my bad English thanks Quote Link to comment https://forums.phpfreaks.com/topic/252205-duplicating-rows/#findComment-1293024 Share on other sites More sharing options...
canabatz Posted December 1, 2011 Author Share Posted December 1, 2011 here is the code im using right now to just copy from other survey, only the answer option. $sqlSURVEY_A=mysql_query("SELECT `admin_id`, `ans_id`, `ans_caption`, `ans_position`, `ans_su_id` FROM `xp_survey_answer` where ans_su_id='".$_GET['SUR_ID']."'")or die(mysql_error()); while ($rowSURVEY_A=mysql_fetch_assoc($sqlSURVEY_A)) { $ans_caption= $rowSURVEY_A['ans_caption']; $ans_position= $rowSURVEY_A['ans_position']; $ans_su_id= $rowSURVEY_A['ans_su_id']; $ans_q_id= $rowSURVEY_A['ans_q_id']; // the one i need to get the MAX() and add +1 , if you got 3 answers then it's the same id $copy_AM=mysql_query("INSERT INTO `xp_survey_answer` (`admin_id`, `ans_id`, `ans_caption`, `ans_position`, `ans_su_id`, `ans_q_id`) VALUES ('$adminid','','$ans_caption','$ans_position','$suid','$ans_q_id')")or die(mysql_error()); } Quote Link to comment https://forums.phpfreaks.com/topic/252205-duplicating-rows/#findComment-1293033 Share on other sites More sharing options...
canabatz Posted December 2, 2011 Author Share Posted December 2, 2011 Thanks all for the help. i did it by my self and im happy. maybe it's not the best way but i did some good combinations. $sqlSURVEY_AnsSUid=mysql_query("SELECT * FROM `xp_survey_answer` ")or die(mysql_error()); while ($rowSURVEY_Ans=mysql_fetch_array($sqlSURVEY_AnsSUid)) { $ans_su_id[]=$rowSURVEY_Ans['ans_su_id']; } $str2 = implode(',',array_unique($ans_su_id)); $zz4= count(explode(",",$str2)); echo $zz4."<br>"; $sqlSURVEY_Amax=mysql_query("SELECT max(ans_q_id) as maxq FROM `xp_survey_answer` ")or die(mysql_error()); $rooo=mysql_fetch_assoc($sqlSURVEY_Amax); $maxq=$rooo['maxq']; $sqlSURVEY_Ac=mysql_query("SELECT * FROM `xp_survey_answer` where ans_su_id='".$_GET['SUR_ID']."'")or die(mysql_error()); $a=0; while ($rowSURVEY_Ac=mysql_fetch_array($sqlSURVEY_Ac)) { $ans_q_id[]=$rowSURVEY_Ac['ans_q_id']; echo $ans_q_id[$a]."<br>"; $a++; } $str1 = implode(',',array_unique($ans_q_id)); // add quote to seprate number of strings echo "<br>".$str1; // 33,34 $zz= count(explode(",",$str1)); // count the number to add plus echo "<br>".$zz; // 2 $zz1=explode(",",$str1); $sqlSURVEY_A=mysql_query("SELECT * FROM `xp_survey_answer` where ans_su_id='".$_GET['SUR_ID']."'")or die(mysql_error()); while ($rowSURVEY_A=mysql_fetch_assoc($sqlSURVEY_A)) { $ans_caption= $rowSURVEY_A['ans_caption']; $ans_position= $rowSURVEY_A['ans_position']; $ans_su_id= $rowSURVEY_A['ans_su_id']; $ans_q_id=$rowSURVEY_A['ans_q_id']; for ($i=0; $i<=$zz; $i++) { if ($ans_q_id==$zz1[$i]){$ans_q_id=($ans_q_id+($maxq / $zz) )+ $zz4;} } $copy_AM=mysql_query("INSERT INTO `xp_survey_answer` (`admin_id`, `ans_id`, `ans_caption`, `ans_position`, `ans_su_id`, `ans_q_id`) VALUES ('$adminid','','$ans_caption','$ans_position','$suid','$ans_q_id')")or die(mysql_error()); } Quote Link to comment https://forums.phpfreaks.com/topic/252205-duplicating-rows/#findComment-1293407 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.