I am trying to include a piece of text from a config file into a php file that generates a random number of tags from a seperate list of tags.
Code to generate the tags :
<?php
include '../config/filenames.php';
$keywords = explode(",", file_get_contents("taglist.php"));
shuffle($keywords);
$keys = array();
foreach (array_slice($keywords, 0, 20) as $word) {
$word = trim($word);
$keys[] = $word;
}
echo join(',', $keys);
?>
This is the taglist :
<a href='<?php echo $filename_a?>'>test</a>,
<a href='<?php echo $filename_a?>'>test2</a>,
<a href='<?php echo $filename_a?>'>test3</a>,
<a href='<?php echo $filename_a?>'>test4</a>
For some reason it is not including the $filename_a. It looks like the result of the random data is not parsing the php, but simply printing the piece of php code rather then the result of it.
Can someone help ?
Related
I have a question about " and ' in PHP. I have to put a complete <li> element into a PHP variable but this doesn't work, the output is completely false...
$list =
"
<li class=\"<?php if($info[1] < 700) {echo \"half\";} else {echo \"full\";} ?>\">
<div class=\"onet-display\">
<?php if ($image = $post->get('image.src')): ?>
<a class=\"onet-display-block\" href=\"<?= $view->url('#blog/id', ['id' => $post->id]) ?>\"><img class=\"onet-thumb\" src=\"<?= $image ?>\" alt=\"<?= $post->get('image.alt') ?>\"></a>
<?php endif ?>
<h1 class=\"onet-thumb-title\"><?= $post->title ?></h1>
<div class=\"uk-margin\"><?= $post->excerpt ?: $post->content ?></div>
</div>
</li>
";
Is it because there is PHP Content in the HTML Code? How can I solve this?
Can someone help me and explain why this doesn't work?
<?php ... <?php
Since your string contains PHP tags, I suppose you expect them to be evaluated. The opening PHP tag within another PHP tag is interpreted as a part of the PHP code. For example, the following outputs <?php echo time();:
<?php echo "<?php echo time();";
There are several ways to build a PHP string from PHP expressions.
Concatenation
You can create functions returning strings and concatenate the calls to them with other strings:
function some_function() {
return time();
}
$html = "<li " . some_function() . ">";
or use sprintf:
$html = sprintf('<li %s>', some_function());
eval
Another way is to use eval, but I wouldn't recommend it as it allows execution of arbitrary PHP code and may cause unexpected behavior.
Output Buffering
If you are running PHP as a template engine, you can use the output control functions, e.g.:
<?php ob_start(); ?>
<li data-time="<?= time() ?>"> ...</li>
<?php
$html = ob_get_contents();
ob_end_clean();
echo $html;
Output
<li data-time="1483433793"> ...</li>
Here Document Syntax
If, however, the string is supposed to be assigned as is, use the here document syntax:
$html = <<<'HTML'
<li data-time="{$variable_will_NOT_be_parsed}">...</li>
HTML;
or
$html = <<<HTML
<li data-time="{$variable_WILL_be_parsed}">...</li>
HTML;
You want to store some html into a variable.
Your source should (if not yet) start with
<?php
Then you start building the contents of $list.
Starting from your code the nearest fix is to build $list by appending strings:
<?php
$list = "<li class=";
if($info[1] < 700)
{
$list .= "\"half\""; // RESULT: <li class="half"
}
else
{
$list .= "\"full\""; // RESULT: <li class="full"
}
// ...and so on...
Now a couple things to note:
$list .= "... is a short form of $list = $list . "...
Where the . dot operator joins two strings.
Second thing you may make code easier to read by mixing single and double quotes:
Use double quotes in PHP and single quotes in the generated HTML:
<?php
$list = "<li class=";
if($info[1] < 700)
{
$list .= "'half'"; // RESULT: <li class='half'
}
else
{
$list .= "'full'"; // RESULT: <li class='full'
}
// ...and so on...
This way you don't need to escape every double quote
i think you have to work like this
$test = "PHP Text";
$list = "<strong>here ist Html</strong>" . $test . "<br />";
echo $list;
I'm trying to display all the images with the same key from a folder.
the images are stored like so:
34526Image1.jpg
34526Image2.jpg
34526Image3.jpg
34526Image4.jpg
etc etc....
so the key is the first part of the image name (the random numbers) which in this case is 34526.
I've tried to use glob() function in PHP but I only get the first image which is 34526Image1.jpg.
my glob() code is this:
<?php
foreach(glob('../my-images/') as $image)
$i = 1;
{
$pic_list .= '<a id="example1" href="'.$image.''.$randKey.'Image'.$i++.'.jpg"><img alt="example1" src="../my-images/'.$randKey.'Image'.$i++.'.jpg" /></a>';
}
echo $pic_list ;
?>
could someone please advise on this?
Your call to glob contains no pattern. You can use wildcards for no or many characters with *, or for single characters using ?. Also ranges like for example [0-9] are possible. In your case you want everything that starts with a random number, determinted by $randKey, followed by the word Image, a counter value and .jpg. So all you have to do is to use a wildcard for the counter value like in your example 34526Image*.jpg.
This results in the following code
<?php
$pic_list = '';
$id = 0;
foreach(glob('../my-images/'.$randKey.'Image*.jpg') as $image) {
$pic_list .= '<a id="example'.++$id.'" href="'.$image.'"><img alt="example'.$id.'" src="'.$image.'" /></a>';
}
echo $pic_list;
?>
you have error in your foreach statement, for each file in folder you execute only 1 code line $i = 1;, everything after ; is executed only one time, after all iterations of foreach
you need to use proper foreach:
$files = glob('../my-images/'); // need to define proper mask here to get only files with $randKey
$i = 1;
foreach($files as $image)
{
$pic_list .= '<a id="example1" href="'.$image.''.$randKey.'Image'.$i.'.jpg">';
$pic_list .= '<img alt="example1" src="../my-images/'.$randKey.'Image'.$i.'.jpg" />';
$pic_list .= '</a>';
$i++;
}
Faced a problem when working on .xml file parsing in PHP
What can i put in the ancher tag in that case :
<?php
foreach ($xml->issue as $issue ) {
echo '<a name="$issue->id"></a>';
//rest of code
}
<a href="# "> //i dont know what to put after the "Diese".
?>
i want to scroll to an element inside the loop .
First, your <a href> tag is empty. And because of that, you won't see any links on your page:
TEXT HERE!!!
Second, in PHP, it is not valid to introduce variables in single quoted strings, like you did. Iterpreter will not recognize them as variables and therefore will not convert them to their values. My solution (take a look at quoting style):
foreach ($xml->issue as $issue ) {
echo '<a name="'.$issue->id.'">some link</a>';
}
You are using quotes and double-quotes wrong.
<?php
foreach ($xml->issue as $issue)
{
// This will work - '.$var.'
echo '';
// And this
echo "";
// And this
echo "";
// These will fail
echo '';
echo '';
echo '';
}
?>
<?php echo strip_tags(stripslashes($row = implode(['meta_keys']))) ?>
all it shows is "meta_keys" as display, and not the info from the database.
I'm trying to get each key individually linked on an <a href=""> tag.
I know I need to array, but any ideas?
$meta_keys = explode(',', $row['meta_keys']);
foreach ($meta_keys as $key) {
echo strip_tags($key) . ' ';
}
Hello I am trying to create the lightbox gallery in my WordPress, I managed to get most of the code working well for grabbing images out of gallery. However I don't know how to get their URL for their original files....
PHP
<?php $post_content = get_the_content();
preg_match('/\[gallery.*ids=.(.*).\]/', $post_content, $ids);
$array_id = explode(",", $ids[1]);
$item = 0;
while ($array_id[$item] != ''){
if($item == 0){
echo wp_get_attachment_image($array_id[$item], 'medium');
}else{
echo "<a ref='".wp_get_attachment_image($array_id[$item])."'>";}
$item++;
}
?>
So my idea is to display IMG of a first image in the gallery which I have accomplished with the first ECHO, not for all other images I only want the links to the original files.
My final code needs to look like this:
<a rel="group1" href="image_big_1.jpg"><img src="image_small_1.jpg" alt=""/></a>
<a rel="group1" href="image_big_3.jpg"></a>
<a rel="group1" href="image_big_4.jpg"></a>
<a rel="group1" href="image_big_5.jpg"></a>
<a rel="group1" href="image_big_6.jpg"></a>
Also what would you suggest for rel='' I need this to be unique for every post with galleries I am not sure what to use I can't use TITLE because if they enter more then one gallery with same title there will be issue. I am guessing maybe some sort of (RANDOM) prefix defined by current_time?
Please help
Thank you
I found a solution and edited my code to work:
This is code to accomplish above stated:
<?php $post_content = get_the_content();
preg_match('/\[gallery.*ids=.(.*).\]/', $post_content, $ids);
$array_id = explode(",", $ids[1]);
$item = 0;
while ($array_id[$item] != ''){
$url = wp_get_attachment_image_src($array_id[$item]);
if($item == 0){
echo "<a href='".$url[0]."'>".wp_get_attachment_image($array_id[$item], 'gallery-thumb')."</a>";
}else{
echo "<a href='".$url[0]."'</a>";}
$item++;
}
?>
Reference used: http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src
Thank you all!