PHP: string data- attribute gets split - php

So I'm creating buttons from a result set of MySQL query, and binding data- attributes from those results. All fine with numbers and dates, but when the field is a string, the data- attribute created gets split by the whitespaces.
My PHP code:
$options = "";
while($row=mysqli_fetch_array($of)) {
$options .= "<p><a class='btn btn-default btn-open-modal' role='button' data-id=".htmlentities($row["art_id"])." data-name=".htmlentities($row["art_name"]).">See details »</a></p>";
}
When I inspect the button, I get something like data-name="nike" shox="" (if the name in the table is "nike shox"), but data-id="1" is just fine.
How can I get prevent that the data-name gets split?

Change this...
$options .= "<p><a class='btn btn-default btn-open-modal' role='button' data-id=".htmlentities($row["art_id"])." data-name=".htmlentities($row["art_name"]).">See details »</a></p>";
To this...
$options .= '<p><a class="btn btn-default btn-open-modal" role="button" data-id="' . htmlentities($row["art_id"]) . '" data-name="' . htmlentities($row["art_name"]) . '">See details »</a></p>';

Related

Syntax error for variable in the php code

I am make a variable to show the display:none; in the php echo code. But my code function cannot work when I've clicked the Delete button. I think this code is errored to put variable style='.$show_display.', maybe the syntax error. Hope anyone can point out which part I am getting error in the code.
if ($is_delete == "1"){
$show_display = "";
}
if ($is_delete == "0"){
$show_display = "display:none;";
}
echo '<a style='.$show_display.' onclick="delete_folder(\'' . md5($rs_wtp['id'].$md5) . '\',1)" class="btn btn-sm btn-primary" data-color-format="hex">Delete</a>';
I've tried to console the error. The error show me like below the picture:
Issue here is with the missing quotes for style attribute. So the echo statement would look like below:
echo '<a style="'.$show_display.'" onclick="delete_folder(\'' . md5($rs_wtp['id'].$md5) . '\',1)" class="btn btn-sm btn-primary" data-color-format="hex">Delete</a>';

I am using ordertable for server side response.Html tag double quotes are mismatched in jsonencode response

I am sending data from controller using json_encode():
$status = "<td id=status_".$value['id'].">".
"<button class='btn btn-icon-toggle active' href='javascript:;' onclick='changeStatus(".$value['id'].", '0', ".$field.", ".$url.",".$action.")><i class='fa fa-check'></i>
</button> </td>";
The Output are:
<button class="btn btn-icon-toggle active" href="javascript:;" onclick="changeStatus(1510, " 0',="" status,="" stores="" ajaxaction,restaurantstatus)="">
I need output is:
<button class="btn btn-icon-toggle active" href="javascript:;" onclick="changeStatus(1510,0,'status','stores/ajaxaction','restaurantstatus')">
what will I do???
Just learn the string escape characters and play with it!
$status = "<td id=\"status_".$value['id']."\">" . "<button class=\"btn btn-icon-toggle active\" href=\"javascript:;\" onclick=\"changeStatus(".$value['id'].", 0, '".$field."', '".$url."','".$action."')\"><i class=\"fa fa-check\"></i> </button> </td>";

PHP echo anchor link not working

I'm trying to echo a link on a page. It should be a bootstrap button, but it's just coming out as plain text.
This is my code. What am I doing wrong?
echo '<a class="btn btn-info" href='.$url.'>Video Link</a>';
Try this. I have checked it in my localhost and it is working perfectly.
$url = 'https://www.facebook.com/';
echo '<a class="btn btn-info" href='.$url.'>Video Link</a>';
Please let me know if you have any further questions or any problem occurs.
You should wrap the string you are contencating in double quotes, like this
echo '<a class="btn btn-info" href="'.$url.'">Video Link</a>';
href need to be wrapped up with href=""
try -----> href="'.$url.'"
Change this:
echo '<a class="btn btn-info" href='.$url.'>Video Link</a>';
To this:
echo '<a class="btn btn-info" href="'.$url.'">Video Link</a>';
You should not use single quotes inside single quotes so you have to change your code to something like this
echo '<a class="btn btn-info" href="'.$url.'">Video Link</a>';

