|
-
Jan 27th, 2010, 01:50 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] anchor with Image clicks
Hi,
I am using this coding to display the list of friends the user has. The friends are listed with their username and images.
- Is the code is the good one to display the list of friends.
- From the list of friends, when the username is clicked it takes to the profile page, i also want that to be worked when i click the image also. How to set anchor tag in images.
Code:
<?PHP
session_start();
$userid = $_SESSION['loggeduser'];
require_once('../include/DBConnect.php');
$query = "select friend from friends where userid='$userid';";
$result = mysql_query($query);
if(mysql_num_rows($result) < 1)
{
echo "No Friends for users" ;
}
echo "<table border=\"0\" >";
echo "<tr>";
while($friendid = mysql_fetch_array($result,MYSQL_ASSOC))
{
$friendid = $friendid['friend'];
$query = "select id,username from tbl_users where id='$friendid';";
$result1 = mysql_query($query);
$userdata = mysql_fetch_array($result1,MYSQL_ASSOC);
$username = $userdata['username'];
$userid = $userdata['id'];
$userimg = "photos/avatar/".$friendid."/".$friendid.".jpg";
echo "<td>";
echo "<table border=\"1\">";
echo "<tr>";
echo "<td>";
echo "<img src=\"$userimg\" alt=\"\" width=\"100px\" height=\"125px\" >";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td align=\"center\">";
echo "<a href=\"home.php?p=64&u=$userid\">$username</a>";
echo "</tr>";
echo "</td>";
echo "</table>";
echo "</td>";
}
echo "</tr>";
echo "</table>";
?>
-
Jan 27th, 2010, 02:08 AM
#2
Re: anchor with Image clicks
if you would like to have an image be a link, then make it into a link.
HTML Code:
<a href=""><img src="" /></a>
I can't give you real feedback on if your method is "the good one" (it's weird that you keep saying it like that, even if you aren't great with english); you basically did what I told you to in the last post you asked about it. it looks like it works okay. one problem I do see is that you're querying the database once and then on every record returned you're querying the database again. this is inefficient, and, well, wrong. you should use SQL JOINs to create one SQL statement that returns all of the desired information for you. I will not get into how to do that, though -- if you're inexperienced with databases it can be a hard concept to grasp. I suggest taking a database class, reading some database (MySQL-based, in this case) books, or reading through the MySQL manual on JOINs. if you have questions, you can ask them in the Database Development section of this forum.
if anything, I would advise you to stop echoing out HTML. PHP is an embedded language and because of that can be integrated within your HTML. here, you're instead using it to output all of your HTML. you could do something like:
PHP Code:
<?php while( ..... ){ ?> <tr> <td><img src="<?php echo $userimg; ?>" /></td> <td><a href="...."><?php echo $username; ?></a></td> </tr> <?php } ?>
this makes things much easier to maintain and read on your part.
Last edited by kows; Jan 27th, 2010 at 02:11 AM.
-
Jan 27th, 2010, 02:16 AM
#3
Thread Starter
Fanatic Member
Re: anchor with Image clicks
hi thankyou. I will work on these and will have a look on joins.
-
Jan 27th, 2010, 04:25 AM
#4
Re: anchor with Image clicks
Don't let VisualAd see you echoing HTML... I've heard he has killed a man for that before :O
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|