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
https://forums.phpfreaks.com/topic/252954-a-href-link-not-working/
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?

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\" />"

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?

"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.

  Quote
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?

  Quote
"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 []!

 

  Quote
Where is $count defined?

 

it is defined earlier on in the code

"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...

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

  Quote

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.