你能在html <a> tag?

时间:2016-06-13 19:54:53

标签: php html

Is there any way to create a new a href that remembers all the submitted data earlier. I don't know how to say it properly so I will describe in the code:

The first button

<a href="thesamefile.php?n1='.$data_from_db['id'].'"> Click me </a>

After the user clicks it, he is redirected to the same page but with the new button :

<a href="thesamefile.php?n1='.$GET the same as before data?...['id'].'&n2='.$data_from_db['id'].'"> Click me </a>

And so on :

<a href="thesamefile.php?n1='.$GET the same as before data?....['id'].'&n2='.$data_from_db['id'].'&n3='.$data_from_db['id'].'"> Click me </a>     

How do i create the n variable to be added and increased after the button is pressed ?

( I have a table displayed from a database by using the while command with mysqli_fetch_array($database); )

The table is created like (trivial ) : $retrieve_items = mysql_query("SELECT * FROM items WHERE id > 0");

$col = 0; 
echo '<table width=100% border= 1><tr>';
while($row = mysql_fetch_array( $retrieve_items )) {
$col ++;
echo '<td>'.$row['name_item'].'</td>';

 if ($col % 5 == 0 )
   {
    echo '</tr><tr>';

     }
  }

 echo '</tr></table>'; 

2 个答案:

答案 0 :(得分:0)

在php中,要访问当前查询String,可以使用

$_SERVER['QUERY_STRING']

所以你可以像

那样
<a href="thesamefile.php?<?php echo $_SERVER['QUERY_STRING'] ?>&othervalue=123"> Click me </a>

或者您可以替换查询字符串中的先前值

答案 1 :(得分:0)

我建议不要尝试管理编号的变量名称。如果没有重要的理由这样做,那将使你的逻辑变得不必要地复杂化。

PHP理解数组中的数组参数。 $_GET。它们从HTML传递,空括号附加到参数名称。

这是一个演示这种替代方法的小演示:

<?php
// get the passed array or generate a new one
$n = isset($_GET['n']) ? (array) $_GET['n'] : [];

//ppend 2 random numbers
$n[] = rand(1,100);
$n[] = rand(1,100);

//output the link with GET parameters in query
?>
<a href="?n[]=<?php echo implode('&n[]=', $n) ?>">the link</a>

<!-- or let PHP's built-in generate a propper query --><br>
<a href="?<?php echo http_build_query(['n' => $n]); ?>">the link</a>

请注意,http_build_query生成的第二个链接包含数组的索引,通常基于0。