View Full Version : hide the class after first row
queerfm
01-29-2009, 07:32 AM
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
<?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>";
}
?>
Try this:
<?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>";
}
?>
Schmoopy
01-29-2009, 10:31 PM
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:
$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 :
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?
More specifically:
if($i == 1){
$class = "class=\"first\";";
} else {
$class = "class=\"default\";";
}
But yeah. :)
Schmoopy
01-29-2009, 10:47 PM
Well yes that, but I was sort of giving a general idea. But that's good, thanks :)
queerfm
01-30-2009, 06:08 AM
tryed the code below and it did not work still shows first on all rows can i ask what the i=0 is?
Try this:
<?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>";
}
?>
Schmoopy
01-30-2009, 06:40 PM
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
$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>";
}
?>
queerfm
01-31-2009, 05:23 AM
This is what I get when i run the code
<a href="http://www.queerhuntradio.com/images/video/music/15%20So%20What.m4v" class="\"first\"">
Pink<br>
<em>So What</em>
</a>
<a href="http://www.queerhuntradio.com/images/video/music/rihana.flv" class="\"first\"">
Rihana<br>
<em>Please Don't Stop The Music</em>
</a>
Here:
<?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>';
}
?>
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.