View Full Version : Set active page based on $_GET[] and mysql_fetch_object in php

12-09-2010, 07:25 PM
Hi All,

I am using PHP's $_GET function to grab an article by its id and display it on a page like below:

//First grab the associated article id from the index page link and display the full article on this page
$id = $_GET['sid'];

//Now build the sql to grab the data in the sightings table in mysql
$sqlCommand = mysql_query("SELECT * FROM sightings WHERE sid='$id'")or die (mysql_error());

//now loop through the data in the articles table
while($row = mysql_fetch_array($sqlCommand)){
$id = $row['sid'];
$catid = $row['scid'];
$title = $row['location'];
$pic = $row['picURL'];
$picAlt = $row['picALT'];
$picTitle = $row['picTitle'];
$content = $row['description'];
$date = $row['sighting_date'];

Next I have constructed another sql query to build a right-hand navigation menu. I have constructed the menu to order all article titles by year using the mysql_fetch_object as below:

//build the sightings article title menu for the right navigation
$query = mysql_query("SELECT sightings.sid
, sightings.scid
, sightings.location
, sightings.sighting_date
, scategories.scategory
, scategories.sidDir
, scategories.details
, scategories.scid
, DATE_FORMAT (sighting_date,'%W, %M %e, %Y') as postdate
, DATE_FORMAT (sighting_date, '%Y') as postyear
FROM sightings
LEFT JOIN scategories
ON scategories.scid
WHERE sightings.scid = scategories.scid
AND sightings.scid = 1
ORDER BY sighting_date DESC") or die (mysql_error());

$response ="";

while($row = mysql_fetch_object($query)) {
$set[$row->postyear][] = $row->location;
$ids[$row->postyear][] = $row->sid;
$csDir[$row->postyear][] = $row->sidDir;
$cPage[$row->postyear][] = $row->details;
$dates[$row->postyear][] = $row->postdate;

//below orders the data by year
foreach ($set as $postyear => $rows) {

$response .= "<h4><a href=\"javascript:;\">Articles in {$postyear}</a></h4>\n";
$response .= "<ul>";
foreach ($rows as $key => $entry_name) {
$response .= "\n\t<li class=\"bullet1\"><a href=\"{$cPage[$postyear][$key]}?sid={$ids[$postyear][$key]}\" title=\"Posted: {$dates[$postyear][$key]}\">{$entry_name}</a></li>";

$response .= "\n</ul>\n";


Everything works perfectly but now I want to add a css class to the <h4> tag where $id equals $ids. This is where I'm getting stuck. I have tried adding a conditional statement after the first for loop like below:

if($id == $ids){
$response .= "<h4 class=\"archiveSelected\"><a href=\"javascript:;\">Cicada Sightings {$postyear}</a></h4>\n";
$response .= "<h4><a href=\"javascript:;\">Cicada Sightings {$postyear}</a></h4>\n";

While the above doesn't throw any errors, it doesn't add the class name to the H4 tag. Any help or suggestions appreciated.

12-09-2010, 10:19 PM
try this

foreach ($set as $postyear => $rows) {
if(in_array($id, $ids[$postyear])){
$response .= "<h4 class=\"archiveSelected\"><a href=\"javascript:;\">Cicada Sightings {$postyear}</a></h4>\n";
} else {
$response .= "<h4><a href=\"javascript:;\">Cicada Sightings {$postyear}</a></h4>\n";

12-10-2010, 05:20 PM
Hi Poyzn

Thanks very much for that little code snippet. I didn't know this could be done:

if(in_array($id, $ids[$postyear]))

Thank you!