lukep11a Posted December 11, 2011 Share Posted December 11, 2011 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>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/252954-a-href-link-not-working/ Share on other sites More sharing options...
dzelenika Posted December 11, 2011 Share Posted December 11, 2011 '$name' doesn't works "$name" works you could use: 'some html' . $name . 'other html' Quote Link to comment https://forums.phpfreaks.com/topic/252954-a-href-link-not-working/#findComment-1296931 Share on other sites More sharing options...
lukep11a Posted December 11, 2011 Author Share Posted December 11, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/252954-a-href-link-not-working/#findComment-1296934 Share on other sites More sharing options...
SergeiSS Posted December 11, 2011 Share Posted December 11, 2011 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\" />" Quote Link to comment https://forums.phpfreaks.com/topic/252954-a-href-link-not-working/#findComment-1296936 Share on other sites More sharing options...
lukep11a Posted December 11, 2011 Author Share Posted December 11, 2011 Thanks for that, still doesn't seem to be picking up th $id variable though Quote Link to comment https://forums.phpfreaks.com/topic/252954-a-href-link-not-working/#findComment-1296937 Share on other sites More sharing options...
-Karl- Posted December 11, 2011 Share Posted December 11, 2011 Try: echo '<td width="230px"><li class="directory-store-name"><a href="directory/name.php?id='.$id.'" title="'.$name.'" />'. $info['name']."</li></td>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/252954-a-href-link-not-working/#findComment-1296955 Share on other sites More sharing options...
SergeiSS Posted December 12, 2011 Share Posted December 12, 2011 "ЭThanks for that, still doesn't seem to be picking up th $id variable though" - sorry I lose one quote before $id. Just add \" before $id in my expression and it will work. Quote Link to comment https://forums.phpfreaks.com/topic/252954-a-href-link-not-working/#findComment-1297000 Share on other sites More sharing options...
lukep11a Posted December 12, 2011 Author Share Posted December 12, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/252954-a-href-link-not-working/#findComment-1297163 Share on other sites More sharing options...
Pikachu2000 Posted December 12, 2011 Share Posted December 12, 2011 You're missing an ampersand between the GET vars in the url string. Quote Link to comment https://forums.phpfreaks.com/topic/252954-a-href-link-not-working/#findComment-1297165 Share on other sites More sharing options...
SergeiSS Posted December 12, 2011 Share Posted December 12, 2011 "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 Link to comment https://forums.phpfreaks.com/topic/252954-a-href-link-not-working/#findComment-1297172 Share on other sites More sharing options...
Drummin Posted December 12, 2011 Share Posted December 12, 2011 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 Link to comment https://forums.phpfreaks.com/topic/252954-a-href-link-not-working/#findComment-1297193 Share on other sites More sharing options...
lukep11a Posted December 12, 2011 Author Share Posted December 12, 2011 "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 Quote Link to comment https://forums.phpfreaks.com/topic/252954-a-href-link-not-working/#findComment-1297212 Share on other sites More sharing options...
SergeiSS Posted December 12, 2011 Share Posted December 12, 2011 "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... Quote Link to comment https://forums.phpfreaks.com/topic/252954-a-href-link-not-working/#findComment-1297221 Share on other sites More sharing options...
lukep11a Posted December 12, 2011 Author Share Posted December 12, 2011 Nice one!!! Finally sorted, thanks alot I really appreciate your help. It's been driving me mad!! I couldn't see where I had gone wrong for the life of me. So what is the difference between using $row and $info?? Quote Link to comment https://forums.phpfreaks.com/topic/252954-a-href-link-not-working/#findComment-1297237 Share on other sites More sharing options...
Drummin Posted December 12, 2011 Share Posted December 12, 2011 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 Link to comment https://forums.phpfreaks.com/topic/252954-a-href-link-not-working/#findComment-1297248 Share on other sites More sharing options...
kicken Posted December 12, 2011 Share Posted December 12, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252954-a-href-link-not-working/#findComment-1297270 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.