Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Konrad Pilch
Konrad Pilch
2,435 Points

PHP News feed, adding dynamicly

HI,

Im trying to do what i learned but ti seem that it doesnt work.

Heres my code :

HTML

<?php 

$posts = array();
$posts[100] = array(
    "heading" => "Car for sell",
    "paragraph" => "This car is brand new",
    "price" => £900 ,
    "img" => "img/fb.png"
);

?>

<!DOCTYPE html>

<html lang="en">
<head>

  <title>Trying PHP News Feed</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
  <!-- Custom CSS -->
    <link href="includes/css/styles.css" rel="stylesheet">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
</head>

  <body>

        <div class="container">
            <div class="row">
                <div class="col-md-9">
                    <div class="row">

                        <h1> Shirt Details </h1>

                        <ul class="posts">
                            <?php foreach($posts as $post){

                            echo    "<li>";
                            echo        '<h3>"$post["heading"]"</h3>';
                            echo        '<h5>$post["price"]</h5>';
                            echo        '<p> $post["paragraph"] </p>';
                            echo        '<img src="">';
                            echo    "</li>";

                                }
                            ?>
                        </ul>

                    </div><!-- /row -->
                </div><!-- /col-md-9 -->
                <div class="col-md-3">
                    <h1> Add info </h1>
                </div>
            </div><!-- /row -->
        </div><!-- /contaiern -->





    </body>
</html>

CSS:

.posts{
   border:2px solid black;
   margin:5px 0;
}
.posts img{
   float:left;
}

1 Answer

Sorry about that. I didn't pay attention lol What I would recommend doing is concatenating the variables within the echo statement like so:

echo        '<h3>"'.$post["heading"].'"</h3>';
echo        '<h5>'.$post["price"].'</h5>';
echo        '<p>'.$post["paragraph"].'</p>';

That will echo the right output to your page!

Konrad Pilch
Konrad Pilch
2,435 Points

wow it works but im wondering, where is my reply, i didnt delete any.

So how does this works? i though concatenating is only needed when two or more elements are used. Or is this concatenating with the heading tag?\

Thank you very much!

I deleted my answer so that no one else would get confused about what I had said since it was very wrong. Sorry, I didn't mean to erase your comment, as well.

I would concatenate anytime you use a PHP variable in your output. Someone else can probably give you more details as to why, but I do know that concatenation of strings and variables will make sure that you always output the correct information.

Konrad Pilch
Konrad Pilch
2,435 Points

Aaa okay ^^ i thought i wrote a answer and then some bug deleted it all : p

okay all use it .

Also one quick question. Do you know how could i now connect it with input fields? i mean so in the input user type something and then when press the button , it adds a new post ?

Take a look at the additions I made to your PHP page. I added a little bit under the PHP section and around the last echo statement. Then, I just added a simple form to the bottom of the page:

<?php 

$posts = array();
$posts[100] = array(
    "heading" => "Car for sell",
    "paragraph" => "This car is brand new",
    "price" => 900,
    "img" => "img/fb.png",
    "typeCar" => ""
);

if (isset($_GET['typeCar'])) {
    $posts[100]['typeCar'] = $_GET['typeCar'];
}

?>

<!DOCTYPE html>

<html lang="en">
<head>

  <title>Trying PHP News Feed</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
  <!-- Custom CSS -->
    <link href="includes/css/styles.css" rel="stylesheet">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
</head>

  <body>

        <div class="container">
            <div class="row">
                <div class="col-md-9">
                    <div class="row">

                        <h1> Shirt Details </h1>

                        <ul class="posts">
                            <?php foreach($posts as $post){

                            echo    "<li>";
echo        '<h3>"'.$post["heading"].'"</h3>';
echo        '<h5>'.$post["price"].'</h5>';
echo        '<p>'.$post["paragraph"].'</p>';
    if (isset($_GET['typeCar'])) {
echo        '<p>'.$post["typeCar"].'</p>';
    }
                            echo        '<img src="">';
                            echo    "</li>";

                                }
                            ?>
                        </ul>

                    </div><!-- /row -->
                </div><!-- /col-md-9 -->
                <div class="col-md-3">
                    <h1> Add info </h1>
                </div>
            </div><!-- /row -->
        </div><!-- /contaiern -->


<form action="index.php" method="get">
    <input type="text" name="typeCar">
    <input type="submit" value="Submit!">
</form>


    </body>
</html>
Konrad Pilch
Konrad Pilch
2,435 Points

Wooow xD :D this is so amazing!!! Thank you very much ! I beieve is should make some more of these and then get over the more complex stuff of accounts , in other words , carry on with the second Randy tutorial xd

One more question , hope im not annojing you . Is this the way the feeds will work?

Like now i have the form inputs and if i log in to facebook and i write to these form inputs , i will get the post as i set it up in the array right?

But i believe there will be more complex data as the $userSpecific with his specific post posted here etc..?

And also one thing : p

Do you know how can i make this so when i feel all these inputs, a new post will be created?