943,976 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 726
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 18th, 2009
0

PHP variable in link

Expand Post »
I think I have read the answer reading through several forums and many posts, but I am not getting it. Please help... I am a novice.

I have a page that displays some info for all the records in a database. It's for real estate listings, so the page displays the street address, subdivision and a description of the property. This page is created dynamically from a database. This page is called allListings.php.

I would like to add a link to the street address that would take you to a detail page -- called inventory.php -- where more information about the property can be viewed.

Help?

Here is what my last try looked like:
<a href="\inventory.php?id=" . $id . "\"><?php echo $row_rsAllListings['element_1_1']; ?></a>

While inventory.php displays the information I want, it is pulling the first record in the database regardless of the property I click.

I have attached my two pages.
Attached Files
File Type: php allListings.php (6.3 KB, 17 views)
File Type: php inventory.php (6.4 KB, 20 views)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
isak is offline Offline
8 posts
since Jul 2009
Aug 18th, 2009
0

Re: PHP variable in link

<a href="\inventory.php?id=" . $id . "\"><?php echo $row_rsAllListings['element_1_1']; ?></a>
Why do you echo $row_rsAllListings['element_1_1'] but you don't echo $id? Check the link href value after the page is loaded. I'm sure you'll spot the problem.
Reputation Points: 232
Solved Threads: 137
Practically a Master Poster
buddylee17 is offline Offline
665 posts
since Nov 2007
Aug 18th, 2009
0

Re: PHP variable in link

php Syntax (Toggle Plain Text)
  1. <div class="content">
  2. <table width="600">
  3. <?php $counter = 0; // initialize counter outside loop
  4. do { echo '<tr';
  5. if ($counter++ % 2) {echo 'class="hilite"';}
  6. echo '><td width="162"><img src="'.$row_rsAllListings['element_16'].'"></td>';
  7. echo '<td width="426"><h2><a href="\inventory.php?id="'.$id.'">'.$row_rsAllListings['element_1_1'];
  8. echo '</a> | '.$row_rsAllListings['element_2'].'<br />';
  9. echo $row_rsAllListings['element_3'].'</h2><br />';
  10. echo $row_rsAllListings['element_4'].'<br />';
  11. echo $row_rsAllListings['element_5'].'Beds / '.$row_rsAllListings['element_6'];
  12. echo $row_rsAllListings['element_7'].'Baths / '.$row_rsAllListings['element_8'].'sf per '.$row_rsAllListings['element_9'].'<br /></td></tr>'; } while ($row_rsAllListings = mysql_fetch_assoc($rsAllListings)); ?>
  13. </table>
  14. </div>
this code is from clicking 'suggestions' devPHP
original code
php Syntax (Toggle Plain Text)
  1. <div class="content">
  2. <table width="600">
  3. <?php $counter = 0; // initialize counter outside loop ?>
  4. <?php do { ?>
  5. <tr <?php if ($counter++ % 2) {echo 'class="hilite"';} ?>>
  6. <td width="162"><img src="<?php echo $row_rsAllListings['element_16']; ?>></td>
  7. <td width="426">
  8. <h2><a href="\inventory.php?id=" . $id . "\"><?php echo $row_rsAllListings['element_1_1']; ?></a> | <?php echo $row_rsAllListings['element_2']; ?><br />
  9. <?php echo $row_rsAllListings['element_3']; ?></h2>
  10. <br />
  11. <?php echo $row_rsAllListings['element_4']; ?><br />
  12. <?php echo $row_rsAllListings['element_5']; ?> Beds / <?php echo $row_rsAllListings['element_6']; ?>-<?php echo $row_rsAllListings['element_7']; ?> Baths / <?php echo $row_rsAllListings['element_8']; ?>sf per <?php echo $row_rsAllListings['element_9']; ?><br />
  13. </td>
  14. </tr>
  15. <?php } while ($row_rsAllListings = mysql_fetch_assoc($rsAllListings)); ?>
  16. </table>
  17. </div>
  18.  
Last edited by almostbob; Aug 18th, 2009 at 7:13 pm.
Reputation Points: 562
Solved Threads: 368
Posting Maven
almostbob is offline Offline
2,970 posts
since Jan 2009
Aug 18th, 2009
0

Re: PHP variable in link

Okay, so now I have the correct id appearing at the top of the next page by editing the link to be:

PHP Syntax (Toggle Plain Text)
  1. <a href="\inventory.php?id=<?php echo $row_rsAllListings['id']; ?>"\><?php echo $row_rsAllListings['element_1_1']; ?></a>

