Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
    
If you want to display the list of tags then don't explode the list. –  VMai Sep 6 '14 at 10:00
    
but there are tags more then one, and i want them to have separate links. –  David Sep 6 '14 at 10:02
    
You should use a loop then. –  VMai Sep 6 '14 at 10:04
    
what kind of out put you need for tags? –  Ram Sharma Sep 6 '14 at 10:06
    
i need separated links shown side by side –  David Sep 6 '14 at 10:11

3 Answers 3

$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 ',').

share|improve this answer
    
he already have comma separated value in $postCat –  Ram Sharma Sep 6 '14 at 10:08
    
I know, see last line of my answer. The implode-part was for completeness, and in case he wanted a custom separator. –  Peter van der Wal Sep 6 '14 at 10:09
    
If you already have the comma separated value in variable than what is the need of implode? –  Ram Sharma Sep 6 '14 at 10:14
    
Maybe because tag1, tag2, tag3 looks better than tag1,tag2,tag3. I didn't recommend to implode again with the same separator as used with explode –  Peter van der Wal Sep 6 '14 at 10:17
    
i.stack.imgur.com/jpO5d.png need this –  David Sep 6 '14 at 10:24

$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>";  

}
share|improve this answer
    
and how to insert this? –  David Sep 6 '14 at 10:22
    
replace <li class='adc'>kategoria: <a href='#'>{$postCatTag}</a></li>"; with my code Check and let me know. –  Abhishek Shetty Sep 6 '14 at 10:25
    
There was a small error in my code. I re-edited it in my answer. Its $i<count($postCatTag) not $i<=count($postCatTag) –  Abhishek Shetty Sep 6 '14 at 10:30
    
just replace? like this: 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> for($i=0;$i<count($postCatTag),$i++){ echo "<li class='adc'>kategoria: <a href='#'>$postCatTag[$i]</a></li>"; }"; it shows nothing/ replaced (") with (') and result was: <li class='adc'>TariRi: 2014-08-22 13:00:00</li> for(=0;<count(Array),++){ echo '<li class='adc'>kategoria: <a href='#'></a></li>'; } –  David Sep 6 '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 6 '14 at 10:49
    echo "<li class='adc'>kategoria:'; 
    for($i=0;$i<count($postCatTag),$i++)
    {

              echo "<a href='#'>$postCatTag[$i]</a>";  

    }
    echo "</li>";

Hope it will work for you.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.