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>';
}
Related
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>";
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.
So I have a dynamic PHP table where I am trying echo one of the table columns as a dynamically generated link. Presently, the $url variable i have declared is returning null. $row['username'] is returning what it should, so I suspect I have the syntax off for that. I also am having trouble with the syntax on the following line. How do I insert the $row['username'] as the link text?
echo "<td>" . "<a href='".$url."'> . $row['username'] . </a>" . "</td>";
this present example returns an error, but when I replace it with:
echo "<td>" . "<a href='".$url."'> blahblahfiller </a>" . "</td>";
the page runs, so I know it's my syntax there.
Here is my full code for the table:
<?php
while ($row = mysql_fetch_array($query)) {
$url = "profilepage.php?name=" + $row['username'];
echo "<tr>";
echo "<td>" . '<input type="checkbox" name="checkbox"/>' . "</td>";
echo "<td>" . "<a href='".$url."'> . $row['username'] . </a>" . "</td>";
echo "<td>" . $row['gender'] . "</td>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $url . "</td>";
echo "</tr>";
}
?>
Sincere thanks for any and all help! It means a lot.
You are using + instead of . for to concatenate. It should be -
$url = "profilepage.php?name=". $row['username'];
How can I assign a class id that is unique for all the <tr>
The id's need to stay stay even when I refresh as I will be using them to style the table.
Here is the code I am using:
while($row = mysql_fetch_array($result2))
{
echo "<tr id='centered' class='"; echo "'";
echo "<td>" . $row['Year_9'] . "</td>";
echo "<td>" . $row['Year_8'] . "</td>";
echo "<td>" . $row['Year_7'] . "</td>";
echo "<td>" . $row['Year_6'] . "</td>";
echo "<td>" . $row['Year_5'] . "</td>";
echo "<td>" . $row['Year_4'] . "</td>";
echo "<td>" . $row['Year_3'] . "</td>";
echo "<td>" . $row['Year_2'] . "</td>";
echo "<td>" . $row['Year_1'] . "</td>";
echo "<td>" . $row['Year_0'] . "</td>";
Perhaps use a counter with a multi-valued class?
$i = 1;
while($row = mysql_fetch_array($result2))
{
$secondClass = 'abc' + $i;
echo "<tr id='centered' class='firstClass $secondClass'>";
...
$i++;
}
I have one while loop which displays data from the database. Now I want multiply two values in one row and display the result in the same row, the same way multiply the values and display the result in every row. I am not getting the result. Can anyone help me? I am new to PHP.
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td class='alt'>" . $row['id'] . "</td>";
echo "<td>" . $row['item'] . "</td>";
echo "<td>" . $row['amount'] . "</td>";
$ss=$row['amount'];
echo '<td >'.'<input type="checkbox" name="status" value="" >'.'</td>';
echo '<td >'.'<input type="text" name="qty">'.'</td>';
echo "<td>" . $rr1 . "</td>";
echo "</tr>";
}
From Where you are getting $rr1??
You can have
echo "<td>" . $row['item'] * $row['amount'] . "</td>";
Hope this is what you want...
you can try
printf('amount is %d',$row['item'] * $row['amount']);
reference
Try below :
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td class='alt'>" . $row['id'] . "</td>";
echo "<td>" . $row['item'] . "</td>";
echo "<td>" . $row['amount'] . "</td>";
echo "<td>" .($row['item'] * $row['amount']). "</td>";
echo "</tr>";
}
Change the query as
$result= mysql_query("Select id, item, amount, (item * amount) total
from table_name");
then in while loop
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td >" . $row['id'] . "</td>";
echo "<td>" . $row['item'] . "</td>";
echo "<td>" . $row['amount'] . "</td>";
echo "<td>" .($row['total'] "</td>";
echo "</tr>";
}