CB150Special Posted July 22, 2017 Share Posted July 22, 2017 (edited) 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. Edited July 22, 2017 by CB150Special Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted July 22, 2017 Share Posted July 22, 2017 <?php while($row_events = mysqli_fetch_array($result_events)):;?> <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: Learn about PDO! Separate your database scope and your page display scope. Consider using a template engine such as twig/smarty. Quote Link to comment Share on other sites More sharing options...
CB150Special Posted July 22, 2017 Author Share Posted July 22, 2017 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. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted July 23, 2017 Share Posted July 23, 2017 There's an amazing new web technology called “anchor”. <a href="link/to/exam_results.php?param_1=value_1¶m_2=value_2"><!-- student list entry --></a> 1 Quote Link to comment Share on other sites More sharing options...
CB150Special Posted July 23, 2017 Author Share Posted July 23, 2017 I'm not sure I understand your reply. <a href="link/to/exam_results.php?param_1=value_1¶m_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. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted July 23, 2017 Share Posted July 23, 2017 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). Quote Link to comment Share on other sites More sharing options...
CB150Special Posted July 23, 2017 Author Share Posted July 23, 2017 Where do you specify the link, in the table rows ? Can you give me an example on the table code please. Quote Link to comment Share on other sites More sharing options...
CB150Special Posted July 23, 2017 Author Share Posted July 23, 2017 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> Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted July 23, 2017 Share Posted July 23, 2017 <!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> 1 Quote Link to comment Share on other sites More sharing options...
CB150Special Posted July 23, 2017 Author Share Posted July 23, 2017 Thank you, so simple. Quote Link to comment Share on other sites More sharing options...
CB150Special Posted July 23, 2017 Author Share Posted July 23, 2017 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 ? Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted July 23, 2017 Share Posted July 23, 2017 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. Quote Link to comment Share on other sites More sharing options...
CB150Special Posted July 23, 2017 Author Share Posted July 23, 2017 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 ? Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted July 23, 2017 Share Posted July 23, 2017 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(). Quote Link to comment Share on other sites More sharing options...
CB150Special Posted July 23, 2017 Author Share Posted July 23, 2017 This is the only page I could find on <a href> with parameter passing if anyone is interested. http://html.net/tutorials/php/lesson10.php Can I get something like this to work ? <a href='test.php?eventID='.<?php$row_events['EventID'];?> > Quote Link to comment Share on other sites More sharing options...
ajoo Posted July 23, 2017 Share Posted July 23, 2017 Hi ! “eventID ” : there is a space between the D and the closing quote. That's what he meant. Yes there is an inbuilt php function called var_dump. PHP: var_dump - Manual Quote Link to comment Share on other sites More sharing options...
CB150Special Posted July 23, 2017 Author Share Posted July 23, 2017 Finally found it. I didn't realize that PHP code can be written within sets of ' ' or " " <td><a href='test.php?eventID=<?php echo $row_events['EventID'];?>' > <?php echo $row_events['Dat'];?></td> Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted July 23, 2017 Share Posted July 23, 2017 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 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.