-1

i'm writing because i need someone who can explain to me some code. I'm learning to coding on website codeavengers.

I'm doing an exercise where I have a page called index.php and 3 html pages called: 1.html, 2.html, 3.html

Each html page have this structure (with different data):

title: Responsive web design
image: /images/blog/responsive-design.png
author: Jenna Walmsley
<p> Lorem ipsum dolor </p>
<p> Lorem ipsum dolor sit amet </p>

In the exercise i have to use a php function called create_index() who basically read this 3 files and output on my webpage 3 div with this structure:

<div>
   <h1>Responsive web design:</h1>
   <img data-src="/images/blog/responsive-design.png" src="/images/blog/responsive-design.png">
   <p> Lorem ipsum dolor </p>
   <p> Lorem ipsum dolor sit amet </p>
</div>

Unlucky in the exercise is not written or explained the code of this function. Somebody can explain me with code how this function works? I'm really interested.

thanks in advance

UPDATE1 (This is the text of exercise in codeavengers)

Setting up an index page with create_index()

WordPress has many stored PHP commands that behave like plug-ins, adding features and functionality to a blog. We surround a command with PHP script tags <?php ?> to use that command.

One example of a WordPress command is:

<?php get_header_image(); ?> This is used to retrieve the header image for a custom header.

We've created some similar commands for you to use in building your blog.

To set up your index page, you will use a command that we have created: create_index(). Basically, this command will search through all the files in your blog folder, retrieve the values for title, image, and content and display them neatly in index.php.

On line 2 type the words Blog Index and surround them with <title> tags.
On line 3 add a stylesheet <link> tag. Set the href attribute to blog.css.
On line 5 add a PHP require statement to include your header in index.php.
On line 7 add a PHP statement with the create_index() command.
On line 9 add a PHP include statement to include the footer in the file.

code avengers index page

code avengers detail html

luke
  • 1
  • 1
  • Check this link https://stackoverflow.com/questions/9813273/web-scraping-in-php – Ravinder Reddy Oct 03 '17 at 20:20
  • 2
    `create_index()` is not a native PHP function. I think it is your task to _write_ this function to achieve the requested functionality. – Patrick Q Oct 03 '17 at 20:26
  • it's a codeavengers function, the task of the exercice is just to use this function and write em on index.php. But I don't want only use them, I want understand how this function works and how she get data from other files – luke Oct 03 '17 at 20:30
  • There's no way for us to tell you how code we can't see works – Patrick Q Oct 03 '17 at 20:34
  • @RavinderReddy This isn't web scraping. It's creating a web page from data in plain text files. – Barmar Oct 03 '17 at 20:55
  • @luke Can you post the full codeavengers problem description? Without seeing it, I suspect Patrick Q is correct that this is a function you're supposed to write, not one that already exists. – Barmar Oct 03 '17 at 20:58
  • @Barmar updated the post with te task asked in codeavengers – luke Oct 03 '17 at 21:10
  • The instructions tell you exactly what you're supposed to do. You don't need to get the data from other files, that's what `create_index()` does for you. – Barmar Oct 03 '17 at 21:15
  • @Barmar i know, I just wanted to understand for personal knowledge how this function works – luke Oct 03 '17 at 21:33
  • @luke Then try writing the function yourself. If you can't get it working, post what you tried, and we'll help you understand where you went wrong. That's how you learn programming. – Barmar Oct 04 '17 at 18:18

1 Answers1

0

You should probably use a loop, I suggest using foreach, e.g.

foreach(array("1.html", "2.html", "3.html") as $file) {

}

You can easily get the content of the individual file by using

$content = file_get_contents($file);

Now that you have the whole file in one string, you might split it into individual lines with explode(). Then go through those lines and extract the information. Again, explode() should work for you (there are more elegant ways, but they're not that beginner friendly).

As a starter:

foreach(array("1.html", "2.html", "3.html") as $file) {
    $content = file_get_contents($file);
    // extract data from $content here
    ?>
    <div>
    <h1><?php echo $title; ?></h1>
    <img data-src="<?php echo $image; ?>" src="<?php echo $image; ?>">
    <?php echo $html; ?>
    </div>
    <?php
}

The hard part is getting $title, $image and $html out of $contents. Give it a try and see what you can do. I trust you know php's online documentation at https://php.net/? Look up the functions I named to get a better understanding of what they do.

Barmar
  • 741,623
  • 53
  • 500
  • 612
janh
  • 2,885
  • 2
  • 22
  • 21