Properly use single and double quatation in PHP - php

I have my PHP code like below and its work fine
<tr onclick="window.location='../info/<?php echo $ouid;?>';" class="tbl_rated_orders">
Now I am loading more data on press load more button via ajax
so I am trying like this
$tableData.= '<tr class="tbl_rated_orders_buyer" onclick="window.location='.'"../info/'.$ouid.';">
<td>'.$date_rated.'</td>
<td><img src="../../global_assets/uploads/users/'.$image.'" width="35" class="rounded-pill" alt="">
<span class="ml-2">'.$rated_row["full_name"].'</span>
</td>
<td>'.$cancelled_by_txt.'</td>
<td><i class="icon-paperplane text-dark mr-1"></i>'.$str1.'</td>
</tr>';
But now tr html code looks like this
<tr class="tbl_rated_orders_buyer" onclick="window.location=" ..="" info="" zxp2ibhfnu;"="">
so its not correct code for window location, its need like this
<tr onclick="window.location='../info/RQ5KWBIY6M';" class="tbl_cancelled_orders">
I am new in PHP and trying from half hour to make it working but not getting idea how I can do it, Let me know if anyone here can help me for do the same.
Thanks!

You will need to escape some single quotes for the single quotes used in the javascript in the onclick attribute.
$ouid = "asd0f8a08f0";
$date_rated = "12/03/2021";
$image = "north_pole.jpg";
$rated_row["full_name"] = "Mighty Mouse";
$cancelled_by_txt = "grinch";
$str1 = "Paper Plane";
$tableData = "";
$tableData.= '<tr class="tbl_rated_orders_buyer" onclick="window.location='.'\'../info/'.$ouid.';\'">
<td>'.$date_rated.'</td>
<td><img src="../../global_assets/uploads/users/'.$image.'" width="35" class="rounded-pill" alt="">
<span class="ml-2">'.$rated_row["full_name"].'</span>
</td>
<td>'.$cancelled_by_txt.'</td>
<td><i class="icon-paperplane text-dark mr-1"></i>'.$str1.'</td>
</tr>';
Output:
<tr class="tbl_rated_orders_buyer" onclick="window.location='../info/asd0f8a08f0;'">
<td>12/03/2021</td>
<td><img src="../../global_assets/uploads/users/north_pole.jpg" width="35" class="rounded-pill" alt="">
<span class="ml-2">Mighty Mouse</span>
</td>
<td>grinch</td>
<td><i class="icon-paperplane text-dark mr-1"></i>Paper Plane</td>

Related

Adding HTML codes inside 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'].'" ... >

Pass value with href in php inside echo

I need to pass value with href inside fetch array echo...
php code
$user = $get['username'];
$resource=mysqli_query($con,$sql);
echo "<font color=\"#000000\">
<h2 align=\"center\"></h2>
<table align=\"center\" border=\"1\" width=\"50%\">
<tr>
<td><b>GROUP NAME</b></td> <td><b>TASK TITLE 1</b></td> <td><b>TASK TITLE 2</b></td> <td><b>CREATED BY</b></td> <td><b>ASSIGNED TO</b></td> <td><b>DUE DATE</b></td> <td><b>PRIORITY</b></td> <td><b>CHANGE</b></td></tr> ";
while($result=mysqli_fetch_array($resource))
{
echo "<tr><td>".$result[0]."</td> <td>".$result[1]."</td> <td>".$result[2]."</td> <td>".$result[3]."</td> <td>".$result[4]."</td> <td>".$result[5]."</td> <td>".$result[6]."</td> <td> "<a href="changetask1.php?username='.$user.'">"</td></tr>";
} echo "</table></font>";
I need to pass username to next page with href value.
You are all messed up with quotes...
First... Learn to indent your code to make it readable.
Then, avoid echoing simple HTML if not necessary:
See your code after my «formatting»:
Try it, i've done a couple corrections...
<?php
$user=$get['username'];
$resource=mysqli_query($con,$sql);
?>
<!-- This is only HTML -->
<font color="#000000">
<h2 align="center"></h2>
<table align="center" border="1" width="50%">
<tr>
<td><b>GROUP NAME</b></td>
<td><b>TASK TITLE 1</b></td>
<td><b>TASK TITLE 2</b></td>
<td><b>CREATED BY</b></td>
<td><b>ASSIGNED TO</b></td>
<td><b>DUE DATE</b></td>
<td><b>PRIORITY</b></td>
<td><b>CHANGE</b></td>
</tr>
<?php
// This is a PHP block until the next ?>
while($result=mysqli_fetch_array($resource)){
echo "<tr>
<td>".$result[0]."</td>
<td>".$result[1]."</td>
<td>".$result[2]."</td>
<td>".$result[3]."</td>
<td>".$result[4]."</td>
<td>".$result[5]."</td>
<td>".$result[6]."</td>
<td><a href='changetask1.php?username=".$user."'></td>
</tr>";
}
?>
</table>
</font>
To avoid these problems of double and single quote you can write your code like following.
<td><a href="cheangetask1.php?username=<?php echo $user;?>"></td>
Now on the changetask1.php you can get username like this.
<?php echo $_REQUEST['username'];?>

