I'm new to coding php, I had first a problem but someone on this site helped me thanks for that! I've fixed when you're on site.com/category.php you see all category's and when you on site.com/category.php?p=4 for like example you will see the same as on category.php but he only takes the ID. I want now something else when i go to category.php?p=4 only description here below is my code.
<?php
include 'config.php';
// create a where clause
if (isset($_GET['p'])) $where="nameID=".(int)$_GET['p'];
else $where=1;
// select all or specific using the where clause
$mysql = "SELECT * FROM category WHERE ".$where;
$result = mysql_query($mysql);
// if there are rows returned
if (mysql_num_rows($result)){
// iterate
while($data=mysql_fetch_array($result)){
{
echo "" . $data['description'] . ""; //displays only when you're on category.php?p=4
}
// only want to display this on category.php
echo "<li><a href=\"category.php?p=" . $data['nameID'] . "\"><b>" . $data['name'] . "</b></a> added on: " . $data['date'] . ", about this game <b>" . $data['description'] . "</b>";
}
}else{
echo 'Nothing to find here';
}
?>
I guess this is what you need / mean if not please comment so that I can edit it:
<?php
include 'config.php';
$id = $_GET['p']; //get the p (id) from the url
//check if the p (id) is in the url and not empty
if(isset($id) && !empty($id)) {
//the query
$query = mysql_query("SELECT * FROM `category` WHERE `nameID` = '$id'");
$count = mysql_num_rows($query);
if($count >= 1) {
while($associate = mysql_fetch_assoc($query)) {
echo $associate['description'];
}
}
} else {
$query = mysql_query("SELECT * FROM `category`");
$count = mysql_num_rows($query);
if($count >= 1) {
while($associate = mysql_fetch_assoc($query)) {
echo '<li><a href="category.php?p='.$associate['nameID'].'"><b>'.$associate['name'].'</b></a> added on: '.$associate['date'].', about this game <b>'.$associate['description'].'</b>';
}
} else {
echo 'nothing found';
}
}
?>
check your select query $mysql = "SELECT * FROM category WHERE ".$where;
It is incomplete.
It should be like
`$mysql = "SELECT * FROM category WHERE id like '$where'
or
`$mysql = "SELECT * FROM category WHERE id ='$where'
Use if statements on whether the p
URL parameter is set.
Put the parts you only want to display on the category.php?p=... pages in
if(isset($_GET['p']))
and the parts you want to display on the category.php page in
if(!isset($_GET['p']))
You should also look to replace mysql with mysqli or PDO, and if you are using the <b>
tags for styling you should replace these with <span style='font-weight:bold'>