But the record being displayed in that next page is always the first record in the database.

Progress for sure, but not there yet.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
isak is offline Offline
8 posts
since Jul 2009
Aug 18th, 2009
0

Re: PHP variable in link

Click to Expand / Collapse  Quote originally posted by almostbob ...
try
php Syntax (Toggle Plain Text)
  1. <tr <?php if ($counter++ % 2) { echo 'class="hilite"'; }
  2. echo '><td width="162"><img src='.$row_rsAllListings['element_16'].'></td><td width="426"><h2><a href=/"/inventory.php?id='.$row_rsAllListings['element_1_1'].'</a> | '.$row_rsAllListings['element_2'].'<br />'; ?>
in the original code sample the php variable $id is not within a <?php ?> block, =>cant be accessed
not sure what editor you are using,
but
when I pasted your code into notepad++ $id in this link was hilighted red immediately, v.easy to find
I think you are missing a closing tag for the table row?

I have added the $id back into the code, but getting the same record on the next page regardless of the link I click.
Last edited by isak; Aug 18th, 2009 at 7:05 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
isak is offline Offline
8 posts
since Jul 2009
Aug 18th, 2009
0

Re: PHP variable in link

inventory.php
there is no 'select * from database where id='.$id
there is no reference to '$id' in the file
the href calling the inventory script
inventory.php?id=$id
if you don't use it, you lose it, and id is getting lost
Reputation Points: 562
Solved Threads: 368
Posting Maven
almostbob is offline Offline
2,970 posts
since Jan 2009
Aug 18th, 2009
0

Re: PHP variable in link

almostbob,

This is where I mention "novice" again. Can you explain more simply what and where this works? I would LOVE to use it.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
isak is offline Offline
8 posts
since Jul 2009
Aug 18th, 2009
0

Re: PHP variable in link

your link to inventory.php has a parameter ?id=$id
assuming $id is the id link of the record in the database
that id is available in inventory.php as $id
somewhere in the inventory.php file there should be (could be, usually is) an sql query that selects the row defined by that id
something like
sql Syntax (Toggle Plain Text)
  1. "select * from database where id=$id"
I could not find such query in the file
I could find many queries, but none that selected on the basis of $id
it may be as simple as finding the variable used in inventory.php to select and
change that named variable to $id /* or */
change the parameter name passed by the link to that used in inventory.php /* or */
add a sql select with the right format
the sql in the files is convoluted, almost as if written to hide its intent
Last edited by almostbob; Aug 18th, 2009 at 9:27 pm.
Reputation Points: 562
Solved Threads: 368
Posting Maven
almostbob is offline Offline
2,970 posts
since Jan 2009
Aug 18th, 2009
0

Re: PHP variable in link

I edited the inventory.php file to include the $id. I just put it at the beginning of the street address however I really would rather it not appear in the inventory page when viewed. I have attached the new inventory.php file.

So while it *works* it is still only pulling the first record in the database.

The sql written in the file is written by Dreamweaver.

BTW... nice clean code that you wrote. Thanks.
Attached Files
File Type: php inventory.php (6.5 KB, 19 views)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
isak is offline Offline
8 posts
since Jul 2009
Aug 18th, 2009
0

Re: PHP variable in link

without debugging the code, don't have the db structure, don't know what table refers to what, you have to end up with something like
php Syntax (Toggle Plain Text)
  1. mysql_select_db($database_rsGunn, $rsGunn);
  2. $query_rsInventory1 = "SELECT ap_form_1.element_1_1, ap_form_1.element_1_2, ap_form_1.element_1_3, ap_form_1.element_1_4, ap_form_1.element_1_5, ap_form_1.element_1_6, ap_form_1.element_2, ap_form_1.element_3, ap_form_1.element_16, ap_form_1.element_4, ap_form_1.element_5, ap_form_1.element_57, ap_form_1.element_6, ap_form_1.element_7, ap_form_1.element_8, ap_form_1.element_9, ap_form_1.id FROM ap_form_1 where id=".$id;
  3. $rsInventory1 = mysql_query($query_rsInventory1, $rsGunn) or die(mysql_error());
horrible data structure ap_form.element_57, not at all intuitive
Last edited by almostbob; Aug 18th, 2009 at 11:40 pm.
Reputation Points: 562
Solved Threads: 368
Posting Maven
almostbob is offline Offline
2,970 posts
since Jan 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Users have to login and logout twice, why?
Next Thread in PHP Forum Timeline: Intergrating javascript with php / mysql





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC