tables and links messing the code up - php

I have a problem with PHP and links, I mean, I'm trying to use <a href to link the id of a ticket and then display it to user, but the problem is that the link I get is like
http://localhost/ucp/viewticket.php?ticketid=8%3Etest%3C/a%3E%3C/td%3E%3Ctd%3EUpute%20i%20informacije%3C/td%3E%3Ctd%3E2016-08-26%2000:06:59%3C/td%3E%3C/tr%3E%3Ctr%3E%3Ctd%3E9%3C/td%3E%3Ctd%3ESale_Lesa%3C/td%3E%3Ctd%3E%3Ca%20href=
and it should only be like
http://localhost/ucp/viewticket.php?ticketid=8
This is the code:
if ($result = $con->query($query))
{
echo '
<table id="tfhover" class="tftable" border="1">
<th>ID Tiketa</th>
<th>Autor</th>
<th>Naslov</th>
<th>Kategorija</th>
<th>Datum</th>
';
while ($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['Autor'] . "</td>";
echo "<td>". $row['Naslov'] ."</td>";
echo "<td>" . $row['Kategorija'] . "</td>";
echo "<td>" . $row['Datum'] . "</td>";
echo "</tr>";
}
$result->free();
}

You are missing a double quote at the end of the href link so it's including all the HTML that follows as the link too. I've fixed it below by add \" before the end of the "a" tag.
echo "<td>". $row['Naslov'] ."</td>";

When you change your Markup everything should be fine:
while ($row = $result->fetch_assoc())
{
echo '<tr>';
echo '<td>' . $row['ID'] . '</td>';
echo '<td>' . $row['Autor'] . '</td>';
echo '<td>'. $row['Naslov'] .'</td>';
echo '<td>' . $row['Kategorija'] . '</td>';
echo '<td>' . $row['Datum'] . '</td>';
echo '</tr>';
}
The Reason for this is, that there is a big difference between Single Quotes and Double Quotes.
Single Quotes are not parsed by PHP, so
$variable = 'hello';
echo '$variable'
will still return: $variable
While
echo "$variable"
will parse the string to: Hello
This way you would have seen, that you missed an escaped double quote.

Related

Background color <td> + INSERT Mysql

