I am new to PHP+MySQL and followed an example to build a web page.
Basically, my front page reads the databas, retrieves all the items, and lists on the page. Each item has a link to a new page showing its details.
the list page:
//itemlist.html.php
<html><body>
<ul>
<?php foreach ($items as $item): ?>
<li><a href="?C1=<?php echo($item['c1']); ?>">
<?php echo($item['c1']); ?></a></li>
<?php endforeach; ?>
</ul>
</body></html>
the item page:
//itemeach.html.php
<html><body>
<h1><?php echo($c1) ?></h1>
<p><?php echo($c2) ?></p>
</body></html>
the index page:
<?php
if (isset($_GET['C1']))
{
/*
I want to reuse $iteminfos and get value for $c1 and $c2
*/
include 'itemeach.html.php';
exit();
}
$result = mysqli_query($link, 'SELECT * FROM TB');
while ($row = mysqli_fetch_array($result))
{
$items[] = array('c1' => $row['c1'], 'c2' => $row['c2']);
}
include 'itemlist.html.php';
?>
I removed the unnecessary code. Since when the first time it loads index.php, $infos is retrieved. I want to avoid accessing to database again to get one specific item, so I am thinking about re-use $infos. But it becomes null when I trigger ‘C1’. Is this possible to keep $items as a global variable? or is there other way to do that?
Any help would be greatly appreciated!
You could store the retrieved rowset in the $_SESSION superglobal. Like:
But yeah what Dagon said is true.