Jump to content

Out of Curiosity: Can You Make Anchors in PHP (like in HTML)


suttercain

Recommended Posts

Lets say I have a list of anchor links, in HTML Turtle, Elephant, Duck, etc. And I have a php echoed table way way further down in the page with one title Turtle. How can I make it so if someone clicks the "Turtle" link at the top of the page it takes them way further down the page to the Turtle table.

 

I know how to do this using HTML but not PHP.

I'm unsure you can do this explicitly, but you could of course use a variety of If statements and functions to scroll down the page until a stopping point where it continues to parse code.

 

Interested to see if there is, but I'm doubting it.

there is no difference. Just use the html code, and if you have to, substitute value into the code. With php you are just echo'ing out html code anyways. What makes php different, is you can loop though and echo out ALOT of html code.

 

Ray

 

 

 

  • 1 month later...

So I hadn't touched the idea since I asked this a couple months back. Tonight I was watching Lost on my TIVO and I thought of this. Works pretty good. It generates a table from MySQL and assigns the anchor property of <a name='$variable'></a> to the table header. I then echoed the list of all possible table header names and used <a href='#" . $makes[$current] . "'>$makes[$current]</a> to get it working.

 

Here is the entire code for newbies like myself that may want to try it:

 

<?php
//Connect to the Database via the Include File!
require ('get_connected.php');

// Perform a statndard SQL query:
$res = mysql_query("SELECT UPPER(vehicle) AS vehicle FROM cars ORDER BY vehicle ASC") or die (mysql_error());

// GENERIC CODE FOR THE ANCHORS
$makes = array('ACR'=>'ACURA', 'TOY'=>'TOYOTA', 'SAT'=>'SATURN');
print $makes[$row['vehicle']];

//CSS for the Alternating Color Rows
$current = '';
while($row = mysql_fetch_array($res)) {
  if($current != $row['vehicle']) {
    $current = $row['vehicle'];
    echo "<a href='#" . $makes[$current] . "'>$makes[$current]</a>\n"; }
  }
//Convert Vehicle Column to out put from the MySQL instead of HTML
$sql = mysql_query("SELECT UPPER(vehicle) AS vehicle, sub_class, disp, fuel FROM cars ORDER BY vehicle ASC") or die (mysql_error());
$current = '';
while($row = mysql_fetch_array($sql)) {
  if($current != $row['vehicle']) {
    $current = $row['vehicle'];
    echo "<table width='100%' border='0' bgcolor=#CCCCCC cellpadding='1' cellspacing='1'>
<tr>
        <td width='70'><b>$makes[$current]</b><a name='$makes[$current]'></a></td>
	<td width='55'><b>FUEL</b></td>
	<td width='110'><b>DISPLACEMENT</b></td>
</tr><br>";
  }
  
  //Continuation of CSS for the Alternating Color Rows
  $class = $class == 'even' ? 'odd' : 'even';
  
  //Populate the Tables from the Database
  echo "<tr class=\"$class\">\n";
  echo "<td>$row[sub_class]</td>\n";
  echo "<td>$row[disp] L</td>\n";
  echo "<td>$row[fuel]</td>\n";
  echo "</tr>\n";
}
?>

 

I am not sure if the is the best way to do it, but it works.

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.