I am playing around with PHP, however I am stuck on this part. I created a table which inserts all the information from the MySQL database, this is working perfectly, however: I want if the 'type' in the MySQL database is 1 that the entire of the table has a red background and if it's something else, it should be a green background.
Could anybody give me advise, where to start?
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['date'] ."</td>";
echo "<td>" . $row['type'] ."</td>";
echo "<td>" . $row['amount'] . "</td>";
echo "<td>" . $row['transaction'] . "</td>";
echo "</td>";
}
echo "</table>";
?>
You need to mark the rows you want to effect somehow. You could do this with a data-type attribute, but if you are just starting and want to keep things simple, a class is the most straightforward.
while($row = mysqli_fetch_array($result))
{
echo "<tr class=\"type" . $row['type'] . "\">";
echo "<td>" . $row['date'] ."</td>";
echo "<td>" . $row['type'] ."</td>";
echo "<td>" . $row['amount'] . "</td>";
echo "<td>" . $row['transaction'] . "</td>";
echo "</td>";
}
echo "</table>";
?>
Then within a stylesheet or <style> tag you can add
.type1 {
background-color: red;
}
You can edit your code to place a condition in the 'type' 'td' :
echo "<td";
if($row['type'] === 1){
echo "class=\"redbg\";
else
echo "class=\"otherbg\";
echo >" . $row['type'] ."</td>";
Or, simply :
echo "<td";
if($row['type'] === 1){
echo "class=\"redbg\";
echo >" . $row['type'] ."</td>";
Then make a standard rule in your css for td:not(redbg).

How to echo text to cell, based on the value of another cell

I have a PHP code, showing me raw data from a MySQL database in a table.
I want to add some text to one of the existing cells, depending on a value in another column which is not displayed in the table.
My code looks like this:
while($row = mysqli_fetch_array($rs))
{
echo '<tr class="lokbes">';
echo "<td class='blaa'>" . $row['Navn'] . "</td>"; // I want the extra text here.
echo "<td>" . $row['Stilling'] . "</td>";
echo "<td>" . $row['Institution'] . "</td>";
echo "<td><a href='mailto:$row[Email]'>" . $row['Email'] . "</a></td>";
echo "<td>" . $row['Mobiltelefon'] . "</td>";
}
echo "</tr>";
echo "</table>";
This outputs a table consisting of Name, job, workplace etc.
In the cell displaying the name, I would like to add some text if a column in my MySQL DB has the value 1 in the row.
What to do? I've tried using if, as seen below - but that doesn't seem to work.
echo "<td class='blaa'>" . $row['Navn'] .
if ($row['Formand'] == 1) {
echo "(Formand)";
} "</td>";
You have to do multiple echos :
echo "<td class='blaa'>" . $row['Navn'];
if ($row['Formand'] == 1) {
echo "(Formand)";
}
echo "</td>";
Or, with ternary operator :
echo "<td class='blaa'>" . $row['Navn'] . ($row['Formand'] == 1 ? "(Formand)" : "") . "</td>";
update like this.
echo "<td class='blaa'>" . $row['Navn'];
if ($row['Formand'] == 1) {
echo " (Formand)";
}
echo "</td>";
or you can use short PHP tag in HTML code.
?>
<td class="blaa">
<?php echo $row['Navn']?><?php echo ($row['Formand'] == 1)?' (Formand)':'';?>
</td>
<?php
Try below code.
while($row = mysqli_fetch_array($rs))
{
echo '<tr class="lokbes">';
echo "<td class='blaa'>" . $row['Navn']." ".($row['Formand'] == 1 ? "(Formand)" : ""). "</td>"; // I want the extra text here.
echo "<td>" . $row['Stilling'] . "</td>";
echo "<td>" . $row['Institution'] . "</td>";
echo "<td><a href='mailto:$row[Email]'>" . $row['Email'] . "</a></td>";
echo "<td>" . $row['Mobiltelefon'] . "</td>";
}
echo "</tr>";
echo "</table>";

how to use 2 fetch_assoc in one while loop?

i need list something like this.
First table is display By using the this code.
<?php
if (isset($_REQUEST['asign'])) {
include 'includes/connection.php';
$sql12= $cid->query("SELECT name FROM user WHERE designation='Technical' AND status='1'");
$query = "SELECT * FROM allinone WHERE flag=3 ORDER BY id ASC";
$rs = $cid -> query($query);
$n = $rs -> num_rows;
echo "<br /><span style='color:red;'><center>$n records found</center></span>";
echo "<div id='record'>";
echo "<table border='1' width='80%' align='center' cellpadding='0' cellspacing='0' id='unsolTable'>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>Ticket Number</th>";
echo "<th>KOID</th>";
echo "<th>PROBLEM</th>";
echo "<th>COMMENT</th>";
echo "<th>DATE</th>";
echo "<th>TIME</th>";
echo "<th>STATUS</th>";
echo "<th>SOLVED BY</th>";
echo "<th>Assign</th>";
echo "<th>Assigned to</th>";
echo "</tr>";
while ($row = $rs -> fetch_assoc() ) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['token'] . "</td>";
echo "<td>" . $row['koid'] . "</td>";
echo "<td>" . $row['problem'] . "</td>";
echo "<td>" . $row['comment'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['time'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "<td>" . $row['solvedBy'] . "</td>";
echo "<td><a href='assign.php?qType=technical&name=anshul&id=" . $row['id'] . "'>anshul</a>
<br />
<a href='assign.php?qType=technical&name=kiran&id=" . $row['id'] . "'>kiran</a>
<br />
<a href='assign.php?qType=technical&name=akhilesh&id=" . $row['id'] . "'>akhilesh</a></td>";
echo "<td>" . $row['assTo'] . "-" . $row['assTime'] . "-" . $row['assDate'] . "</td>";
echo "</tr>";
}
echo"</table>";
echo "</div>";
}
?>
On the another side is list on the assign i don't want to write it manually just get from the database how can i do this??
$sql= $cid->query("SELECT name FROM user WHERE designation='Technical' AND status='1'");
while ($row=$sql->fetch_assoc()) {
$name= $row['name'];
}
this is the another code i want to add in first code.
Nothing prevents you from executing another SQL query inside you while loop; just replace the echo...anshul...echo...kiran... part with another while loop containing the code you already provided.
You just have to make sure that you use different variables, otherwise your $row variable is overwritten by the second query. Hence, you can do something like this:
// ...
echo "<td>" . $row['solvedBy'] . "</td>";
$resultSetAssignedUsers = $cid->query("SELECT name FROM user WHERE designation = 'Technical' AND status = '1'");
while ($rowAssignedUsers = $resultSetAssignedUsers->fetch_assoc()) {
echo "<td><a href='assign.php?qType=technical&name=" . $rowAssignedUsers['name'] . "&id=" . $row['id'] . "'>" . $rowAssignedUsers['name'] . "</a>";
}
echo "<td>" . $row['assTo'] . "-" . $row['assTime'] . "-" . $row['assDate'] . "</td>";
// ...

Putting a link in a php/html table which uses mysql

can anyone help?
Apologies if I'm being stupid but I'm very new to this and I don't really know what I'm doing...
I have generated a table which uses php to get data from a mysql database, but I want to put an extra column at the end (of every row) which contains a link. The link will be to further details about that entry.
The code I have which displays the table is as follows:
$result = mysql_query("SELECT * FROM orders");
echo "<table border='5'>
<tr>
<th>order_no</th>
<th>ord_date</th>
<th>est_completion_date</th>
<th>status</th>
<th>invoice_date</th>
<th>inv_amount</th>
<th>name</th>
<th>fName</th>
<th>lName</th>
</tr>";
// -- Use 'while' to check each row in $result in turn:
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['order_no'] . "</td>";
echo "<td>" . $row['ord_date'] . "</td>";
echo "<td>" . $row['est_completion_date'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "<td>" . $row['invoice_date'] . "</td>";
echo "<td>" . $row['inv_amount'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['fName'] . "</td>";
echo "<td>" . $row['lName'] . "</td>";
echo "</tr>";
}
echo "</table>";
Like I say, I'm a beginner. Basically I am reasonably happy (using the code above) in making html tables which display the results of a mysql query. However I need the users to be able to click on rows/cells in order link to other tables.
Any help much appreciated...
echo "<table border='5'>
<tr>
<th>order_no</th>
<th>ord_date</th>
<th>est_completion_date</th>
<th>status</th>
<th>invoice_date</th>
<th>inv_amount</th>
<th>name</th>
<th>fName</th>
<th>lName</th>
<!-- extra column here -->
<th> </th>
</tr>";
while($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['order_no'] . "</td>";
echo "<td>" . $row['ord_date'] . "</td>";
echo "<td>" . $row['est_completion_date'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "<td>" . $row['invoice_date'] . "</td>";
echo "<td>" . $row['inv_amount'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['fName'] . "</td>";
echo "<td>" . $row['lName'] . "</td>";
// add link here
echo "<td><a href=''>link</a></td>";
echo "</tr>";
}
Please note: You should stop using mysql_* functions. They're being deprecated. Instead use PDO (supported as of PHP 5.1) or mysqli (supported as of PHP 4.1). If you're not sure which one to use, read this article.
try this. add a column. It will link to another page "view_more_details.php". To identify rows uniquely, pass a unique id with this link. Here I passed order_no. In the
"view_more_details.php" page, you can select this value by using $_GET['key']
$result = mysql_query("SELECT * FROM orders");
echo "<table border='5'>
<tr>
<th>order_no</th>
<th>ord_date</th>
<th>est_completion_date</th>
<th>status</th>
<th>invoice_date</th>
<th>inv_amount</th>
<th>name</th>
<th>fName</th>
<th>lName</th>
<th> </th>
</tr>";
// -- Use 'while' to check each row in $result in turn:
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['order_no'] . "</td>";
echo "<td>" . $row['ord_date'] . "</td>";
echo "<td>" . $row['est_completion_date'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "<td>" . $row['invoice_date'] . "</td>";
echo "<td>" . $row['inv_amount'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['fName'] . "</td>";
echo "<td>" . $row['lName'] . "</td>";
echo "<td>" . "View More" . "</td>";
echo "</tr>";
}
echo "</table>";

How to hyperlink a table data in a cell in php

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" '. $row['id'] .'</td>";
echo "<td>" . $row['LayarType'] . "</td>";
echo "<td>" . $row['Attribution'] . "</td>";
echo "</tr>";
}
it gives an error...
how do i give a link suggested here in bold part..
You were not concatenating the different strings with the . operator.
Either of the following will work:
echo "<td>" . ''. $row['id'] .'' . "</td>";
// or
echo '<td>'. $row['id'] .'</td>';
Your quotes are messed up in the second echo.
Also, for strings with no variables inside them, you should use single quotes instead of double quotes so that PHP doesn't have to process the contents.
while($row = mysql_fetch_array($result))
{
echo '<tr>';
echo '<td>'. $row['id'] .'</td>'; echo '<td>' . $row['LayarType'] . '</td>';
echo '<td>' . $row['Attribution'] . '</td>';
echo '</tr>';
}

Categories