Adding HTML codes inside PHP - php

I'm trying to add a view button to my table but I'm getting a syntax error, unexpected href. Seems like I'm wrong with the formatting. Still trying to learn PHP but is it possible to add href to the table?
Here's my code:
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td>'.$row["name"].'</td>
<td>'.$row["temperature"].'</td>
<td>'.$row["phoneno"].'</td>
<td> '<a href='read.php?id='. $row['id'] .'' title='View Record' data-toggle='tooltip'><i class='fa fa-eye' style='font-size:30px; color: black;''></i></a>';' </td>
</tr>
';
}
echo $output;
And here's the image for the color coding that seems wrong.

It is because you're using single quotes to delimit your strings while using single quotes in the strings to denote values.
This -
<td> '<a href='read.php?id='. $row['id'] .'' title='View Record' data-toggle='tooltip'><i class='fa fa-eye' style='font-size:30px; color: black;''></i></a>';' </td>
Should be this -
<td><i class="fa fa-eye" style="font-size:30px; color: black;"></i></td>';

Just mismatched quotes.
There're many ways to mix PHP and HTML and you've chosen a hard syntax. Please compare with e.g.:
<?php foreach($foo as $row) { ?>
<tr>
<td><?= $row["name"] ?></td>
<td><?= $row["temperature"] ?></td>
<td><?= $row["phoneno"] ?></td>
<td>
<a href='read.php?id=<?= $row['id'] ?>' title='View Record' data-toggle='tooltip'><i class='fa fa-eye' style='font-size:30px; color: black;'></i></a>
</td>
</tr>
<?php }
BTW, your are injecting raw text into HTML, beware that it can break your markup any time.

Your quotes are all over the place. You open a string variable with single quotes so every time you use single quotes unescaped in the string you just created will be interrupted.
It should be like this: <a href="read.php?id='.$row['id'].'" ... >

Related

Content Editable Field not working in PHP but works in HTML

OK I know the code for editable field working in HTML, inline edit. But I am unable to use the same i PHP code
I am attaching below HTML working one and PHP in echo where I am lacking
Please Guide
Thanks
HTML (WORKING)
<td style="text-align:center;" contenteditable="true" data-old_value="<?php echo $row1["view"];?>" onBlur="saveInlineEdit(this,'view','<?php echo $row1["id"]; ?>')" onClick="highlightEdit(this);"><?php echo $row1["view"]; ?></td>
PHP (Surely there is a mistake by me - Not Working)
echo "<td style='text-align:center;' contenteditable='true' data-old_value='<?php echo $row1["view"];?>' onBlur='saveInlineEdit(this,"view","<?php echo $row1["id"]; ?>")' onClick='highlightEdit(this);'><?php echo $row1["view"]; ?></td>
<?php echo is only used when you're in HTML output mode and need to get back into PHP execution. But you're already executing PHP. It doesn't do anything special inside a string, it's just echoed literally.
Just use normal variable substitution or concatenation.
echo "<td style='text-align:center;' contenteditable='true' data-old_value='{$row1["view"]}' onBlur='saveInlineEdit(this,\"view\",\"{$row1["id"];\")' onClick='highlightEdit(this);'>{$row1["view"]}</td>
Also, you need to escape the literal " inside the string.
I doubt that the excepted answer will work, atleast one " is missing. I also prefer concatenation, since it is better readable.
Concatenation:
echo '<td style="text-align:center;" contenteditable="true" data-old_value="' . $row["notess"] . '" onBlur="saveInlineEdit(this,\"notess\",' . $row["id"] . ')" onClick="highlightEdit(this);">' . $row["notess"] . '</td>';
you could also go the dirty way with output buffering:
<?php ob_start(); ?>
<td style="text-align:center;" contenteditable="true" data-old_value="<?php echo $row1["view"];?>" onBlur="saveInlineEdit(this,'view','<?php echo $row1["id"]; ?>')" onClick="highlightEdit(this);"><?php echo $row1["view"]; ?></td>
<?php ob_end_clean(); ?>

how to correctly write HTML markup inside PHP echo outputs

