Hello everyone,
I am trying to put a value into $value;
after the value is clicked the count stays at one or it resets itself any help will be awesome

Thank you.

<table width="61%" border="1" align="center" cellpadding="4">
<?php

$count = 1;
if (isset($_GET['value'])) {
	$count++;
	$value[$count] = $_GET['value'];
	print "<tr><td><big>You clicked: $value[$count]</big></td></tr>";
}
$data = 10;
for ($i=1; $i<=$data; $i++) {
    print "<tr><td><a href=\"?value=$i\">Data: $i</a></td></tr>";
}
?>
</table>

Dani AI

Generated

Your script sets $count = 1 at the top on every request, so the variable is reinitialized each time the page reloads. That is why the click count never grows. 's debug echo confirms the value seen on a single run, but it does not make the server remember the previous value. To persist a counter across clicks you must send it back with the request (query string or hidden form field), store it on the server (session, database) or in the browser (cookie/localStorage).

A minimal, safe query-string approach (passes the counter in the URL and sanitizes input):

<?php
$clickCount = isset($_GET['c']) ? intval($_GET['c']) : 0;
$last = null;
if (isset($_GET['value'])) {
  $clickCount++;
  $last = htmlspecialchars($_GET['value'], ENT_QUOTES, 'UTF-8');
}
echo "You clicked: " . ($last ?? 'none') . " (count: $clickCount)";
for ($i = 1; $i <= 10; $i++) {
  echo '<a href="?value=' . $i . '&c=' . $clickCount . '">Data: ' . $i . '</a> ';
}

A session-based solution keeps the count server-side and is cleaner for per-user state:

<?php
session_start();
if (!isset($_SESSION['clickCount'])) $_SESSION['clickCount'] = 0;
if (isset($_GET['value'])) {
  $_SESSION['clickCount']++;
  $_SESSION['lastValue'] = htmlspecialchars($_GET['value'], ENT_QUOTES, 'UTF-8');
}
echo 'Last: ' . ($_SESSION['lastValue'] ?? 'none') . ' (count: ' . $_SESSION['clickCount'] . ')';

Quick tips: always call session_start() before output, cast GET values with intval() when numeric, use htmlspecialchars() for HTML output, and enable error_reporting(E_ALL) while debugging to catch undefined-index notices. If tracking clicks longer-term or across users, persist counts in a database rather than relying on query strings.

Recommended Answers

All 2 Replies

Seems to work fine for me, I modified it a little so you could see the value of $value[$count] at the bottom of the script

<table width="61%" border="1" align="center" cellpadding="4">
<?php

$count = 1;
if (isset($_GET['value'])) {
	$count++;
	$value[$count] = $_GET['value'];
	print "<tr><td><big>You clicked: $value[$count]</big></td></tr>";
}
$data = 10;
for ($i=1; $i<=$data; $i++) {
    print "<tr><td><a href=\"?value=$i\">Data: $i</a></td></tr>";
}
?>
</table>
<br />value of $value[$count] = <?php echo $value[$count]; ?>

Thanks R0bb0b,
I added this at then very end of the code and count doesn't seem to increase, please help thank yo

<br />value $count = <?php echo $count ?>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.