-3

there are currently three posts but when onclick alerts only one post.If I echo the posts insite the loop,then all three posts are shown,however if I alert them,then onlyone posts is show.plz help or suggest any alternative approach.

  $sql=mysqli_query($db3,"SELECT * from user where  id='$id'");
            $num_rows=mysqli_num_rows($sql);

            while($row=mysqli_fetch_array($sql)){

                $posts=$row['posts'];
            }


            ?>
        <span onclick=u(<?php echo $posts; ?>)> <?php echo $num_rows  ?> </span>

        <script type="text/javascript">
            function u(posts) {
                alert(posts);
            }
        </script>
        <?php

Update

Here is second query After using the array approach ,If i use fancy box with $num rows to show all posts in the fancybox .I am again getting the only one result on the fancybox.Plz help

            $posts[] = $row['posts'];

} foreach ($posts as $af){

                echo "<div id='#modelbox_id'>$af</div>";}

            ?>


            <a href="#modelbox_id" class="modelbox"><?php   echo $num_rows; ?></a>

            <?php
Stephen
  • 18,827
  • 9
  • 60
  • 98
Aana Saeed
  • 299
  • 1
  • 4
  • 14

2 Answers2

2

It is because you are assigning a single post row into the $posts variable, and then overwriting that variable with a new value on each iteration of the while loop.

Try something like this instead:

$posts = array();
while($row=mysqli_fetch_array($sql)){
    $posts[] = $row['posts'];
}

Then you will need to print each value of the $posts array.

For example, you could implode the array into a string:

<span onclick=u(<?php echo implode(', ', $posts); ?>)> ...
Stephen
  • 18,827
  • 9
  • 60
  • 98
  • ,thank you.It solved the onclick problem. I also want all posts to be show on fancybox.but after using your approach.I am still getting only one post on fancybox.I have ipdated my questionplz help – Aana Saeed Dec 06 '12 at 06:28
  • @AanaSaeed You shouldn't ask another question in the same thread. Try to understand how my first answer solved your problem and apply that to your new problem. If that doesn't work after some experimentation, open a new question about fancy box. – Stephen Dec 06 '12 at 06:34
  • plz also reciew this.http://stackoverflow.com/questions/13761008/is-this-a-true-long-polling#comment18914635_13761008 – Aana Saeed Dec 07 '12 at 10:52
0

Instead try something like this

        while($row=mysqli_fetch_array($sql)){
            $posts .= $row['posts'].",";
        }
      ?>
    <span onclick="u('<?php echo $posts; ?>')"> <?php echo $num_rows  ?> </span>

    <script type="text/javascript">
        function u(posts) {
            alert(posts);
        }
    </script>
senK
  • 2,782
  • 1
  • 27
  • 38