I am not able to write the HTML inside the PHP properly. The main problem is that I am not able to render the <td> with class="btn".
<tbody class="text-center">
<?php
foreach($result as $row)
{
$name=$row['name'];
$email=$row['email'];
$password=$row['password'];
echo "<tr>";
echo "<td>$name</td>";
echo "<td>$email</td>";
echo "<td>$password</td>";
echo (<td><span class="btn btn-outline-primary">Edit</span></td>)";
echo `<td>
<span class="btn btn-outline-danger">Delete</span>
</td>
echo "</tr>`;
}
There are three ways of doing this:
1) Escape
Use backslashes to escape the quotes within the PHP string, this is only needed when the quotes within are the same as the quotes encapsulating the PHP string:
echo "<td><span class=\"btn btn-outline-primary\">Edit</span></td>";
2) Alternative quotes
Use the "other" quote style within the HTML; such as if you use double-quotes " to encapsulate the PHP string, then use single quotes ' within the HTML markup.
echo "<td><span class='btn btn-outline-primary'>Edit</span></td>";
3) "here document" syntax
PHP Print function also uses the "here document" syntax to output
multiple lines with $variable interpolation.
Note: that the here document terminator must appear on a
line with just a semicolon no extra whitespace!
print <<<END
<td><span class="btn btn-outline-primary">Edit</span></td>
END;
My preferred/recommended option is to use method 2.
Please refer to the PHP manual for further information.
Just write the code right way or follow the standard.
You need backslash before quote sign inside echo
echo "<tr>";
echo "<td>$name</td>";
echo "<td>$email</td>";
echo "<td>$password</td>";
echo "<td><span class=\"btn btn-outline-primary\">Edit</span></td>";
echo "<td><span class=\"btn btn-outline-danger\">Delete</span></td>";
echo "</tr>";
if You will use this implementation everything will be fine :)
foreach loop, echo usage and alternative syntax for control structures
<tbody class="text-center">
<?php foreach($result as $row) : ?>
<tr>
<td><?= $row['name']; ?></td>
<td><?= $row['email']; ?></td>
<td><?= $row['password']; ?></td>
<td>
<span class="btn btn-outline-primary">Edit</span>
</td>
<td>
<span class="btn btn-outline-danger">Delete</span>
</td>
</tr>
<?php endforeach; ?>
The following way also working for your code
When we use Quotes " inside a Quote we need to use the other Quote '
<?php
foreach($result as $row)
{
echo "<tr>
<td>$row['name']</td>
<td>$row['email']</td>
<td>$row['password']</td>
<td>
<span class='btn btn-outline-primary'>Edit</span>
</td>
<td>
<span class='btn btn-outline-danger'>Delete</span>
</td>
</tr>";
}
?>

Echo a string literal from PHP that includes both quotes and apostrophes

I have a little bit more complicated row than I can handle.
I am echoing and I got lost in the " ', could someone help me out and tell me how to have this line correctly?
echo '<td style="text-align: center"><a onclick=" window.open('/edit.php?id=' . $row['id'] . ','_self')"><img height="30" width="30" src="/wp-content/themes/sparkling/edit.png"/></a></td>';
Escape single quotation?
echo '...<a onclick=" window.open(\'/edit.php?id=...';
Edit
To show single quotation in string wrapped with single quotation, you need to escape it, like this
echo 'Hello \' world';
So your code should be
echo '<td style="text-align: center"><a onclick=" window.open(\'/edit.php?id=' . $row['id'] . '\',\'_self\')"><img height="30" width="30" src="/wp-content/themes/sparkling/edit.png"/></a></td>';
Use the heredoc syntax to create the string and your " and ' can be used freely inside the string.
See manual about heredoc http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
Example of heredoc:
https://3v4l.org/SJTBO
I don't know about a third quotation mark in code.
But, using "heredoc" syntax is really usefull when you want/need to keep both the single and double quotes unescaped.
Using it, your code should look like this:
echo <<<EOD
<td style="text-align: center">
<a onclick="window.open('/edit.php?id='{$row['id']}', '_self')">
<img height="30" width="30" src="/wp-content/themes/sparkling/edit.png"/>
</a>
</td>
EOD;
Think about using { } around your variables to make them more visible.
See documentation about it here: http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

PHP HTML a-element URL

I want to give some values with GET to another site. The value is a ID which is from a database.
The values are stored in $row.
<table>
<?php
foreach ($pdo->query($sql) as $row) {
?>
<tr>
<td><?php echo $row['ID'];?></td>
<td><?php echo $row['Titel'];?></td>
<td><a href='./edit.php?id=$row['ID']>Edit</a></td>
</tr>
<?php
}
?>
How do i have to concenate the string in the third td tag? I want to give the value of the ID to the next page (so the value of the first column of the matching row). I know now it is wrong concenated.
Thanks!
You have not put your php code inside <?php ?> and a closing quote for the href property value is also missing. Compare the corrected code below.
<td>
<a href='./edit.php?id=<?php echo $row['ID']; ?>'>Edit</a>
</td>
<td><a href='./edit.php?id=<?php echo $row['ID'] ?>'>Edit</a></td>

How to do href in php

At the beginning I would like to say that this is my first post and I apologize in advance for any possible mistakes. And these are my beginnings in programming.
I have a problem with appropriate action download.php in the following code snippet:
<?php
$cv= $row[25];
$output .= '
<tr>
<td> <a href='download.php?dow=$cv'>Download</a> </td>
</tr>
';
?>
I wish it worked like the following code (in which it operates correctly):
<tr>
<td><?php echo "<a href='download.php?dow=$cv'>Download</a><br>"; ?></td>
</tr>
You need to concatenate the string. To concatenate you use a dot . This will put the variable value inside your string.
<?php
$output .= "
<tr>
<td><a href='download.php?dow=".$cv."'>Download</a> </td>
</tr>
";
you closed string before it's even finished!
the $output is a variable and you tried to store the HTML as an string inside it, but you've closed the ' sign before the download.php?dow=$cv and then opened it again. the correct way to write it is:
<?php
$cv= $row[25];
$output .= "
<tr>
<td> Download </td>
</tr>
";
?>
the \ before " called escaping and is needed because we dont want to end our string! just want to add that quote sign as a character like the others.

Categories