Too many " or ' - php

I seriously suck at getting the quotes and quotation marks correctly. is there a rule that would help me easily remember?
echo "<option value='".$data['recordc']."' . "selected=selected>" '". data['recordc'] ."' . "</option>";

You could easily fix the quotes problem with a decent editor that has syntax highlighting (even StackOverflow's basic highlighting quickly shows where you went wrong) but that is still a horrible mess to read.
Most of the time if you find yourself outputting too much HTML in an echo you are probably best served to do it in a cleaner way, like breaking out of PHP. I would do at the very least:
printf('<option value="%1$s" selected=selected>%1$s</option>', $data['recordc']);

In general you can use this instead:
echo "<option value=\"$data[recordc]\" selected=selected>$data[recordc]</option>";
which is a little easier to read. Array keys don't need to be quoted when used inside a string.
In this case, paolo's printf method is nice since the variable is reused

Related

Quotas in PHP and onClick

When I try to use this:
<?php
$html = "<p id="test"><input class='is' id='live' type='checkbox' onclick='update(".htmlspecialchars($myid).");'></p>";
?>
If $myid is a number the above works fine. If it contains text like mytext_30, then onClick I get a console message that mytext_30 is not defined. How in the top syntax I can include some kind of quotas for the result to be always like this:
<input .... onclick='update("30")'/> or
<input .... onclick='update("mytext_30")'/>
?
Thank you in advance.
Quotes you are using are mislead for PHP. try this:
$html = "<p id=\"test\"><input class='is' id=\"live\" type='checkbox' onclick='update(\"".htmlspecialchars($myid)."\");'></p>";
The problem belongs to missing escaping of the quotes. Thats easy to fix.
But first, you should decide on a way you will use. Preferred way to write tags in HTML is to always use quotes ". But at least, you should not mix quotes and apostrophes. Decide for one way and use them, but not switch between them here and there.
The best way here is, to use quotes for the tags, and apostrophe for the php string. With using apostrophes for this, you have clean HTML and don't need to escape anything.
$html = '<p id="test"><input class="is" id="live" type="checkbox" onclick="update(' . htmlspecialchars($myid) . ');"></p>';

redirect to php page from sql server fetch array loop

I'm trying to redirect to another php page from sql server fetch array with WHILE loop.
All records are fetched well, but i want to add to the table "delete" link to every fetched record.
I tried the code below separatedly and it works just fine.
The problem is because the code is inside PHP tag and i'm confused with all of the ' ' signs.
while compailing the code below i just can't see nothing (without the "delete" line everything is OK):
while ( $record = sqlsrv_fetch_array($rs, SQLSRV_FETCH_NUMERIC) )
{
$o .= '<tr><td><center>'.$record [0].'</center></td><td><center>'.$record [1].'</center></td>
<td><center>'.$record [2].'</center></td><td><center>'.$record [3].'</center></td>
<td><center>'.$record [4].'</center></td><td><center>'.$record [5].'</center></td>
<td><center>'.$record [6].'</center></td><td><center>'.$record [7].'</center></td>
<td>**<a href="JavaScript:if(confirm('delete record?')==true)
{window.location='phpSQLServerDeleteRecord.php?EventID=<?php echo $record[0];';}">delete</a>**
</tr>';
}
$o .= '</tbody></table>';
echo $o;
Thanks a lot for your help!
The use of single-quotes is creating a syntax error:
'</center></td><td>**<a href="JavaScript:if(confirm('delete record?')==true)
{window.location='phpSQLServerDeleteRecord.php?EventID=<?php echo $record[0];';}">delete</a>**
</tr>'
Since the server-side string literal began with a single quote, as soon as the string contains a single-quote then the PHP engine will interpret that as the end of the string, which then results in a couple of syntax errors here as things intended to be part of the string are interpreted as code.
There are a couple of ways to go about this. One quick fix would be to "escape" the single-quotes which are meant to be part of the string rather than a boundary of the string:
'</center></td><td>**<a href="JavaScript:if(confirm(\'delete record?\')==true)
{window.location=\'phpSQLServerDeleteRecord.php?EventID=<?php echo $record[0];\';}">delete</a>**
</tr>'
Of course, there's still the error that now PHP code (the echo statement) is being emitted as part of a string. You can terminate the string boundaries around that and concatenate the value to the string like any other string concatenation:
'</center></td><td>**<a href="JavaScript:if(confirm(\'delete record?\')==true)
{window.location=\'phpSQLServerDeleteRecord.php?EventID=' . $record[0] . '\';}">delete</a>**
</tr>'
(Note that immediately after the reference to $record[0] there are two single-quotes. One to begin the new string literal and then an escaped one to be the first character in that string literal, since the client-side code will need that single-quote character to terminate its string literal. That's probably the root of your confusion in the matter... These server-side strings aren't just HTML, they're also JavaScript which has its own client-side strings.)
You'll also want to URL-encode that value being emitted as part of a query string (even though it should just be a numeric identifier I suspect, it's still good form):
'</center></td><td>**<a href="JavaScript:if(confirm(\'delete record?\')==true)
{window.location=\'phpSQLServerDeleteRecord.php?EventID=' . urlencode($record[0]) . '\';}">delete</a>**
</tr>'
There are a number of other ways to potentially clean this up. Take a look at the PHP documentation on strings, particularly around the "heredoc" syntax. My PHP is to rusty to whip up an example of that, but I suspect it will end up looking a little cleaner in the resulting code. Either way, it's good to get some practice in the differences between the various ways PHP handle string literals.
I will echo the comment from #mario above but give you an example of how to use Heredoc syntax to make large blocs of text much more readable and easy to work with, as you will not need to mess with escaping quotes at all.
while (...)
{
$o .= <<<EOT
<tr>
<td><center>{$record[0]}</center></td>
<td><center>{$record[1]}</center></td>
<td><center>{$record[2]}</center></td>
<td><center>{$record[3]}</center></td>
<td><center>{$record[4]}</center></td>
<td><center>{$record[5]}</center></td>
<td><center>{$record[6]}</center></td>
<td><center>{$record[7]}</center></td>
<td>**<a href="JavaScript:if(confirm('delete record?')==true)
{window.location='phpSQLServerDeleteRecord.php?EventID={$record[0]}}">delete</a>**</td>
</tr>
EOT
}
Now doesn't that look a whole lot nicer?
Note: brackets around PHP variables are optional. I just like them when using Heredoc as I find it makes it easier to see where your variables are.
Read here for more information on Herdoc syntax:
http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
For future readers, please look to answer by #David as well. He does good job of explaining why the OP was having his problem. I really look at this answer as complementary to that one, giving the reader an example of what might be a good way to go about solving the problem.

URL link in php

echo '<td>'.$row_sv['name'].'</td>';
I don't want to use any target and changed it like this but it messed up my table
echo '<td><a href="'.$row_sv['website].'$row_sv['name'].'</a></td>';
something wrong?
To make such pieces clearer I prefer using templates. In your case that would be:
printf( '<td>%s</td>', $row_sv['website'], $row_sv['name'] );
No mess with the quotes and opening/closing tags.
You should use the following:
echo '<td>'.$row_sv['name'].'</td>';
You mixed up the quotes a bit:
echo '<td>'.$row_sv['name'].'</td>';
You deleted too much, and then messed up something that was ok to begin with.
Use:
echo '<td>'.$row_sv['name'].'</td>';
In addition to deleting too much, you also had $row_sv['website] instead of $row_sv['website'] which should've cause a parse error too (unless it was just a typo here).
In the future here, you could also paste the HTML output instead of saying "it messed up my table" -- it'll make it easier for you to see the problem as well as folks here, I am sure.

Newline character in print statement PHP

Another question for all you pros out here. I am still trying to learn the language and don't want to pick up any bad habits along the way, and it is easier to fix a problem now rather than later when it is a habit.
This is regarding the correct way to use the \n in a string.
They way the book I am reading does it is as follows:
print "$student: $grade<br />\n";
After some reading, I have heard that it is best practice to use single quotes whenever possible, and this is how I have written the same statement:
print $student . ': ' . $grade . '<br />' . "\n";
What is the preferred method of doing this? Is the books way or my way more correct?
It seems like the books way is easier to read when reviewing the code, so should I use double or single quotes in a situation like this?
Thank you,
Alex
http://us3.php.net/manual/en/reserved.constants.php
Scroll down to PHP_EOL.
EOL refers to 'end of line'.

Sending variables in URLs in PHP with echo

I can't really find good guidelines through Google searches of the proper way to escape variables in URLs. Basically I am printing out a bunch of results from a MySQL query in a table, and I want one of the entries in each row to be a link to that result's page. I think this is easy, that I'm just missing a apostrophe or backslash somewhere, but I can't figure it out. Here's the line that's causing the error:
echo " Who Owns It? ";
and this is the error I'm getting:
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING
How do I fix this error?
In addition, what are some general guidelines for working with echo and variables in URLs?
echo " Who Owns It? ";
There are two things here that should be changed. For one thing there shouldn't be a / after movies.php. The second is that there aren't any apostrophies around url variables. It should be movie_id=$row['movie_id']. Whenever I use a php variable I usually concatonate it instead of embed it in the quotations. So in the end I'd do something like this:
echo " Who Owns It? ";
The $row['movie_id'] inside the double quoted string is not allowed (especially the single quotes). Either write it without ' or use the curly braces syntax:
echo " Who Owns It? ";
echo " Who Owns It? ";
See variable parsing for further information.
This is a better way:
$movie_id = urlencode($row["movie_id"]);
echo ' Who Owns It? ';
Good luck!
This is a better(er) way:
$movie_id = urlencode($row["movie_id"]);
echo " Who Owns It? ";
Easier to read. Besides the single and double quote speed thing is not much of an issue any more.
Echo or URLs have nothing to do with your problem. It's PHP strings syntax.

Categories