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
Related
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'].'" ... >
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.
i have a big table where should print 5 php variables.
I am printing this way
echo '
<td valign="center" align="center" colspan="2">Number : <br/>$num</td>
';
but it prints $num not the value of $num
EDIT :
I realized that.
echo '
' <td valign="center" align="center" colspan="2">Number : <br/>'.$num.'</td> ';
Thank you for help :)
Single quoted PHP strings do not interpolate variables. Double quoted strings do. Use a double quoted string.
Better yet, don't build up HTML in strings in the first place. Drop into PHP mode only when you need access to PHP.
<td valign="center" align="center" colspan="2">Number : <br/><?php echo $num;?></td>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i have following code but unable to get full variable values on return.
<?php
$no7="Girls pink";
$no8="All Categories";
$aw="2";
echo'<table width="auto" border="0">
<tr>
<td width="auto" class="s">'; echo "".$aw.""; echo'</td>
</tr>
</table>';
?>
Thanks for help Regards,
Add quotes on href value:
echo "<a href='search.php?search_name=".$no7."&mydropdown=".$no8."&pno=".$aw."'>".$aw."</a>";
// ^--------------------------- quotes ----------------------------^
Use urlencode and htmlspecialchars:
echo "<a href='search.php?search_name=" . urlencode($no7) . "&mydropdown=" . urlencode($no8) . "&pno=" . urlencode($aw) . "'>" . htmlspecialchars($aw) . "</a>";
Also: add quotes around the URL, and replace & with & for better HTML syntax.
Clean up the horrible formatting/layout and use a HEREDOC:
echo <<<EOL
<table width="auto" border="0">
<tr>
<td width="auto" class="s">{$aw}</td>
</tr>
</table>
EOL;
Note how the HEREDOC removes the need for all those individual echoes, allows for variable interpolation into the string (no more 'break out of string to concatenate'), and allows for proper formatting/indentation of the HTML code.
The problem is the spaces in your variables. It's more of an issue with browsers interperting spaces as the end of your href tag. As suggested earlier, use urlencode or convert the spaces in your variables to +
<?php
$str='<p style="text-align: center;">
<img style="width: 448px; height: 321px;" src="http://admin.vn/images/images/car_1.jpg" alt="">
</p>';
$search='"';
$replace=''';
$string= str_replace($search,$replace,$str);
echo $string;
?>
When I echo $string is result no convert from " to ' , how to fix it
In your original code, you were passing an undefined variable ($str) to the str_replace function
I believe your intention was to pass the $tr variable to the str_replace function.
Also, in your $replace variable, I've changed it to having double quotes (") between the character.
<?php
$tr='<p style="text-align: center;">
<img style="width: 448px; height: 321px;" src="http://admin.vn/images/images/car_1.jpg" alt="">
</p>';
$search= '"';
$replace= "'";
$string = str_replace($search,$replace,$tr);
echo $string;
?>
Change the $str to $tr as $str is not defined. It will also help if you
Change your $replace to have double quotes
$replace= "'";
You have the text stored in $tr not $str.
<?php
$tr='<p style="text-align: center;">
<img style="width: 448px; height: 321px;" src="http://admin.vn/images/images/car_1.jpg" alt="">
</p>';
$search='"';
$replace="'";
$string= str_replace($search,$replace,$tr); // Changed to $tr
echo $string;
?>
And make sure to escape the ' in $replace='''; (or use "")
$replace='''; is incorrect. You need to escape the second '.
Use either
$replace = "'" so that the single quote does not interfere, or
$replace = '\'' which escapes the middle single quote.
Escape the quote.
$replace='\'';
// or
$replace="'";