Ok, I have a basic all-in-one page for my user profiles on my site like so:
<?php
if($_GET['p']=='pb'){
echo '<p>pb</p>';
}elseif($_GET['p']=='example2'){
echo '<p>Example Two';
}elseif($_GET['p']=='example1'){
echo '<p>Example One';
}else{
echo '<h3>Page not defined; error!</h3>';
}
?>
I am having issues with some inline javascript though, when I try to add a onclick="location.href='' the php confuses it. I've tried adding \' to the front and back, but it doesn't seem to work, here is what I have:
<?php
if($_GET['p']=='pb'){
echo '<p>pb</p>';
}elseif($_GET['p']=='example2'){
echo '
<h1>Example Link One</h1>
<p>You clicked Example Link One!</p>
<table border="0" cellspacing="15" width="100%">
<tr>
<td bgcolor="grey" onclick="location.href=\''/media/profiles/person/'\'">
<p>HOME</p>
</td>
</tr>
</table>';
}elseif($_GET['p']=='example1'){
echo '
<h1>Example Link One</h1>
<p>You clicked Example Link One!</p>
<table border="0" cellspacing="15" width="100%">
<tr>
<td bgcolor="grey" onclick="location.href=\''/media/profiles/person/'\'">
<p>HOME</p>
</td>
</tr>
</table>';
}else{
echo '<h3>Page not defined; error!</h3>';
}
?>
But I still get several errors, including:
-Use of undefined constant media - assumed 'media'
-Warning: Division by zero
-Notice: Use of undefined constant profiles - assumed 'profiles'
-Notice: Use of undefined constant person - assumed 'person'
So on and so forth.
I know it what it has to do with, but how can I fix it?
Why not just close the PHP tags?
<?php
if($_GET['p']=='pb'){
echo '<p>pb</p>';
}elseif($_GET['p']=='example2'){
?>
<h1>Example Link One</h1>
<p>You clicked Example Link One!</p>
<table border="0" cellspacing="15" width="100%">
<tr>
<td bgcolor="grey" onclick="javascript:location.href='/media/profiles/person/'">
<p>HOME</p>
</td>
</tr>
</table>
<?php
}elseif($_GET['p']=='example1'){
?>
<h1>Example Link One</h1>
<p>You clicked Example Link One!</p>
<table border="0" cellspacing="15" width="100%">
<tr>
<td bgcolor="grey" onclick="javascript:location.href='/media/profiles/person/'">
<p>HOME</p>
</td>
</tr>
</table>
<?php
}else{
echo '<h3>Page not defined; error!</h3>';
}
?>
Also, it would be more elegant to use a design pattern within your application - This application, if you stick with the "design pattern" you're currently using will become a burden to update and maintain.
Your quoting is wrong. You are escaping the first single quote but with another one following imediatly. This second one is interpreted by php as the end of string which is not what you intended. Just remove this 2nd single quotes and it should fix your errors.
echo '
<h1>Example Link One</h1>
<p>You clicked Example Link One!</p>
<table border="0" cellspacing="15" width="100%">
<tr>
<td bgcolor="grey" onclick="location.href=\'/media/profiles/person/\'">
<p>HOME</p>
</td>
</tr>
</table>';
Edit: Also worth mentioning: If you have a string with mixed quotes it is often easier to use heredoc style
$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
Related
I am making a PHP web application and fetching data from MySql into the HTML table below.
<table id = "table" width="500px" cellpadding=2 celspacing=5 border=2 >
<tr>
<th>Subject Code</th>
<th>Subject Name</th>
<th>Question Paper</th>
</tr>
<?php while($row=mysqli_fetch_array($result)):?>
<tr>
<td align="center"><?php echo $row['Subject_Code'];?></td>
<td align="center"><?php echo $row['Subject_Name'];?></td>
<td align="center"><img border="0" alt="image" src="ProjectPictures/questionPaper.png" width="30" height="30"></td>
</tr>
<?php endwhile;?>
</table>
Here is the output of the table
The third column consists of clickable image icons which loads question papers in an iFrame besides it.
I want that when this image is clicked, the cell value of the first column --> 'Subject code' for that particular row is obtained.
Here is the code:
function showPaper(){
var v;
//method1 not working
for(var i=1;i<table.rows.length; i++){
table.rows[i].onclick = function(){
rIndex = this.rowsIndex;
v=this.cells[0].innerHTML;
alert(v);
};
}
//method 2 not working
v = $("#table tr.selected td:first").html();
alert(v);
/*loc1 path rest of code
document.getElementById('myiFrame').src = loc1*/;
}
I require this subject code value as one of the variables in the pdf src path of the iFrame.
But nothing seems to be working, the value in 'v' variable is showing undefined.
How can I make this code work?
you need to use onclick() event to get value of Subject_Code .
Change
<td align="center"><img border="0" alt="image" src="ProjectPictures/questionPaper.png" width="30" height="30"></td>
With
<td align="center" id="<?php echo $row['Subject_Code'];?>" onclick="showPaper(this.id);"><img border="0" alt="image" src="ProjectPictures/questionPaper.png" width="30" height="30"></td>
And your showPaper() should be
<script>
function showPaper(id)
{
alert(id);
//Do something
}
</script>
I'm trying to find a way to filter my sql data from a mysql database on my html table. The trouble I've had is because it's a dynamic table. Should I be sorting on the html table, and if so, how do I get the result html from the sql query (the entire table), as opposed to the html that's just a single row.
If I need to sort on the SQL query itself, how can I do this without needing to refresh the page every time?
Thanks for any help!
Here's my code for reference:
<?php
$host=""; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name=""; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
// OREDER BY id DESC is order result by descending
}
$result=mysql_query($sql);
?>
<html>
<head>
<script type="text/javascript" charset="utf-8" src="jquery.js"></script>
</head>
<table id="forum" width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<thead>
<tr>
<th width="6%" align="center" bgcolor="#E6E6E6"><strong>#</strong></td>
<th width="43%" align="center" bgcolor="#E6E6E6"><strong>Topic</strong></td>
<th width="10%" align="center" bgcolor="#E6E6E6"><strong>Author</strong></td>
<th width="15%" align="center" bgcolor="#E6E6E6"><strong>Views</strong></td>
<th width="13%" align="center" bgcolor="#E6E6E6"><strong>Replies</strong></td>
<th width="13%" align="center" bgcolor="#E6E6E6"><strong>Date/Time</strong></td>
</tr>
</thead>
<?php
// Start looping table row
while($rows=mysql_fetch_array($result)){
?>
<tbody>
<tr>
<td bgcolor="#FFFFFF"><? echo $rows['threadtype']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['topic']; ?><BR></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['email']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['view']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['reply']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['datetime']; ?></td>
</tr>
</tbody
<?php
// Exit looping and close connection
}
mysql_close();
?>
<tfoot>
<tr>
<td colspan="6" align="right" bgcolor="#E6E6E6"><strong>Create New Topic</strong> </td>
</tr>
</tfoot>
</table>
</html>
If you don't have a lot of data [less than 100 rows], it is reasonable to load the entire thing with one query and do all the sorting/filtering client-side.
There are tons of great jquery plugins for doing this. Here is a good one I like:
https://datatables.net/
If you want to be scaleable and handle a lot of rows then you don't want to just load all of them at once.
In this case you want to implement a paging pattern with your queries. If you need to do paging then you will also need your query to sort on the backend when the user changes which column they want to sort by.
Rather than reinvent the wheel, especially if you don't feel comfortable rolling your own here, there are lots of tools you can use to do this. This one seems promising: http://blog.drale.com/mysql-table-viewer-with-pagination-and-sorting/
I was checking the site and found JavaScript: How to strip HTML tags from string? but this doesn't really explains how to take this:
<tr id="element.incident.comments.additional">
<td colspan="2">
<span style="">
<table cellpadding="0" cellspacing="0" style="table-layout:fixed" width="100%">
<tbody>
<tr>
<td colspan="2">
<hr>
</td>
</tr>
<tr style="">
<td class="tdwrap"><strong>2014-01-23 14:45:40 - <a style="color:blue" href="sys_user.do?sysparm_view=itil&sysparm_query=user_name=Superhero#superhero.com">SuperHero</a></strong></td>
<td align="right" nowrap="true"><sup>Additional comments</sup></td></tr>
<tr style="">
<td colspan="2"><span style="word-wrap:break-word;">received from: SDUperhero#superhero.com<br><br>lalalalalala
<br>lotsofwords<br><br><br><br><br><br>
The information transmitted, including any attachments, is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material</span></td></tr></tbody></table></span></td>
</tr>
and get the text inside:
<tr id="element.incident.comments.additional">
for further parsing.
I tried with
function strip(html)
{
var tmp = document.createElement("DIV");
tmp.innerHTML = html;
return tmp.textContent||tmp.innerText;
}
var commentsField = document.getElementById("element.incident.comments.additional").innerHTML;
alert(strip(commentsField));
but I'm not sure if this is the right way as I'm not getting anything in the alert.
Any thoughts?
Because you have . in your ids, you will need to escape them in your jQuery selector:
$("#txt").val($("#element\\.incident\\.comments\\.additional").html());
and that should work.
jsFiddle: http://jsfiddle.net/hescano/EQ9b3/
i want to load a html table in a div
The HTML-Code is loaded via:
$("#table_wrapper").hide();
$.get("<?echo base_url();?>schichtplan/employee_fields/"+plan_id+"true",function(data){
$("#table_wrapper").html(data);
$("#table_wrapper").show();
});
Data is validated via alert and looks like:
<table border= '0'cellpadding='4' cellspacing='0' class='mitarbeiter' />
<thead>
<tr>
<th> </th><th><div id='plan_id:1;sort_id:1' class='edit_employee'>User1</div></th><th><div id='plan_id:1;sort_id:2' class='edit_employee'>User2</div></th></tr>
</thead>
<tbody>
<tr>
<td class='first'>Anstellung</td><td> </td><td> </td></tr>
<tr>
<td class='first'>Stundenlohn</td><td> </td><td> </td></tr>
<tr>
<td class='first'>Festlohn</td><td> </td><td> </td></tr>
<tr>
<td class='first'>Bonus</td><td> </td><td> </td></tr>
<tr>
<td class='first'>Kassenminus</td><td> </td><td> </td></tr>
<tr>
<td class='first'>Nachtzuschlag</td><td> </td><td> </td></tr>
<tr>
<td class='first'>Sonstiges</td><td> </td><td> </td></tr>
</tbody>
</table>
After the JQuery-Action the div looks like:
<div id="table_wrapper" style="display: block; "><table border="0" cellpadding="4" cellspacing="0" class="mitarbeiter"></table>
<div id="plan_id:1;sort_id:1" class="edit_employee">User1</div><div id="plan_id:1;sort_id:2" class="edit_employee">User2</div>
Anstellung
Stundenlohn
Festlohn
Bonus
Kassenminus
Nachtzuschlag
Sonstiges
</div>
Table-Code is generated with CodeIgniter.
I have no idea why the result looks like that
Some hint?
Thanks
There is a slash at the end of the tag that starts the table. Only some tags can be closed with the slash, and if a tag isn't allowed to have the slash, it will be kept open for the rest of the page, which will make your html invalid. This is what it should look like:
<table border='0' cellpadding='4' cellspacing='0' class='mitarbeiter'>
<thead>
<!-- ... -->
</thead>
<tbody>
<!-- ... -->
</tbody>
</table>
It looks like your Table-Code is rendered NOT COMPLETELY LIKE HTML but table renders like normal view. Check is there any config option for Table-Code module to generate FULL HTML table.
Which method do you use to render table?
Just noticed - the table is self closed on the first line:
<table border= '0'cellpadding='4' cellspacing='0' class='mitarbeiter' />
Should be:
<table border= '0'cellpadding='4' cellspacing='0' class='mitarbeiter' >
I strongly suspect this is your problem sir.
I have a table with 'see more' written on the side of the title "Trainee Insurance.."
I want people to be able to click see more and then information appears below the title
How do I do this?
Thanks!
James
<table height="20" width="775" cellpadding="0" cellspacing="0">
<tr>
<td>
<font face="lucidagrande" size="4" color="black">Trainee Insurance Broker - London</font>
<td align="right">
<font face="lucidagrande" size="2" color="red">See More...</font>
</td>
</tr>
</table>
With JQuery:
Give IDs to your 'See More' button and the content that is to be displayed:
$('#see_more').click( function() { $('#more_content').show(); });
You've got a bad case of 1990s HTML.
Here's the 1990s way to do it:
<div>Trainee Insurance Broker - London</div>
<div id="a1" style="display:none">### more information ###</div>
See More...
JS:
function showThis(id) {
document.getElementById(id).style.display='block'
}
A few tips:
don't use tables for layout. Use HTML + CSS
don't use inline font statement, use CSS
explore jQuery.
There might be other ways -- non-JavaScript ways -- but I have always used JavaScript to do this. Add an onclick handler to the "see more" td and, in that handler, set the more element to have display:inline style, something like this (untested):
<script type="text/javascript">
function show_more ( element_to_show ) {
var element_to_show = getRefToDiv( element_to_show );
element_to_show.style.display = "inline";
}
</script>
<table height="20" width="775" cellpadding="0" cellspacing="0">
<tr>
<td>
<font face="lucidagrande" size="4" color="black">Trainee Insurance Broker - London</font>
<td align="right" onclick="show_more('more');">
<font face="lucidagrande" size="2" color="red">See More...</font>
</td>
<td id="more" style="display:none;">The guy will see this when he clicks See More...</td>,
</tr>
</table>
I would use jQuery to show/hide the block of text whenever "See More...." is clicked.
http://api.jquery.com/show/
Of course, this assumes you're already familiar with jQuery. If you aren't, or you can't/won't use it, a quick Google search turned up http://www.cssnewbie.com/showhide-content-css-javascript/
Use display=none; in style and remove it on onclick event.
You will need to either; Have a hidden DIV that appears when you click 'see more'. Which will need to be Javascript or JQuery. OR Have some PHP to generate another row in the table.
If you want to load data in from a separate file you'll have to use AJAX. If you want to show an element that's already on the page when "See More" is clicked you can use regular javascript. Just add a new row to the table, give it an ID and hide it using inline styles. Then add a link around your "see more" text and give it an onclick handler that shows the hidden row. Like this:
<table height="20" width="775" cellpadding="0" cellspacing="0">
<tr>
<td><font face="lucidagrande" size="4" color="black">Trainee Insurance Broker - London</font></td>
<td align="right"><font face="lucidagrande" size="2" color="red">See More...</font></td>
</tr>
<tr id="showme" style="display: none">
<td colspan="2">More info appears here!!!</td>
</tr>
</table>
<script language="javascript">
$(document).ready(function(){
$('#see_more').click(function(){
$("#more_text").show();
});
$("#more_text").hide();
});
</script>
<table height="20" width="775" cellpadding="0" cellspacing="0">
<tr>
<td>
<font face="lucidagrande" size="4" color="black">Trainee Insurance Broker - London</font>
<td align="right" id="see_more">
<font face="lucidagrande" size="2" color="red">See More...</font>
</td>
</tr>
<tr><td id="more_text">This is more text here......</td></tr>
</table>