Changing the table row based on a condition - php

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.

Related

Mailto: inside php and html table

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.

Not able to get value at td class using jquery

I am new to web development and one thing that is driving my head mad is that I am unable to get the value for one of the values in my table.
At present this is my code:
<?php
...
if(($result->num_rows)>0){
while($row = $result->fetch_assoc()){
print
"<tr>".
"<td id='numbers' class='col-md-4 text-center'>{$row["number"]}</td>".
"<td class='col-md-1 text-center'>$row["names"]</td>".
"</tr>";
}
?>
<script type="text/javascript">
var jz = $('#numbers').val();
alert(jz);
</script>
I am using bootstrap so I am probably not setting the ID properly for the td tag. Nothing is being returned for the #numbers. I don't get why
Would mean a lot if someone could help me out
Thanks
It's not .val(), use .text() or .html() instead. .val() is used on input fields like textarea.
http://api.jquery.com/val/
http://api.jquery.com/text/
Code for the question below:
$index = 1;
while($row = $result->fetch_assoc()){
print
"<tr>".
"<td id='numbers".$index."' class='col-md-4 text-center'>{$row["number"]}</td>".
"<td class='col-md-1 text-center'>$row["names"]</td>".
"</tr>";
$index += 1;
}

how to make pictures stored from db, as different links?

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.

Using xml attribute id in page url and on page

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
}
}

Hyperlink across table cells?

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>";
}

Categories