Jump to content

a href link not working


lukep11a

Recommended Posts

Hi, I have a directory page the displays all names beginning with B across 3 columns, that part works fine but when I try to add a link on each of the results the link doesn't work. It's not picking up the $id or $name variables for some reason, can anyone tell me why this is? Any help would be very much appreciated.

 

<?php
$data = mysql_query("SELECT id, name FROM new WHERE name LIKE 'B%' ORDER by name ASC LIMIT 30");
$prev_row ='';

echo '<table width="690px">';
while($info = mysql_fetch_array( $data )) {

	$letter = strtoupper(substr($info['name'],0,1));
	if ($letter != $prev_row) {

		if($count % 3) {

			for ($i = ($count % 3); $i < 3; $i++) echo '<td> </td>';
			echo '</tr>';
			$count =0;

		}
		$prev_row = $letter;

		echo '<tr><td class="directory-letter" colspan="3">',$letter,'</td></tr>';
	}
	$id = $row['id'];
	$name = $row['name'];
	if ($count % 3 == 0) echo '<ul><tr>';
	$count++;
	echo '<td width="230px"><li class="directory-store-name"><a href="directory/name.php?id=$id" title="$name" />', $info['name'],"</li></td>\n";
	if ($count % 3 == 0) echo '</tr></ul>';
}
if($count % 3) {
	for ($i = ($count % 3); $i < 3; $i++) echo '<td> </td>';
	echo '</tr>';
}
echo '</table>';
?>

Link to comment
Share on other sites

Thanks for the reply but its this part of the code that doesn't seem to be working:

 

$id = $row['id'];
	$name = $row['name'];
	if ($count % 3 == 0) echo '<ul><tr>';
	$count++;
	echo '<td width="230px"><li class="directory-store-name"><a href="directory/name.php?id=$id" title="$name" />', $info['name'],"</li></td>\n";

 

it doesn't seem to be picking the variables up, have they been stated in the wrong part of the code maybe?

Link to comment
Share on other sites

dzelenika - you are right, of course. But I like the form that you show as "$name", because it's obvious in many situations. Mainly when there are many parameters are variables.

Concerning topic starter's question:

// this string
'<td width="230px"><li class="directory-store-name"><a href="directory/name.php?id=$id" title="$name" />'
// could be changed to this - also variable $id will be used in correct way
"<td width=\"230px\"><li class=\"directory-store-name\"><a href=\"directory/name.php?id=$id\" title=\"$name\" />"

Link to comment
Share on other sites

Thankyou both for your help, but it is still not working, the page diaplay as it should using both of your suggestions and the results have a link to the correct page as they should up until the point where it gets the $id variable, so the link for all results is "directory/name.php?id=" where the equals should be followed by the id?

Link to comment
Share on other sites

Thanks for the reply but its this part of the code that doesn't seem to be working:

$id = $row['id'];
	$name = $row['name'];
	if ($count % 3 == 0) echo '<ul><tr>';
	$count++;
	echo '<td width="230px"><li class="directory-store-name"><a href="directory/name.php?id=$id" title="$name" />', $info['name'],"</li></td>\n";

Where is $count defined?

Link to comment
Share on other sites

"so the link for all results is "directory/name.php?id=" where the equals should be followed by the id?"

 

Check if you have something in this variable. For example, do it:

echo "[$id]";

and say - if you have something between square brackets.

 

Yes that is correct, echo "[$id]"; is resulting in lots of []!

 

Where is $count defined?

 

it is defined earlier on in the code

Link to comment
Share on other sites

"Yes that is correct, echo "[$id]"; is resulting in lots of []!" - is it mean that you have nothing between brackets? If "yes" it means that it's incorrect. Because you have to see ids between brackets!!!

 

BTW.... It seems I've found a reason for your problem :)

// For the first time I didn't read you code carefully. But just now I saw you initial code 
$info = mysql_fetch_array( $data )

// but later your wrote 
$id = $row['id'];
$name = $row['name'];

// I think you might write
$id = $info['id'];
$name = $info['name'];

Check it...

Link to comment
Share on other sites

Glad you worked it out.  Here's a variation of your code.

<?PHP
$data = mysql_query("SELECT id, name FROM new WHERE name LIKE 'B%' ORDER by name ASC LIMIT 30");
//To account for adding a title row, I set $count to -1
$count =-1;
echo "<table style=\"width:690px\">";
while($info = mysql_fetch_array( $data )) {	
	$letter = strtoupper(substr($info['name'],0,1));
	$name=$info['name'];
	$id=$info['id'];
	$count++;
//echo title row if count is zero	
if($count==0){
echo "<tr><td class=\"directory-letter\" colspan=\"3\">". $letter . "</td></tr>";
}
else{
	//if count remainder is one, start new row		
    	if(($count % 3) == 1)  { echo "<tr>\n"; }
echo "<td style=\"width:230px\"><span class=\"directory-store-name\"><a href=\"directory/name.php?id=$id\" title=\"$name\" />" . $name . "</span></td>\n";
	//if count remainder is zero, end row
	if ($count % 3 == 0){ echo "</tr>\n";}
}//if($count==0) else
}//while($info = mysql_fetch_array( $data )) {
echo '</table>';
?>

Link to comment
Share on other sites

So what is the difference between using $row and $info??

 

You never defined $row, so it doesn't exist.  If you'd have had your error_reporting set to E_ALL you would have gotten a notice about it.

 

while($info = mysql_fetch_array( $data )) {
-------^^^^ 

 

You defined $info as the variable holding your results.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.