How can i echo a variable from my database inside my echo? - php

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 . '" />';

Related

How do I escape quotes in URL?

I want to insert a URL all into single quotes. I have tried using both single and double quotes, but it is not working. I checked this question - Escaping single quote in URL link, but it did not help:
<?php
echo '\'' base_url().'\'index.php?admin/product/delete/1\'';
echo '\''; echo base_url().index.php?admin/product/delete/1\'';
echo '\'. base_url().index.php?admin/product/delete/1\'';
echo "\'"; echo base_url().'index.php?admin/product/delete/1\'";
?>
I am trying to achieve this:
'http://localhost/my_site/index.php?admin/product/delete/1'
The URL in single quotes.
If you really want to use only single quotes, try this:
echo '\'' . base_url() . 'index.php?admin/product/delete/1\'';
Just simple:
echo "'" . base_url() . "index.php?admin/product/delete/1'";
You can use double-quotes to pass variables into strings in PHP. On top of enhancing readability, this also saves you the trouble of escaping single quotes.
So for example:
<?php
$foo = 'bar';
echo 'single-quoted string: $foo <br />';
echo "double-quoted string: $foo <br />"; // "$foo" works, "{$foo}" is better
echo "double-quoted string: \$foo <br />"; // Backslash escapes
?>
Prints (emphasis mine):
single-quoted string: $foo
double-quoted string: bar
double-quoted string: $foo
So to update your example:
<?php
/* ... */
$baseurl = base_url();
echo "'{$baseurl}index.php?admin/product/delete/1'";
?>
Or how about we put it all into a variable and gain a little readability:
<?php
/* ... */
$baseurl = base_url();
$fullurl = $baseurl . 'index.php?admin/product/delete/1';
echo "'{$fullurl}'";
?>
echo "'" . base_url() . "index.php?admin/product/delete/1'";

How to handle special characters like semi colons and quotation marks in php

So I've created a CMS that takes text input. This is how the data is used when I grab it from the database.
echo "<img src=" . $row['image_url'] . " alt=" . $row['caption'] . ">";
Now the problem is, whenever there's a comma or a semi colon, php treats it as part of the code and the page ends up either not rendering well or completely breaking with errors like
Parse error: syntax error, unexpected '=' in
I've tried using htmlspecialcase() when posting the data to the MySQL database but it didn't fix the problem.
EDIT: The main problem is with the alt part not the src part.
When you're using double quotes (") to create a string literal and in that string literal you want to use double quotes ("), then you may escape those double quotes (") to form a valid string.
echo "<img src=\"" . $row['image_url'] . "\" alt=\"" . $row['caption'] . "\">";
You can try like that:
<img src="<?php echo $row['image_url'];?>" alt="<?php echo $row['caption'];?> ">

formatting a php variable into a web address

can you guys help me out with this, it should be easy but after 6 hours of work its frying my brain.
echo '<img src="', $webadd['data']['0']['address'], '"/>;
I just want it to be like:
<img src="http://www.image.com" />
the variable in the middle is the web address.
echo '<img src="'.$webadd['data']['0']['address'].'"/>';
echo "<img src='{$webadd['data']['0']['address']}'/>";
This should work:
echo '<img src="' . $webadd['data']['0']['address'] . '" />';
echo "<img src=\"" . $webadd['data']['0']['address'] . "\" />";
Consider adding spaces around the dots for readability.
You missed . by ,
Try this:
echo '<img src="'. $webadd['data']['0']['address'].'"/>;
or
echo "<img src=".'{$webadd['data']['0']['address']}'."/>";
TC lost to add qoutes to the end. Its works fine http://3v4l.org/5JCNc :)
$webadd['data']['0']['address'] = 'http://www.image.com';
echo '<img src="',$webadd['data']['0']['address'],'"/>';
See Example #1(separate by commas) from http://www.php.net/manual/en/function.echo.php.

using single and double quotes in php form

