I am trying to create a hyperlink from two pieces of text split over two cells in a table row.
I am generating my table using PHP to echo out the results from my database to a table.
When it echo's it generates a hyperlink with GET variables at the end which allow the user to visit a page relevant to that information.
The problem is that I can't seem to generate a hyperlink that will go across those table cells, I have looked around the web and there is nothing that says I cannot do this.
As you can see from the screenshot below I am generating a hyperlink inside one table cell but I want the other table cell to have the same hyperlink.
Code
while ($row = $db->fetch_assoc($newest))
{
echo "<tr>";
echo "<td>";
echo "<a href='manager.php?method=view&id=".$row['id']."'>".$row['first_name']." ". $row['second_name']. "</td><td>".$row['company_name']."</a>";
echo "</td>";
echo "</tr>";
}
I have a feeling that I will just have to generate two separate hyperlinks for the table cells.
However I am hoping someone here can prove me wrong and save me a few lines of code.
Thanks :)
Using native hyperlinks, you will have to create separate wrappers for each cell.
However, if you want to use JS for linking and redirecting, you could do something like:
.....
<tr class="clickable" data-href="http://google.com">
<td>cell-1</td>
<td>cell-2</td>
<td>cell-3</td>
</tr>
....
and then:
$(function(){
$('tr.clickable').click(function(){
window.location.href = $(this).attr('data-href');
});
});
Simply work around it with JS:
echo "<tr onclick=\"location.href='manager.php?method=view&id=".$info.";'\">";
If you do not want to break the table structure (ie. putting the name and the company into one (multi-column) cell), there is IMHO no way other than generating two hyperlinks.
What you might want to do is to use some CSS for a hover effect and some JavaScript to register a user clicked on a cell (which you can, given the structure above, associate with the tr element).
You can not do it like this. Try instead:
while ($row = $db->fetch_assoc($newest))
{
$url = "manager.php?method=view&id=".$row['id'];
echo "<tr>";
echo "<td>" . $row['first_name']." ". $row['second_name']. "</td>";
echo "<td>" . $row['company_name'] . "</td>";
echo "</tr>";
}
while ($row = $db->fetch_assoc($newest))
{
echo "<tr>";
echo "<td>";
echo "<a href='manager.php?method=view&id=".$row['id']."'>".$row['first_name']." ". $row['second_name']."</a></td><a href='manager.php?method=view&id=".$row['id']."'>".$row['company_name']."</a><td></td>";
echo "</td>";
echo "</tr>";
}
Related
I'm populating an html table with data from MySQL DB and I want to add a mailto function on the click of one of the columns. Problem is when I do it, the column is blank, but when I inspect it in the browser it shows up in the inspect panel.
My Code:
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>".$row['Property_ID']."</td>";
echo "<td>".$row['House_Number']."</td>";
echo "<td>".$row['Street_Address']."</td>";
echo "<td>".$row['Postal_Code']."</td>";
echo "<td>".$row['City']."</td>";
echo "<td>"."<a href='mailto:".$row['Submitted_By']."'></a>"."</td>";
echo "<td>".$row['Date_Submitted']."</td>";
echo "</tr>";
What the browser shows:
On inspection:
You <a> tag is empty, that's why you don't see your link.
You have to add your text inside <a> and </a>:
echo "<td>"
. "<a href='mailto:".$row['Submitted_By']."'>".$row['Submitted_By']."</a>"
. "</td>";
echo "<td>"."<a href='mailto:".$row['Submitted_By']."'>".$row['Submitted_By']."</a>"."</td>";
this line should be exactly like this.
The below code creates a table from mysql on a web page and changes the column background colour based on the gender column value being male or female but I want to extend this to change the row's colour without using javascript/jquery. Please can someone help me with this? :)
while($tablev2=mysql_fetch_assoc($records)) {
echo "<td>".$tablev2['primary_key']."</td>";
echo "<td>".$tablev2['name']."</td>";
if($tablev2['gender']=='male')
echo "<td style='background-color:powderblue;'>".$tablev2['gender']."</td>";
elseif($tablev2['gender']=='female')
echo "<td style='background-color:pink;'>".$tablev2['gender']."</td>";
else echo "<td>".$tablev2['gender']."</td>";
echo "<td><a target = '_blank' href='".$tablev2['link']."'>Click to see their facebook profile</a></td>";
echo "</tr>";
}
You just need to apply a style on the 'tr' tag depending on the gender value.
if($tablev2['gender']=='male'){
echo '<tr class="bg-male">';
}elseif($tablev2['gender']=='female'){
echo '<tr class="bg-female">';
}
while($tablev2=mysql_fetch_assoc($records)) {
if($tablev2['gender']=='male')
echo "<tr style='background-color:powderblue;'>";
elseif($tablev2['gender']=='female')
echo "<tr style='background-color:pink;'>";
else
echo "<tr>";
echo "<td>".$tablev2['primary_key']."</td>";
echo "<td>".$tablev2['name']."</td>";
echo "<td>".$tablev2['gender']."</td>";
echo "<td><a target = '_blank' href='".$tablev2['link']."'>Click to see their facebook profile</a></td>";
echo "</tr>";}
use background color to TR tag instead of TD tag.
otherwise you have to apply for all TD and it's a bad practice.
I want to create a site, which displays a lot of records from a database. To make it more reader-friendly, I want to use a styling. One record is white background, the next blue, the next hhite again.
So I tried this:
<?php while ($info = mysql_fetch_array($data)){
PRINT "<tr>";
PRINT "<td>" .$info['articlenr']. "</td>";
PRINT "<td>" .$info['stock']. "</td>";
PRINT "</tr>";
PRINT "<tr>";
PRINT "<td bgcolor=#0066FF>" .$info['articlenr']. "</td>";
PRINT "<td bgcolor=#0066FF>" .$info['stock']. "</td>";
PRINT "</tr>";
}
?>
This works for the view, but the problem is, the blue record is the same as the white, not the next one, it just doubles the record and make it another color.
How can I do this right?
Use :nth-of-type(even) to get even/odd combination of color's.
Here is a demo example:
html:
<table>
<tr><td>item1</td>
</tr>
<tr><td>item2</td>
</tr>
<tr><td>item3</td>
</tr>
<tr><td>item4</td>
</tr>
</table>
css:
tr:nth-of-type(even) { background-color: #0066FF; }
Demo
If you want to do this in PHP, you could do it like this :
<?php
$iter = 0;
$color1 = 'red'; //can se hex code too, like #0066FF;
$color2 = 'blue';
while ($info = mysql_fetch_array($data))
{
echo '<tr style="background-color:'.( ($iter%2==0) ? $color1 : $color2).';">';
// rest of the printing stuff
$iter++;
}
?>
Statement
($iter%2==0) ? $color1 : $color2
does this : it asks the question whether iterator (or row number) is even. If yes, the it takes color1. If not (row is uneven) it takes the second color.
PHP Smarty is good for doing this kind of stuff (iterating over colors and styles), but it may be difficult for beginners.
Please go through this links:
https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Getting_started
http://css-tricks.com/complete-guide-table-element/
Including CSS in .php file
Can I offer some advice on your code? Your approach mixes PHP and HTML in a way that makes it difficult for your development environment to parse your HTML, and you can achieve the same with less keystrokes! Consider this approach instead:
<?php while ($info = mysql_fetch_array($data)): ?>
<tr>
<td><?php echo $info['articlenr'] ?></td>
<td><?php echo $info['stock'] ?></td>
</tr>
<?php endwhile ?>
Changes I've made:
Removed the duplicate row
Rendered everything in HTML mode, and opened PHP tags just for PHP code
Switched the loop to the colon form, which is often thought to be clearer in templates. Do carry on using the brace approach, however, in large chunks of code (e.g. classes)
Written PHP keywords in lower case
Used echo rather than print
Combine this with Joke_Sense10's answer for a full solution.
Im pulling in data about sports events via an xml feed, im using simplexml to do so. So far ive got a foreach loop that loops through all of the events and echos them out as a list of event names wrapped in <a> tags, pointing to a page event.php?=id (id is determined via the events attribute called id).
to do this im using
<?php
$xml = simplexml_load_file("openbet_cdn.xml");
foreach($xml->response->williamhill->class->type->market as $market) {
$market_attributes = $market->attributes();
printf("%s\n",
$market_attributes->id,
$market_attributes->name);
}
?>
the feed I'm using is http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=5&marketSort=HH&filterBIR=N
What im having trouble with is on my page event.php i keep getting the first event in the xml feed displayed. To do this im using :
<?php
foreach ($xml->response->williamhill->class->type->market->participant as $participant) {
$participant_attributes = $participant->attributes();
echo "<tr>";
// EVENT NAME
echo "<td>";
echo "<a href=".$market_attributes['url'].">";
echo $participant_attributes['name'];//participants name
echo "</a>";
echo"</td>";
//ODDS
echo "<td>";
echo $participant_attributes['odds'];
echo "</td>";
echo "</tr>";
}
?>
I can see why it is because im not referencing the id which is in the URL of the event page. But I'm not quite sure of how to do this, any idea how I can tackle this ?
You just need to add an if within the loop so that you only target the event ID that matches the one in the querystring. A nested loop is also needed because you want to loop over each market to find the matching id, then loop over each of its participants.
foreach ($xml->response->williamhill->class->type->market as $market) {
if($market->attributes()->id == $_GET['id']) {
foreach($market->participant as $participant) {
$participant_attributes = $participant->attributes();
echo "<tr>";
// EVENT NAME
echo "<td>";
echo "<a href=".$market->attributes()->url.">";
echo $participant_attributes['name'];//participants name
echo "</a>";
echo"</td>";
//ODDS
echo "<td>";
echo $participant_attributes['odds'];
echo "</td>";
echo "</tr>";
}
break; // <-- we've found the target and echo'ed it so no need to keep looping
}
}
So... I have a mysql_fetch_array and I'm running into an issue when some of the mysql data contains single or double quotes. This is the dumbed down version of my code:
while($row=mysql_fetch_array($list)) {
echo "<tr>";
echo "<td onclick='edit_form(\"" . $row['item'] . "\");'>" . $row['item'];
echo "</td></tr>";
}
The edit_form() function is used to send the value of the current item back to the value of the input in the form so the user can then easily edit their entry and then sends an UPDATE command to mysql along with the proper primary key id (which I left out because of irrelevancy). The only issue I have is if a user puts single or double quotes into the form then it messes up the onclick attribute. Please help!! I am pretty new to php and can't figure this out. I've messed around with htmlentites() and html_entity_decode() but am still getting no where. Thank you so much!
Use htmlspecialchars on $row['item'] before inserting it in your document.
So your "dumbed-down" code should be:
while($row=mysql_fetch_array($list)) {
$item = htmlspecialchars($row['item']);
echo "<tr>";
echo "<td onclick='edit_form(\"" . $item . "\");'>" . $item;
echo "</td></tr>";
}
Try the below line instead of the original in your code and see if it works:
echo "<td onclick='edit_form(\"" . str_replace('"',""e;",$row['item']) . "\");\">" . $row['item'];
And where you would like to display the $item field, just replace it in reverse if you get "e; instead of ' " '. For example:
$qoute_free= str_replace('"e','"',$passed_value);
If you are using it in javascript, the function can be as below:
function edit_form(passed_value)
{
new_value=passed_value.replace(/"e;/g,'"');
}
I advise using json_encode. That way you don't have to worry about special cases htmlspecialchars might miss (such as newlines).
while($row=mysql_fetch_array($list)) {
echo "<tr>";
echo "<td onclick='edit_form(" . json_encode($row['item'], JSON_HEX_APOS) . ");'>" . $row['item'];
echo "</td></tr>";
}
I'm just putting the values back into my <form> (when they click on a <td>) and when they hit SUBMIT it directs the data to a different .php file that uses a MySQL UPDATE instead of an INSERT. I finally found some code that works!
$list is a mysql_query I ran at the top of the document
$item is the title of one of my columns in MySQL
while($row=mysql_fetch_array($list)){
$item = json_encode($row['item']);
$item = str_replace("'","'",$item);
echo "<td onclick='edit_form(" . $item . ");'>" . $row['item'] . "</td>";
}
Now it display's correctly in the <form> when <td> is clicked (by jQuery input.val($item)) and it shows up in the table correctly via $row['item']. I don't really understand exactly how it's working (how the encode is making it work) but I am glad it is. Crazyness!!!! Thanks for responding! and thanks for your effort!!