Jump to content

problem to disaply freinds POST


ahmadPHP

Recommended Posts

Hello

 

I have probelm to display freinds post 

HTML:

 <!-- freinds POST -->
        <div style="position: absolute;margin-right: 120px; width: 500px; margin-top: 1px;">
                <h4>Friends posts</h4>
            <?php foreach (get_friends_posts() as $key => $post):?>
                <div class="panel panel-default">
                    <div class="panel-heading"><?= $post[$key]['creation_date'] ?></div>
                    <div class="panel-body">
                        <?= $post[$key]['content'] . '<div class="vertical-space-40"></div>' ?>
                        <?php if (isset($post['picture'])): ?>
                            <img src="<?= config('g.siteurl') ?>/uploads/<?= $post[$key]['picture'] ?>" alt="" class="thumbnail img-responsive" style="max-width: 180px">
                        <?php endif; ?>
                    </div>
                </div>
            <?php endforeach;?>
            </div>

PHP:

if ( ! function_exists('get_friends_posts'))
{
    function get_friends_posts()
    {
        global $db;

        $friends = $db->select('friends', '*', ['fid' => $_SESSION['LOGIN'], 'accept' => 1]);
        $posts = [];
        foreach ($friends as $friend)
        {
            $post = $db->select('post', '*', ['aid' => $friend['aid']]);
            $posts[] = $post;
        }

        return $posts;
    }
}

DB TABLE 1:  POST

post-204301-0-10367600-1494701025_thumb.png

/ /  TABLE 2:  Freinds

post-204301-0-88653800-1494701146_thumb.png

/ /  TABLE 3: Account

post-204301-0-37149300-1494701127_thumb.png

 

 

Link to comment
Share on other sites

Re-making your html block to make it much easier to read:

foreach (get_friends_posts() as $key => $post))
{
echo "<div style='position: absolute;margin-right: 120px; width: 500px; margin-top: 1px;'>";
echo "<h4>Friends posts</h4>";
echo "<div class='panel panel-default'>";
echo "<div class='panel-heading>";
echo $post[$key]['creation_date'];
echo "</div>";
echo "<div class='panel-body'>";
echo $post[$key]['content'];
echo "<div class='vertical-space-40'></div>";
if (isset($post['picture']))
{
    echo "<img src=" . config('g.siteurl'). "/uploads/". $post[$key]['picture']." alt='' class='thumbnail img-responsive' style='max-width: 180px;'>";
}
echo "</div>";
echo "</div>";
}
echo "</div>";

 

I thinkj you have the foreach built wrong based upon what I see when you use the parts.

 

$key is the index of the array and $post is the value of that key.  But then you echo a line with $post[$key]['creation_date'];

 

Makes no sense  It would seem that the date is in $key['creation_date'];

Same for all your other uses of the foreach vars.  If your $key value actually contains a sub-array you have to handle it as one.

 

Also the format of the foreach is foreach (array as key => value) .  NO parens on the array name.

 

And if you TURNED ON PHP ERROR CHECKING you would probably been shown that.  See my signature

Edited by ginerjm
Link to comment
Share on other sites

Yes it would be easier if only the damn forum didn't reshape my posts with lack of indentation and double spacing.  

 

What's the secret of taking my code and pasting into the forum?  It looks good when the code is first brought in, but when I save it it gets altered.

Link to comment
Share on other sites

Or maybe...

 

<?php
foreach (get_friends_posts() as $key => $post)
{
    $siteurl=config('g.siteurl');
    $picture=isset($post['picture'])
    ?"<img src='$siteurl/uploads/$post[$key][picture]' alt='' class='thumbnail img-responsive' style='max-width: 180px;'>"
    :null;
    echo <<<EOT
<div style='position: absolute;margin-right: 120px; width: 500px; margin-top: 1px;'>
    <h4>Friends posts</h4>
    <div class='panel panel-default'>
        <div class='panel-heading>
            $post[$key]['creation_date']
        </div>
        <div class='panel-body'>
        $post[$key]['content']
        <div class='vertical-space-40'></div>
        $picture
    </div>
</div>
EOT;
}

PS Some of the arrays might be considered complex, and require curly quotes. 

Link to comment
Share on other sites

What's the secret of taking my code and pasting into the forum?  It looks good when the code is first brought in, but when I save it it gets altered.

 

Since you seem to be the only one with this problem, it looks more like a client-side issue with your browser, your editor or whatever.

 

In any case, the OP's code is far, far cleaner than this echoechoechoecho mess. Then there are real template engines, but I understand many PHP programmers love their old-school code.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.