954,576 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

PHP variable in link

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:
<?php echo $row_rsAllListings['element_1_1']; ?>

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.

Attachments allListings.php (6.35KB) inventory.php (6.43KB)
isak
Newbie Poster
8 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

<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.

buddylee17
Practically a Master Poster
697 posts since Nov 2007
Reputation Points: 232
Solved Threads: 137
 
<div class="content">
<table width="600">
<?php $counter = 0; // initialize counter outside loop 
do { echo '<tr';
if ($counter++ % 2) {echo 'class="hilite"';} 
echo '><td width="162"><img src="'.$row_rsAllListings['element_16'].'"></td>';
echo '<td width="426"><h2><a href="\inventory.php?id="'.$id.'">'.$row_rsAllListings['element_1_1'];
echo '</a> |  '.$row_rsAllListings['element_2'].'';
echo $row_rsAllListings['element_3'].'</h2>';
echo $row_rsAllListings['element_4'].'';
echo $row_rsAllListings['element_5'].'Beds / '.$row_rsAllListings['element_6']; 
echo $row_rsAllListings['element_7'].'Baths / '.$row_rsAllListings['element_8'].'sf per '.$row_rsAllListings['element_9'].'</td></tr>'; } while ($row_rsAllListings = mysql_fetch_assoc($rsAllListings)); ?>
</table>
</div>


this code is from clicking 'suggestions' devPHP
original code

<div class="content">
  <table width="600">
  <?php $counter = 0; // initialize counter outside loop ?>
<?php do { ?>
<tr <?php if ($counter++ % 2) {echo 'class="hilite"';} ?>>
      <td width="162"><img src="<?php echo $row_rsAllListings['element_16']; ?>></td>
      <td width="426">
<h2><a href="\inventory.php?id=" . $id . "\"><?php echo $row_rsAllListings['element_1_1']; ?></a> | <?php echo $row_rsAllListings['element_2']; ?>
        <?php echo $row_rsAllListings['element_3']; ?></h2>
        
        <?php echo $row_rsAllListings['element_4']; ?>
        <?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']; ?>
      </td>
    </tr>
    <?php } while ($row_rsAllListings = mysql_fetch_assoc($rsAllListings)); ?>
  </table>
</div>
almostbob
Posting Sensei
3,149 posts since Jan 2009
Reputation Points: 571
Solved Threads: 376
 

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

<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.

isak
Newbie Poster
8 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

try

<tr <?php if ($counter++ % 2) { echo 'class="hilite"'; }
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'].''; ?>

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.

isak
Newbie Poster
8 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

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

almostbob
Posting Sensei
3,149 posts since Jan 2009
Reputation Points: 571
Solved Threads: 376
 

almostbob,

This is where I mention "novice" again. Can you explain more simply what and where this works? I would LOVE to use it.

isak
Newbie Poster
8 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

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

"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

almostbob
Posting Sensei
3,149 posts since Jan 2009
Reputation Points: 571
Solved Threads: 376
 

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.

Attachments inventory.php (6.52KB)
isak
Newbie Poster
8 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

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

mysql_select_db($database_rsGunn, $rsGunn);
$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;
$rsInventory1 = mysql_query($query_rsInventory1, $rsGunn) or die(mysql_error());


horrible data structure ap_form.element_57, not at all intuitive

almostbob
Posting Sensei
3,149 posts since Jan 2009
Reputation Points: 571
Solved Threads: 376
 

The "horrible" structure... I am using a program called Machform to create the form that enters the info into the database. This program gives the horrible names you are noting. And yes, they are hard to work with, especially since my form has 65 fields.

I added the WHERE as you had it (WHERE id=".$id;), but I got this error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

When I change it to WHERE ap_form_1.id, I do not get the error message, but I still only get the first record from the database.

isak
Newbie Poster
8 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

$id have some value or may be its blank?

BzzBee
Posting Whiz
327 posts since Apr 2009
Reputation Points: 16
Solved Threads: 48
 

$id is the record number as assigned by the database. There are currently 2 records in the database -- 1 and 2. When you click on either record, it displays the first one and it shows the record number to the left of the street address. So not blank.

isak
Newbie Poster
8 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You