Results 1 to 9 of 9

Thread: hide the class after first row

  1. #1
    Join Date
    May 2006
    Posts
    266
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default hide the class after first row

    Hi i need away to hide the class style after the first row.
    At the moment it is giving it to all rows.
    any help welcomed

    Code:
    <?php
    
      $sql = "SELECT * FROM jos_video ORDER BY id";
      $result = mysql_query($sql);
    
    while($row = mysql_fetch_assoc($result))
    {
        echo "<a href=\"{$row['url']}\"";?> class=\"first\" <? echo ">
    				{$row['artist']}<br />
    				<em>{$row['song']}</em>
                                
    			</a>";
    } 
    ?>
    The web in one word.

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Try this:
    Code:
    <?php
    
      $sql = "SELECT * FROM jos_video ORDER BY id";
      $result = mysql_query($sql);
      $i = 0;
    while($row = mysql_fetch_assoc($result))
    {
        ++$i;
        $class = ($i == 1) ? "class=\"first\"" : "class=\"default\";
        echo "<a href=\"{$row['url']}\"";?> {$class} <? echo ">
    				{$row['artist']}<br />
    				<em>{$row['song']}</em>
                                
    			</a>";
    } 
    ?>
    Jeremy | jfein.net

  3. #3
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    Ok this isn't specific to this topic but you've brought it up here and it's a chance for me to learn a bit more about programming. When you put:

    Code:
    $class = ($i == 1) ? "class=\"first\"" : "class=\"default\";
    Does that code translate to : Does i = 1? If it does put class = first, if it doesn't put class = default. Is it just a quicker way of saying :

    Code:
    if ($i == 1) {
    class=\"first\";
    }
    else {
    class=\"default\";
    }
    I've seen this in other languages as well such as JavaScript, is it the same in both?

  4. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    More specifically:
    Code:
    if($i == 1){
      $class = "class=\"first\";";
    } else {
      $class = "class=\"default\";";
    }
    But yeah.
    Jeremy | jfein.net

  5. #5
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    Well yes that, but I was sort of giving a general idea. But that's good, thanks

  6. #6
    Join Date
    May 2006
    Posts
    266
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    tryed the code below and it did not work still shows first on all rows can i ask what the i=0 is?

    Quote Originally Posted by Nile View Post
    Try this:
    Code:
    <?php
    
      $sql = "SELECT * FROM jos_video ORDER BY id";
      $result = mysql_query($sql);
      $i = 0;
    while($row = mysql_fetch_assoc($result))
    {
        ++$i;
        $class = ($i == 1) ? "class=\"first\"" : "class=\"default\";
        echo "<a href=\"{$row['url']}\"";?> {$class} <? echo ">
    				{$row['artist']}<br />
    				<em>{$row['song']}</em>
                                
    			</a>";
    } 
    ?>
    The web in one word.

  7. #7
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    In this case, it's used as a counter, so when it is first declared it is set at 0 and then as it gets into the while loop it will be incremented each time so it can go through the database, you'll have to wait for Nile to reply as to why the code isn't working however.

    Edit: I think I've found the problem, Nile ommitted one of the speech marks, try the code below:

    PHP Code:
    <?php

      $sql 
    "SELECT * FROM jos_video ORDER BY id";
      
    $result mysql_query($sql);
      
    $i 0;
    while(
    $row mysql_fetch_assoc($result))
    {
        ++
    $i;
        
    $class = ($i == 1) ? "class=\"first\"" "class=\"default\"";
        echo 
    "<a href=\"{$row['url']}\"";?> {$class} <? echo ">
                    
    {$row['artist']}<br />
                    <em>
    {$row['song']}</em>
                                
                </a>"
    ;

    ?>
    Last edited by Schmoopy; 01-30-2009 at 06:47 PM.

  8. #8
    Join Date
    May 2006
    Posts
    266
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    This is what I get when i run the code
    Code:
    <a href="http://www.queerhuntradio.com/images/video/music/15%20So%20What.m4v" class="\&quot;first\&quot;">
    				Pink<br>
    				<em>So What</em>
                                
    			</a>
    
    <a href="http://www.queerhuntradio.com/images/video/music/rihana.flv" class="\&quot;first\&quot;">
    
    				Rihana<br>
    				<em>Please Don't Stop The Music</em>
                                
    			</a>
    The web in one word.

  9. #9
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Here:

    PHP Code:
    <?php
    $sql 
    "SELECT * FROM `jos_video` ORDER BY `id`";
    $result mysql_query($sql);
    $i 0;
    while(
    $row mysql_fetch_assoc($result)) {
     ++
    $i;
     
    $class = ($i == 1) ? 'class="first"' 'class="default"';
     echo 
    '<a href='.$row['url'].' '.$class.' '.$row['artist'].'<br />
    <em>'
    .$row['song'].'</em>
    </a>'
    ;
    }
    ?>
    Jeremy | jfein.net

Bookmarks

Posting Permissions

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