Most sensible approach to generate repeated HTML source. Use PHP? [closed] - php

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed last month.
Improve this question
Hi I was wondering what is a sensible approach to generating repeated HTML source code such as that below. Suppose I have, say, thirty, lines where the numbering goes from 1 to 30, say. And I have a number of such pages that need this structure. I had jumped to generating this code in the page itself using a PHP loop, as I'm using PHP anyway in the project.
My question is, is this a sensible approach? What is best practice in such cases? I'm not sure using PHP in the page to generate the HTML each time like this is wise. Or is it?
Thank you for any steer or advice.
<a href="images/mypic (1).jpg">
<img src="images/mypic (1).jpg" alt="Picture 1">
</a>
<a href="images/mypic (2).jpg">
<img src="images/mypic (2).jpg" alt="Picture 2">
</a>
etc..

That's precisely the purpose of PHP : generating HTML code.
You can use a simple for loop :
<?php for($i=1; $i<=30; $i++) : ?>
<a href="images/mypic (<?= $i ?>).jpg">
<img src="images/mypic (<?= $i ?>).jpg" alt="Picture <?= $i ?>">
</a>
<?php endif; ?>

Why not JS? That would transfer the least code from the server
document.body.innerHTML = Array.from({length:30})
.map((_,i) => `<img src="images/mypic%20(${(i+1)}).jpg" alt="Picture ${(i+1)}">`)
.join('<br/>')

Related

PHP img src (simple) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am trying to add an image file to my PHP so when I echo, the picture will appear alongside the message. However I am getting a syntax error on line 3. Any help would be appreciated.
<?php
echo "President has been killed";
<IMG SRC = "D:/User Data\Documents/Sheridan/Summer Year 3/Enterprise Java Development/Projects/PhpAssignment/skull.png;"/>
?>
<?php
echo 'President has been killed
<img src="D:/User Data\Documents/Sheridan/Summer Year 3/Enterprise Java Development/Projects/PhpAssignment/skull.png;" /> ';
?>
Note the change in quotes -- single and double -- and placement of the semi-colon.
or
<?php
echo "President has been killed";
?>
<img src="D:/User Data\Documents/Sheridan/Summer Year 3/Enterprise Java Development/Projects/PhpAssignment/skull.png;" />
Move the image outside the php. Using this depends on overall markup though.
The semi-colon ends, or stops the echo statement.
Of course there's an syntax error. You are trying to output HTML inside PHP block:
Change your code to:
<?php
echo "President has been killed";
?>
<IMG SRC = "D:/User Data\Documents/Sheridan/Summer Year 3/Enterprise Java Development/Projects/PhpAssignment/skull.png;"/>
<?php
echo "President has been killed";
<IMG SRC = "D:/User Data\Documents/Sheridan/Summer Year 3/Enterprise Java Development/Projects/PhpAssignment/skull.png;"/>
?>
firstly you don't need the entire location, place the image in the folder in your root called images then place the image in there.
<?php
echo '<p>President has been killed<p><img src = "D:/User Data\Documents/Sheridan/Summer Year 3/Enterprise Java Development/Projects/PhpAssignment/skull.png"/>';
?>

PHP HTML {$variable} [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How can i print variable in brackets? Like ip board, for example I have this code:
<?php
$variable = "test";
?>
<div class="test">{$variable}</div>
How can I do this? Not
echo $variable;?>
but {$variable} ! Thanks in advance :3
It's called concatenation and it uses '.' to string things together...
<div class="test"><?php echo '{'.$variable.'}' ?></div>
you can use it like this
<?= $variable ?>
Use a php templating engine. Best for you is that use Laravel and Balde
yes, you can just do it like
<?php
$variable = "test";
?>
<div class="test"><?= $variable; ?></div>
but if you want to use above approach(your one) you've to use blade templating engine
have a look

add space in html when using php echo [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am writing a html class attribute which need space to allow multiple values. However, among these values, there are variables and constants. Code shows below:
<article id="portfolio-item-1" data-loader="include/ajax/portfolio-ajax-image.php" class="value1 value2">
Here I want value1 in the class to be a variable like $itemTitle, but still to maintain the "value2" with a space between them.
I have tried many method like add &nbsp and using
echo $itemName." "."value2";
but seems didn't work.
Any ideas?
Thanks!
Solved:
Solution1:
<article id="portfolio-item-1" data-loader="include/ajax/portfolio-ajax-image.php" class="<?php echo "$item->category_id"." "."value0"; ?>" >
Solution2:
<article id="portfolio-item-1" data-loader="include/ajax/portfolio-ajax-image.php" class="<?php echo "$item->category_id Value0"; ?>" >
Thanks!
<?php
echo "$itemName value2";
?>
If you use double quotes, you can drop the variable right in there, no concatenation needed!
<article id="portfolio-item-1" data-loader="include/ajax/portfolio-ajax-image.php" class="<?php echo "$itemName value2"; ?>">
It is not an error to have a space without a class on each side.
echo '<article... class="'.$itemName.' value2"...
$id = 'portfolio-item-1';
$url = 'include/ajax/portfolio-ajax-image.php';
echo '<article id="'.$id.'" data-loader="'.$url.'" class="'.$itemName.' value2">';
will work, for instance.

Replace with php each font size="" to font-size: ; how to? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want to replace each occurency of font size="somenumber" (the number every time is different!) with the font-size="somenumber".
How to make this with php?
I have a text that looks like this:
Hello <font size="4">today</font> is my
<font size="3">special day</font> and am
<font size="11">ready</font> for it...
And i want to convert it into this:
Hello <span style="font-size:4;">today</span> is my
<span style="font-size:3;">special day</span> and am
<span style="font-size:11;">ready</span> for it...
You can use preg_replace(). http://php.net/manual/en/function.preg-replace.php
$str = 'Hello <font size="4">today</font> is my <font size="3">special day</font> and am <font size="11">ready</font> for it...';
preg_replace('/<font size="(\d+)">(.+?)<\/font>/', '<span style="font-size:$1;">$2</span>', $str)
Output:
Hello <span style="font-size:4;">today</span> is my <span style="font-size:3;">special day</span> and am <span style="font-size:11;">ready</span> for it...
Just a little hint to get you started: you need to check the XML Manipulation chapter in the PHP manual, more specifically the Document Object Model extension:
DOMDocument::getElementsByTagName() will help you find the <font> tags
DOMElement::getAttribute() will let you read the size attribute
And DOMElement::setAttribute() will let you set a new value
Happy coding!
P.S. I suppose you're working with third-party HTML but you should be aware that presentation tags like <font> were replaced by something better when CSS was released in 1996.

how to wrap inside <a href> all URL occurrence in my query [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
alright, i think the solution to my question is to use REGEX, but my current knowledge of it hinders me to achieve the result that i want.
so basically, how do i make all URL occurrence in my posts/text entries to wrap inside an <a href> element when being queried.
when i echo $content; from a database it will display as:
this is a link http://somerandomsite.com, check it out!
but how can i make the link inside a text to be contained in <a href> tag when displayed.
id be honest to say that i dont know how to properly write the syntax. but this is what ive come up with.
//get url from content
$link = strstr($content, 'http://');
//insert <a href> for all $link inside $content
//then display $content with all links wrapped in <href>
$a = 'this is a link http://somerandomsite.com, check it out!';
$a = preg_replace("/\b((?:http\:\/\/)|(?:www\.))([^ ,]+)/", "$1$2", $a);
echo $a;

Categories