Alex Goaga Alex Goaga - 4 months ago 4x
PHP Question

Can you put multiple elements in an html <a> tag?

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

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>';


I recommend not to try managing numbered variable names. If there is no important reason to do so, it will make your logic unnecessarily complicated.

PHP understands array parameters in e.g. $_GET. They are passed from HTML with empty braces appended to the parameter name.

This is a little demo to illustrate this alternative approach:

// 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>

Be aware, that the second link generated by http_build_query contains indexes of the array, which are commonly based on 0.