How to change the html table row color on PHP fetched row value?

I want to change the color of my table row on the bases of any cell value.
As, I am using JQuery, PHP & MYSQL.
On Page load I am including PHP file that contains one function that fetch the all rows from the MYSQl table and return it back to the UI page.
The way I am fetching is as
temp1.php contains below partial code
<?php echo fxn_ExpensesList();
?>
</tbody>
<tfoot>
and the main php file contains the below code as:
$Lquery = "SELECT * FROM tblexpenseslist";
$Lquery_result = mysqli_query($conn, $Lquery);
$dataRow = "";
$RowsCounter = 0;
while($row = mysqli_fetch_array($Lquery_result)){
++$RowsCounter;
$dataRow = $dataRow."<tr><td>$RowsCounter</td>
<td>$row[ExpensesType]</td>
<td>$row[Purpose]</td>
<td>$row[Description]</td>
<td>$row[ReceiptNo]</td>
<td>$row[PAID]</td>
<td>$row[DTime]</td>
<td>$row[UnitPrice]</td>
<td>$row[Qty]</td>
<td>$row[Amount]</td>
<td style='display:none;'>$row[SNo]</td>
<td>
<p data-placement='top' data-toggle='tooltip' title='Edit'>
<button class='btn btn-danger btn-xs btn-edit' data-title='Edit' data-toggle='modal' data-target='#edit' >
<span class='glyphicon glyphicon-pencil'></span>
</button>
</p>
</td>
<!--<td><p data-placement='top' data-toggle='tooltip' title='Delete'><button class='btn btn-danger btn-xs' data-title='Delete' data-toggle='modal' data-target='#delete' ><span class='glyphicon glyphicon-trash'></span></button></p></td>-->
</tr>";
}
return $dataRow;
Now, I want to add one more functionality to the table row if the amount is paid or not.. I am thinking for that but nothing strikes to mind.
You could add a class to a table row and then modify that class using jQuery.
For example, you can make a variable called paid as a boolean value. If the specific row is paid for, then add the class isPaid to the table row. If not, then add the class notPaid, like this...
if (paid == true) {
'<tr class="isPaid">'
else if (paid != true) {
'<tr class="notPaid">'
}
...then using jQuery you can do something like...
$('.isPaid').css('background-color', 'green');
$('.notPaid').css('background-color', 'red')
Or something of the sort.

error in embeding html tags in php

<?php
$id=16;
echo '<td><a class="btn btn-primary" href="edit_news.php?id=$id"><i class="fa fa-pencil fa-fw"></i> Edit</a></td>';
?>
I have a search program that performs search via ajaX,whwre i need to embed html tag with php. Search works fine but when i click on edit button the id cant pass on another page. It shows thathttp://localhost/web/edit_news.php?id=$id. But i cant get the id=16. And error shows Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\web\edit_news.php on line 9. what is the error in my code?
Can any one help me to solve this?
you have change "edit_news.php?id=$id" to "edit_news.php?id='.$id.'". Because data in double quotes treated it like string. or you can use single quotes '. like href='edit_news.php?id=$id'.
<?php
$id=16;
echo '<td><a class="btn btn-primary" href="edit_news.php?id='.$id.'"><i class="fa fa-pencil fa-fw"></i> Edit</a></td>';
?>
just change it quote like ' to " and " to '
<?php
$id=16;
echo "<td><a class='btn btn-primary' href='edit_news.php?id=$id'><i class='fa fa-pencil fa-fw'></i> Edit</a></td>";
?>
OR
<?php
$id=16;
echo '<td><a class="btn btn-primary" href="edit_news.php?id='.$id.'"><i class="fa fa-pencil fa-fw"></i> Edit</a></td>';
?>
Change the single quote with double and double with single Like this :
<?php
$id=16;
echo "<td><a class='btn btn-primary' href='edit_news.php?id=$id'><i class='fa fa-pencil fa-fw'></i> Edit</a></td>";
?>

Categories