Jump to content


Photo

having trouble with linking to record. Help please.


  • Please log in to reply
2 replies to this topic

#1 bkthomas18

bkthomas18
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 12 September 2006 - 11:54 PM

Ok,
  I am a newbie and have setup a site that writes a form to a database, which is going good.  Now I have setup an admin area where you can view the records of the database.  The code dispalys the id fields and sorts by name perfectly but the linking to the id's is not working right.  I made a file for each name to link to a page (1.php) that displays each row in a table.  The url looks like this: http://test.com/admin/1?page=1.  I need each link to link to the corresponding page number. i.e. page=2, etc.  Can someone help me please??  Here is the code that I have for the page that displays the id field for each row and sorts by name:
<?php

// Database Connection
mysql_connect('localhost','username','password');
mysql_select_db('db');


// If current page number, use it
// if not, set one!

if(!isset($_GET['page'])){
    $page = 1;
} else {
    $page = $_GET['page'];
}

// Define the number of results per page
$max_results = 25;

// Figure out the limit for the query based
// on the current page number.
$from = (($page * $max_results) - $max_results); 

// Perform MySQL query on only the current page number's results

$sql = mysql_query("SELECT id, name FROM gentleman ORDER BY name");

while($row = mysql_fetch_array($sql)){
    // Build your formatted results here.
   echo ('<a href="' . $row['id'] . ' ">' . $row['name'] . ' </a><br />');
}

// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(id) as Num FROM gentleman"),0);

// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);

// Build Page Number Hyperlinks
echo "<center>Select a Page<br />";

// Build Previous Link
if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><<Previous</a> ";
}

for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
        echo "$i ";
        } else {
            echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
    }
}

// Build Next Link
if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next>></a>";
}
echo "</center>";
?>



Here is the code for 1.php:

<?php

// Database Connection
mysql_connect('localhost','username','password');
mysql_select_db('db');


// If current page number, use it
// if not, set one!

if(!isset($_GET['page'])){
    $page = 1;
} else {
    $page = $_GET['page'];
}

// Define the number of results per page
$max_results = 1;

// Figure out the limit for the query based
// on the current page number.
$from = (($page * $max_results) - $max_results); 

// Perform MySQL query on only the current page number's results

$sql = mysql_query("SELECT id, name, city, state, phone, age, height, weight, hairc, eyec, occupation, income, networth, benefactor, benyn, other, datingsit, common, companion, traveler, perfectwoman, heighta, heightb, weighta, weightb, agea, ageb, haircolor, race, bodytype, specific, hour34, investa, investb FROM gentleman LIMIT $from, $max_results");

