I'm trying to prepare a url that has special characters in it, to be used as GET variables in a php file. I think I need html entities and urlencode, which I then need to decode in the other php file. But I'm running into some problems with correctly encoding them.
This is what I have:
<?php $title = "This is a ‘simple’ test"; ?>
<?php $titleent = htmlentities($title); ?>
<?php $titleentencoded = urlencode($titleent); ?>
<?php $date = '21-12-2011'; ?>
<p>Title: <?php echo $title; ?></p>
<p>Title html entities: <?php echo $titleent; ?></p>
<p>Title encoded: <?php echo $titleencoded; ?></p>
<p>Go!</p>
The $titleencoded variable turns out to be empty. I'm overlooking something obvious but I can't see it. What am I doing wrong?
EDIT: New code after suggestions
Okay, so here's what I came up with:
<?php $title = "This is a ‘simple’ test"; ?>
<?php $titleentencoded = urlencode($title); ?>
<?php $htmlent = htmlentities($titleentencoded); ?>
<?php $date = '21-12-2011'; ?>
<p>Title: <?php echo $title; ?></p>
<p>Title encoded: <?php echo $titleentencoded; ?></p>
<p>Title html entities: <?php echo $htmlent; ?></p>
<p>Go!</p>
Is this the right way?
Your variable is empty because you have a typo. You initialize $titleentencoded but later use $titleencoded:
<?php $titleentencoded = urlencode($titleent); ?>
// Should be
<?php $titleencoded = urlencode($titleent); ?>
See #Quentin's answer for a logic error.
You are doing it backwards.
You are putting data in a URL, then a URL in an HTML document.
You need to urlencode the data, put it in the URL then htmlencode the URL and put it in the document.
Related
I have field collections within an entity and I'm trying to theme the values but having a hard time saving some of those values as variables. In the code below, I am trying to print the value for 'field_area_headline' but getting an 'undefined index' notice in my browser. I've included an image of my browser where I have dpm-'d my variables that will likely be a clue for those who know what to look for.
What am I doing wrong? Thanks in advance!
<?php foreach($variables['field_focusareas'] as $delta => $item) : ?>
<?php $focus_area_node = $item['entity']; ?>
<?php foreach($focus_area_node->field_area as $focus_delta => $area) : ?>
<!-- <?php dpm($area); ?> -->
<?php $focus_area = entity_load('field_collection_item', array($area[0]['value']));
dpm($focus_area);?>
<?php $headline = $focus_area['field_area_headline'][LANGUAGE_NONE][0]['safe_value']; ?>
<h3><?php print $headline; ?> </h3>
<span></span>
<?php endforeach; ?>
<?php endforeach; ?>
$focus_area is an object, not an array. You need to retrieve an object property - $headline = $focus_area->field_area_headline[LANGUAGE_NONE][0]['safe_value'];
IN PHP I noticed that if we have code like below:
<?php if ( function('parameter')):?>
<?php //do something here ?>
<?php endif; ?>
why can't we write this code like:
<?php if ( function('parameter'))
//do something here
endif; ?>
I am new to PHP, Thanks a lot!!
The PHP code has to be inside <?php ?> and the HTML markup needs to be outside. You can also print out the HTML markup with echo.
Here is an example (much cleaner in my opinion, than example 2). The HTML markup is inside a PHP string. The return value of the_field(), a string, is then concated with .:
<?php
the_post_thumbnail('square');
if(get_field('quote_url')) {
echo '<p class="btn">Request a Quote</p>';
}
if(get_field('rfq_pdf_url')) {
echo '<p class="btn">Download PDF</p>';
}
?>
And here is another valid example (2). You can end the PHP part with ?> and output regular HTML markup and then start the PHP part again with <?php:
<?php
the_post_thumbnail('square');
if(get_field('quote_url')) { ?>
<p class="btn"><a href="
<?php the_field('quote_url'); ?>
">Request a Quote</a></p>
<?php }
if(get_field('rfq_pdf_url')) { ?>
<p class="btn"><a href="
<?php the_field('rfq_pdf_url');?>
">Download PDF</a></p>
<?php }
?>
It would however be redundant to start with <?php on every line and end it then again with ?>.
Another possibility would be:
<?php
the_post_thumbnail('square');
if(get_field('quote_url')) {
?>
<p class="btn"><a href='<?php echo the_field('quote_url'); ?>'>Request a Quote</a></p>
<?php
}
if(get_field('rfq_pdf_url')) {
?>
<p class="btn">Download PDF</p>
<?php
}
?>
I want to print the div with the class called inner-content-div, if the variable $name is not consisted with the following strings.
Gingelly Rolls
Kithul Treacle
Coconut Vinegar
But my PHP code is not working.
Here is my code.
<?php
$vid = explode("/", $_GET["q"]);
$name = taxonomy_term_load($vid[2]);
?>
<h1 id="page-title" class="title"><?php print $name->name; ?></h1>
<?php
$exclude_list = array("Gingelly Rolls","Kithul Treacle","Coconut Vinegar");
if(!in_array($name, $exclude_list)){ ?>
<div class="inner-content-div">
<!-- Here are some HTML code.-->
</div>
<?php
}
?>
It should be -
if(!in_array($name->name, $exclude_list)){
as you are printing it -
<h1 id="page-title" class="title"><?php print $name->name; ?></h1>
<?php
$vid = explode("/", $_GET["q"]);
$name = taxonomy_term_load($vid[2]);
$exclude_list = array("Gingelly Rolls","Kithul Treacle","Coconut Vinegar");
?>
<h1 id="page-title" class="title"><?php echo $name->name; ?></h1>
<?php if(!in_array($name->name, $exclude_list)): ?>
<div class="inner-content-div">
<!-- Here are some HTML code.-->
</div>
<?php endif; ?>
It could be because $name is an object. Also, probably better to use PHP's alternate syntax for cleaner html/templates.
The other potential issue here is that the casing of the items in the exclude list could potentially not match the value of name. You should probably normalize those to all lower case when doing the comparison.
you can try like this. First print $vid[2], using echo $vid[2];. Then put $vid[2] values into the array called $exclude_list. Then use the following code.
$exclude_list = array(//value 1, value 2, value 3);
if(!in_array($vid[2], $exclude_list)) {
//Your HTML code
}
I've created a simple query to create a list of dates and descriptions from my calendar database.
The idea is that I have a PHP webpage that show a text list that I can quickly copy and paste into an email or text message.
My problem is that, although the text shows up correctly on the web page, when I paste the info into a text editor (Word, email, whatever) I'm getting tabs between each column.
How can I format the text in PHP so that it pastes correctly?
This is my code:-
if(mysql_num_rows($AvDates) > 0){
?>
<ul>
<?php
while ($row_AvDates = mysql_fetch_assoc($AvDates)){
?>
<li>
<?php echo htmlentities($row_AvDates['Month']);?>
<?php echo "-";?>
<?php echo htmlentities ($row_AvDates['the_days']);?>
</li>
<?php
}
?>
</ul>
<?php
}
?>
This gives me output that looks correct, but pastes like this...
(Month[tab]"-"[tab] Dates)
How do I loose the tabs?
You're closing php and letting the HTML output be rendered. See tab chars below:
[tab]<li><?php echo htmlentities($row_AvDates['Month']);?>
[tab]<?php echo "-";?>
[tab]<?php echo htmlentities ($row_AvDates['the_days']);?></li>
Solution:
<?php echo htmlentities($row_AvDates['Month']) . '-' . htmlentities ($row_AvDates['the_days']); ?>
Darren.
Try to use this code, it should solve your problem:
<?php if (mysql_num_rows($AvDates) > 0) : ?>
<ul>
<?php while ($row_AvDates = mysql_fetch_assoc($AvDates)) : ?>
<li><?php echo htmlentities($row_AvDates['Month'])."-".htmlentities ($row_AvDates['the_days']);?></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
Hope it helps.
you can have the whole output in one single like this
<?php echo htmlentities($row_AvDates['Month']) . '-' . htmlentities($row_AvDates['the_days']);?>
instead of separate php tags
If you want to separate code across multiple lines, not all code in one line:
if(mysql_num_rows($AvDates) > 0){
?>
<ul>
<?php
while ($row_AvDates = mysql_fetch_assoc($AvDates)){
?>
<li><?php
echo htmlentities($row_AvDates['Month']);
echo "-";
echo htmlentities($row_AvDates['the_days']);
?></li>
<?php
}
?>
</ul>
<?php
}
?>
I'm trying to call an HTML/PHP content that it's inside my database using:
<?php echo $row_content['conteudo']; ?>
When the row is called the HTML appears correctly but the PHP doesn't.
I belieave it's cause of the echo inside the main echo.
<?php echo "
<h3>Hello</h3>
<?php do { ?>
<div class=\"indios\">
<a href=\"indio.php?id=<?php echo $row_indiosct['id']; ?>\">
<img src=\"galeria/indios/<?php echo $row_indiosct['foto']; ?>\" alt=\"<?php echo $row_indiosct['nome']; ?>\" />
<br /><?php echo $row_indiosct['nome']; ?></a></div>
<?php } while ($row_indiosct = mysql_fetch_assoc($indiosct)); ?> "
?>
The line one of this code is the same echo as the first code field, it's not repeating, it's there just for help and to understand where is the problem.
I already fixed some quotation marks but it gives an error in the line of the 1st echo.
That is some of the ugliest code I have ever seen...
<?php
echo '
<h3>Hello</h3>';
while ($row_indiosct = mysql_fetch_assoc($indiosct))
{
echo '
<div class="indios">
<a href="indio.php?id='.$row_indiosct['id'].'">
<img src="galeria/indios/'. $row_indiosct['foto'].'" alt="'.$row_indiosct['nome'].'" />
<br />'.$row_indiosct['nome'].'</a>
</div>';
}
?>
You could also use the HEREDOC syntax.
Don't do this. Multi-line echoes, especially when you've got embedded quotes, quickly become a pain. Use a HEREDOC instead.
<?php
echo <<<EOL
<h3>Hello</h3>
...
<div class"indios">
...
EOL;
and yes, the PHP inside your echo will NOT execute. PHP is not a "recursively executable" language. If you're outputting a string, any php code embedded in that string is not executed - it'll be treated as part of the output, e.g.
echo "<?php echo 'foo' ?>"
is NOT going to output just foo. You'll actually get as output
<?php echo 'foo' ?>
You have misunderstood how PHP works. PHP is processed by the server. When it encounters your script, it sees the following:
<?php echo "some long piece of text that you have told PHP not to look at" ?>
What is the reasoning behind trying to nest PHP calls inside strings?
evaluate code php in string using the function eval(): this post Execute PHP code in a string
<?php
$motto = 'Hello';
$str = '<h1>Welcome</h1><?php echo $motto?><br/>';
eval("?> $str <?php ");
http://codepad.org/ao2PPHN7
also if your need the code buffer output in a string also you can using the ob_start() method:
<?php ob_start(); ?>
<h3>Hello</h3>;
<?php
while ($row_indiosct = mysql_fetch_assoc($indiosct)){ ?>
<div class="indios">
<a href="indio.php?id='<?php echo $row_indiosct['id']'">
<img src="galeria/indios/'<?php echo $row_indiosct['foto'].'" alt="'.$row_indiosct['nome'].'" />
<br />'.$row_indiosct['nome'].'</a>
</div>';
<?php } ?>