Regex PHP: Get specific content from a block of code from another website

I have a site from which I want to get specific content from 7 posts. Those all 7 seven posts have same HTML layout (See Below)
<div class="eventInfo">
<h3>Z's(矢沢永吉)</h3>
<h4>Z's TOUR 2015</h4>
<dl>
<dt><img src="/event/img/btn_day.png" alt="公演日時" width="92" height="20"> </dt>
<dd>
<table width="99%" border="0" cellpadding="0" cellspacing="0">
<tbody><tr>
<td width="9%" nowrap="nowrap">2015年6月</td>
<td width="74%">4日 (木) 19:00開演</td>
</tr>
</tbody></table>
</dd>
<dt><img src="/event/img/btn_price.png" alt="料金" width="92" height="20"> </dt>
<dd>S¥10,500 A¥7,500 (全席指定・消費税込)<br><span class="attention">※</span>注意事項の詳細を矢沢永吉公式サイトより必ずご確認ください</dd>
<dt><img src="/event/img/btn_ticket.png" alt="一般発売" width="92" height="20"> </dt>
<dd>
<table width="99%" border="0" cellpadding="0" cellspacing="0">
<tbody><tr>
<td width="9%" nowrap="nowrap">2015年5月</td>
<td width="74%">16日(土)</td>
</tr>
</tbody></table>
</dd>
<dt><img src="/event/img/btn_contact.png" alt="お問合わせ" width="92" height="20"> </dt>
<dd>ソーゴー大阪 06-6344-3326</dd>
<dt><img src="/event/img/btn_info.png" alt="公演詳細" width="92" height="20"> </dt>
<dd>http://www.siteurl.com </dd>
</dl>
</div>
I just want to fetch the H3 from this layout and the first table in the code. What regex method should I use to get the desired results?
Also these are 7 posts just like the code above and I have to get H3 and the first table from each of it.
I have tested it but not sure that is it a correct way or not: https://regex101.com/r/sO6tJ8/1
But as you can see that I have to add unwanted data too like H4 DT IMG :(
I don't think regex is your best choice here. If you can get away without using regex I would do so. Have a look at Goutte a PHP web-scraper.
$crawler = $client->request('GET', 'http://www.example.com/some-page');
$heading = $crawler->filter('h3')->first();
$table = $crawler->filter('table')-> first();
This will not only be better readable, it will also make it easier to fix something when the html-structure changes.
If you must choose regex you could do something like the following for the h3 (haven't tested it, but something like that):
$html = preg_replace_callback(
'/<h3>(.*?)<\/h3>/u',
function ($match) {
return $match[1];
},
$html
);
For the table it is similar only you have to use the multiline-modifier m (it wouldn't hurt to add it to the h3 as well, but from your example you don't need it).
// I'm assuming you can get the HTML into a variable somehow
// I did my testing w/ a local file with your HTML content
$data = file_get_contents('foo.html');
$h3_content = array();
$table_content = array();
// h3 content is easy to grab, but it could be on multiple lines!
// I didn't account for multiline here:
preg_match('/<h3>([^<]+)<\/h3>/', $data, $h3_content);
// regex can't find the ending table tag easily, unless the
// entire HTML on one line, so make everything one line
// you don't need a new variable here, I did it only to be explicit
// that we have munged the original HTML into something else
$data2 = str_replace("\n", '', $data);
// to separate tables, put new line after each one
$data2 = str_replace('</table>', "</table>\n", $data2);
// now regex is easy
preg_match_all('/(<table.+<\/table>)/m', $data2, $table_content);
echo $h3_content[1], "\n";
echo $table_content[0][1], "\n";

Firefox not showing results of my php code

So I have problem with firefox not showing results of search form for query i enter, but code is working normally in Opera,Chrome,IE and Android default browser.
This is the search form :
<div id="menu_right">
<div id="searchBar">
<form id='search_form' method='POST' action='index.php?menu=search'>
<input id="topSearchText" class="inputText" type="text" name="movie" size="30" onfocus="this.value=''" value="Search" style="color: rgb(119, 119, 119);"/>
<input type="image" name="search_movies" value="Search" src="./images/search.png"/>
</form>
</div>
and this is the php part of the code:
<table width='100%' cellspacing='0' cellpadding='0' border='0'>
<tbody>
<tr>
<td class='normal_header' colspan='2'>Search Results</td>
<td class='normal_header' width='40' align='center' nowrap=''>Score</td>
</tr>
<?php
if(isset($_POST['search_movies']))
{
$movie=$tmdb->searchMovie($_POST['movie']);
foreach ($movie['results'] as $value) {
$filepath = $value['poster_path'];
$image_url = $tmdb->getImageUrl($filepath, TMDb::IMAGE_POSTER, 'w92');
echo "
<tr>
<td class='borderClass bgColor' width='50' valign='top'>
<div class='picSurround'>
<a href='http://www.themoviedb.org/movie/{$value['id']}'>
<img border='0' src='{$image_url}'>
</a>
</div>
</td>
<td class='borderClass bgColor' valign='top'>
<a href='http://www.themoviedb.org/movie/{$value['id']} '>
<strong>{$value['title']}</strong>
</a>
<a class='button_add' title='Quick add movie to my list' href='addmovie.php?id={$value['id']}'>add</a>
<div class='spaceit'>
<a href='http://www.themoviedb.org/movie/{$value['id']}'>Read more about: {$value['title']}</a>
</div>
</td>
<td class='borderClass bgColor' align='center'>{$value['vote_average']}</td>
</tr>";
}
}
?>
</tbody>
Is it something wrong with my query or maybe some tag because I cant seem to find the problem.
Have you checked the error_log that was possibly created?
Also.. and I might be wrong here.. but as far as I know php only interprets code between double quotes "", not between single quotes ''.
So for instance:
$test = 5;
echo "this string shows the value of $test"
echo 'this string just shows the string $test';
If this is a search form based on the results of the input field then it should be;
isset($_POST['movie'])
Look at this question :
HTML Input (type=image) not working on Firefox 4
Maybe your problem comes from the way firefox handles image's input tag.
Edit : More infos in the accepted answer to this question : Code working in Chrome but not in Firefox
This seem to be a common "problem" with image-type inputs.

basic php form help (currency display)

You guys were very helpful yesterday. I am still a bit confused here though.
I want to make it so that the numbers on the rightmost column are rounded off to the nearest dollar:
http://www.nextadvisor.com/voip_services/voip_calculator.php?monthlybill=50&Submit=Submit
the code for the table looks like this:
I want $offer[1,2,3,4,5,6,7]calcsavingsann to be rounded, how can do this?
<table width="100%;" border="0" cellspacing="0" cellpadding="0"class="credit_table2" >
<tr class="credit_table2_brd">
<td class="credit_table2_brd_lbl" width="100px;">Services:</td>
<td class="credit_table2_brd_lbl" width="120px;">Our Ratings:</td>
<td class="credit_table2_brd_lbl" width="155px;">Monthly VoIP Bill:</td>
<td class="credit_table2_brd_lbl" width="155px;">Annual Savings:</td>
</tr>
<?php
$offer1price="24.99";
$offer2price="20.00";
$offer3price="21.95";
$offer4price="23.95";
$offer5price="19.95";
$offer6price="23.97";
$offer7price="24.99";
$offer1calcsavings= $monthlybill - $offer1price;
$offer2calcsavings= $monthlybill - $offer2price;
$offer3calcsavings= $monthlybill - $offer3price;
$offer4calcsavings= $monthlybill - $offer4price;
$offer5calcsavings= $monthlybill - $offer5price;
$offer6calcsavings= $monthlybill - $offer6price;
$offer7calcsavings= $monthlybill - $offer7price;
$monthybill="monthlybill";
$offer1calcsavingsann= $offer1calcsavings * 12;
$offer2calcsavingsann= $offer2calcsavings * 12;
$offer3calcsavingsann= $offer3calcsavings * 12;
$offer4calcsavingsann= $offer4calcsavings * 12;
$offer5calcsavingsann= $offer5calcsavings * 12;
$offer6calcsavingsann= $offer6calcsavings * 12;
$offer7calcsavingsann= $offer7calcsavings * 12;
$re=1;
$offer ='offer'.$re.'name';
$offername= ${$offer};
while($offername!=""){
$offerlo ='offer'.$re.'logo';
$offerlogo=${$offerlo};
$offerli ='offer'.$re.'link';
$offerlink=${$offerli};
$offeran ='offer'.$re.'anchor';
$offeranchor=${$offeran};
$offerst ='offer'.$re.'star1';
$offerstar=${$offerst};
$offerbot='offer'.$re.'bottomline';
$offerbottomline=${$offerbot};
$offerca ='offer'.$re.'calcsavings';
$offercalcsavings=${$offerca};
$offerpr ='offer'.$re.'price';
$offerprice=${$offerpr};
$offersavann ='offer'.$re.'calcsavingsann';
$offercalcsavingsann=${$offersavann};
echo '<tr >
<td >
<a href="'.$offerlink.'" target="blank"><img src="http://www.nextadvisor.com'.$offerlogo.'" alt="'.$offername.'" />
</a>
</td>
<td ><span class="rating_text">Rating:</span>
<span class="star_rating1">
<img src="http://www.nextadvisor.com'.$offerstar.'" alt="" />
</span>
<br />
<div style="margin-top:5px; color:#0000FF;">
Go to Site
<span style="margin:0px 7px 0px 7px;">|</span>Review
</div> </td>
<td >$'.$offerprice.'</td>
<td >$'.$offercalcsavingsann.'</td>
</tr>';
$re=$re+1;
$offer ='offer'.$re.'name';
$offername= ${$offer};
}
?>
</table>
Do you want rounded up/down/truncated to the nearest dollar?
Here are some suggested functions you can use:
Rounding
round
floor
ceil
Formatting/Truncating
sprintf
Grepsedawk's answer is good; the only thing I would add is that rather than displaying $336.6, for example, you could could use number_format to output $336.60.
(I know this wasn't your question, but looking at the link, I thought that might be useful for you.)
Edit - Thanks to Andy for suggesting money_format instead.
money_format() is a function that returns a string value of a formatted number. You have control over the formatting and, obviously, your number. A simple example, if you have your value in the variable $myNumber, you could incorporate the result into a given table's data cell like so;
<?php echo ("<td>".money_format('%n',$myNumber)."</td>"); ?>
And you would need to do this for every value, e.g. via a for loop if you had all your values in an array. The n here is one of the formatting options - there are several. A good place to look would be on the PHP web page at http://au2.php.net/manual/en/function.money-format.php
Hope this helps.
I can't seem to get the usage right. The way I am using echo is
echo '<tr >
<td ><img src="http://www.nextadvisor.com'.$offerlogo.'" alt="'.$offername.'" /></td>
<td ><span class="rating_text">Rating:</span><span class="star_rating1"><img src="http://www.nextadvisor.com'.$offerstar.'" alt="" /></span><br />
<div style="margin-top:5px; color:#0000FF;">Go to Site<span style="margin:0px 7px 0px 7px;">|</span>Review</div> </td>
<td >$'.$offerprice.'</td>
<td >$'.$offercalcsavingsann.'</td>
</tr>';
I put the "set locale" up where the
"<?php"
is. I don't understand how to write it, and every way I do just returns an error.

Categories