Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Oct 2010
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Set active page based on $_GET[] and mysql_fetch_object in php

    Hi All,

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

    PHP Code:
    //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:

    PHP Code:
    //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:
    PHP Code:

        
    if($id == $ids){
                    
    $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";
            } 
    While the above doesn't throw any errors, it doesn't add the class name to the H4 tag. Any help or suggestions appreciated.
    Thanks.

  • #2
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    try this
    PHP Code:
    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";
        }


  • #3
    New Coder
    Join Date
    Oct 2010
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hi Poyzn

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

    PHP Code:
    if(in_array($id$ids[$postyear])) 
    Thank you!
    Gerry


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •