How can I output my URL row as a hyperlink (PHP)? - php

I have been trying to use various combinations of anchor and html tags to code my third row as an actual URL (each time) instead of plain text, and it's not working. To be clear, I am not trying to hardcode a single URL and output as a hyperlink but rather attempting to code in such a way that this third row is consistently outputted as a clickable hyperlink. This is a .php page, and the links are .pdf URLs. I'm also finding this a little more difficult to do because it's a table (so, extra formatting there).
What I'm doing is retrieving many articles from my database, each with the third row as a URL, and then using the below code to output it in my webserver, using tags, with the php . as a separator, etc. However, I'm not able to output the row, which currently displays as plain text, as a set of clickable hyperlinks.
I'm not looking for how to HTML link or output a single link as a hardcoded hyperlink. That's not what I'm asking. I'd also like someone to take the third line below and provide a demo.
<table class="table" width="920" style="border:thin" cellpadding="10" border="1">
<tr>
<th>Topic</th>
<th>Category</th>
<th>URL</th>
</tr>
<?php
while($row = $result->fetch_assoc()){
?>
<tr>
<td><?php echo $row['Topic']; ?></td>
<td><?php echo $row['Category']; ?></td>
<td><?php echo $row['URL']; ?></td>
</tr>
<?php
}
?>
</table>

Your third line should be like this
<td> the link text </td>
if you need the link and text to be same the code will look like this
<td><?php echo $row['URL']; ?></td>
Basically, its like this
<td> ECHO THE URL TEXT HERE </td>

In HTML you can create a link with an anchor tag <a>.
Please try this:
<td>
<a target="_blank" href="<?php echo $row['URL']; ?>"> <?php echo $row['URL']; ?></a>
</td>

Related

How to hide table rows that fetch the info from a database based on string given and when a button is pressed

An HTML table is given in which there are people (agents in this case) who have the following information displayed Id, Name, Rank, Location, and Status. Status can have 3 values. Online, Offline, Disconnected. Based on the status that one of the agents has I want to put 3 buttons as such Online, Offline, and Disconnected, and when a button is pressed to hide the other rows with different values. For example, if I press Online, table rows on the HTML side that contain the status Offline and Disconnected disappear, this goes for the others too the other way around. I have no idea to achieve what I said earlier and I am open to any solution that is deemed to resolve my problem.
<table id="main_table">
<tr>
<td>Id</td>
<td>Agent Name</td>
<td>Agent Rank</td>
<td>Agent Location</td>
<td>Status</td>
</tr>
<?php
require 'CMS/processing/conn.php';
$result = mysqli_query($con,"SELECT * FROM agents");
while($info = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $info['id']; ?></td>
<td><?php echo $info['agentName']; ?></td>
<td><?php echo $info['agentRank']; ?></td>
<td><?php echo $info['locationNames']; ?></td>
<td><?php echo $info['agentStatus']; ?></td>
</tr>
<?php
}
?>
</table>
Try using the below logic : Create 3 buttons with <a> and pass status value as GET to the same page then while fetching check whether it has GET or not
Note:This is not a answer :passing direct GET value to sql query may cause some security issues
Online//put the status value based on your status
Offline//put the status value based on your status
Disconnected//put the status value based on your status
<table id="main_table">
<tr>
<td>Id</td>
<td>Agent Name</td>
<td>Agent Rank</td>
<td>Agent Location</td>
<td>Status</td>
</tr>
<?php
require 'CMS/processing/conn.php';
if(isset($_GET['status']))
{
$result = mysqli_query($con,"SELECT * FROM agents where agentStatus='".$_GET['status']."'");
}
else
{
$result = mysqli_query($con,"SELECT * FROM agents");
}
while($info = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $info['id']; ?></td>
<td><?php echo $info['agentName']; ?></td>
<td><?php echo $info['agentRank']; ?></td>
<td><?php echo $info['locationNames']; ?></td>
<td><?php echo $info['agentStatus']; ?></td>
</tr>
<?php
}
?>
</table>
I'd use javascript within a tag or jQuery since this sounds like you need dynamic functionality on a static webpage.
For your button pass in the name of the function you're creating inside your tag
<button onclick="yourFunctionName(yourString)"></button>
Inside your tag have the function accept the yourString paramater
function yourFunctionName(yourString){
if(yourString === "whatever"){
$('tbody tr').hide()
$('tbody tr').show()
}
etc! Let me know if you need any more clarification on anything
Output echo an attribute on each row <tr data-status="online"> etc.
Then in script use document.querySelectorAll("tr[data-status='online'] to find all rows with that status, loop thru the array and toggle each row style.display to 'table-row' or 'none' as required.
You could also do the same using CSS by assigning a class <tr class="online"> etc. Then in script dynamically altering the CSS definition of that class, changing the display to 'table-row' or 'none' as required.

`iframe` inside an HTML table

I am displaying some data from a database into my cms, using tables (tr, td).
<tr align="center">
<td><?php echo $i++; ?></td>
<td><?php echo $post_image; ?></td>
<td><?php echo $post_title; ?></td>
<td>
<iframe width="100px" frameborder="0" id='inneriframe' scrolling=yes >
<?php echo $post_description; ?>
</iframe>
</td>
<td>Edit</td>
<td>Delete</td>
</tr>
Unfortunately the description php variable is not displayed at all inside my iframe. However, if I take out the iframe tag, everything works fine, and I can see all the stored values from the database.
<tr align="center">
<td><?php echo $i++; ?></td>
<td><?php echo $post_image; ?></td>
<td><?php echo $post_title; ?></td>
<td><?php echo $post_description; ?></td>
<td>Edit</td>
<td>Delete</td>
</tr>
So my main question is how to use iframes inside tds?
As I mentioned in comments (but gotten no response from).
iframe requires a source src="file.xxx".
https://developer.mozilla.org/en/docs/Web/HTML/Element/iframe
Taken from example #1:
<iframe src="page.html" width="400" height="300">
<p>Your browser does not support iframes.</p>
</iframe>
What you need to do is to remove the <?php echo $post_description; ?> from the iframe and just do:
<iframe src="file_for_iframe.php" width="100px" frameborder="0" id='inneriframe' scrolling=yes >
</iframe>
and have the variables inside file_for_iframe.php to be echo'd in there.
That's how you'll get your iframe to show the contents "on screen" rather than in HTML source.
If you had a look at your HTML source as I also mentioned in comments, you would indeed have seen the contents.
They're in there alright, but not being echo'd properly (on screen).
The (and as an example) file_for_iframe.php file that I have stated above, will contain whatever content you wish to display.
For example:
<?php
echo $post_description = "Whatever is presently assigned to this...";
?>
Important note:
echo $post_description = "x"; is valid syntax and you need to keep the echo for it. Otherwise, it won't work.
iframe elements load external documents; the HTML that goes between their start and end tags is alternative content for browsers which do not support iframes.
If you want a scrolling area on a page, apply the CSS overflow property to an appropriate element (probably a div in this case).

result with code is fine but last column 'update' show not active with href

result with code is fine but last column 'update' show not active with
href
<?php
while($row=mysql_fetch_array($res))
{
?>
<tr><th colspan="4">Image</th><th>Title</th><th>Details</th>
<th>Country</th><th>City</th></tr>
<tr><td colspan="4"><img src="<?php echo $row['image']?>"width="50px"
height="50px" > </td>
<td><?php echo $row['title']?></td>
<td><?php echo $row['details']?></td>
<td><?php echo $row['country']?></td>
<td><?php echo $row['city']?></td>
<td>update</td>
</tr>
<?php }?>
The issue is, you are using the href value as # for your update link! Instead, you should use a URL there. Because when you click on the link, the browser takes you to the page that you had specified in that href attribute!
So in short, replace this:
<td>update</td>
with something like this:
<td>update</td>
Hope it helps.

How to echo a variable into a div in php?

Im trying to echo a variable into an already existing DIV within a table.
EG:
<table>
<tr>
<td class="error"> </td>
</tr>
</table>
<?php
echo "<td class='error'>".$variable."</td>";
?>
Note: the error td tag is already style and positioned within a table.
The method above causes a lone td tag to be created and added to the top of
the page.
I realise it's probably best to echo php in HTML rather than vice versa but
I need echo HTML in this instance.
Is this possible ?
The simplest solution, if I am understanding correctly:
<table>
<tr>
<td class="error"><?php echo $variable; ?></td>
</tr>
</table>
You have to echo in-place.

How could I determine the data from clicking a link?

I am a newbie in php and working on something something and I can't figure out how will it work. I have a query like this which will display specific rows from the database:
<?php
include ('test/tru/conek.php');
$result=mysql_query("SELECT * FROM post_jobs WHERE job_category='Administration'");
while($row=mysql_fetch_array($result)){
?>
<tr >
<th style="font-weight:normal"><input type="text" name="name" value="<?php $name=$row['company_name']; echo $name ; ?>" readonly="readonly" /></th>
<th style="font-weight:normal"><?php echo $row['job_responsibilities']; ?></th>
<th style="font-weight:normal" id="industry"><?php echo $row['industry']; ?></th>
<th style="font-weight:normal" id="date"><?php echo $row['date']; ?></th>
<th style="font-weight:normal" id="loc"><?php echo $row['location']; ?></th>
</tr>
</table>
<?php } ?>
My question is, how could I display or determine the information on a specific row from clicking the link and then displaying it?
It seems like you want differentiate between the rows after printing them onto screen. The most common way of doing this is by using an id number for each row. You add an 'id' row which is auto incrementing and primary into your mysql table. You would then add it to the link in your html:
<th style="font-weight:normal">
<a href="#" onclick="ss(<?php echo htmlspecialchars($row['id'], ENT_QUOTES, 'UTF-8'); ?>)" name="res">
<?php echo htmlspecialchars($row['job_responsibilities'], ENT_QUOTES, 'UTF-8'); ?>
</a>
</th>
You would then do something with that id number in Javascript in your ss() function. If you infact don't want to use Javascript at all, then you should write your link tag as:
<th style="font-weight:normal">
<a href="somewhere.php?id=<?php echo htmlspecialchars($row['id'], ENT_QUOTES, 'UTF-8'); ?>" name="res">
<?php echo htmlspecialchars($row['job_responsibilities'], ENT_QUOTES, 'UTF-8'); ?>
</a>
</th>
I think that's what you're looking for.
Edit: How silly of me, I also forgot to mention that you should never print user-supplied data in your html without first escaping it. This is to prevent Cross Site Scripting (XSS) which is a common yet very serious vulnerability in web pages. I've edited my code accordingly. You should replace the 'UTF-8' with the correct encoding of your data. This is the reason you should also explicitly state the encoding: http://shiflett.org/blog/2007/may/character-encoding-and-xss

Categories