Jump to content

Display content of table row with hover


CB150Special

Recommended Posts

This may not have a PHP answer but looking for guidance here.

 

This is part one of a 2 stage process.

I have a table generated from an SQL query. I use the following to display the contents.

<?php while($row_events = mysqli_fetch_array($result_events)):;?>
    <tr>
    <td><?php echo $row_events['Dat'];?></td>
    <td><?php echo $row_events['EventName'];?></td>   
    <td style="display:none;"><?php echo $row_events['EventID'];?>                 
    </tr>
<?php endwhile;?>

I would like the EventID to be displayed (or something similar just to confirm it is working) when I hover over the corresponding table line.

 

Part two will be to transfer to a new HTML page with the EventID used as a key with an OnClick event.

Link to comment
Share on other sites

  1. <?php while($row_events = mysqli_fetch_array($result_events)):;?>

  2. <tr data-popup="<?php echo $row_events['EventID'];?>">

...

</tr>

<?php endwhile;?>

 

Look for some jQuery or JavaScript library to show it, or right your own.

 

You should do the following:

  1. Learn about PDO!

Separate your database scope and your page display scope.  Consider using a template engine such as twig/smarty.

Link to comment
Share on other sites

Alternately is there another way to display a table and execute a link to another html page with some parameter of the table passed with that selection choice.

 

An example would be a list of students and clicking on a student brings up their exam results.

Link to comment
Share on other sites

I'm not sure I understand your reply.

<a href="link/to/exam_results.php?param_1=value_1&param_2=value_2"><!-- student list entry --></a>

Whether I use param_1=value_1 or a session variable is a matter of choice, the question is how do I get the parameter from the row I select on the table into that variable.

Link to comment
Share on other sites

Forget about variables, onclick events and whatnot.

 

This is the definition of a plain old link: You click on something, and then the browser takes you to another page. In your case, you click on an entry in a list of events, and then you're taken to the event page (with the ID in the URL).

Link to comment
Share on other sites

In the mean time

  <td onclick="alert(<?php $row_events['EventID'];?>)"><?php echo $row_events['EventName'];?></td>                     

works but below does not. I get the on event message but no EventID

    <td onclick=disp_event()><?php echo $row_events['EventName'];?></td>   
     ....

    <script>
        function disp_event() {
            alert(<?php echo $row_events['EventID'];?>)
        }
    </script> 
Link to comment
Share on other sites


<!DOCTYPE html>
<html>
<head>
<title>An HTML standard template</title>
<meta charset="utf-8">
<style>
.records {
border-collapse: collapse;
}
.records td {
border: 1px solid black;
padding: 0;
}
.records a {
display: block;
padding: 15px;
}
</style>
</head>
<body>
<table class="records">
<tr>
<td><a href="http://example.com?row=1">Click anywhere in the cell</a></td>
<td><a href="http://example.com?row=1">Click anywhere in the cell</a></td>
</tr>
<tr>
<td><a href="http://example.com?row=2">Click anywhere in the cell</a></td>
<td><a href="http://example.com?row=2">Click anywhere in the cell</a></td>
</tr>
</table>
</body>
</html>
Link to comment
Share on other sites

Not so simple. I've tried all the options except the right one !!

   <td><a href='mem_event.php?eventID = 71'> ><?php echo $row_events['Dat'];?></td>
   $sql_event = 'SELECT * FROM fees WHERE EventID = "'.$_GET['eventID'].'" ORDER BY MemName';

I get Undefined Index eventID

 

How do I list all the variables so I can see exactly what I have  ?

Link to comment
Share on other sites

This is the third and last time I'm explaining this: Use – prepared – statements. As long as you don't take care of basic code sanity, it's pointless to talk about errors, because errors are to be expected.

 

If you simply inspect your parameters with var_dump(), you'll also see that you don't have an eventID parameter. Your parameter is called “eventID ” with a space after the name and within the value.

Link to comment
Share on other sites

Your parameter is called “eventID ” with a space after the name and within the value.

I have no idea what you are talking about ?

 

Is there something like var_dump() that list all the variables on a form, or can you see them with Inspect Element on a web page ?

Link to comment
Share on other sites

I have no idea what you are talking about ?

 

Look:

mem_event.php?eventID = 71
                     ^^^

You have a parameter with the name “eventID<space>” (PHP turns that into “eventD_”) and the value “<space>71”. If you don't want that, remove the spaces.

 

 

 

Is there something like var_dump() that list all the variables on a form, or can you see them with Inspect Element on a web page ?

 

Well, URL parameters are in the URL bar. And POST parameters can be seen in the HTTP request in the Network tab.

 

What's more important, though, is how the parameters actually look like in PHP, hence var_dump().

Link to comment
Share on other sites

Let's turn that into

<a href="<?= html_escape('events.php?'.http_build_query(['id' => $event['event_id']]), 'UTF-8') ?>">Link text</a>
/**
 * HTML-escapes a string, so that it can safely be included in simple HTML contexts
 *
 * @param string $raw_input the string which should be escaped
 * @param string $encoding  the character encoding of the input string
 *
 * @return string the encoded string
 */
function html_escape($raw_input, $encoding)
{
    return htmlspecialchars($raw_input, ENT_QUOTES | ENT_SUBSTITUTE, $encoding);
}
  • The http_build_query() function takes care of turning a set of parameters into a syntactically valid URL query
  • HTML-escaping makes sure the URL can be inserted into an HTML context without breaking anything
  • Following standard naming conventions (e. g. snake_case instead of CamelCase) avoids confusion and interoperability trouble
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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