Im trying to pass a variable in the URL and to post the variable on the page. I currently have a table and a variable named $record['Filename']. The value is not displaying correctly.
Right now I have the following
while($record = mysql_fetch_array($mydata)){
echo "<tr>";
echo "<td>" ."<a href='Info.html?page='.$record['Filename'].> $record['Filename'] </a>". " </td>";
echo "<td>" . $record['Description'] . "</td>";
echo "</tr>";
}
PHP strings 101:
echo "<td>" ."<a href='Info.html?page='.$record['Filename'].> $record['Filename'] </a>". " </td>";
^--start string end string --^
Since you never "exit" your "-quoted strings, your . are just plaintext periods, not concatenation operators.
You probably want this:
echo <<<EOL
<td>{$record['Filename']}</td>
EOL;
Notice how using a heredoc removes the need to hop in/out of string mode, and you can use proper quoting in the HTML. Also note the {}-extended string syntax around the variables.
try to change,
<a href='Info.html?page='.$record['Filename'].> $record['Filename'] </a>
to
<?php echo $record['Filename'];?>
Use single quotes instead of double quotes. It's faster and more efficient.
while($record = mysql_fetch_array($mydata)){
echo '<tr>';
echo '<td>'.$record['Filename'].'</td>';
echo '<td>'. $record['Description'].'</td>';
echo '</tr>';
}
Related
I have been using this syntax
print "<td>" . "$" . round($res->DollarValue) . "</td>";
Which will output something like this: $8812 I want the format to be $8,812 so I tried this
print "<td>" . "$" . round(number_format($res->AveragePrice)) . "</td>";
but that did not do the trick either.
What I am after is rounding with a dollar sign and comma, but no decimals.
Further Examples
1000.78 -> $1,001
1000.23 -> $1,000
What is the php synatx for this?
Hi guys i'm trying to output my database values in a datalist
this is my code
my query works:
but my datalist is empty
Your syntax is incorrect:
echo "<input id="numero" list="posti1" >
<datalist id="posti1" >";
Should be:
echo '<input id="numero" list="posti1" >
<datalist id="posti1">';
Because you have double quotes in the string, you can use single quotes to contain it.
Try
echo "<input id='numero' list='posti1' ><datalist id='posti1'>";
instead of
echo "<input id="numero" list="posti1" ><datalist id="posti1" >";
If you want to use double quotes inside the double quotes then you need to use slash (\) before the double quote
i.e,
echo "<input id=\"numero\" list=\"posti1\" ><datalist id=\"posti1\">";
That is not only error you having on your script when you done fixing that error you gonna get another error:
bellow are the errors you have:
echo "<input id="numero" list="posti1" >
<datalist id="posti1" >";
echo "<option value="$row[numero]"/>"; // Format for adding options
They should look like this:
echo "<input id=\"numero\" list=\"posti1\">
<datalist id=\"posti1\">";
echo "<option value=\"".$row['numero']."\"/>"; // Format for adding options.
Therefore your full code should be:
<?php
session_start();
$conn = oci_connect('insidedba', 'progetto16', 'localhost/XE');
if (!$conn) {
$e = oci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
echo "<input id=\"numero\" list=\"posti1\">
<datalist id=\"posti1\">";
$sql = oci_parse($conn, "SELECT NUMERO FROM POSTI WHERE PRENOTAZIONE=NULL");
oci_execute($sql);
foreach ($dbo->query($sql) as $row) {
echo "<option value=\"" . $row['numero'] . "\"/>"; // Format for adding options
}
echo "</datalist>";
?>
NB: When you have a double quotes inside double quotes, you will need escape characters.
Some common php escape characters.
\" -Print the next character as a double quote, not a string closer
\' - Print the next character as a single quote, not a string closer
\n- Print a new line character
\t -Print a tab character
\$ -Print the next character as a dollar, not as part of a variable
\ -Print the next character as a backslash, not an escape character
echo "<input id=" numero "list=" posti1 "><datalist id=" posti1 " >";
when php try to read your code line , It sees it as above. it thinks only highlighted words are strings and specially those strings aren't connected by a .dot it throws the syntax error .
best way to fix this is as to do it according to the above answers .,
echo '<input id="numero" list="posti1" > <datalist id="posti1">';
Okay so i was wondering how i echo a variable from a database that i already have connected to a php script so basicly it would be like
echo '<img src="blablabla.com/VARIABLENAMEHERE" />'
Use double quotes, and then variables will be expanded inside the string:
echo "<img src='blablabla.com/$variable' />";
Or user string concatenation:
echo '<img src="blablabla.com/' . $variable . '" />';
you can use double quotes as the double quotes evaluates the variable inside.
echo "<img src='blablabla.com/$variablename' />";
but if you insist using single quote you can use concatenation.
echo '<img src="blablabla.com/' . $variablename . '" />';
I can't find a solution how to combine single and double quotes to echo HTML and call a function at the same time:
foreach ($result as $r) {
echo "<a href='get_permalink(get_page($r->id))'>".get_permalink(get_page($r->id)).'</a><br>';
}
Problem is this part is parsed as text, not php
"<a href='get_permalink(get_page($r->id))'>"
Cansome one help me to combine this? get_permalinks and get page are wordpress built in functions, so they should have function behavior
You can't call a function inside double " quotes.
foreach ($result as $r)
{
echo "<a href='".get_permalink(get_page($r->id))."'>".get_permalink(get_page($r->id)).'</a><br>';
}
It's not possible to run PHP code when it's inside a string (unless with eval). However, you can use printf() to separate code from string:
$url = get_permalink(get_page($r->id));
printf('%1$s<br>', htmlspecialchars($url, ENT_QUOTES, 'UTF-8'));
The %1$s is a positional format specifier; this is done so that the encoded $url value only has to be passed once.
Just concat the string like this:
echo "<a href='". get_permalink(get_page($r->id)) . "'>" . get_permalink(get_page($r->id)) . "</a><br>";
Also if you want to know what's the difference between single and double quotes see this:
What is the difference between single-quoted and double-quoted strings in PHP?
try this way:
if($result as $r)
{
echo "<a href='" . get_permalink(get_page($r->id)) . "'>" . get_permalink(get_page($r->id)) . '</a><br>';
}
I want to pass the php variable value in onClick function.
When i pass the php variable, in the UI i am getting the variable itself instead I need the value in the variable.
Below is code snippet, please help me.
<?php
print '<td>';
$node = $name->item(0)->nodeValue;
$insert= "cubicle"."$node<br>";
Echo '<a href= "#" onClick= showDetails("$node");>'. $insert .'</a> ';
print '</td>';
?>
Variable parsing is only done in double quoted strings. You can use string concatenation or, what I find more readable, printf [docs]:
printf('%s ', $node, $insert);
The best way would be to not echo HTML at all, but to embed PHP in HTML:
<?php
$node = $name->item(0)->nodeValue;
$insert = "cubicle" . $node;
?>
<td>
<a href= "#" onClick="showDetails('<?php echo $node;?>');">
<?php echo $insert; ?> <br />
</a>
</td>
You have to think less about quotes and debugging your HTML is easier too.
Note: If $node is representing a number, you don't need quotations marks around the argument.
you shouldn't be wrapping $node in '"':
Echo '<a href= "#" onClick= showDetails($node);>'. $insert .'</a> ';
If you want the value of $node to be in a string, thn i would do:
Echo '<a href= "#" onClick= showDetails("' . $node. '");>'. $insert .'</a> ';
$var = "Hello World!";
echo "$var"; // echoes Hello World!
echo '$var'; // echoes $var
Don't mix up " and ', they both have importance. If you use some " in your string and don't want to use the same character as delimiter, use this trick:
echo 'I say "Hello" to ' . $name . '!';
I think you are searching for PHP function json_encode which converts PHP variable into JavaScript object.
It's more secure than passing the value right in the output.
Echo '<a href= "#" onClick= showDetails("'.$node.'");>'. $insert .'</a> ';
I have been using curly braces lately instead of concatenation. I think it looks better/is more readable, and mostly I find it is easier and less prone to human error - keeping all those quotes straight! You will also need quotes around the contents inside of onClick.
Instead of this:
Echo '<a href= "#" onClick= showDetails($node);>'. $insert .'</a> ';
Try this:
Echo '{$insert} ';
As a side note, I usually use double quotes to wrap my echo statement and strictly use single quotes within it. That is just my style though. However you do it, be sure to keep it straight. So my version would look like this:
echo "<a href='#' onClick='showDetails({$node});'>{$insert}</a>";