0

Here is my code:

$postsql = "SELECT * FROM post WHERE id='{$_GET['id']}'";
$posts = mysqli_query($connect,$postsql) or die("Error: ".mysqli_error($connect));
$post = mysqli_fetch_array($posts);

$postAuthor = $post['author'];
$postDate = $post['date'];

$postCat = mysqli_fetch_row($post['cat']);// I've tags here separated by commas.
$postCatTag = explode(",",$postCat);

$postText = $post['text'];

echo "<img class='view_newsimg' src='{$postImg}'>
<h3 class='lath'>{$postTitle}</h3>
<ul class='det'><li class='adc'>avtori: {$postAuthor}</li>
<li class='adc'>TariRi: {$postDate}</li>
<li class='adc'>kategoria: <a href='#'>{$postCatTag}</a></li>"; // tags are shown here
echo <<<TEXT
<p class="news">{$postText}</p>
TEXT;

As a result for tag section in browser it displays: kategoria: Array instead of displaying tags from table. Why? How can get desired result?

David
  • 23
  • 1
  • 7

3 Answers3

0

$postCatTag is an array. Array to string conversions always results in Array. You could use the implode-function to impode an array with a given seperator:

implode(', ', $postCatTag);

Or you can just use $postCat since that is already a string (tags seperated by ',').

Peter van der Wal
  • 11,141
  • 2
  • 21
  • 29
0

$postCatTag is an array. Arrays cannot be displayed by echo.

Use loop for this:

for($i=0;$i<count($postCatTag),$i++){

  echo "<li class='adc'>kategoria: <a href='#'>$postCatTag[$i]</a></li>";  

}
  • There was a small error in my code. I re-edited it in my answer. Its `$i – Abhishek Shetty Sep 06 '14 at 10:30
  • just replace? like this: `echo "

    {$postTitle}

    • avtori: {$postAuthor}
    • TariRi: {$postDate}
    • for($i=0;$ikategoria: $postCatTag[$i]"; }";` it shows nothing/ replaced (") with (') and result was: `
    • TariRi: 2014-08-22 13:00:00
    • for(=0;kategoria: '; }`
    – David Sep 06 '14 at 10:41
  • Thats because your array $postCatTag is empty. Dont replace (") with ('). Compiler reads and convert everything inside (") but when it comes accross (') it simply prints it. write `print_r($postCatTag)` somewhere and see the result. It will display content inside array. Will help you to debug the code. – Abhishek Shetty Sep 06 '14 at 10:49