PDA

View Full Version : How would i do this in PHP?



LMS16
07-11-2010, 06:57 PM
Hey :)

I have a small question...

In PHP how would I add an extra value to the outputted while functions?


example:

<?php $slide = mysql_query("SELECT * FROM `slider` ORDER BY `id` DESC LIMIT 10");
while($val = mysql_fetch_object($slide)){
echo "<a href=\"" . $val->url . "\"><img src=\"" . $val->img_link . "\" alt=\"" . $val->alt . "\" width=\"580\" height=\"282\" rel=\"<h3>" . $val->rel_title . "</h3>" . $val->rel_desc . "\"/></a>";
} ?>


I have this already, but for my jquery script to work correctly I need to define the first image.. so I need this to be shown first, then after show thr above. So I want it to look like this...


<?php $slide = mysql_query("SELECT * FROM `slider` ORDER BY `id` DESC LIMIT 10");
while($val = mysql_fetch_object($slide)){
echo "<a href=\"" . $val->url . "\"><img src=\"" . $val->img_link . "\" alt=\"" . $val->alt . "\" width=\"580\" height=\"282\" rel=\"<h3>" . $val->rel_title . "</h3>" . $val->rel_desc . "\"/></a>";
} ?>

<?php $slide = mysql_query("SELECT * FROM `slider` ORDER BY `id` DESC LIMIT 10");
while($val = mysql_fetch_object($slide)){
echo "<a href=\"" . $val->url . "\" class=\"show\"><img src=\"" . $val->img_link . "\" alt=\"" . $val->alt . "\" width=\"580\" height=\"282\" rel=\"<h3>" . $val->rel_title . "</h3>" . $val->rel_desc . "\"/></a>";
} ?>


I hope you under stand...

Thanks + REP

Lew.

MattFr
07-11-2010, 07:05 PM
A real hacky way to do this would be defining a var as 0, then adding 1 per iteration. If the var is 0, it's the first iteration of the loop, so do the first thing.

Moh
07-11-2010, 07:41 PM
I don't quite get what you're wanting o.O

ThisNameWillDo!
07-11-2010, 11:47 PM
I really don't understand what you're trying to do here, can you explain a little more please?

LMS16
08-11-2010, 08:02 AM
Right, basically, Im outputting values from the database. And I want the first outputted values to have something added to it. so I want it to do this...



<?php $slide = mysql_query("SELECT * FROM `slider` ORDER BY `id` DESC LIMIT 10");
while($val = mysql_fetch_object($slide)){
echo "<a href=\"" . $val->url . "\" class=\"show\"><img src=\"" . $val->img_link . "\" alt=\"" . $val->alt . "\" width=\"580\" height=\"282\" rel=\"<h3>" . $val->rel_title . "</h3>" . $val->rel_desc . "\"/></a>";
} ?>
<?php $slide = mysql_query("SELECT * FROM `slider` ORDER BY `id` DESC LIMIT 10");
while($val = mysql_fetch_object($slide)){
echo "<a href=\"" . $val->url . "\"><img src=\"" . $val->img_link . "\" alt=\"" . $val->alt . "\" width=\"580\" height=\"282\" rel=\"<h3>" . $val->rel_title . "</h3>" . $val->rel_desc . "\"/></a>";
} ?>

Notice, the top one had <a href="" class="show"></a>

Thanks

Lew.

MattFr
08-11-2010, 08:10 AM
The way I said would work fine..

LMS16
08-11-2010, 08:33 AM
The way I said would work fine..

My head's gone blank, any examples pls +REP ;)

Lew.

Moh
08-11-2010, 10:09 AM
<?php
$i = 0;
$slide = mysql_query( "SELECT * FROM `slider` ORDER BY `id` DESC LIMIT 20" );
while( $val = mysql_fetch_object( $slide ) ){

$class = ( $i <= 10 ) ? "class=\"show\"" : "";
$i++;

echo "<a href=\"" . $val->url . "\" " . $class . "><img src=\"" . $val->img_link . "\" alt=\"" . $val->alt . "\" width=\"580\" height=\"282\" rel=\"<h3>" . $val->rel_title . "</h3>" . $val->rel_desc . "\"/></a>";
}
?>

Something like that? :)

LMS16
08-11-2010, 10:55 AM
<?php
$i = 0;
$slide = mysql_query( "SELECT * FROM `slider` ORDER BY `id` DESC LIMIT 20" );
while( $val = mysql_fetch_object( $slide ) ){

$class = ( $i <= 10 ) ? "class=\"show\"" : "";
$i++;

echo "<a href=\"" . $val->url . "\" " . $class . "><img src=\"" . $val->img_link . "\" alt=\"" . $val->alt . "\" width=\"580\" height=\"282\" rel=\"<h3>" . $val->rel_title . "</h3>" . $val->rel_desc . "\"/></a>";
}
?>

Something like that? :)

And that will only add class="show" to the very first output?

Lew.

Moh
08-11-2010, 11:22 AM
And that will only add class="show" to the very first output?

Lew.
To the first 10, simply edit the 10 to 1 if you only want the first to show.

Also, if you want the others to have class="hide"

$class = ( $i <= 10 ) ? "class=\"show\"" : "class=\"hide\"";

MattFr
09-11-2010, 07:15 AM
My head's gone blank, any examples pls +REP ;)

Lew.

Perhaps learning PHP would be a good idea. Your mind has gone blank way too many times on this forum.

LMS16
09-11-2010, 11:35 AM
Perhaps learning PHP would be a good idea. Your mind has gone blank way too many times on this forum.

This is a way of learning... :) This is how I learn, if I get stuck, ask.

Lew.

Moh
09-11-2010, 09:31 PM
Did that code work btw?

LMS16
10-11-2010, 08:16 AM
Did that code work btw?

Sure did, tyvm :)

Lew.

Want to hide these adverts? Register an account for free!