Hi - hope someone can help me with this, I think it should be quite easy for some clever person but it's putting my head in a twist trying to think it through!
I have set up a database of vegetables which records (amongst other things) their name, species, the user (i.e site user), when veg were sown and which sowing of the year it was (e.g first sowing in jan, second sowing in march etc etc).
I'm trying to query these records using a GET value passed from another page and generate a list of links through to particular pages containing the full details of each specific sowing for a specific user.
currently I'm just getting one long list of links. What I want is for the page to look like is:
<h1>veg name</h1>
first subspecies name
sow 1 sow2 sown (these should be links)
second subspecies name
sow 1 sow2 sown (these should be links)
My code is below
<?php
session_start();
// Require the dbc_connect file before any PHP code:
require_once('./includes/dbc_connect.php');
// Check for a category ID in the URL:
$vegetable = NULL;
$user_id=$_SESSION['user_id'];
if (isset($_GET['veg_id'])) {
// Typecast it to an integer:
$veg_id = (int) $_GET['veg_id'];
// An invalid $_GET['veg_id'] value would
// be type-casted to 0.
// $veg_id must have a valid value.
if ($veg_id > 0) {
// Get the information from the database
// for this category:
$q = "SELECT vegetable_type
FROM vegetables, users
WHERE vegetable_pk=$veg_id
AND user_pk=$user_id";
$r = mysqli_query($dbc, $q);
// Fetch the information:
if (mysqli_num_rows($r) ==1) {
list ($vegetable) = mysqli_fetch_array($r, MYSQLI_NUM);
} // End of mysqli_num_rows() IF.
} // End of ($veg_id > 0) IF.
} // End of isset($_GET['veg_id']) IF.
// Use the category as the page title:
if ($vegetable) {
$page_title = $vegetable;
}
if ($vegetable) { // Show the veg.
echo "<h1>$vegetable</h1>\n";
// Get the vegetables in this category:
$q = "SELECT name, name_pk, sow_number
FROM names, vegetables, sowings, users
WHERE sowings.vegetable_fk=12
AND users.user_pk=15
AND users.user_pk=sowings.user_fk
AND sowings.vegetable_fk=names.vegetable_pk
AND vegetables.vegetable_pk=sowings.vegetable_fk";
$r = mysqli_query($dbc, $q);
if (mysqli_num_rows($r) > 1) {
// Print each:
while (list($name, $name_pk, $sow_number) = mysqli_fetch_array($r, MYSQLI_NUM)) {
// Link to the product.php page:
echo "<h2><a href=\"specific_veg.php?name_id=$name_pk&sow_number=$sow_number\">$name</a></h2>\n";
} // End of while loop.
} else { // No veg here!
echo '<p class="error">There are no veg in this category.</p>';
}
} else { // Invalid $_GET['veg_id']!
echo '<p class="error">This page has been accessed in error.</p>';
}
?>
Any help much appreciated 🙂