Jump to content

Add Button To Each Table Row Which Is Linked To The Data On That Row


hobbiton73

Recommended Posts

I wonder whether someone may be able to help me please.

 

I'm using the code below to create a table which lists location records for the current user.

 

<form name="locationsconsole" id="locationsconsole" method="post" action="locationsaction.php">
            <table width="865">
                    <tr>
                        <th width="22"></th>
                        <th width="236"><div align="left">Location Name</div></th>
                        <th width="244"><div align="left">Location Address</div></th>
                        <th width="71"></th>                                
                    </tr>   

                    <?php


                    $query = "SELECT  l.*, COUNT(f.locationid) totalfinds FROM detectinglocations l LEFT JOIN finds f ON f.locationid = l.locationid WHERE l.userid = '$idnum' GROUP BY l.locationname";
                    $result = mysql_query($query) or die('error');

                    while($obj=mysql_fetch_object($result))
                    {

                    ?>

                    <tr>
                        <td><input type="hidden" name="lid" value="<?php echo $obj->locationid;?>"/></td>
                        <td><?php echo $obj->locationname;?></td>
                    <td><?php echo $obj->returnedaddress;?></td>
                        <td><input name="type" type="submit" value="View Details"/></td>
                    </tr>
                                <?php
                                    }   

                                ?>


              </table>
      </form>

 

You'll see from the code that on each row there is a button which takes the user to another page via 'locationsaction.php'. There are currently 3 location records in the table which is correct, and when the button is clicked the user is taken to the correct screen.

 

However, the problem I'm having, is that no matter which row button I select, the record retrieved is always related to the last location record and I just can't work out what the problem is. I've been working in this for sometime, and I just can't seem to find a solution. I do know that the query is working OK because I've tested this independently.

 

I just wondered whether someone could take a look at this please and let me know where I'm gong wrong.

 

Many thanks and kind regards

Link to comment
Share on other sites

Hi, thank you for taking the time to reply to my post.

 

I'm very sorry but I'm relatively  new to PHP, could you perhaps explain a little more about the array.

 

For additional information I have added the 'locationsaction.php' script which is called when the button is clicked.

 

<?php 
session_start();
$_SESSION['lid'] = $_POST['lid'];
if (isset($_POST['type'])) {
    $urls = array(
        'View Details' => 'viewlocation.php'
    );
    $url = $urls[$_POST['type']];
    header("Location: " . $url);
}
?>

 

Many thanks and kind regards

Link to comment
Share on other sites

Note: You have ONE large form being submitted, which nullifies the need for several submit buttons.  If you want to process every location on submission, then you only need one submit button.  However, if you want to only submit the item where the submit button is present, you will need to wrap each in their own form, or use javascript techniques that are likely beyond your skill level.  Regardless, the following applies.

 

According to your query, location id references a stored location in your database with an auto incremet ID, and this is what you are referring to in your first td as name=lid.  The problem is, you have several location id's, but field names must be unique.  This is why only one field with name "lid" is being retuned.  What you will need to do is create an array of field names so uniqueness is preserved while maintaining data integrity.  This is done by attaching an array key to `name` that can be used as an identifier - which is handedly available in your code already.

 

<?php
$query = "SELECT  l.*, COUNT(f.locationid) totalfinds FROM detectinglocations l LEFT JOIN finds f ON f.locationid = l.locationid WHERE l.userid = '$idnum' GROUP BY l.locationname";
$result = mysql_query($query) or die('error');

while($obj=mysql_fetch_object($result)):
?>

  <tr>
      <td><input type="hidden" name="<?php echo "lid[' . $obj->locationid . ']"; ?> value="<?php echo $obj->locationid;?>"/></td>
      <td><?php echo $obj->locationname;?></td>
      <td><?php echo $obj->returnedaddress;?></td>
      <td><input name="type" type="submit" value="View Details"/></td>
  </tr>

<?php endwhile; ?>

 

If you have 3 Location IDs (1, 2, 3), then when the form is submitted they will be identified as lid[1], lid[2], and lid[3]

 

That is the general gist.  So depending on the actual purpose of the form submission will depend on what you ought to be doing.

 

In regards to your $_SESSION and header(), it makes little sense what you're doing.  Foremost, if you want to check if a form is submitted, you should check if the submit button was pressed - then you'll want to validate the inputs themselves. 

 

Furthermore, as your script is starting to become clear in it's purpose to me, there's really no need to be using POST at all.  You should use GET and query your URI for the location the user is looking up.

 

Instead of deleting the first portion of my reply, consider yourself getting two lessions.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.