How do I concatenate html and php? - php

I currently have this:
$output .= '
<tr>
<th style="text-align: left">'.(__("Item", "WSPSC")).'</th><th>'.(__("Quantity", "WSPSC")).'</th><th>'.(__("Price", "WSPSC")).'</th><th></th>
</tr>';
But I need to replace Item with a piece of php like:
$output .= '
<tr>
<th style="text-align: left">'.(__("
<?php if(ICL_LANGUAGE_CODE=='en'); ?>
Item
<?php elseif(ICL_LANGUAGE_CODE=='it'); ?>
Products
<?php endif; ?>
", "WSPSC")).'</th><th>'.(__("Quantity", "WSPSC")).'</th><th>'.(__("Price", "WSPSC")).'</th><th></th>
</tr>';
The issue I am having is that this is obviously wrong but I can't get my head around the correct concatenation of html and php

If I right understand you need something like this:
$output .= '
<tr>
<th style="text-align: left">'.
(
(ICL_LANGUAGE_CODE=='en')?
'Item':
( (ICL_LANGUAGE_CODE=='it')? 'Products': '' ),
"WSPSC"
)
.'</th><th>'.(__("Quantity", "WSPSC")).'</th><th>'.(__("Price", "WSPSC")).'</th><th></th>
</tr>';

You should not use it in that way. Look at this pseudo code:
$output .= '
<tr>
<th style="text-align: left">';
if (something...) {
$output.= 'sss';
}
elseif (something...) {
$output.= 'ddd';
}
That's the way you should do it.

Your replies helped me thanks, this worked:
$output .= '
<tr>
<th style="text-align: left">';
if (ICL_LANGUAGE_CODE=='en') {
$output .= (__("Item", "WSPSC"));
} elseif (ICL_LANGUAGE_CODE=='it') {
$output .= (__("PRODOTTO", "WSPSC"));
}
$output .= '</th><th>'.(__("Quantity", "WSPSC")).'</th><th>'.(__("Price", "WSPSC")).'</th><th></th>
</tr>';

Related

Color table row by status