while($row = mysql_fetch_array($sql)){
    // Build your formatted results here.
   echo ('<table border="1" cellspacing="1" style="border-collapse: collapse" bordercolor="#000066" width="650" cellpadding="5">
  <tr><td colspan="3" bgcolor="#D6DFEF" height="16" bordercolor="#FFFFFF">&nbsp;</td></tr>
  <tr>
    <td height="30" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
    <td height="30" bgcolor="#EFF3F7" bordercolor="#FFFFFF">ID</td>
    <td height="30" bgcolor="#EFF3F7" bordercolor="#FFFFFF">' . $row['id'] . ' </td>
  </tr>
  <tr><td height="30" width="4" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
  <td height="30" width="295" bgcolor="#EFF3F7" bordercolor="#FFFFFF">
<font face="Verdana" size="2">Full Name </font></td>
<td height="30" width="812" bgcolor="#EFF3F7" bordercolor="#FFFFFF">' . $row['name'] . '</td>
</tr><tr><td height="30" width="4" bgcolor="#EFF3F7" bordercolor="#FFFFFF"></td>
  <td height="30" width="295" bgcolor="#EFF3F7" bordercolor="#FFFFFF">
<font face="Verdana" size="2">City</font></td>
<td height="30" width="812" bgcolor="#EFF3F7" bordercolor="#FFFFFF"><p>' . $row['city'] . ' </p>
  </td>
</tr><tr><td height="30" width="4" bgcolor="#EFF3F7" bordercolor="#FFFFFF"></td>
  <td height="30" width="295" bgcolor="#EFF3F7" bordercolor="#FFFFFF">
<font face="Verdana" size="2">State</font></td>
<td height="30" width="812" bgcolor="#EFF3F7" bordercolor="#FFFFFF">' . $row['state'] . ' </td>
</tr><tr><td height="30" width="4" bgcolor="#EFF3F7" bordercolor="#FFFFFF"></td>
  <td height="30" width="295" bgcolor="#EFF3F7" bordercolor="#FFFFFF">
<font face="Verdana" size="2">Phone</font></td>
<td height="30" width="812" bgcolor="#EFF3F7" bordercolor="#FFFFFF">' . $row['phone'] . ' </td>
</tr><tr><td height="30" width="4" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
  <td height="30" width="295" bgcolor="#EFF3F7" bordercolor="#FFFFFF">
<font face="Verdana" size="2">Age</font></td>
<td height="30" width="812" bgcolor="#EFF3F7" bordercolor="#FFFFFF">' . $row['age'] . ' </td>
</tr><tr><td height="30" width="4" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
  <td height="30" width="295" bgcolor="#EFF3F7" bordercolor="#FFFFFF">
<font face="Verdana" size="2">Height</font></td>
<td height="30" width="812" bgcolor="#EFF3F7" bordercolor="#FFFFFF">' . $row['height'] . ' </td>
</tr><tr><td height="30" width="4" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
  <td height="30" width="295" bgcolor="#EFF3F7" bordercolor="#FFFFFF">
<font face="Verdana" size="2">Weight</font></td>
<td height="30" width="812" bgcolor="#EFF3F7" bordercolor="#FFFFFF">' . $row['weight'] . ' </td>
</tr><tr><td height="30" width="4" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
  <td height="30" width="295" bgcolor="#EFF3F7" bordercolor="#FFFFFF">
<font face="Verdana" size="2">Hair Color </font></td>
<td height="30" width="812" bgcolor="#EFF3F7" bordercolor="#FFFFFF">' . $row['hairc'] . ' </td>
</tr><tr><td height="30" width="4" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
  <td height="30" width="295" bgcolor="#EFF3F7" bordercolor="#FFFFFF">
<font face="Verdana" size="2">Eye Color </font></td>
<td height="30" width="812" bgcolor="#EFF3F7" bordercolor="#FFFFFF">' . $row['eyec'] . ' </td>
</tr><tr><td height="30" width="4" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
  <td height="30" width="295" bgcolor="#EFF3F7" bordercolor="#FFFFFF">
<font face="Verdana" size="2">Occupation/ Business </font></td>
<td height="30" width="812" bgcolor="#EFF3F7" bordercolor="#FFFFFF">' . $row['occupation'] . ' </td>
</tr><tr><td height="30" width="4" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
  <td height="30" width="295" bgcolor="#EFF3F7" bordercolor="#FFFFFF">
<font face="Verdana" size="2">Income</font></td>
<td height="30" width="812" bgcolor="#EFF3F7" bordercolor="#FFFFFF">$ ' . $row['income'] . ' </td>
</tr><tr><td height="30" width="4" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
  <td height="30" width="295" bgcolor="#EFF3F7" bordercolor="#FFFFFF">
<font face="Verdana" size="2">Net Worth </font></td>
<td height="30" width="812" bgcolor="#EFF3F7" bordercolor="#FFFFFF">$ ' . $row['networth'] . ' </td>
</tr><tr><td height="30" width="4" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
  <td height="30" width="295" bgcolor="#EFF3F7" bordercolor="#FFFFFF">
<font face="Verdana" size="2">Benefactor Situation? </font></td>
<td height="30" width="812" bgcolor="#EFF3F7" bordercolor="#FFFFFF">' . $row['benyn'] . ' </td>
</tr><tr><td height="30" width="4" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
  <td height="30" width="295" bgcolor="#EFF3F7" bordercolor="#FFFFFF"> Type of date match preferred
<font face="Verdana" size="2">&nbsp; </font></td>
<td height="30" width="812" bgcolor="#EFF3F7" bordercolor="#FFFFFF">' . $row['benefactor'] . ' </td>
</tr><tr><td height="30" width="4" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
  <td height="30" width="295" bgcolor="#EFF3F7" bordercolor="#FFFFFF"><font size="2" face="Verdana">If other, specified </font></td>
<td height="30" width="812" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
</tr><tr><td height="30" width="4" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
  <td height="30" width="295" bgcolor="#EFF3F7" bordercolor="#FFFFFF">
<font face="Verdana" size="2">Dating Situation </font></td>
<td height="30" width="812" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
</tr><tr><td height="30" width="4" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
  <td height="30" width="295" bgcolor="#EFF3F7" bordercolor="#FFFFFF">favorite activities/ hobbies and those you would like your date to have in commo</td>
<td height="30" width="812" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
</tr><tr><td height="30" width="4" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
<td height="30" width="295" bgcolor="#EFF3F7" bordercolor="#FFFFFF">When, how often, and where would you plan or expect to spend quality time with your companion?</td>
<td height="30" width="812" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
</tr><tr><td height="30" width="4" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
<td height="30" width="295" bgcolor="#EFF3F7" bordercolor="#FFFFFF">Would you like your dating match to be available for travel or do you prefer to meet new girls to date in each city?</td>
<td height="30" width="812" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
</tr><tr><td height="30" width="4" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
<td height="30" width="295" bgcolor="#EFF3F7" bordercolor="#FFFFFF">Describe your idea of the perfect woman for you. Include hobbies, interests, etc.</td>
<td height="30" width="812" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
</tr><tr><td height="30" width="4" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
  <td height="30" width="295" bgcolor="#EFF3F7" bordercolor="#FFFFFF">
<font face="Verdana" size="2">Height Range </font></td>
<td height="30" width="812" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
</tr><tr><td height="30" width="4" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
  <td height="30" width="295" bgcolor="#EFF3F7" bordercolor="#FFFFFF">
<font face="Verdana" size="2">Weight Range </font></td>
<td height="30" width="812" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
</tr><tr><td height="30" width="4" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
  <td height="30" width="295" bgcolor="#EFF3F7" bordercolor="#FFFFFF">
<font face="Verdana" size="2">Age Range </font></td>
<td height="30" width="812" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
</tr><tr><td height="30" width="4" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
  <td height="30" width="295" bgcolor="#EFF3F7" bordercolor="#FFFFFF">
<font face="Verdana" size="2">Hair color preference </font></td>
<td height="30" width="812" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
</tr><tr><td height="30" width="4" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
  <td height="30" width="295" bgcolor="#EFF3F7" bordercolor="#FFFFFF">
<font face="Verdana" size="2">Race</font></td>
<td height="30" width="812" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
</tr><tr><td height="30" width="4" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
  <td height="30" width="295" bgcolor="#EFF3F7" bordercolor="#FFFFFF"><font size="2" face="Verdana">Body Type </font></td>
<td height="30" width="812" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
</tr><tr><td height="30" width="4" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
  <td height="30" width="295" bgcolor="#EFF3F7" bordercolor="#FFFFFF">
<font face="Verdana" size="2">Bra Size </font></td>
<td height="30" width="812" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
</tr><tr><td height="30" width="4" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
<td height="30" width="295" bgcolor="#EFF3F7" bordercolor="#FFFFFF">
<font face="Verdana" size="2">bodytype</font></td>
<td height="30" width="812" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
</tr><tr><td height="30" width="4" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
  <td height="30" width="295" bgcolor="#EFF3F7" bordercolor="#FFFFFF"><font size="2" face="Verdana">Specific Preferences </font></td>
<td height="30" width="812" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
</tr><tr><td height="30" width="4" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
  <td height="30" width="295" bgcolor="#EFF3F7" bordercolor="#FFFFFF">
<font face="Verdana" size="2">Commit to 3 or 4 hour lunch/ dinner date </font></td>
<td height="30" width="812" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
</tr><tr><td height="30" width="4" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
  <td height="30" width="295" bgcolor="#EFF3F7" bordercolor="#FFFFFF">
<font face="Verdana" size="2">Monthly amount to invest on date, if benefactor. </font></td>
<td height="30" width="812" bgcolor="#EFF3F7" bordercolor="#FFFFFF">&nbsp;</td>
</tr>
</table>');
}

// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(id) as Num FROM gentleman"),0);

// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);

// Build Page Number Hyperlinks
echo "<center>Select a Page<br />";

// Build Previous Link
if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><<Previous</a> ";
}

