Page 1 of 1

linking from list[SOLVED]

Posted: Fri Sep 09, 2005 8:24 am
by tom.cull
Hi,

i have a page where I can list the contents of a db:

Code: Select all

<html>
<body>


<?php
   // listing script
   
$username="xxxx_xxxx";
$password="xxxx";
$database="xxxx_xxxx";
$table="xxxx_xxxx";
   
   // connect to the server
   mysql_connect( 'localhost',$username,$password)
      or die( "Error! Could not connect to database: " . mysql_error() );
   
   // select the database
   mysql_select_db( $database)
      or die( "Error! Could not select the database: " . mysql_error() );
   
   // retrieve all the rows from the database
   $query = "SELECT * FROM $table ORDER BY `contact_nlast`, `contact_nfirst`";
   
   $results = mysql_query( $query );

   // print out the results
   if( $results )
   {
      while( $contact = mysql_fetch_object( $results ) )
      {
         // print out the info
         $id = $contact -> id;
         $contact_nlast = $contact -> contact_nlast;
         $contact_nfirst = $contact -> contact_nfirst;
         echo( "$id, $contact_nlast, $contact_nfirst<br>" );
      }
   }
   else
   {
      die( "Trouble getting contacts from database: " . mysql_error() );
   }
   
?>

</body>
</html>
This works fine. But i want to make it so that each item of the list is linked to a edititem.php page, passing on the id.

I have tried with putting in the below but it didnt work:

Code: Select all

$row = mysql_fetch_array( $result ); 


echo( "<a href=\"editdetails.php?id=".$row['id']."\">".$row['id']." . ".$row['contact_nfirst']." ".$row['contact_nlast']." - ".$row['car_make']."</a><br />" );
could one of you great experts help me out - im learning, just not quick enough!

Posted: Fri Sep 09, 2005 8:46 am
by feyd
tried that where? it doesn't jive with your code all that well...

It may be a good time to start using

Code: Select all

tags when you are posting php code now..

Posted: Fri Sep 09, 2005 9:02 am
by tom.cull
i tried the following:

Code: Select all

// retrieve all the rows from the database 
   $query = "SELECT * FROM $table ORDER BY `contact_nlast`, `contact_nfirst`"; 
    
   $results = mysql_query( $query ); 

   // print out the results 
   if( $results ) 
   { 
      while( $contact = mysql_fetch_object( $results ) ) 
      { 
         // print out the info 
         $id = $contact -> id; 
         $contact_nlast = $contact -> contact_nlast; 
         $contact_nfirst = $contact -> contact_nfirst; 
         echo( "<a href=\"editdetails.php?id=".['id']."\">".$['id']." . ".['contact_nfirst']." ".['contact_nlast']." - ".['car_make']."</a><br />" ); 
      } 
   } 
   else 
   { 
      die( "Trouble getting contacts from database: " . mysql_error() ); 
   }

Posted: Fri Sep 09, 2005 9:05 am
by feyd
try this instead

Code: Select all

$id = $contact -> id;
         $contact_nlast = $contact -> contact_nlast;
         $contact_nfirst = $contact -> contact_nfirst;
         echo( "<a href=\"editdetails.php?id={$id}\">{$id} . {$contact_nfirst} {$contact_nlast} - {$car_make}</a><br />" );

Posted: Fri Sep 09, 2005 9:19 am
by tom.cull
your nothing short of a genius- thankyou !