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.
Related
I want to make that row a link, but I dont know how to add an a href attribute... That row shows information from a DB and I just want to make ir clickeable and not only as plain text. Thank you
This is the row I wanna turn into a link...
echo '<td>' . $row['nombre'] . '</td>';
Wrap the text in an anchor (a) tag.
echo '<td>' . $row['nombre'] . '</td>';
You should also probably take a look at the W3schools HTML5 tutorial.
Just add the <a> to your HTML, and include the URL in it.
echo "<td><a href='script.php?id={$row['nombre']}'>{$row['nombre']}</a></td>";
echo "<td>
<a href='yourfile.php?subDom=$row['nombre']'</a>
</td>";
Then you can check for if link is clicked, you can trigger action for click. Like below:
if($_GET['whateverComingFromDB'] == "whatDoYouWantToCompare"){
//...Anything you want to happen after click the link
}
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 have a php file that shows some pictures from a db..i want to make each picture as a link to a different page like: pic 1-link 1, pic 2-link 2 etc
How i do that? Do i need to change something in this code maybe?
echo "<td width=100>";
echo "<div class='pulse'><img src='pictures/".$row['picture']."'></div>";
echo "</td>";
This will work for opening the image in a new browser window
echo "<td width=100>";
echo "<div class='pulse'><a target='_blank' href='pictures/".$row['picture']."'>".$row['picture']."</a></div>";
echo "</td>";
It sounds like maybe you need this:
echo '<td width=100>'
echo '<div class="pulse"><img src="pictures/' . $row['picture'] . '"></div>';
echo '</td>';
Notice that I changed your double quotes to singles. That was not entirely necessary, but it makes the concatenation easier for me to follow.
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
}
}
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>";
}