Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
339 views
in Technique[技术] by (71.8m points)

javascript - 如何使用php动态更改html链接的ID?(how to dynamically change the id of a html link with php?)

I have links which is on one page and and I sent the links to the database and then on another page I am retrieving the the links from the database.(我在一页上有链接,然后将链接发送到数据库,然后在另一页上,我正在从数据库中检索链接。)

I can only send four links to the database, I made it that way, but I want the four links to have different id's but the ids should be the same even if the user sends different links to the database.(我只能这样发送四个链接到数据库,但是我希望这四个链接具有不同的ID,但是即使用户向数据库发送不同的链接,ID也应该相同。) for example: if the links are:(例如:如果链接是:) <a onclick='cover'>ACCOUNTING</a> <a onclick='cover'>maths</a> <a onclick='cover'>english</a> <a onclick='cover'>ict</a> the ID's should be something like this:(ID应该是这样的:) <a onclick='cover' id='10'>ACCOUNTING</a> <a onclick='cover' id='11'>maths</a> <a onclick='cover' id='12'>english</a> <a onclick='cover' id='13'>ict</a> even if the links sent are different the ids of the four links should still be 10,11,12 and 13.(即使发送的链接不同,四个链接的ID仍应为10、11、12和13。) here is some of mycode: page1:(这是一些mycode:page1:) if(isset($_POST['English'])) { //his is to make the categpories shows show in the maijn page headerr("Location:layout.php?userid=${id}&name=${name}&id=1"); array_push($sub,mysqli_real_escape_string($conn,"<li><a onclick='cover'>ENGLISH</a></li>")); $plo = implode(" ",$sub); $query = "UPDATE `user` SET `sub`='$plo' WHERE `id` = '$id'"; $clicked = $clicked +1; $result = mysqli_query($conn,$query); if($result) { echo "done"; $name .= 'eng,'; } else{ echo "not done"; echo $query; } } if(isset($_POST['maths'])) { headerr("Location:layout.php?userid=${id}&name=math&id=1"); array_push($sub,mysqli_real_escape_string($conn,"<li><a onclick='cover'>MATHEMATICS</a></li>")); $plo = implode(" ",$sub); $query = "UPDATE `user` SET `sub`='$plo' WHERE `id` = '$id'"; $clicked = $clicked +1; $result = mysqli_query($conn,$query); if($result) { echo "done"; $name .= 'math,'; } else{ echo "not done"; echo $query; } } if(isset($_POST['accounting'])) { headerr("Location:layout.php?userid=${id}&name=accounting&id=1"); array_push($sub,mysqli_real_escape_string($conn,"<li><a onclick='cover'>ACCOUNTING</a></li>")); $plo = implode(" ",$sub); $query = "UPDATE `user` SET `sub`='$plo' WHERE `id` =$id"; $clicked = $clicked +1; $result = mysqli_query($conn,$query); if($result) { echo "done"; $name .= 'accounting,'; } else{ echo "not done"; echo $query; } } page2:(第2页:) <?php $userid = $_GET['userid']; $name =$_GET['name']; $cler = explode(',',$name); $i= 0; $dog =array_filter($cler); $query ="SELECT sub from user where id = '$userid'"; $result = mysqli_query($conn,$query); if($row = mysqli_fetch_assoc($result)) { $t = $row['sub']; echo $i; echo $t; //this is to change the position of the quetion } ?> thanks!!!(谢谢!!!)   ask by SMART G. translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

welcome to SO :)(欢迎来到:))

I am not sure that i am following you to a 100% on the question.(我不确定我是否会在这个问题上全力以赴。) If i understand it correctly you want the id's to remain the same with different links fetched from the database?(如果我正确理解,您希望ID与从数据库中获取的不同链接保持相同吗?) I would create an attribute in the database for the id of the link, not using increment or primary key, just an int value.(我会在数据库中为链接的ID创建一个属性,而不使用增量或主键,而只是一个int值。) When the link is fetched into the html document you use the id attribute value as the id of the element.(将链接提取到html文档中后,您可以使用id属性值作为元素的id。) To avoid unexpected id's i would limit the user input into the database for that specific attribute to 10, 11, 12 and 13 with for example a select element,(为了避免出现意外ID,我会使用例如select元素将特定属性的用户输入限制为10、11、12和13,) use php to create the links like:(使用php创建如下链接:) <?php echo "<a onclick='cover' id='database_var_accounting'>ACCOUNTING</a> <a onclick='cover' id='database_var_maths'>maths</a> <a onclick='cover' id='database_var_englist'>english</a> <a onclick='cover' id='database_var_ict'>ict</a>" ?> The id is variables you've assigned with the value from the database, then simply import the php script into your html/php page where you want the menu.(id是从数据库中分配了值的变量,然后只需将php脚本导入想要菜单的html / php页面即可。) hope i understood your question :)(希望我理解你的问题:))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...