I wanna do if in by my status field.
if my status is "blocked" so the font background color will be in red and if the status is "authorized" so in green.
how should i do that?
foreach($data as $row)
{
$output .= '
<tr>
<td>'.$row->client_name.'</td>
<td>'.$row->adrress.'</td>
<td>'.$row->occupation.'</td>
<td>'.$row->payeee.'</td>
<td>'.$row->status.'</td>
</tr>
';
}
Try this
foreach($data as $row)
{
$output .= '
<tr>
<td>'.$row->client_name.'</td>
<td>'.$row->adrress.'</td>
<td>'.$row->occupation.'</td>
<td>'.$row->payeee.'</td>
#if($row->status == "blocked")
<td> <button type="button" class="btn btn-danger">blocked</button> </td>
#elseif($row->status == "authorized")
<td> <button type="button" class="btn btn-success">authorized</button> </td>
#endif
</tr>
OR
foreach($data as $row){
$output .= '
<tr>
<td>'.$row->client_name.'</td>
<td>'.$row->adrress.'</td>
<td>'.$row->occupation.'</td>
<td>'.$row->payeee.'</td>';
if($row->status == "bad"){
$output .= '<td style="background-color:red;">'.$row->status.'</td>';
} else if($row->status == "good") {
$output .= '<td style="background-color:green;">'.$row->status.'</td>';
}
$output .= '</tr>';
}
You shouldn't really be dealing with view logic in your controller.
But to do what you want in the controller
foreach($data as $row){
$output .= '
<tr>
<td>'.$row->client_name.'</td>
<td>'.$row->adrress.'</td>
<td>'.$row->occupation.'</td>
<td>'.$row->payeee.'</td>';
if($row->status == "bad"){
$output .= '<td style="background-color:red;">'.$row->status.'</td>';
} else if($row->status == "good") {
$output .= '<td style="background-color:green;">'.$row->status.'</td>';
}
$output .= '</tr>';
}
However what you should do is handle it in your blade file. Also you probably want to create styles in your style sheet and apply classes rather than inline the css.
this is the code that worked
foreach($data as $row){
$output .= '
<tr>
<td>'.$row->client_name.'</td>
<td>'.$row->adrress.'</td>
<td>'.$row->occupation.'</td>
<td>'.$row->payeee.'</td>';
if($row->status == "bad"){
$output .= '<td style="background-color:red;">'.$row->status.'</td>';
} else if($row->status == "good") {
$output .= '<td style="background-color:green;">'.$row->status.'</td>';
}
$output .= '</tr>';
}

How to apply PHP inside HTML that is running in a if statement

I need to get this code running but i need your help. It is a PHP script with a if statement combined to a categorie. The problem is that the output isn't compleet. The php action that give back a value that needs to be apply'd on the product page in the backend off Wordpress should be displays in the tabel after the day prijs but isn't visible.
<?php
add_action( 'woocommerce_after_single_product_summary', 'bbloomer_single_category_slug' );
function bbloomer_single_category_slug() {
if ( has_term( 'verhuur-mini-gravers', 'product_cat' ) ) { ?>
<div class="view-right">
<h2>Verhuur prijzen</h2>
<table style="width:100%; border: 1px #000;">
<tr>
</tr>
<tr>
<td style="font-weight:500;">Dag prijs</td>
<td style="font-weight:500;">4 dagen prijs</td>
<td style="font-weight:500;">Week prijs</td><td>
</tr>
<tr>
<td>€ <?php echo get_post_meta( $post->ID, '_text_field_17', true ) ?></td>
<td>€ <?php echo get_post_meta( $post->ID, '_text_field_18', true ) ?></td>
<td>€ <?php echo get_post_meta( $post->ID, '_text_field_19', true ) ?></td>
</tr>
<tr>
</tr>
</table>
</div>
<?php; } elseif ( has_term( 'tables', 'product_cat' ) ) {
echo 'Something else';
}
}?>
The value of the _text_field_17 should be visible with the value. Now the get_post_meta is not visible or not running.
Welcome!
Good thing that you wrote a function to do so. You might consider indenting your codes, so that you can see the bugs before testing.
One way is to just define a string $html and store everything in it and at the end just return that, maybe similar to:
add_action('woocommerce_after_single_product_summary', 'bbloomer_single_category_slug');
function bbloomerSingleCategorySlug($post)
{
$html = '';
if (isset($post) && has_term('verhuur-mini-gravers', 'product_cat')) {
$html .= '<div class="view-right">';
$html .= '<h2>Verhuur prijzen</h2>';
$html .= '<table style="width:100%; border: 1px #000;">';
$html .= '<tr></tr>';
$html .= '<tr><td style="font-weight:500;">Dag prijs</td><td style="font-weight:500;">4 dagen prijs</td><td style="font-weight:500;">Week prijs</td><td></tr>';
$html .= '<tr><td>€ ' . get_post_meta($post->ID, "_text_field_17", true) . '</td>';
$html .= '<td>€ ' . get_post_meta($post->ID, "_text_field_18", true) . '</td>';
$html .= '<td>€ ' . get_post_meta($post->ID, "_text_field_19", true) . '</td>';
$html .= '</tr><tr>';
$html .= '</table>';
$html .= '</div>';
} elseif (has_term('tables', 'product_cat')) {
echo 'Something else';
} else {
echo 'Something is not right! Maybe, check the $post variable!';
}
echo $html;
}
I'm not so sure about your variables in Woocommerce, you may want to check on that. If you wish, you can pass $post as a variable in the function.

Include if statement in HTML table

I want to include an if statement in my PHP code that renders an HTML table. The output should be in a table row, too.
echo '<table style="border:1px solid red;">',
'<tr align="center" ><td><div class="item-font1">', $mounts['name'], '</div></td>
</tr>',
if ($itembind == 1) {
echo '<tr align="center" ><td><div class="item-font1">Text1</div></td>
</tr>',
} elseif ($itembind== 2) {
echo '<tr align="center" ><td><div class="item-font1">Text2</div></td>
</tr>',
echo "\t";
}
'<tr align="center" ><td><div class="item-font1">', $mounts['name'], '</div></td>
</tr>
</table>';
?>
I tried it like this, but there are some errors.
With echo, the strings can be separated by a comma (i.e. ,). However, when additional logic is needed, the echo statement must be terminated. Use a semi-colon (i.e. ;) for that.
echo '<table style="border:1px solid red;">',
'<tr align="center" ><td><div class="item-font1">', $mounts['name'],
'</div></td>
</tr>'; //use a semi-colon here to end the call to echo
if ($itembind == 1) {
As in C or Perl, PHP requires instructions to be terminated with a semicolon at the end of each statement. The closing tag of a block of PHP code automatically implies a semicolon; you do not need to have a semicolon terminating the last line of a PHP block.
1
While it is acceptable to pass multiple strings to echo, the strings could be concatenated into one. For that, use a dot operator.
So update this line (which is missing an echo at the beginning) :
'<tr align="center" ><td><div class="item-font1">', $mounts['name'], '</div>
</td></tr></table>';
To
echo '<tr align="center" ><td><div class="item-font1">'. $mounts['name']. '</div>
</td></tr></table>';
Final output:
<?php
$mounts = ['name'=>'cat'];
$itembind = 1;
echo '<table style="border:1px solid red;">',
'<tr align="center" ><td><div class="item-font1">', $mounts['name'], '</div></td>
</tr>';
if ($itembind == 1) {
echo '<tr align="center" ><td><div class="item-font1">Text1</div></td>
</tr>';
} elseif ($itembind== 2) {
echo '<tr align="center" ><td><div class="item-font1">Text2</div></td>
</tr>';
echo "\t";
}
echo '<tr align="center" ><td><div class="item-font1">', $mounts['name'], '</div></td>
</tr>
</table>';
?>
See a demonstration of the updated code in this playground example.
1http://php.net/manual/en/language.basic-syntax.instruction-separation.php
Use . operator while concatenation. You are using comma , operator. See your code :-
echo '<table style="border:1px solid red;">','<tr align="center" ><td><div class="item-font1">', $mounts['name'], '</div></td></tr>',
should be
echo '<table style="border:1px solid red;">'.'<tr align="center" ><td><div class="item-font1">'. $mounts['name']. '</div></td></tr>'.
and same for rest of the code.
Your syntax is wrong. You should use . to concatenate strings and ; to terminate lines.
You can also use double quotes (") and PHP will parse the string looking for variables. For example:
$myvar = 'example';
$singleQuote = 'This is an ' . $myvar;
$doubleQuote = "This is an {$myvar}";
I usually find double quoting strings easier when you are going to be using variables.
echo '<table style="border:1px solid red;">';
echo '<tr align="center"><td><div class="item-font1">' . $mounts['name'] . '</div></td></tr>';
if ($itembind == 1) {
echo '<tr align="center" ><td><div class="item-font1">Text1</div></td></tr>';
} elseif ($itembind== 2) {
echo '<tr align="center" ><td><div class="item-font1">Text2</div></td></tr>';
echo "\t";
}
'<tr align="center" ><td><div class="item-font1">' . $mounts['name'] . '</div></td></tr></table>';
It would be a better idea to make the conditional statement on the value that's being inserted, but doing this your way looks like:
echo '<table style="border:1px solid red;">',
'<tr align="center" ><td><div class="item-font1">'. $mounts['name']. '</div></td>
</tr>';
if ($itembind == 1) {
echo '<tr align="center" ><td><div class="item-font1">Text1</div></td>
</tr>';
} elseif ($itembind== 2) {
echo '<tr align="center" ><td><div class="item-font1">Text2</div></td></tr>';
} else {
echo "\t";
}
echo '<tr align="center" ><td><div class="item-font1">'. $mounts['name']. '</div></td>
</tr>
</table>';
It wouldn't be a bad idea to practice a tutorial or two before posting here.
Something that some new people don't realize is that you can pull the html out of the php a little. This same thing could be written:
<?php
if ($itembind == 1) $text = 'text1';
elseif ($itembind== 2) $text = 'text2';
if(isset($text)){
$row = '<tr align="center" >
<td>
<div class="item-font1">'.$text.</div>
</td>
</tr>
} else {
$row = '';
}
?>
<table style="border:1px solid red;">
<tr align="center">
<td>
<div class="item-font1"><?=$mounts['name']?></div>
</td>
</tr>
<?=$row?>
<tr align="center" >
<td>
<div class="item-font1">'<?=$mounts['name']?></div>
</td>
</tr>
</table>
You are using , instead of .. Also need to add ; at your echo end.
<?php
$mounts['name'] = 'yyy';
$itembind = 1;
echo '<table style="border:1px solid red;">',
'<tr align="center" ><td><div class="item-font1">'. $mounts['name']. '</div></td>
</tr>';
if ($itembind == 1) {
echo '<tr align="center" ><td><div class="item-font1">Text1</div></td>
</tr>';
} elseif ($itembind== 2) {
echo '<tr align="center" ><td><div class="item-font1">Text2</div></td>
</tr>';
echo "\t";
}
'<tr align="center" ><td><div class="item-font1">'.$mounts['name'].'</div></td>
</tr>
</table>';
?>
$output = "<table style=\"border:1px solid red;\">";
$output .= "<tr align=\"center\"><td><div class=\"item-font1\">";
$output .= $mounts['name'];
$output .= "</div></td>";
$output .= "</tr>";
if ($itembind == 1) {
$output .= "<tr align=\"center\"><td><div class=\"item-font1\">Text1</div></td></tr>";
} else if ($itembind== 2) {
$output .= "<tr align=\"center\"><td><div class=\"item-font1\">Text2</div></td></tr>\t";
}
$output .= "<tr align=\"center\"><td><div class=\"item-font1\">";
$output .= $mounts['name'];
$output .= "</div></td></tr>";
$output .= "</table>";
echo $output;
No need of write table within PHP. it will make complexity of the code. try this:
<?php if(condition) { ?>
<tr> <td>some content</td></tr>
<?php } elseif(condition) { ?>
<tr><td>some content</td></tr>
<?php } else { ?>
<tr><td>some content</td></tr>
<?php } ?>

PHP pagination error with only 1 page

I have a problem with pagination, i see only first page, i try to undestand. I have a file HTML with a script and the script launch a file php. But the result is only a file
<?php
include 'database.php';
$output = '';
$start=0;
$limit=2;
if(isset($_GET['id'])){
$id=$_GET['id'];
$start=($id-1)*$limit;
}else{
$id=1;
}
$search = $_POST["search"];
if($search == ''){
$sql = "SELECT * FROM dati ORDER BY id DESC LIMIT $start, $limit";
}
else {
$sql = "SELECT * FROM dati WHERE nome LIKE '%".$_POST["search"]."%' ORDER BY id DESC LIMIT $start, $limit";
}
$retval = mysqli_query( $dbconfig, $sql);
$output .= '
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Nome</th>
<th>Categoria</th>
<th>Numero di Telefono</th>
</tr>
</thead>
<tbody>';
while($row=mysqli_fetch_array($retval)){
$output .= '
<tr>
<td>'. $row['nome'] . '</td>
<td>'. $row['categoria'] . '</td>
<td>'. $row['telefono'] . '</td>
</tr>';
}
$output .= '
</tbody>
</table>';
$ret = mysqli_num_rows(mysqli_query( $dbconfig, $sql));
$total=ceil($ret/$limit);
$output .= '
<div class="row">
<ul class="page">';
for($i=1;$i<=$total;$i++){
if($i==$id) {
$output .= '<li class="corrente">'.$i.'</li>';
}else {
$output .= '<li><a href=?id='.$i.'>'.$i.'</a></li>';
}
}
$output .= '
</ul>
</div>';
echo $output;
?>
This is file PHP with pagination but nothing
<input type="submit" name="test" id="test" value="Ricerca">

How can I make this PHP <td> Conditional?

I am trying to get this PHP table cell to write a color depending on a condition but I am missing something that is causing a Syntax error?
Here is the code:
$table = '<table>
<tr>
<th> Qty </th>
<th> Length </th>
<th> Description </th>
<th> Color </th>
</tr>
<tr>
<td></td>
<td></td>
<td>'.$gauge. ' ' .$panel. '</td>'
if ($sscolor == "None")
{
'<td>' .$color. '</td>';
}
else
{
'<td>' .$sscolor. '</td>';
}
'</td>
</tr> ';
Yes. You can't put an if/else conditional inside a string. You can use a ternary though.
$str = 'text'.($sscolor == 'None' ? $color : $sscolor).' more text'; // etc
Otherwise you'll need to end the string before the if, then concatenate more onto it using .=
You can't place a IF condition in a variable after you write some string to it
I suggest this is how you do it
if ($sscolor == "None")
{
$extra_string = '<td>' .$color. '</td>';
}
else
{
$extra_string = '<td>' .$sscolor. '</td>';
}
$table = '<table>
<tr>
<th> Qty </th>
<th> Length </th>
<th> Description </th>
<th> Color </th>
</tr>
<tr>
<td></td>
<td></td>
<td>'.$gauge. ' ' .$panel. '</td>' . $extra_string . '
</tr> ';
The problem is that you need to close the string concatenation with a semicolon, ;, before the if statement. If you do not then you will get a syntax error:
<td>'.$gauge. ' ' .$panel. '</td>' <-- Semicolon here
if ($sscolor == "None") <-- Syntax error, unexpected if token
A good way to avoid stuff like this is to use a heredoc string:
// Figure out the color before going into the string
if ($sscolor === 'None') {
$color = $sscolor;
}
// heredoc string, with string interpolation
$table = <<< HTML
<table>
<tr>
<th>Qty</th>
<th>Length</th>
<th>Description</th>
<th>Color</th>
</tr>
<tr>
<td>-</td>
<td>-</td>
<td>{$gauge} {$panel}</td>
<td>{$color}</td>
</tr>
</table>
HTML;
Learn more about strings.
Also, even the best PHP programmers have to deal with errors. It is a part of learning PHP. So, you should get in the habit of googling error messages; they are easy to find, and you will be able to help yourself and learn at the same time.
You need to concatenate the lines withint the if statement.
<td>'.$gauge. ' ' .$panel. '</td>';
if ($sscolor == "None") {
$table .= '<td>' .$color. '</td>';
} else {
$table .= '<td>' .$sscolor. '</td>';
}
$table .= '</td>';

Categories