for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
        echo "$i ";
        } else {
            echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
    }
}

// Build Next Link
if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next>></a>";
}
echo "</center>";
?>


Thanks for your help.

#2 joshi_v

joshi_v
  • Members
  • PipPipPip
  • Advanced Member
  • 168 posts

Posted 13 September 2006 - 09:24 AM

I haven't understand completly what u trying to point.But what i am feeling is u have a problem with passing id value to display the complete record when the user clicks on the id(am i right?).

  So..for that in u r href link in first code file put ''filepath/1.php?id=<?php echo $idfield;?>.. when u click on that link it leads to 1.php file with id value to display the complete record ,based on that id value you are  passing to it.

Hope i haven't confuse you.if it so please try to make point clear little bit more.

Cheers :)

#3 gerkintrigg

gerkintrigg
  • Members
  • PipPipPip
  • Magician
  • 828 posts
  • LocationBristol, UK
  • Age:37

Posted 13 September 2006 - 02:27 PM

joshi_v is (I'm guessing here) probably not english. However, he/she seems to be spot-on. To link to another page with the necessary variables for that page to use, you can do it in several ways, some better than others, depending on the application.
You can use sessions which write to the hard-drive of the server and are what we call "globals" (that just means they can be accessed from any page of the site at any time - they're similar to cookies, but use the server, not the client).For more info on cookies have a look in the tutorial section of phpfreaks.
You could use hidden form elements to transfer info - not the best way in the majority of cases, but can be handy every now and then.
Or you could use POST / Get variables. both use similar methods, and there's loads on this in the php manual or on the phpfreaks tutorials.
The next way is "REQUEST". which works like joshi_v says... ''filepath/1.php?id=<?php echo $idfield;?>"
Then when you get to 1.php you'll need to use $_REQUEST['id'] to get the id value into a form that it can be used, either by $id=$_REQUEST['id']; or echoing / using the $_REQUEST['id'] as it is.

Hope that helps. I think it reads better too. ;)
you'll need to
Neil Trigger - http://www.ghostlypublishing.co.uk - Ghostly Publishing - Children's Fantasy Books




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users