Welcome to WebmasterWorld Guest from 54.145.246.183

Forum Moderators: coopster & jatar k

Combining While and For Loops

   
12:06 am on Feb 28, 2008 (gmt 0)

5+ Year Member



Hello,

I'm trying to output some content from a MySQL table using a while loop:

$query=mysql_query("SELECT * FROM table LIMIT 9");
while($result=mysql_fetch_array($query)) $mobile.="<a href=\"".$result["link"]."\">".$result["title"]."</a>";

Works great.

Now I'm trying to make it so I have an accesskey value that increases with each new link.

$query=mysql_query("SELECT * FROM table LIMIT 9");
while($result=mysql_fetch_array($query)){
$accesskey=1;
$mobile.="<a href=\"".$result["link"]."\" accesskey=\"".$accesskey."\">".$result["title"]."</a>";
$accesskey++;}

No luck.

How can I make it so each new row has a different accesskey?

Thanks

12:12 am on Feb 28, 2008 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



you have your counter initializing inside your while loop, put it before the loop

$query=mysql_query("SELECT * FROM table LIMIT 9");
$accesskey=1;
while($result=mysql_fetch_array($query)){
$mobile.="<a href=\"".$result["link"]."\" accesskey=\"".$accesskey."\">".$result["title"]."</a>";
$accesskey++;}

should work then

12:19 am on Feb 28, 2008 (gmt 0)

5+ Year Member



Awesome, it works! Thanks
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month