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

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.

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.

How can I turn this row into a link?

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
}

Changing the table row based on a condition

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.

Display link as hyperlink enabled output from mysql in php page but I am getting error [duplicate]

This question already has answers here:
Print string with a php variable in it
(4 answers)
Closed 11 months ago.
If I execute this I am getting output properly:
echo "'.$elink.'";
but when I want to display my output in a table column format I am not able to insert:
echo "<td width='200'>" '.$elink.' "</td>";
or
echo "<td width='200'>" "'.$elink.'" "</td>";
or
echo "<td width='200'>" ''.$elink.' "</td>";
Please correct the syntax errors.
echo '<td width="200">' . ''.$elink.'</td>';
Looks like you have mismatched quotes. But I would use sprintf.
echo sprintf("<td width='200'><a href='%1$s'>%1$s</a></td>", $elink);
First off you quotes are messed up. They should look like this:
echo "<td width='200'> <a href='".$elink."'>".$elink."</a></td>";
Correct your string format
echo '<td width="200">' . $elink . '</td>';
I suggest you to use the attribute style (style="width:200px;") instead the width attribute.
Remember to Url Encode the parameters contained in the href attribute.

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