A Link That when clicked displays database

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
jeggah
Forum Newbie
Posts: 12
Joined: Sat Jun 19, 2004 7:53 pm

A Link That when clicked displays database

Post by jeggah »

How would I go about making a menu of A-Z where when I clicked on B it would give a list of the B entries of a certain column DISTINCT.

I've tried to alter my search script but I can't figure out how to change it to instead use a link instead of a form.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

for a given url of: foo.com/bar.php?letter=A
untested, example

Code: Select all

<?php

$letter = preg_match('|^([#a-z])|i', (!empty($_GET['letter'])?$_GET['letter']{0}:'-'), $match);

if(!$letter)
{
  echo '[&nbsp;';
  for($x = 0; $x < 27; $x++)
  {
    if($x == 0)
      $y = '#';
    else
      $y = chr($x - 1 + ord('A'));
    echo '<a href="'.$_SERVER['SCRIPT_NAME'].'?letter='.$y.'">'.$y.'</a>&nbsp;';
  }
  echo ']'."\n";
  exit;
}

// do your sql connection here

// you'll need to tweak this next line for your particulars
$result = mysql_query("SELECT DISTINCT * FROM `foo` WHERE `bar` LIKE '{$match[1]}%'");

while($row = mysql_fetch_assoc($result))
{
  echo '<pre>'.print_r($row,true).'</pre>';  // replace this line with your presentation code
}

?>

[edit]oops, my bad, missed a single quote...[/edit]
Last edited by feyd on Thu Jun 24, 2004 6:12 am, edited 1 time in total.
jeggah
Forum Newbie
Posts: 12
Joined: Sat Jun 19, 2004 7:53 pm

Post by jeggah »

im getting this error

Parse error: parse error, unexpected '[', expecting ']'
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Post the code with the line containing the error highlighted.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

try it now..
jeggah
Forum Newbie
Posts: 12
Joined: Sat Jun 19, 2004 7:53 pm

Post by jeggah »

I've altered the script around a bit and its all good apart from the DISTINCT is not working and it is displaying database entries that are the same

Code: Select all

<table><tr><td>Genre</td><td>Artist</td><td>Albums</td><td>Songs</td></tr>
<?php 

$letter = preg_match('|^([#a-z])|i', (!empty($_GET['letter'])?$_GET['letter']{0}:'-'), $match); 

if(!$letter) 
{ 
  echo '[ '; 
  for($x = 0; $x < 27; $x++) 
  { 
    if($x == 0) 
      $y = '#'; 
    else 
      $y = chr($x - 1 + ord('A')); 
    echo '<a href="'.$_SERVER['SCRIPT_NAME'].'?letter='.$y.'">'.$y.'</a> '; 
  } 
  echo ']'."\n"; 
  exit; 
} 


// you'll need to tweak this next line for your particulars 
$result = mysql_query("SELECT DISTINCT * FROM songs WHERE artist LIKE '{$match[1]}%'"); 

while($row = mysql_fetch_array($result)) 
{ 
  		printf("<tr><td>%s</td><td>%s</td><td></td><td></td>\n",
		   $row[4],$row[2]);} 

?> 

</table>
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

jeggah
Forum Newbie
Posts: 12
Joined: Sat Jun 19, 2004 7:53 pm

Post by jeggah »

is it me or is the text on that website loads of little squares???
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

I see the ones in the title..
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it's in japanese..
jeggah
Forum Newbie
Posts: 12
Joined: Sat Jun 19, 2004 7:53 pm

Post by jeggah »

oh noes

i cant read japanese
jeggah
Forum Newbie
Posts: 12
Joined: Sat Jun 19, 2004 7:53 pm

Post by jeggah »

can someone help me then with my problem about DISTINCT
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

What is the problem? feyd's script told you what to do.
jeggah
Forum Newbie
Posts: 12
Joined: Sat Jun 19, 2004 7:53 pm

Post by jeggah »

there is a problem with feyds script, it doesnt show DISTINCT but all of them
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

read up in the mysql manual on distinct or google for it. Feyd showed you pretty much all there is to it.
Post Reply