PDA

Click to See Complete Forum and Search --> : [RESOLVED] anchor with Image clicks


bharanidharanit
Jan 27th, 2010, 12:50 AM
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.

<?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>";
?>

kows
Jan 27th, 2010, 01:08 AM
if you would like to have an image be a link, then make it into a link.
<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
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.

bharanidharanit
Jan 27th, 2010, 01:16 AM
hi thankyou. I will work on these and will have a look on joins.

I_Love_My_Vans
Jan 27th, 2010, 03:25 AM
Don't let VisualAd see you echoing HTML... I've heard he has killed a man for that before :O