我想传递一个PHP生成的按钮从theory.php
文件到theory1.php
文件的id值。下面是代码:
//theory.php file
require('components/db.php');
$query = "SELECT * FROM `courses`";
$result = mysqli_query($connect, $query) or die("Error:" . mysqli_error($connect));;
$numrows = mysqli_num_rows($result);
for ($i = 0; $i < $numrows; $i++) {
$query = "SELECT * FROM `courses` WHERE courseID = '$i'";
$result = mysqli_query($connect, $query) or die("Error:" . mysqli_error($connect));;
$rowQuery = mysqli_fetch_assoc($result);
$_SESSION['course_ID'] = $i;
echo '
<div class="card">
<img class = "cardImage" src="';
echo $rowQuery['imageLink'];
echo '" alt="Course 1">
<h3>';
echo $rowQuery['courseName'];
echo '</h3>
<p>';
echo $rowQuery['courseTextOne'];
echo '</p>
<a href="theory1.php?course_ID=$i" class="button">Proceed</a>
</div>'; //a - is a button which needs to have an ID to pass to theory1.php
}
代码生成带有按钮的卡片。我希望每个按钮存储从MySQL数据库的课程各自的ID。该ID需要根据所单击的按钮(卡片)传递到另一个页面,以便将来可以从数据库检索正确的数据。
解决方案
theory.php:
require('components/db.php');
$query = "SELECT * FROM `courses`";
$result = mysqli_query($connect, $query) or die("Error:" . mysqli_error($connect));;
$numrows = mysqli_num_rows($result);
for ($i = 0; $i < $numrows; $i++) {
$query = "SELECT * FROM `courses` WHERE courseID = '$i'";
$result = mysqli_query($connect, $query) or die("Error:" . mysqli_error($connect));;
$rowQuery = mysqli_fetch_assoc($result);
$_SESSION['course_ID'] = $i;
echo '
<div class="card">
<img class = "cardImage" src="';
echo $rowQuery['imageLink'];
echo '" alt="Course 1">
<h3>';
echo $rowQuery['courseName'];
echo '</h3>
<p>';
echo $rowQuery['courseTextOne'];
echo '</p>
<a href="theory1.php?courseID=';
echo $i;
echo '"class="button">Перейти</a>
</div>'; //a - is a button which needs to have an ID to pass to theory1.php
}
theory1.php:
<?php
$courseID = $_GET['courseID'];
echo $courseID;
?>