SitePoint Sponsor |
|
User Tag List
Results 1 to 4 of 4
Thread: simple while loop
-
May 22, 2001, 09:13 #1
- Join Date
- Mar 2001
- Location
- the Netherlands
- Posts
- 519
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I know this is probably very easy, but somehow I'm quite hard-headed and still don't understand fully how to do the following...
I have a table:
links (userid, linkid, name, URL)
userid foreign key user.id
So, one user can have 0, 1 or many links.
I want to print the links a user has.
How can I do this so it will look like this:
Link 1
Link 2
Link 3
Link 4
Link 5
-
May 22, 2001, 09:29 #2
- Join Date
- May 2001
- Location
- Virginia
- Posts
- 126
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
ok...
well, I haven't used mysql's db functions much since I abstracted them to my own - much simpler library which I use all the time - so please pardon any slight mistakes
anyhow, you should be able to do something like:
Code:$result = mysql_query("select link, URL from links where userid = $userid"); if($result) { if(mysql_num_rows($result) > 0) { while(list($linkname, $linkurl) = mysql_fetch_row($result)) { echo "<a href=\"$linkurl\">$linkname</a><br>"; } } } else { echo "no links"; }
see if that doesn't work for you-=Alarion=-
Protollix - Linux hosting from $3.95/m
-
May 22, 2001, 09:59 #3
- Join Date
- Aug 2000
- Location
- San Diego, CA
- Posts
- 5,460
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Or to simplify it a bit
PHP Code:<?
$result = mysql_query("select link, URL from links where userid = $userid");
if(mysql_num_rows($result) > 0) {
while($row = mysql_fetch_array($result)) {
extract($row);
print "<a href=\"$URL\">$link</a><br>";
}
}
else {
echo "no links";
}
?>Please don't PM me with questions.
Use the forums, that is what they are here for.
-
May 22, 2001, 11:50 #4
- Join Date
- May 2001
- Location
- Virginia
- Posts
- 126
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
oO Freddy... I didn't know you could do it that way (talking about the "extract" and then reference the column name as a variable.
thanks-=Alarion=-
Protollix - Linux hosting from $3.95/m
Bookmarks