I ran into this code:
function input_text($elem, $val) {
print '<input type = "test" name="' . $elem .'" val="';
print htmlentities($val[elem]) . '"/>';
I m confused about the code: name="' . $elem .'" val="';
print htmlentities($val[elem]) . '"/>'
1) why put single quotes and dot inside double quotes around $elem? Can i just use double quotes like name="$elem".
2) what is the meaning of these code: val="';
print htmlentities($val[elem]) . '"/>'
1) Since the string being printed is surrounded with single quotes, variables are not expanded inside it; variables are only expanded inside double-quoted strings. So concatenation is necessary. If you change it to use double quotes, you could do variable interpolation:
print "<input type='test' name='$elem' val='";
2) There's no special meaning to it. The programmer simply chose to split up the commands to print this piece of HTML into two PHP print statements. So first he prints val=", then he prints htmlentities($val[elem]) . "">>'
The function could be rewritten as:
function input_text($elem, $val) {
print "<input type='test' name='$elem' val='" . htmlentities($val[elem]) . "'/>";
}
You have to use concatenation around htmlentities() -- only variables can be interpolated into strings, not function calls. However, you could assign the value to a variable first if you want:
function input_text($elem, $val) {
$valent = htmlentities($val[elem]);
print "<input type='test' name='$elem' val='$valent'/>";
}
BTW, $val[elem] looks like a typo, it probably should be $val[$elem].
The single quotes in this case denote a string in PHP.
$var = 'This is a String';
The reason they are used in conjunction with the double quotes is because the double quotes must be printed to get the correct HTML output of
<input type="test" name="someName" val="someValue" />
The . operator in PHP is the concatenation operator meaning combine 2 strings into 1.
$var = 'This' . ' and that'; //Evaluates to 'This and that'

Dynamic ID assigning

I have a problem with my page. I am displaying images using PHP Loop statement. Now I want to assign these images with different id's. Example first loop the first image displayed will have an id="img1", next loop and second image has id="img2". Numbers on the id changes based on the loop iteration variable, while "img" is constant. Here's my code:
for ($i=1;$i<=6;$i++){
echo ("<img src='gangjeong.png' width='113' id='img'.$i>");
}
but it's not working. Any help would be much appreciated.
UPDATE: I got it to work now, thanks for the answers. The working code is:
for ($i=1;$i<=6;$i++){
echo ("<img src='gangjeong.png' width='113' id='img$i'>");
}
You can't use unescaped single quotes if you start your string by single quotes.
These are the possibilities you have:
Using double quotes inside single quotes:
for ($i=0;$i<6;$i++){
echo ('<img src="gangjeong.png" width="113" id="img' . ($i+1) . '" />');
}
Using escaped single quotes inside single quotes (ugly):
for ($i=0;$i<6;$i++){
echo ('<img src=\'gangjeong.png\' width=\'113\' id=\'img' . ($i+1) . '\' />');
}
Using double quotes for starting/ending the string:
for ($i=0;$i<6;$i++){
echo ("<img src='gangjeong.png' width='113' id='img" . ($i+1) . "' />");
}
Using escaped double quotes inside double quotes (ugly):
for ($i=0;$i<6;$i++){
echo ("<img src=\"gangjeong.png\" width=\"113\" id=\"img" . ($i+1) . "\" />");
}
Quotes inside quotes are a no no without proper escaping. Also you have a missing > for the img tag. This should do:
for ($i=1;$i<=6;$i++){
echo "<img src='gangjeong.png' width='113' id='img".$i."'>";
}
Where's your escape characters? Your quotes need escaping, or put your HTML part in double quotes and PHP strings in single, or vice versa. Just make sure you're not confusing start and end quotes.
your syntax is not correct, use:
for ($i=1;$i<=6;$i++){
echo '<img src="gangjeong.png" width="113" id="img'.$i.'" alt="">';
}
Use:
for ($i=1;$i<=6;$i++){
echo "<img src=\"gangjeong.png\" width=\"113\" id=\"img$i\">";
}

Categories