PHP variable in link

Reply

Join Date: Jul 2009
Posts: 8
Reputation: isak is an unknown quantity at this point 
Solved Threads: 0
isak isak is offline Offline
Newbie Poster

PHP variable in link

 
0
  #1
Aug 18th, 2009
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, 2 views)
File Type: php inventory.php (6.4 KB, 3 views)
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 596
Reputation: buddylee17 has a spectacular aura about buddylee17 has a spectacular aura about 
Solved Threads: 125
buddylee17's Avatar
buddylee17 buddylee17 is offline Offline
Posting Pro

Re: PHP variable in link

 
0
  #2
Aug 18th, 2009
<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.
Lost time is never found again.
- Benjamin Franklin
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 1,315
Reputation: almostbob has a spectacular aura about almostbob has a spectacular aura about 
Solved Threads: 161
almostbob's Avatar
almostbob almostbob is offline Offline
Nearly a Posting Virtuoso

Re: PHP variable in link

 
0
  #3
Aug 18th, 2009
  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
  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.
Failure is not an option It's included free
If at first you dont succeed, join the club
Of course its always in the last place you look, you dont keep looking after you find it

Please mark solved problems, solved
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 8
Reputation: isak is an unknown quantity at this point 
Solved Threads: 0
isak isak is offline Offline
Newbie Poster

Re: PHP variable in link

 
0
  #4
Aug 18th, 2009
Okay, so now I have the correct id appearing at the top of the next page by editing the link to be:

  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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 8
Reputation: isak is an unknown quantity at this point 
Solved Threads: 0
isak isak is offline Offline
Newbie Poster

Re: PHP variable in link

 
0
  #5
Aug 18th, 2009
Originally Posted by almostbob View Post
try
  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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 1,315
Reputation: almostbob has a spectacular aura about almostbob has a spectacular aura about 
Solved Threads: 161
almostbob's Avatar
almostbob almostbob is offline Offline
Nearly a Posting Virtuoso

Re: PHP variable in link

 
0
  #6
Aug 18th, 2009
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
Failure is not an option It's included free
If at first you dont succeed, join the club
Of course its always in the last place you look, you dont keep looking after you find it

Please mark solved problems, solved
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 8
Reputation: isak is an unknown quantity at this point 
Solved Threads: 0
isak isak is offline Offline
Newbie Poster

Re: PHP variable in link

 
0
  #7
Aug 18th, 2009
almostbob,

This is where I mention "novice" again. Can you explain more simply what and where this works? I would LOVE to use it.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 1,315
Reputation: almostbob has a spectacular aura about almostbob has a spectacular aura about 
Solved Threads: 161
almostbob's Avatar
almostbob almostbob is offline Offline
Nearly a Posting Virtuoso

Re: PHP variable in link

 
0
  #8
Aug 18th, 2009
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
  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.
Failure is not an option It's included free
If at first you dont succeed, join the club
Of course its always in the last place you look, you dont keep looking after you find it

Please mark solved problems, solved
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 8
Reputation: isak is an unknown quantity at this point 
Solved Threads: 0
isak isak is offline Offline
Newbie Poster

Re: PHP variable in link

 
0
  #9
Aug 18th, 2009
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, 2 views)
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 1,315
Reputation: almostbob has a spectacular aura about almostbob has a spectacular aura about 
Solved Threads: 161
almostbob's Avatar
almostbob almostbob is offline Offline
Nearly a Posting Virtuoso

Re: PHP variable in link

 
0
  #10
Aug 18th, 2009
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
  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.
Failure is not an option It's included free
If at first you dont succeed, join the club
Of course its always in the last place you look, you dont keep looking after you find it

Please mark solved problems, solved
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC