ahmadPHP Posted May 13, 2017 Share Posted May 13, 2017 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 / / TABLE 2: Freinds / / TABLE 3: Account Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted May 13, 2017 Share Posted May 13, 2017 What type of problem? Where is it not working? Try this. $friends = $db->select('friends', '*', ['fid' => $_SESSION['LOGIN'], 'accept' => 1]); var_dump($friends);exit; What do you see? If it doesn't exit, you never got here. If you don't have data, you have other issues. PS. What is $db? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted May 13, 2017 Share Posted May 13, 2017 (edited) 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 May 13, 2017 by ginerjm Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted May 13, 2017 Share Posted May 13, 2017 You really find this easier to read? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted May 13, 2017 Share Posted May 13, 2017 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. Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted May 13, 2017 Share Posted May 13, 2017 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. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted May 13, 2017 Share Posted May 13, 2017 regardless of our formatting ideas, the OP still has a plain syntax problems. If the foreach has a key of $key and a value of $post how can he then reference $post[$key] ? Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted May 13, 2017 Share Posted May 13, 2017 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. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted May 13, 2017 Share Posted May 13, 2017 So you think my IDE is the culprit even tho it looks perfect there and when I paste into forum. What format should I be using - unix or windows? UTF-8 or something else? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.