Connecting Tech Pros Worldwide Help | Site Map

Add link to search results (<a href="">)

Familiar Sight
 
Join Date: Jan 2008
Posts: 198
#1: Oct 8 '09
In my php page there is a search function and when user search it will display table containing search results. I want to add
Expand|Select|Wrap|Line Numbers
  1. <a href ..></a>
  2.  
. Because when user click one record I want to pass values to ajax page.
to those results. But my code is not working. Please help me.This is my code.
Expand|Select|Wrap|Line Numbers
  1. while($row = mysql_fetch_array($result))
  2.     {
  3.     echo "<a href='javascript:void(0);' onclick=\"LookupVehicle('$VehicleNo','$VehicleType','$VehicleMake','$Price'); return false;\">";
  4.         $array = array('VehicleNo','VehicleType','VehicleMake','Price');
  5.         foreach ($array as $v)
  6.         $$v = $row[$v];?>
  7.  
  8.           <tr>
  9. <?php
  10.  
  11.                 foreach ($array as $v)
  12.                 echo "<td height=20 cursor: pointer;'> &nbsp;".$$v."</td>";
  13.         ?>
  14.         </a>
  15.          </tr>
  16.  
  17.     <?php }
  18.  
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,741
#2: Oct 8 '09

re: Add link to search results (<a href="">)


Hey.

Not feeling particularly articulate today, so I just commented your code :-]
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. while($row = mysql_fetch_array($result)) 
  3. {
  4.     // Isn't this supposed to be *after* you crate the variables you are echoing?
  5.     // And why is the ending </a> somewhere way inside the <tr> tag?
  6.     echo "<a href='javascript:void(0);' onclick=\"LookupVehicle('$VehicleNo','$VehicleType','$VehicleMake','$Price'); return false;\">";
  7.  
  8.     // Why bother with this? You can just as easilly use
  9.     // "$row['VehicleNo']" as "$VehicleNo"
  10.     $array = array('VehicleNo','VehicleType','VehicleMake','Price');
  11.     foreach ($array as $v){
  12.         $$v = $row[$v];
  13.     }
  14.     // P.S. Note the addition of the brackets to your foreach loop.
  15.     // Always use brackets. Leaving them out is just sloppy.
  16.  
  17.     // No poing exiting the <?php block just to output 4 letters.
  18.     // Echo was created for a reason ;-)
  19.     echo "<tr>";
  20.  
  21.     foreach ($array as $v) {
  22.         echo "<td height=20 cursor: pointer;'> &nbsp;".$$v."</td>";
  23.     }
  24.     // Again, note the added brackets to the loop.
  25.  
  26.     // 9 letters this time. Still not worth exiting the PHP block ;-)
  27.     // What's with the random </a> here? Shouldn't it be up there with the
  28.     // rest of the <a> tag?
  29.     // Unless you are trying to make the entire row a link, in which case
  30.     // you should use the <tr> onclick event, not enclose the <tr> in a <a> tag.
  31.     echo "</a></tr>";
  32. }
  33. ?>
Reply