Hi, DaniWeb

I'm doing a website for a music school and i have a table with all the instruments:

[img]http://img195.imageshack.us/img195/427/50490685.jpg[/img]

And i want to click in one image and it goes to other page where's the instrument program. I mean, each image has 1 page with its own program.

If the relation is one-on-one, then why not add a column with the destination page to your existing table ?

Member Avatar for diafol

OK, should be straightforward - here's a freebie:

each img should be in a link, which could have the following structure:

<a href="program.php?i=trombone" title="go to the trombone program"><img src="trombone.png" /></a>

You get the idea - you can set php to produce this list dynamically:

$insts = array('trombone'=>'trombone.png,'trumpet'=>'trumpet2336.jpg','violin'='fiddle_small.png',...);

foreach($insts as $name=>$image){
 $output[] = "<a href=\"program.php?i=$name\" title=\"go to the $name program\"><img src=\"images/$image\" /></a>"; 
}

So the images are now in an array ready for formatting. You could place them in list items, table cells, just plain inline stuff, whatever you want.

In your program.php page you need to catch the $_GET variable to know which bit of data to show.

if(isset($_GET['i'])){
  //put connection details here or include file with said details
  $i = mysql_real_escape_string($_GET['i']);
  $r = mysql_query("...you query... WHERE `instrument` = '$i'");
  if(mysql_num_rows($r)>0){
    while($data = mysql_fetch_array($r)){
       ...output data...
    }
  }else{
     $bad = 1;
  }
}else{
   $bad = 1;
}

if(isset($bad))echo "<p>You've chosen an instrument that doesn't exist, go back to the <a href="index.php">main page</a> and select again.</p>";

Anyway, something like that. Written quickly, so may be some typos, logic weirdness.

** You may be using instrument_id as opposed to instrument name ** - just change the data in the array - should be easy if you're getting this from the DB in the first place.

<?php

include "db_connect.php";

$sql_imagem = "SELECT cod_curso, imagem_curso, nome_curso FROM curso ORDER BY nome_curso ASC";

$i=0;

echo "<table width = 90%  height = 45% align = center>";
$executa=mysql_query($sql_imagem,$connect);
 
while($dados=mysql_fetch_array($executa))
{
   $i+=1;
   if ($i == 1) 
   {
    echo "<tr> <td align = center>";
   }
  else
   {
    echo "<td align = center>";
   }
  
  
  echo "<img src=\"".$dados['imagem_curso']."\"/></a> <br /> ".$dados['nome_curso'];
  
  if ($i == 4)
   {
    echo "</td> </tr>";
	$i=0;
   }
  else
   {
    echo "</td>";
   } 
}
echo "</table>";
?>

I have a theory is using a lot of "if statements" like if $dados = 1 then it goes to guitar program, for example. Do you understand?

Member Avatar for diafol

You don't have an open anchor tag. Look at my code again.

I understand your idea, but how do i do that with this:

echo "<img src=\"".$dados['imagem_curso']."\"/></a> <br /> ".$dados['nome_curso'];
Member Avatar for diafol
$cells = 4; //no. cells per row

$out='';
$output = "<table><tr>";
$x=1;
while(...){
	$end = ($x%4==0) ? '</tr><tr>' : '';
	$out .= "<td align = center><a href=\"program.php?i={$dados['cod_curso']}\" title=\"go to the {$dados['nome_curso']} program in Portugese!\"><img src=\"{$dados['imagem_curso']}\" /></a></td>$end";
	$x++;
}
$reps = ($cells - ($x-1)%$cells == $cells) ? 0 : $cells - ($x-1)%$cells;
$output .= $out . str_repeat("<td>&nbsp;</td>",$reps) . "</tr></table>";

echo $output;

I will try that, but i would like to ask if it's possible to turn the id of the instrument (given in mysql db) into an array? and tell the id == 1 goes to program of guitar (...)

Just thinking out loud. Is that possible?

Member Avatar for diafol

It's what I posted last time - notice the use of cod_curso. And I mentioned this in my previous post:

** You may be using instrument_id as opposed to instrument name ** - just change the data in the array - should be easy if you're getting this from the DB in the first place.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.